chore(flowsIOStore.ts): remove unused variables and functions for better code readability and maintainability

refactor(flowsIOStore.ts): remove unused variables and functions to improve code quality and reduce complexity
This commit is contained in:
anovazzi1 2024-01-18 16:38:46 -03:00
commit 5e5a64872c
2 changed files with 144 additions and 224 deletions

View file

@ -1,12 +1,11 @@
import { cloneDeep } from "lodash"; import { cloneDeep } from "lodash";
import { create } from "zustand"; import { create } from "zustand";
import { FlowType, NodeDataType, NodeType } from "../types/flow"; import { FlowType, NodeDataType, NodeType } from "../types/flow";
import useFlowStore from "./flowStore";
import { isInputNode, isOutputNode } from "../utils/reactflowUtils";
import useFlowsManagerStore from "./flowsManagerStore";
import useAlertStore from "./alertStore";
import { flowIOStoreType } from "../types/zustand/flowIOStore"; import { flowIOStoreType } from "../types/zustand/flowIOStore";
import { buildVertices } from "../utils/buildUtils"; import { isInputNode, isOutputNode } from "../utils/reactflowUtils";
import useAlertStore from "./alertStore";
import useFlowStore from "./flowStore";
import useFlowsManagerStore from "./flowsManagerStore";
/* const { getNodeId, saveFlow } = useContext(FlowsContext); /* const { getNodeId, saveFlow } = useContext(FlowsContext);
const { setErrorData, setNoticeData } = useContext(alertContext); */ const { setErrorData, setNoticeData } = useContext(alertContext); */
@ -16,23 +15,26 @@ const { setErrorData, setNoticeData } = useAlertStore();
const useFlowIOStore = create<flowIOStoreType>((set, get) => ({ const useFlowIOStore = create<flowIOStoreType>((set, get) => ({
flowPool: {}, flowPool: {},
getFilterEdge: [],
isBuilt: false,
outputTypes: [], outputTypes: [],
inputTypes: [], inputTypes: [],
inputIds: [], inputIds: [],
outputIds: [], outputIds: [],
isBuilding: true,
actualFlow: null,
setFilterEdge: (filterEdge) => { set({getFilterEdge: filterEdge}) }, setFlowPool: (flowPool) => {
setFlowPool: (flowPool) => { set({flowPool}) }, set({ flowPool });
setIsBuilt: (isBuilt) => { set({isBuilt}) }, },
setOutputTypes: (outputTypes) => { set({outputTypes}) }, setOutputTypes: (outputTypes) => {
setInputTypes: (inputTypes) => { set({inputTypes}) }, set({ outputTypes });
setInputIds: (inputIds) => { set({ inputIds }) }, },
setOutputIds: (outputIds) => { set({outputIds}) }, setInputTypes: (inputTypes) => {
setIsBuilding: (isBuilding) => { set({isBuilding}) }, set({ inputTypes });
},
setInputIds: (inputIds) => {
set({ inputIds });
},
setOutputIds: (outputIds) => {
set({ outputIds });
},
updateFlowPoolNodes: (nodes: NodeType[]) => { updateFlowPoolNodes: (nodes: NodeType[]) => {
//this function will update the removing the old ones //this function will update the removing the old ones
@ -60,18 +62,6 @@ const useFlowIOStore = create<flowIOStoreType>((set, get) => ({
CleanFlowPool: () => { CleanFlowPool: () => {
set({ flowPool: {} }); set({ flowPool: {} });
}, },
updateNodeFlowData: (nodeId: string, newData: NodeDataType) => {
let oldNodes = reactFlowInstance?.getNodes();
let targetNode = oldNodes?.find((node) => node.id === nodeId);
if (targetNode) {
targetNode.data = cloneDeep(newData);
reactFlowInstance?.setNodes([...oldNodes!]);
console.log(
"after reactflow update",
JSON.parse(JSON.stringify(reactFlowInstance?.toObject()))
);
}
},
checkInputandOutput: (flow?: FlowType) => { checkInputandOutput: (flow?: FlowType) => {
let has_input = false; let has_input = false;
let has_output = false; let has_output = false;
@ -163,60 +153,7 @@ const useFlowIOStore = create<flowIOStoreType>((set, get) => ({
set({ outputIds }); set({ outputIds });
return outputIds; return outputIds;
}, },
pasteFileOnFLow: async (file?: File) => { /* buildFlow: async (nodeId?: string) => {
if (file) {
const text = await file.text();
let fileData = JSON.parse(text);
if (fileData.flows) {
fileData.flows.forEach((flow: FlowType) => {
paste(
{ nodes: flow!.data!.nodes, edges: flow!.data!.edges },
{ x: 10, y: 10 }
);
});
} else {
let flow: FlowType = JSON.parse(text);
paste(
{ nodes: flow.data!.nodes, edges: flow.data!.edges },
{ x: 10, y: 10 }
);
}
} else {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
// add a change event listener to the file input
input.onchange = async (e: Event) => {
if (
(e.target as HTMLInputElement).files![0].type === "application/json"
) {
const currentfile = (e.target as HTMLInputElement).files![0];
get().pasteFileOnFLow(currentfile);
}
};
// trigger the file input click event to open the file dialog
input.click();
}
},
downloadFlow: (
flow: FlowType,
flowName: string,
flowDescription?: string
) => {
// create a data URI with the current flow data
const jsonString = `data:text/json;chatset=utf-8,${encodeURIComponent(
JSON.stringify({ ...flow, name: flowName, description: flowDescription })
)}`;
// create a link element and set its properties
const link = document.createElement("a");
link.href = jsonString;
link.download = `${flowName && flowName != "" ? flowName : "flow"}.json`;
// simulate a click on the link element to trigger the download
link.click();
},
buildFlow: async (nodeId?: string) => {
function handleBuildUpdate(data: any) { function handleBuildUpdate(data: any) {
get().addDataToFlowPool(data.data[data.id], data.id); get().addDataToFlowPool(data.data[data.id], data.id);
} }
@ -251,7 +188,7 @@ const useFlowIOStore = create<flowIOStoreType>((set, get) => ({
setErrorData({ list, title }); setErrorData({ list, title });
}, },
}); });
}, }, */
})); }));
export default useFlowIOStore; export default useFlowIOStore;

View file

@ -1,6 +1,4 @@
import { ReactFlowInstance } from "reactflow"; import { FlowType, NodeType } from "../../flow";
import { tweakType } from "../../components";
import { FlowType, NodeDataType, NodeType } from "../../flow";
export type chatInputType = { export type chatInputType = {
result: string; result: string;
@ -25,37 +23,22 @@ export type FlowPoolType = {
}; };
export type flowIOStoreType = { export type flowIOStoreType = {
actualFlow: null | FlowType;
flowPool: FlowPoolType; flowPool: FlowPoolType;
getFilterEdge: any[];
isBuilt: boolean;
inputTypes: string[]; inputTypes: string[];
outputTypes: string[]; outputTypes: string[];
inputIds: string[]; inputIds: string[];
outputIds: string[]; outputIds: string[];
isBuilding: boolean;
setFlowPool: (flowPool: FlowPoolType) => void; setFlowPool: (flowPool: FlowPoolType) => void;
setIsBuilding: (state: boolean) => void;
setIsBuilt: (state: boolean) => void;
setOutputTypes: (outputTypes: string[]) => void; setOutputTypes: (outputTypes: string[]) => void;
setInputTypes: (inputTypes: string[]) => void; setInputTypes: (inputTypes: string[]) => void;
setInputIds: (inputIds: string[]) => void; setInputIds: (inputIds: string[]) => void;
setOutputIds: (outputIds: string[]) => void; setOutputIds: (outputIds: string[]) => void;
setFilterEdge: (newState) => void;
updateFlowPoolNodes: (nodes: NodeType[]) => void; updateFlowPoolNodes: (nodes: NodeType[]) => void;
addDataToFlowPool: (data: any, nodeId: string) => void; addDataToFlowPool: (data: any, nodeId: string) => void;
CleanFlowPool: () => void; CleanFlowPool: () => void;
updateNodeFlowData: (nodeId: string, newData: NodeDataType) => void;
checkInputandOutput: (flow?: FlowType) => boolean; checkInputandOutput: (flow?: FlowType) => boolean;
getInputTypes: (flow?: FlowType) => string[]; getInputTypes: (flow?: FlowType) => string[];
getOutputTypes: (flow?: FlowType) => string[]; getOutputTypes: (flow?: FlowType) => string[];
getInputIds: (flow?: FlowType) => string[]; getInputIds: (flow?: FlowType) => string[];
getOutputIds: (flow?: FlowType) => string[]; getOutputIds: (flow?: FlowType) => string[];
pasteFileOnFLow: (file?: File) => Promise<void>;
downloadFlow: (
flow: FlowType,
flowName: string,
flowDescription?: string
) => void;
buildFlow: (nodeId?:string) => Promise<void>;
}; };