From b4f7285b335e77ce5519cff1b7911e00799df7d4 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Sat, 6 Jan 2024 11:34:36 -0300 Subject: [PATCH] Implemented undoRedo on Zustand --- .../components/parameterComponent/index.tsx | 4 +- .../src/CustomNodes/GenericNode/index.tsx | 4 +- .../components/menuBar/index.tsx | 4 +- .../src/components/inputComponent/index.tsx | 3 - .../components/inputFileComponent/index.tsx | 2 +- .../components/inputListComponent/index.tsx | 6 - .../src/components/intComponent/index.tsx | 2 +- .../components/keypairListComponent/index.tsx | 6 - .../src/components/promptComponent/index.tsx | 2 +- src/frontend/src/contexts/index.tsx | 4 +- src/frontend/src/contexts/undoRedoContext.tsx | 192 ------------------ .../components/PageComponent/index.tsx | 25 ++- .../components/nodeToolbarComponent/index.tsx | 8 +- src/frontend/src/stores/flowsManagerStore.ts | 69 ++++++- .../src/types/zustand/flowsManager/index.ts | 3 + 15 files changed, 100 insertions(+), 234 deletions(-) delete mode 100644 src/frontend/src/contexts/undoRedoContext.tsx diff --git a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx index 959865766..c0a462571 100644 --- a/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx +++ b/src/frontend/src/CustomNodes/GenericNode/components/parameterComponent/index.tsx @@ -26,7 +26,6 @@ import { LANGFLOW_SUPPORTED_TYPES, TOOLTIP_EMPTY, } from "../../../../constants/constants"; -import { undoRedoContext } from "../../../../contexts/undoRedoContext"; import { postCustomComponentUpdate } from "../../../../controllers/API"; import useAlertStore from "../../../../stores/alertStore"; import useFlowStore from "../../../../stores/flowStore"; @@ -88,7 +87,7 @@ export default function ParameterComponent({ const myData = useTypesStore((state) => state.data); - const { takeSnapshot } = useContext(undoRedoContext); + const takeSnapshot = useFlowsManagerStore((state) => state.takeSnapshot); const handleUpdateValues = async (name: string, data: NodeDataType) => { const code = data.node?.template["code"]?.value; @@ -528,7 +527,6 @@ export default function ParameterComponent({ duplicateKey={errorDuplicateKey} onChange={(newValue) => { const valueToNumbers = convertValuesToNumbers(newValue); - data.node!.template[name].value = valueToNumbers; setErrorDuplicateKey(hasDuplicateKeys(valueToNumbers)); handleOnNewValue(valueToNumbers); }} diff --git a/src/frontend/src/CustomNodes/GenericNode/index.tsx b/src/frontend/src/CustomNodes/GenericNode/index.tsx index 3a90c9ce4..339eb73ac 100644 --- a/src/frontend/src/CustomNodes/GenericNode/index.tsx +++ b/src/frontend/src/CustomNodes/GenericNode/index.tsx @@ -7,7 +7,6 @@ import InputComponent from "../../components/inputComponent"; import { Textarea } from "../../components/ui/textarea"; import { priorityFields } from "../../constants/constants"; import { useSSE } from "../../contexts/SSEContext"; -import { undoRedoContext } from "../../contexts/undoRedoContext"; import NodeToolbarComponent from "../../pages/FlowPage/components/nodeToolbarComponent"; import useFlowStore from "../../stores/flowStore"; import { validationStatusType } from "../../types/components"; @@ -17,6 +16,7 @@ import { nodeColors, nodeIconsLucide } from "../../utils/styleUtils"; import { classNames, cn, getFieldTitle } from "../../utils/utils"; import ParameterComponent from "./components/parameterComponent"; import { useTypesStore } from "../../stores/typesStore"; +import useFlowsManagerStore from "../../stores/flowsManagerStore"; export default function GenericNode({ data, @@ -44,7 +44,7 @@ export default function GenericNode({ const [handles, setHandles] = useState([]); let numberOfInputs: boolean[] = []; - const { takeSnapshot } = useContext(undoRedoContext); + const takeSnapshot = useFlowsManagerStore((state) => state.takeSnapshot); function countHandles(): void { numberOfInputs = Object.keys(data.node!.template) diff --git a/src/frontend/src/components/headerComponent/components/menuBar/index.tsx b/src/frontend/src/components/headerComponent/components/menuBar/index.tsx index e2fd345c5..20ac20f04 100644 --- a/src/frontend/src/components/headerComponent/components/menuBar/index.tsx +++ b/src/frontend/src/components/headerComponent/components/menuBar/index.tsx @@ -8,7 +8,6 @@ import { } from "../../../ui/dropdown-menu"; import { useNavigate } from "react-router-dom"; -import { undoRedoContext } from "../../../../contexts/undoRedoContext"; import FlowSettingsModal from "../../../../modals/flowSettingsModal"; import useAlertStore from "../../../../stores/alertStore"; import useFlowsManagerStore from "../../../../stores/flowsManagerStore"; @@ -19,7 +18,8 @@ export const MenuBar = (): JSX.Element => { const addFlow = useFlowsManagerStore((state) => state.addFlow); const currentFlow = useFlowsManagerStore((state) => state.currentFlow); const setErrorData = useAlertStore((state) => state.setErrorData); - const { undo, redo } = useContext(undoRedoContext); + const undo = useFlowsManagerStore((state) => state.undo); + const redo = useFlowsManagerStore((state) => state.redo); const [openSettings, setOpenSettings] = useState(false); const navigate = useNavigate(); diff --git a/src/frontend/src/components/inputComponent/index.tsx b/src/frontend/src/components/inputComponent/index.tsx index 0efd29c0d..8017c5458 100644 --- a/src/frontend/src/components/inputComponent/index.tsx +++ b/src/frontend/src/components/inputComponent/index.tsx @@ -59,9 +59,6 @@ export default function InputComponent({ e.preventDefault(); }} onKeyDown={(e) => { - if (e.ctrlKey && e.key === "c") { - // Perform any actions you need when Ctrl+C is detected - } handleKeyDown(e, value, ""); if (blurOnEnter && e.key === "Enter") refInput.current?.blur(); }} diff --git a/src/frontend/src/components/inputFileComponent/index.tsx b/src/frontend/src/components/inputFileComponent/index.tsx index a365b188c..0d7b180d2 100644 --- a/src/frontend/src/components/inputFileComponent/index.tsx +++ b/src/frontend/src/components/inputFileComponent/index.tsx @@ -20,7 +20,7 @@ export default function InputFileComponent({ // Clear component state useEffect(() => { - if (disabled) { + if (disabled && value !== "") { setMyValue(""); onChange(""); onFileChange(""); diff --git a/src/frontend/src/components/inputListComponent/index.tsx b/src/frontend/src/components/inputListComponent/index.tsx index ae154484d..e98bba304 100644 --- a/src/frontend/src/components/inputListComponent/index.tsx +++ b/src/frontend/src/components/inputListComponent/index.tsx @@ -44,12 +44,6 @@ export default function InputListComponent({ newInputList[idx] = event.target.value; onChange(newInputList); }} - onKeyDown={(e) => { - if (e.ctrlKey && e.key === "Backspace") { - e.preventDefault(); - e.stopPropagation(); - } - }} /> {idx === value.length - 1 ? (