💄 style(tabsContext.tsx): add save function to TabsContextInitialValue

🚀 feat(tabsContext.tsx): add save function to save the current state of the flows to local storage
The save function was added to TabsContextInitialValue to provide a default value for the save function. The save function was added to save the current state of the flows to local storage. The function uses lodash's cloneDeep to avoid mutating the original object. It also looks for file fields to prevent saving the content and breaking the flow for exceeding the data limit for local storage. The function then saves the tabId, flows, and id to local storage.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-06-14 15:47:18 -03:00
commit e229510907

View file

@ -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,