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.
This commit is contained in:
anovazzi1 2023-09-21 10:30:59 -03:00
commit bf1597a9f4

View file

@ -490,15 +490,12 @@ export function TabsProvider({ children }: { children: ReactNode }) {
flow?: FlowType flow?: FlowType
): Promise<String | undefined> => { ): Promise<String | undefined> => {
if (newProject) { if (newProject) {
let flowData = extractDataFromFlow(flow!); let flowData = flow
if (flowData.description == "") { ? processDataFromFlow(flow)
flowData.description = getRandomDescription(); : { nodes: [], edges: [], viewport: { zoom: 1, x: 0, y: 0 } };
}
// Create a new flow with a default name if no flow is provided. // Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow!); const newFlow = createNewFlow(flowData, flow!);
processFlowEdges(newFlow);
processFlowNodes(newFlow);
const flowName = addVersionToDuplicates(newFlow, flows); 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; let data = flow?.data ? flow.data : null;
const description = flow?.description ? flow.description : "";
if (data) { if (data) {
processFlowEdges(flow); processFlowEdges(flow);
processFlowNodes(flow); processFlowNodes(flow);
@ -537,7 +533,7 @@ export function TabsProvider({ children }: { children: ReactNode }) {
updateIds(data, getNodeId); // Assuming updateIds is defined elsewhere updateIds(data, getNodeId); // Assuming updateIds is defined elsewhere
} }
return { data, description }; return data;
}; };
const updateEdges = (edges: Edge[]) => { const updateEdges = (edges: Edge[]) => {
@ -584,12 +580,12 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}; };
const createNewFlow = ( const createNewFlow = (
flowData: { data: ReactFlowJsonObject | null; description: string }, flowData: ReactFlowJsonObject | null,
flow: FlowType flow: FlowType
) => ({ ) => ({
description: flowData.description, description: flow.description ?? getRandomDescription(),
name: flow?.name ?? getRandomName(), name: flow?.name ?? getRandomName(),
data: flowData.data, data: flowData,
id: "", id: "",
}); });