feat(flowsContext.tsx): add nodesOnFlow and setNodesOnFlow functions to FlowsContext to track changes in nodes on the flow

fix(PageComponent/index.tsx): update onNodesChangeMod function to only trigger when there are changes in the nodes on the flow and update nodesOnFlow state in FlowsContext
feat(tabs/index.ts): add nodesOnFlow and setNodesOnFlow functions to FlowsContextType to track changes in nodes on the flow
This commit is contained in:
cristhianzl 2023-12-04 20:48:11 -03:00
commit 8714ecfada
3 changed files with 32 additions and 13 deletions

View file

@ -92,6 +92,8 @@ const FlowsContextInitialValue: FlowsContextType = {
saveComponent: async (component: NodeDataType, override: boolean) => "", saveComponent: async (component: NodeDataType, override: boolean) => "",
deleteComponent: (key: string) => {}, deleteComponent: (key: string) => {},
version: "", version: "",
nodesOnFlow: "",
setNodesOnFlow: (nodes: string) => "",
}; };
export const FlowsContext = createContext<FlowsContextType>( export const FlowsContext = createContext<FlowsContextType>(
@ -106,6 +108,7 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
const [tabId, setTabId] = useState(""); const [tabId, setTabId] = useState("");
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [nodesOnFlow, setNodesOnFlow] = useState("");
const [flows, setFlows] = useState<Array<FlowType>>([]); const [flows, setFlows] = useState<Array<FlowType>>([]);
const [id, setId] = useState(uid()); const [id, setId] = useState(uid());
@ -666,6 +669,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
}, },
}; };
}); });
console.log(tabsState);
} }
} catch (err) { } catch (err) {
setErrorData({ setErrorData({
@ -730,6 +735,8 @@ export function FlowsProvider({ children }: { children: ReactNode }) {
isLoading, isLoading,
saveComponent, saveComponent,
deleteComponent, deleteComponent,
nodesOnFlow,
setNodesOnFlow,
}} }}
> >
{children} {children}

View file

@ -83,6 +83,7 @@ export default function Page({
const reactFlowWrapper = useRef<HTMLDivElement>(null); const reactFlowWrapper = useRef<HTMLDivElement>(null);
const { takeSnapshot } = useContext(undoRedoContext); const { takeSnapshot } = useContext(undoRedoContext);
const { nodesOnFlow, setNodesOnFlow } = useContext(FlowsContext);
const position = useRef({ x: 0, y: 0 }); const position = useRef({ x: 0, y: 0 });
const [lastSelection, setLastSelection] = const [lastSelection, setLastSelection] =
@ -219,7 +220,7 @@ export default function Page({
...prev, ...prev,
[tabId]: { [tabId]: {
...prev[tabId], ...prev[tabId],
isPending: true, isPending: false,
}, },
}; };
}); });
@ -229,7 +230,11 @@ export default function Page({
const onNodesChangeMod = useCallback( const onNodesChangeMod = useCallback(
(change: NodeChange[]) => { (change: NodeChange[]) => {
const changeString = JSON.stringify(change);
if (changeString !== nodesOnFlow) {
onNodesChange(change); onNodesChange(change);
updateNodeFlow(changeString);
//@ts-ignore //@ts-ignore
setTabsState((prev: FlowsState) => { setTabsState((prev: FlowsState) => {
return { return {
@ -240,10 +245,15 @@ export default function Page({
}, },
}; };
}); });
}
}, },
[onNodesChange, setTabsState, tabId] [onNodesChange, setTabsState, tabId, updateNodeFlow]
); );
function updateNodeFlow(changeString: string) {
setNodesOnFlow(changeString);
}
const onConnect = useCallback( const onConnect = useCallback(
(params: Connection) => { (params: Connection) => {
takeSnapshot(); takeSnapshot();

View file

@ -55,6 +55,8 @@ export type FlowsContextType = {
) => Promise<String | undefined>; ) => Promise<String | undefined>;
deleteComponent: (key: string) => void; deleteComponent: (key: string) => void;
version: string; version: string;
nodesOnFlow: string;
setNodesOnFlow: (nodes: string) => void;
}; };
export type FlowsState = { export type FlowsState = {