Skip to content

Commit a838896

Browse files
committed
refactor modalcontroller
1 parent ca36a4b commit a838896

File tree

11 files changed

+63
-83
lines changed

11 files changed

+63
-83
lines changed

src/features/editor/ModalController.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/features/editor/Toolbar/FileMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const FileMenu = () => {
3333
</StyledToolElement>
3434
</Menu.Target>
3535
<Menu.Dropdown>
36-
<Menu.Item fz={12} onClick={() => setVisible("import")(true)}>
36+
<Menu.Item fz={12} onClick={() => setVisible("ImportModal", true)}>
3737
Import
3838
</Menu.Item>
3939
<Menu.Item fz={12} onClick={handleSave}>

src/features/editor/Toolbar/OptionsMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const OptionsMenu = () => {
9191
<Menu.Item
9292
closeMenuOnClick
9393
leftSection={<VscLock />}
94-
onClick={() => setVisible("upgrade")(true)}
94+
onClick={() => setVisible("UpgradeModal", true)}
9595
>
9696
<Text size="xs">Customize Graph Colors</Text>
9797
</Menu.Item>

src/features/editor/Toolbar/ToolsMenu.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const ToolsMenu = () => {
6060
fz={12}
6161
leftSection={<VscSearchFuzzy />}
6262
onClick={() => {
63-
setVisible("jq")(true);
63+
setVisible("JQModal", true);
6464
gaEvent("open_jq_modal");
6565
}}
6666
>
@@ -70,7 +70,7 @@ export const ToolsMenu = () => {
7070
fz={12}
7171
leftSection={<VscJson />}
7272
onClick={() => {
73-
setVisible("schema")(true);
73+
setVisible("SchemaModal", true);
7474
gaEvent("open_schema_modal");
7575
}}
7676
>
@@ -80,7 +80,7 @@ export const ToolsMenu = () => {
8080
fz={12}
8181
leftSection={<MdFilterListAlt />}
8282
onClick={() => {
83-
setVisible("jpath")(true);
83+
setVisible("JPathModal", true);
8484
gaEvent("open_json_path_modal");
8585
}}
8686
>
@@ -91,7 +91,7 @@ export const ToolsMenu = () => {
9191
fz={12}
9292
leftSection={<SiJsonwebtokens />}
9393
onClick={() => {
94-
setVisible("jwt")(true);
94+
setVisible("JWTModal", true);
9595
gaEvent("open_jwt_modal");
9696
}}
9797
>
@@ -101,7 +101,7 @@ export const ToolsMenu = () => {
101101
fz={12}
102102
leftSection={<VscGroupByRefType />}
103103
onClick={() => {
104-
setVisible("type")(true);
104+
setVisible("TypeModal", true);
105105
gaEvent("open_type_modal");
106106
}}
107107
>
@@ -115,7 +115,7 @@ export const ToolsMenu = () => {
115115
leftSection={<LuGlobe />}
116116
rightSection={<VscLock />}
117117
onClick={() => {
118-
setVisible("upgrade")(true);
118+
setVisible("UpgradeModal", true);
119119
gaEvent("rest_client_modal");
120120
}}
121121
>
@@ -126,7 +126,7 @@ export const ToolsMenu = () => {
126126
leftSection={<FaWandMagicSparkles />}
127127
rightSection={<VscLock />}
128128
onClick={() => {
129-
setVisible("upgrade")(true);
129+
setVisible("UpgradeModal", true);
130130
gaEvent("open_ai_filter_modal");
131131
}}
132132
>
@@ -137,7 +137,7 @@ export const ToolsMenu = () => {
137137
leftSection={<MdCompare />}
138138
rightSection={<VscLock />}
139139
onClick={() => {
140-
setVisible("upgrade")(true);
140+
setVisible("UpgradeModal", true);
141141
gaEvent("open_compare_data_modal");
142142
}}
143143
>

src/features/editor/Toolbar/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const Toolbar = ({ isWidget = false }: ToolbarProps) => {
9090
size="compact-sm"
9191
fz="12"
9292
fw="600"
93-
onClick={() => setVisible("upgrade")(true)}
93+
onClick={() => setVisible("UpgradeModal", true)}
9494
leftSection={<LuCrown />}
9595
mr="6"
9696
>
@@ -100,7 +100,10 @@ export const Toolbar = ({ isWidget = false }: ToolbarProps) => {
100100
<SearchInput />
101101
{!isWidget && (
102102
<>
103-
<StyledToolElement title="Save as Image" onClick={() => setVisible("download")(true)}>
103+
<StyledToolElement
104+
title="Save as Image"
105+
onClick={() => setVisible("DownloadModal", true)}
106+
>
104107
<FiDownload size="18" />
105108
</StyledToolElement>
106109
<ZoomMenu />

src/features/editor/views/GraphView/CustomNode/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const CustomNodeWrapper = (nodeProps: NodeProps<NodeData["data"]>) => {
2727
const handleNodeClick = React.useCallback(
2828
(_: React.MouseEvent<SVGGElement, MouseEvent>, data: NodeData) => {
2929
if (setSelectedNode) setSelectedNode(data);
30-
setVisible("node")(true);
30+
setVisible("NodeModal", true);
3131
},
3232
[setSelectedNode, setVisible]
3333
);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from "react";
2+
import type { ModalProps } from "@mantine/core";
3+
import * as ModalComponents from "src/features/modals";
4+
import useModal from "src/store/useModal";
5+
6+
const modalNames = Object.keys(ModalComponents);
7+
const modals = Object.freeze(modalNames) as Extract<keyof typeof ModalComponents, string>[];
8+
9+
export type Modal = (typeof modals)[number];
10+
type ModalComponent = {
11+
key: Modal;
12+
component: React.FC<ModalProps & any>;
13+
};
14+
15+
export const modalComponents: ModalComponent[] = modals.map(modal => ({
16+
key: modal,
17+
component: ModalComponents[modal],
18+
}));
19+
20+
const ModalController = () => {
21+
const setVisible = useModal(state => state.setVisible);
22+
const modalStates = useModal(state => modalComponents.map(modal => state[modal.key]));
23+
24+
return (
25+
<>
26+
{modalComponents.map(({ key, component }, index) => {
27+
const ModalComponent = component;
28+
const opened = modalStates[index];
29+
30+
return <ModalComponent key={key} opened={opened} onClose={() => setVisible(key, false)} />;
31+
})}
32+
</>
33+
);
34+
};
35+
36+
export default ModalController;

src/features/modals/NodeModal/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const NodeModal = ({ opened, onClose }: ModalProps) => {
3535
</Stack>
3636
<Button
3737
onClick={() => {
38-
setVisible("upgrade")(true);
38+
setVisible("UpgradeModal", true);
3939
gaEvent("click_node_edit");
4040
}}
4141
rightSection={<VscLock strokeWidth={0.5} />}

src/features/modals/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,3 @@ export { SchemaModal } from "./SchemaModal";
77
export { JQModal } from "./JQModal";
88
export { TypeModal } from "./TypeModal";
99
export { JPathModal } from "./JPathModal";
10-
11-
type Modal =
12-
| "download"
13-
| "import"
14-
| "node"
15-
| "upgrade"
16-
| "jwt"
17-
| "schema"
18-
| "jq"
19-
| "type"
20-
| "jpath";
21-
22-
export type { Modal };

src/pages/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import useGraph from "src/features/editor/views/GraphView/stores/useGraph";
1717
import useConfig from "src/store/useConfig";
1818
import useFile from "src/store/useFile";
1919

20-
const ModalController = dynamic(() => import("src/features/editor/ModalController"));
20+
const ModalController = dynamic(() => import("src/features/modals/ModalController"));
2121
const ExternalMode = dynamic(() => import("src/features/editor/ExternalMode"));
2222

2323
const queryClient = new QueryClient({

0 commit comments

Comments
 (0)