From e229510907600e28caeb9758fa9c78214568ffd6 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 14 Jun 2023 15:47:18 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20style(tabsContext.tsx):=20add=20?= =?UTF-8?q?save=20function=20to=20TabsContextInitialValue=20=F0=9F=9A=80?= =?UTF-8?q?=20feat(tabsContext.tsx):=20add=20save=20function=20to=20save?= =?UTF-8?q?=20the=20current=20state=20of=20the=20flows=20to=20local=20stor?= =?UTF-8?q?age=20The=20save=20function=20was=20added=20to=20TabsContextIni?= =?UTF-8?q?tialValue=20to=20provide=20a=20default=20value=20for=20the=20sa?= =?UTF-8?q?ve=20function.=20The=20save=20function=20was=20added=20to=20sav?= =?UTF-8?q?e=20the=20current=20state=20of=20the=20flows=20to=20local=20sto?= =?UTF-8?q?rage.=20The=20function=20uses=20lodash's=20cloneDeep=20to=20avo?= =?UTF-8?q?id=20mutating=20the=20original=20object.=20It=20also=20looks=20?= =?UTF-8?q?for=20file=20fields=20to=20prevent=20saving=20the=20content=20a?= =?UTF-8?q?nd=20breaking=20the=20flow=20for=20exceeding=20the=20data=20lim?= =?UTF-8?q?it=20for=20local=20storage.=20The=20function=20then=20saves=20t?= =?UTF-8?q?he=20tabId,=20flows,=20and=20id=20to=20local=20storage.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/contexts/tabsContext.tsx | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index 4daac7b71..a5e3aead6 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -25,6 +25,7 @@ import { const uid = new ShortUniqueId({ length: 5 }); const TabsContextInitialValue: TabsContextType = { + save: () => {}, tabId: "", setTabId: (index: string) => {}, flows: [], @@ -69,6 +70,32 @@ export function TabsProvider({ children }: { children: ReactNode }) { return newNodeId.current; } + function save() { + // added clone deep to avoid mutating the original object + let Saveflows = _.cloneDeep(flows); + if (Saveflows.length !== 0) { + Saveflows.forEach((flow) => { + if (flow.data && flow.data?.nodes) + flow.data?.nodes.forEach((node) => { + // console.log(node.data.type); + //looking for file fields to prevent saving the content and breaking the flow for exceeding the the data limite for local storage + Object.keys(node.data.node.template).forEach((key) => { + // console.log(node.data.node.template[key].type); + if (node.data.node.template[key].type === "file") { + // console.log(node.data.node.template[key]); + node.data.node.template[key].content = null; + node.data.node.template[key].value = ""; + } + }); + }); + }); + window.localStorage.setItem( + "tabsData", + JSON.stringify({ tabId, flows: Saveflows, id }) + ); + } + } + // function loadCookie(cookie: string) { // if (cookie && Object.keys(templates).length > 0) { // let cookieObject: LangFlowState = JSON.parse(cookie); @@ -543,6 +570,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { tabId, setTabId, flows, + save, incrementNodeId, removeFlow, addFlow,