fix(App.tsx): increase the timer interval for fetching data to 20 seconds for better performance

feat(App.tsx): add cleanup function to clean up flow state when component unmounts
feat(PageComponent/index.tsx): add cleanFlow function to reset flow state when component unmounts
fix(PageComponent/index.tsx): fix bug where newEdges were not being set correctly in onConnect function
fix(PageComponent/index.tsx): remove unnecessary dependencies from onConnect function
fix(PageComponent/index.tsx): fix bug where setEdges was not returning the newEdges array correctly
feat(flowStore.ts): add cleanFlow function to reset flow state to initial values
feat(flowStore.ts): add cleanFlow function to reset flow state to initial values in flowStore type
This commit is contained in:
anovazzi1 2024-01-15 16:11:56 -03:00 • committed by Lucas Oliveira
commit 59de3fe50b
4 changed files with 43 additions and 22 deletions

View file

@ -149,7 +149,7 @@ export default function App() {
.catch(() => { .catch(() => {
setFetchError(true); setFetchError(true);
}); });
}, 5000); }, 20000);
// Clean up the timer on component unmount // Clean up the timer on component unmount
return () => { return () => {

View file

@ -64,6 +64,7 @@ export default function Page({
const onEdgesChange = useFlowStore((state) => state.onEdgesChange); const onEdgesChange = useFlowStore((state) => state.onEdgesChange);
const setNodes = useFlowStore((state) => state.setNodes); const setNodes = useFlowStore((state) => state.setNodes);
const setEdges = useFlowStore((state) => state.setEdges); const setEdges = useFlowStore((state) => state.setEdges);
const cleanFlow = useFlowStore((state) => state.cleanFlow);
const deleteNode = useFlowStore((state) => state.deleteNode); const deleteNode = useFlowStore((state) => state.deleteNode);
const deleteEdge = useFlowStore((state) => state.deleteEdge); const deleteEdge = useFlowStore((state) => state.deleteEdge);
const undo = useFlowsManagerStore((state) => state.undo); const undo = useFlowsManagerStore((state) => state.undo);
@ -170,6 +171,12 @@ export default function Page({
} }
}, [currentFlowId, reactFlowInstance]); }, [currentFlowId, reactFlowInstance]);
useEffect(() => {
return () => {
cleanFlow();
}
}, [])
const onConnectMod = useCallback( const onConnectMod = useCallback(
(params: Connection) => { (params: Connection) => {
takeSnapshot(); takeSnapshot();
@ -210,26 +217,30 @@ export default function Page({
const onConnect = useCallback( const onConnect = useCallback(
(connection: Connection) => { (connection: Connection) => {
const newEdges = addEdge( let newEdges:Edge[] = []
{ setEdges((oldEdges) => {
...connection, newEdges = addEdge(
data: { {
targetHandle: scapeJSONParse(connection.targetHandle!), ...connection,
sourceHandle: scapeJSONParse(connection.sourceHandle!), data: {
targetHandle: scapeJSONParse(connection.targetHandle!),
sourceHandle: scapeJSONParse(connection.sourceHandle!),
},
style: { stroke: "#555" },
className:
((scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text"
? "stroke-foreground "
: "stroke-foreground ") + " stroke-connection",
animated:
(scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text",
}, },
style: { stroke: "#555" }, oldEdges
className: );
((scapeJSONParse(connection.targetHandle!) as targetHandleType) return newEdges;
.type === "Text"
? "stroke-foreground " })
: "stroke-foreground ") + " stroke-connection",
animated:
(scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text",
},
edges
);
setEdges(newEdges);
useFlowsManagerStore useFlowsManagerStore
.getState() .getState()
.autoSaveCurrentFlow( .autoSaveCurrentFlow(
@ -238,7 +249,7 @@ export default function Page({
reactFlowInstance?.getViewport() ?? { x: 0, y: 0, zoom: 1 } reactFlowInstance?.getViewport() ?? { x: 0, y: 0, zoom: 1 }
); );
}, },
[nodes, edges, setEdges, reactFlowInstance, addEdge] [nodes, setEdges, reactFlowInstance, addEdge]
); );
const onDrop = useCallback( const onDrop = useCallback(
@ -357,7 +368,7 @@ export default function Page({
<div className="h-full w-full"> <div className="h-full w-full">
<div className="h-full w-full" ref={reactFlowWrapper}> <div className="h-full w-full" ref={reactFlowWrapper}>
{Object.keys(templates).length > 0 && {Object.keys(templates).length > 0 &&
Object.keys(types).length > 0 ? ( Object.keys(types).length > 0 ? (
<div id="react-flow-id" className="h-full w-full"> <div id="react-flow-id" className="h-full w-full">
<ReactFlow <ReactFlow
nodes={nodes} nodes={nodes}

View file

@ -250,6 +250,15 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
setLastCopiedSelection: (newSelection) => { setLastCopiedSelection: (newSelection) => {
set({ lastCopiedSelection: newSelection }); set({ lastCopiedSelection: newSelection });
}, },
cleanFlow: () => {
set({
nodes: [],
edges: [],
flowState: undefined,
sseData: {},
isBuilt: false,
});
},
})); }));
export default useFlowStore; export default useFlowStore;

View file

@ -49,4 +49,5 @@ export type FlowStoreType = {
) => void; ) => void;
isBuilt: boolean; isBuilt: boolean;
setIsBuilt: (isBuilt: boolean) => void; setIsBuilt: (isBuilt: boolean) => void;
cleanFlow: () => void;
}; };