diff --git a/src/components/Graph/index.tsx b/src/components/Graph/index.tsx index bc51c1d..7816a49 100644 --- a/src/components/Graph/index.tsx +++ b/src/components/Graph/index.tsx @@ -19,15 +19,13 @@ import useConfig from "src/hooks/store/useConfig"; import styled from "styled-components"; import shallow from "zustand/shallow"; -interface GraphProps { +interface LayoutProps { json: string; - isWidget?: boolean; + isWidget: boolean; + openModal: () => void; + setSelectedNode: (node: object) => void; } -const wheelOptions = { - step: 0.05, -}; - const StyledEditorWrapper = styled.div<{ isWidget: boolean }>` position: absolute; width: 100%; @@ -43,23 +41,12 @@ const StyledEditorWrapper = styled.div<{ isWidget: boolean }>` } `; -export const Graph: React.FC = ({ +const MemoizedGraph = React.memo(function Layout({ json, - isWidget = false, - ...props -}) => { - const [isModalVisible, setModalVisible] = React.useState(false); - const updateSetting = useConfig((state) => state.updateSetting); - const [expand, layout] = useConfig( - (state) => [state.settings.expand, state.settings.layout], - shallow - ); - - const onInit = (ref: ReactZoomPanPinchRef) => { - updateSetting("zoomPanPinch", ref); - }; - - const [selectedNode, setSelectedNode] = React.useState(null); + isWidget, + openModal, + setSelectedNode, +}: LayoutProps) { const [nodes, setNodes] = React.useState([]); const [edges, setEdges] = React.useState([]); const [size, setSize] = React.useState({ @@ -67,6 +54,12 @@ export const Graph: React.FC = ({ height: 2000, }); + const updateSetting = useConfig((state) => state.updateSetting); + const [expand, layout] = useConfig( + (state) => [state.settings.expand, state.settings.layout], + shallow + ); + React.useEffect(() => { const { nodes, edges } = getEdgeNodes(json, expand); @@ -74,6 +67,10 @@ export const Graph: React.FC = ({ setEdges(edges); }, [json, expand]); + const onInit = (ref: ReactZoomPanPinchRef) => { + updateSetting("zoomPanPinch", ref); + }; + const onCanvasClick = () => { const input = document.querySelector("input:focus") as HTMLInputElement; if (input) input.blur(); @@ -88,54 +85,74 @@ export const Graph: React.FC = ({ e: React.MouseEvent, props: NodeProps ) => { - setSelectedNode(props.properties); - setModalVisible(true); + setSelectedNode(props.properties.text); + openModal(); }; return ( - <> - - + + - - ( - handleNodeClick(e, props)} - {...props} - /> - )} - zoomable={false} - readonly - {...props} - /> - - - - {!isWidget && selectedNode && ( + ( + handleNodeClick(e, props)} + {...props} + /> + )} + zoomable={false} + readonly + /> + + + + ); +}); + +export const Graph: React.FC<{ json: string; isWidget?: boolean }> = ({ + json, + isWidget = false, +}) => { + const [isModalVisible, setModalVisible] = React.useState(false); + const [selectedNode, setSelectedNode] = React.useState({}); + + const openModal = React.useCallback(() => setModalVisible(true), []); + + return ( + <> + + {!isWidget && ( setModalVisible(false)} /> )} diff --git a/src/containers/Modals/NodeModal/index.tsx b/src/containers/Modals/NodeModal/index.tsx index 2250fe2..8aa0fb7 100644 --- a/src/containers/Modals/NodeModal/index.tsx +++ b/src/containers/Modals/NodeModal/index.tsx @@ -1,10 +1,17 @@ import React from "react"; import toast from "react-hot-toast"; import { FiCopy } from "react-icons/fi"; +import { NodeData } from "reaflow"; import { Button } from "src/components/Button"; import { Modal } from "src/components/Modal"; import styled from "styled-components"; +interface NodeModalProps { + selectedNode: object; + visible: boolean; + closeModal: () => void; +} + const StyledTextarea = styled.textarea` resize: none; width: 100%; @@ -19,16 +26,19 @@ const StyledTextarea = styled.textarea` border: none; `; -export const NodeModal = ({ selectedNode, visible = true, setVisible }) => { +export const NodeModal = ({ + selectedNode, + visible, + closeModal, +}: NodeModalProps) => { const handleClipboard = () => { toast("Content copied to clipboard!"); navigator.clipboard.writeText(JSON.stringify(selectedNode)); - setVisible(false); + closeModal(); }; - return ( - + Node Content { )} /> - +