diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index 73db3fc1b..fb51d2ffc 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -263,7 +263,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { * If the file type is application/json, the file is read and parsed into a JSON object. * The resulting JSON object is passed to the addFlow function. */ - function uploadFlow() { + function uploadFlow(newProject?: boolean) { // create a file input const input = document.createElement("input"); input.type = "file"; @@ -278,7 +278,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { // parse the text into a JSON object let flow: FlowType = JSON.parse(text); - addFlow(flow); + addFlow(flow, newProject); }); } }; @@ -415,30 +415,35 @@ export function TabsProvider({ children }: { children: ReactNode }) { reactFlowInstance.setEdges(edges); } - const addFlow = async (flow?: FlowType): Promise => { - let flowData = extractDataFromFlow(flow); - if(flowData.description == ""){ - flowData.description = "This is a new flow."; - } - - // Create a new flow with a default name if no flow is provided. - const newFlow = createNewFlow(flowData, flow); - - try { - const id = await saveFlowToDatabase(newFlow); - // Change the id to the new id. - newFlow.id = id.id; - - // Add the new flow to the list of flows. - addFlowToLocalState(newFlow); - - // Return the id - return id.id; - } catch (error) { - // Handle the error if needed - console.error('Error while adding flow:', error); - throw error; // Re-throw the error so the caller can handle it if needed + const addFlow = async (flow?: FlowType, newProject?: Boolean): Promise => { + if(newProject){ + let flowData = extractDataFromFlow(flow); + if(flowData.description == ""){ + flowData.description = "This is a new flow."; + } + + // Create a new flow with a default name if no flow is provided. + const newFlow = createNewFlow(flowData, flow); + + try { + const id = await saveFlowToDatabase(newFlow); + // Change the id to the new id. + newFlow.id = id.id; + + // Add the new flow to the list of flows. + addFlowToLocalState(newFlow); + + // Return the id + return id.id; + } catch (error) { + // Handle the error if needed + console.error('Error while adding flow:', error); + throw error; // Re-throw the error so the caller can handle it if needed + } + } else { + paste({nodes: flow.data.nodes, edges: flow.data.edges}, {x:10, y:10}) } + }; const extractDataFromFlow = (flow) => { diff --git a/src/frontend/src/modals/importModal/index.tsx b/src/frontend/src/modals/importModal/index.tsx index d0808c541..72b44e671 100644 --- a/src/frontend/src/modals/importModal/index.tsx +++ b/src/frontend/src/modals/importModal/index.tsx @@ -160,7 +160,7 @@ export default function ImportModal() { } onClick={() => { - uploadFlow(); + uploadFlow(false); setModalOpen(false); }} textColor="text-blue-500 dark:text-blue-500/75" @@ -189,7 +189,7 @@ export default function ImportModal() { } onClick={() => { - addFlow(example); + addFlow(example, false); setModalOpen(false); }} textColor="text-emerald-500 dark:text-emerald-500/75" diff --git a/src/frontend/src/types/tabs/index.ts b/src/frontend/src/types/tabs/index.ts index 0422aeeab..0f5048b4d 100644 --- a/src/frontend/src/types/tabs/index.ts +++ b/src/frontend/src/types/tabs/index.ts @@ -5,7 +5,7 @@ export type TabsContextType = { setTabId: (index: string) => void; flows: Array; removeFlow: (id: string) => void; - addFlow: (flowData?: FlowType, newFlow?: boolean) => Promise; + addFlow: (flowData?: FlowType, newProject?: boolean) => Promise; updateFlow: (newFlow: FlowType) => void; incrementNodeId: () => string; downloadFlow: (flow: FlowType) => void;