import React, { PureComponent } from "react";
import cn from "classnames";
import isBrowser from "is-in-browser";
import copy from "copy-text-to-clipboard";
import unfetch from "unfetch";
import { List } from "react-content-loader";
import dynamic from "next/dynamic";
const modeMapping = {
javascript: {
name: "javascript"
},
json: {
name: "javascript",
json: true
},
typescript: {
name: "javascript",
typescript: true
},
html: {
name: "xml",
htmlMode: true
},
flow: {
name: "javascript",
typescript: true
},
mysql: "text/x-mysql",
scala: "text/x-scala"
};
type Props = {
leftMode: ?string,
rightMode: ?string,
resultValue: string,
onCheckboxChange: Function,
checkboxText: ?string,
pathname: string,
initialCheckboxValue: ?boolean,
leftTitle: string,
rightTitle: string,
prettifyRightPanel: ?boolean,
splitLeft: ?boolean,
splitTitle: ?string,
splitValue: ?string,
splitMode: ?string,
showFetchButton: boolean,
fetchButtonText: ?string,
getTransformedValue: Function,
extensions: ?(string[])
};
const codeMirrorOptions = {
lineNumbers: true,
theme: "chrome-devtools",
lineWrapping: true,
scrollbarStyle: null
};
function UploadIcon() {
return (
);
}
function ClearIcon() {
return (
);
}
function Stubs({ numberOfStubs = 3 }) {
return (
{[...new Array(numberOfStubs)].map((x, i) =>
)}
);
}
const CodeMirror = dynamic(import("./CodeMirror"), {
ssr: false,
loading: () =>
});
export default class ConversionPanel extends PureComponent {
props: Props;
constructor(props) {
super(props);
this.state = {
resultValue: "",
value: props.defaultText,
splitValue: "",
info: "",
infoType: ""
};
}
static defaultProps = {
leftMode: "javascript",
rightMode: "javascript",
pathname: "/",
prettifyRightPanel: true,
splitLeft: false,
showFetchButton: true,
fetchButtonText: "Fetch JSON from URL",
extensions: [".json"]
};
componentDidMount() {
const { defaultText, splitValue } = this.props;
this.onChange(defaultText, splitValue);
}
clearText = () => {
this.setState({
value: ""
});
};
fetchJSON = async () => {
const url = window.prompt("Enter URL to fetch JSON from a remote server.");
if (!url) return;
try {
const res = await unfetch(url);
const json = await res.json();
this.onChange(JSON.stringify(json, null, 2));
} catch (e) {
this.setState({
info: e.message,
infoType: "error"
});
}
};
onChange = async (newValue, leftSplitValue) => {
const { leftMode } = this.props;
const nValue = newValue || this.state.value;
const splitValue =
leftSplitValue && typeof leftSplitValue === "string"
? leftSplitValue
: this.state.splitValue;
try {
// Convert JS to JSON
const json =
leftMode === "json"
? JSON.stringify(eval("(" + nValue + ")", null, 2))
: nValue;
let code = IN_BROWSER
? await this.props.getTransformedValue(json, splitValue)
: "";
code = code.prettyCode || code;
this.setState({
resultValue: code,
info: "",
infoType: ""
});
} catch (e) {
this.setState({
info: e.message,
infoType: "error"
});
}
this.setState({
value: nValue,
splitValue: splitValue
});
};
toggleCheckbox = e => {
const checked = e.currentTarget.checked;
if (this.props.onCheckboxChange) {
this.props.onCheckboxChange(checked, () => {
this.onChange();
});
}
};
copyCode = () => {
const { resultValue } = this.state;
copy(resultValue);
this.setState({
info: "Code copied to clipboard.",
infoType: "success"
});
};
loadFile = e => {
const file = e.target.files[0];
const reader = new FileReader();
reader.readAsText(file, "utf-8");
reader.onload = () => {
this.onChange(reader.result);
};
};
render() {
const {
leftMode,
rightMode,
onCheckboxChange,
checkboxText,
initialCheckboxValue,
leftTitle,
rightTitle,
splitLeft,
splitTitle,
splitMode,
showFetchButton,
fetchButtonText,
extensions,
prettifyRightPanel
} = this.props;
const { infoType, resultValue, value, info, splitValue } = this.state;
const leftClass = cn("section left", {
split: splitLeft
});
// hide success footer banner after 2 seconds. Let other types be visible until fixed.
clearTimeout(this.footerTimeout);
if (infoType === "success") {
this.footerTimeout = setTimeout(
() =>
this.setState({
info: "",
infoType: ""
}),
2000
);
}
return (
{leftTitle}
{leftMode === "json" &&
showFetchButton && (
)}
this.onChange(value)
}
value={value}
options={{
mode: modeMapping[leftMode] || leftMode,
...codeMirrorOptions
}}
mode={leftMode}
autoPrettify={false}
/>
{extensions &&
extensions.length > 0 && (
)}
{splitLeft &&
isBrowser && (
{splitTitle}
this.onChange(this.state.value, value)
}
value={splitValue}
options={{
mode: modeMapping[splitMode] || splitMode,
...codeMirrorOptions
}}
mode={splitMode}
/>
)}
{infoType !== "" && (
{info}
)}
);
}
}