From bf1597a9f4b71501dad607dbc3eb06445f2b66f5 Mon Sep 17 00:00:00 2001 From: anovazzi1 Date: Thu, 21 Sep 2023 10:30:59 -0300 Subject: [PATCH] refactor(tabsContext.tsx): simplify code in TabsProvider component The changes in this commit simplify the code in the TabsProvider component in the tabsContext.tsx file. Here's a summary of the changes: - In the `createNewFlow` function, the `flowData` parameter is now of type `ReactFlowJsonObject | null` instead of `{ data: ReactFlowJsonObject | null; description: string }`. This simplifies the function signature and removes the need for destructuring the `flowData` object. - The `extractDataFromFlow` function has been renamed to `processDataFromFlow` to better reflect its purpose. The function now returns only the `data` property from the `flow` object instead of an object with `data` and `description` properties. - The `description` property in the `createNewFlow` function is now set to `flow.description ?? getRandomDescription()`. This ensures that if the `flow` object has a `description` property, it is used, otherwise a random description is generated. - The `processFlowEdges` and `processFlowNodes` functions are no longer called in the `createNewFlow` function. It seems that these functions are defined elsewhere and are not necessary in this context. These changes simplify the code and improve readability by removing unnecessary checks and simplifying function signatures. --- src/frontend/src/contexts/tabsContext.tsx | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index 3c24fda15..0f52d160d 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -490,15 +490,12 @@ export function TabsProvider({ children }: { children: ReactNode }) { flow?: FlowType ): Promise => { if (newProject) { - let flowData = extractDataFromFlow(flow!); - if (flowData.description == "") { - flowData.description = getRandomDescription(); - } + let flowData = flow + ? processDataFromFlow(flow) + : { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } }; // Create a new flow with a default name if no flow is provided. const newFlow = createNewFlow(flowData, flow!); - processFlowEdges(newFlow); - processFlowNodes(newFlow); const flowName = addVersionToDuplicates(newFlow, flows); @@ -526,9 +523,8 @@ export function TabsProvider({ children }: { children: ReactNode }) { } }; - const extractDataFromFlow = (flow: FlowType) => { + const processDataFromFlow = (flow: FlowType) => { let data = flow?.data ? flow.data : null; - const description = flow?.description ? flow.description : ""; if (data) { processFlowEdges(flow); processFlowNodes(flow); @@ -537,7 +533,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { updateIds(data, getNodeId); // Assuming updateIds is defined elsewhere } - return { data, description }; + return data; }; const updateEdges = (edges: Edge[]) => { @@ -584,12 +580,12 @@ export function TabsProvider({ children }: { children: ReactNode }) { }; const createNewFlow = ( - flowData: { data: ReactFlowJsonObject | null; description: string }, + flowData: ReactFlowJsonObject | null, flow: FlowType ) => ({ - description: flowData.description, + description: flow.description ?? getRandomDescription(), name: flow?.name ?? getRandomName(), - data: flowData.data, + data: flowData, id: "", });