Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add go struct generator #369

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"allotment": "^1.19.3",
"axios": "^1.5.0",
"dayjs": "^1.11.10",
"gofmt.js": "^0.0.2",
"html-to-image": "^1.11.11",
"jq-in-the-browser": "^0.7.2",
"jq-web": "^0.5.1",
Expand Down
38 changes: 32 additions & 6 deletions src/containers/Modals/TypeModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,55 @@ import { Stack, Modal, ModalProps, ScrollArea, Select } from "@mantine/core";
import { Prism } from "@mantine/prism";
import vsDark from "prism-react-renderer/themes/vsDark";
import vsLight from "prism-react-renderer/themes/vsLight";
import useJsonQuery from "src/hooks/useJsonQuery";
import useJson from "src/store/useJson";

enum Language {
TypeScript = "typescript",
Go = "go",
}

const typeOptions = [
{
label: "TypeScript",
value: "typescript",
value: Language.TypeScript,
},
{
label: "Go",
value: Language.Go,
},
];

export const TypeModal: React.FC<ModalProps> = ({ opened, onClose }) => {
const { getJsonType } = useJsonQuery();
const getJson = useJson(state => state.getJson);
const [type, setType] = React.useState("");
const [selectedType, setSelectedType] = React.useState<Language>(Language.TypeScript);

React.useEffect(() => {
if (opened) {
setType(getJsonType());
if (selectedType === Language.TypeScript) {
import("json-to-ts").then(jts => {
const types = jts.default(JSON.parse(getJson()));
setType(types.flat().join("\n\n"));
});
} else if (selectedType === Language.Go) {
import("src/lib/utils/json2go").then(jtg => {
import("gofmt.js").then(gofmt => {
const types = jtg.default(getJson());
setType(gofmt.default(types.go));
});
});
}
}
}, [getJsonType, opened]);
}, [getJson, opened, selectedType]);

return (
<Modal title="Generate Types" size="auto" opened={opened} onClose={onClose} centered>
<Stack py="sm">
<Select value="typescript" data={typeOptions} />
<Select
value={selectedType}
data={typeOptions}
onChange={e => setSelectedType(e as Language)}
/>
<ScrollArea>
<Prism
miw={350}
Expand Down
Loading