Removed manual saving, only autosave now

This commit is contained in:
Lucas Oliveira 2024-01-06 15:28:05 -03:00
commit c3bfcca6e2
6 changed files with 44 additions and 79 deletions

View file

@ -16,7 +16,6 @@ export default function Chat({ flow }: ChatType): JSX.Element {
const [canOpen, setCanOpen] = useState(false); const [canOpen, setCanOpen] = useState(false);
const isBuilt = useFlowStore((state) => state.isBuilt); const isBuilt = useFlowStore((state) => state.isBuilt);
const setIsBuilt = useFlowStore((state) => state.setIsBuilt); const setIsBuilt = useFlowStore((state) => state.setIsBuilt);
const isPending = useFlowStore((state) => state.isPending);
const currentFlowState = useFlowsManagerStore((state) => state.currentFlowState); const currentFlowState = useFlowsManagerStore((state) => state.currentFlowState);
useEffect(() => { useEffect(() => {
@ -55,7 +54,6 @@ export default function Chat({ flow }: ChatType): JSX.Element {
_.cloneDeep(node.data.node?.template) _.cloneDeep(node.data.node?.template)
); );
if ( if (
isPending &&
JSON.stringify(prevNodes) !== JSON.stringify(currentNodes) JSON.stringify(prevNodes) !== JSON.stringify(currentNodes)
) { ) {
setIsBuilt(false); setIsBuilt(false);

View file

@ -13,6 +13,7 @@ import ReactFlow, {
Controls, Controls,
Edge, Edge,
NodeDragHandler, NodeDragHandler,
OnMove,
OnSelectionChangeParams, OnSelectionChangeParams,
SelectionDragHandler, SelectionDragHandler,
addEdge, addEdge,
@ -27,12 +28,13 @@ import useFlowStore from "../../../../stores/flowStore";
import useFlowsManagerStore from "../../../../stores/flowsManagerStore"; import useFlowsManagerStore from "../../../../stores/flowsManagerStore";
import { useTypesStore } from "../../../../stores/typesStore"; import { useTypesStore } from "../../../../stores/typesStore";
import { APIClassType } from "../../../../types/api"; import { APIClassType } from "../../../../types/api";
import { FlowType, NodeType } from "../../../../types/flow"; import { FlowType, NodeType, targetHandleType } from "../../../../types/flow";
import { import {
generateFlow, generateFlow,
generateNodeFromFlow, generateNodeFromFlow,
getNodeId, getNodeId,
isValidConnection, isValidConnection,
scapeJSONParse,
validateSelection, validateSelection,
} from "../../../../utils/reactflowUtils"; } from "../../../../utils/reactflowUtils";
import { cn, getRandomName, isWrappedWithClass } from "../../../../utils/utils"; import { cn, getRandomName, isWrappedWithClass } from "../../../../utils/utils";
@ -74,16 +76,13 @@ export default function Page({
const edges = useFlowStore((state) => state.edges); const edges = useFlowStore((state) => state.edges);
const onNodesChange = useFlowStore((state) => state.onNodesChange); const onNodesChange = useFlowStore((state) => state.onNodesChange);
const onEdgesChange = useFlowStore((state) => state.onEdgesChange); const onEdgesChange = useFlowStore((state) => state.onEdgesChange);
const onConnect = useFlowStore((state) => state.onConnect);
const setNodes = useFlowStore((state) => state.setNodes); const setNodes = useFlowStore((state) => state.setNodes);
const setEdges = useFlowStore((state) => state.setEdges); const setEdges = useFlowStore((state) => state.setEdges);
const deleteNode = useFlowStore((state) => state.deleteNode); const deleteNode = useFlowStore((state) => state.deleteNode);
const deleteEdge = useFlowStore((state) => state.deleteEdge); const deleteEdge = useFlowStore((state) => state.deleteEdge);
const setPending = useFlowStore((state) => state.setPending);
const undo = useFlowsManagerStore((state) => state.undo); const undo = useFlowsManagerStore((state) => state.undo);
const redo = useFlowsManagerStore((state) => state.redo); const redo = useFlowsManagerStore((state) => state.redo);
const takeSnapshot = useFlowsManagerStore((state) => state.takeSnapshot); const takeSnapshot = useFlowsManagerStore((state) => state.takeSnapshot);
const isPending = useFlowStore((state) => state.isPending);
const paste = useFlowStore((state) => state.paste); const paste = useFlowStore((state) => state.paste);
const position = useRef({ x: 0, y: 0 }); const position = useRef({ x: 0, y: 0 });
@ -216,10 +215,15 @@ export default function Page({
}, [takeSnapshot]); }, [takeSnapshot]);
const onNodeDragStop: NodeDragHandler = useCallback(() => { const onNodeDragStop: NodeDragHandler = useCallback(() => {
// 👇 make dragging a node undoable
autoSaveCurrentFlow(nodes, edges, reactFlowInstance?.getViewport()!); autoSaveCurrentFlow(nodes, edges, reactFlowInstance?.getViewport()!);
// 👉 you can place your event handlers here // 👉 you can place your event handlers here
}, [takeSnapshot]); }, [takeSnapshot, autoSaveCurrentFlow, nodes, edges, reactFlowInstance]);
const onMoveEnd: OnMove = useCallback(() => {
// 👇 make moving the canvas undoable
autoSaveCurrentFlow(nodes, edges, reactFlowInstance?.getViewport()!);
}
, [takeSnapshot, autoSaveCurrentFlow, nodes, edges, reactFlowInstance]);
const onSelectionDragStart: SelectionDragHandler = useCallback(() => { const onSelectionDragStart: SelectionDragHandler = useCallback(() => {
// 👇 make dragging a selection undoable // 👇 make dragging a selection undoable
@ -235,6 +239,39 @@ export default function Page({
} }
}, []); }, []);
const onConnect = useCallback(
(connection: Connection) => {
const newEdges = addEdge(
{
...connection,
data: {
targetHandle: scapeJSONParse(connection.targetHandle!),
sourceHandle: scapeJSONParse(connection.sourceHandle!),
},
style: { stroke: "#555" },
className:
((scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text"
? "stroke-foreground "
: "stroke-foreground ") + " stroke-connection",
animated:
(scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text",
},
edges
);
setEdges(newEdges);
useFlowsManagerStore
.getState()
.autoSaveCurrentFlow(
nodes,
newEdges,
reactFlowInstance?.getViewport() ?? { x: 0, y: 0, zoom: 1 }
);
},
[nodes, edges, setEdges, reactFlowInstance, addEdge]
);
const onDrop = useCallback( const onDrop = useCallback(
(event: React.DragEvent) => { (event: React.DragEvent) => {
event.preventDefault(); event.preventDefault();
@ -345,9 +382,6 @@ export default function Page({
setFilterEdge([]); setFilterEdge([]);
}, []); }, []);
const onMove = useCallback(() => {
if (!isPending) setPending(true);
}, [setPending]);
return ( return (
<div className="flex h-full overflow-hidden"> <div className="flex h-full overflow-hidden">
@ -370,7 +404,6 @@ export default function Page({
</div> </div>
<ReactFlow <ReactFlow
nodes={nodes} nodes={nodes}
onMove={onMove}
edges={edges} edges={edges}
onNodesChange={onNodesChange} onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange} onEdgesChange={onEdgesChange}
@ -388,6 +421,7 @@ export default function Page({
onSelectionStart={onSelectionStart} onSelectionStart={onSelectionStart}
connectionLineComponent={ConnectionLineComponent} connectionLineComponent={ConnectionLineComponent}
onDragOver={onDragOver} onDragOver={onDragOver}
onMoveEnd={onMoveEnd}
onDrop={onDrop} onDrop={onDrop}
onSelectionChange={onSelectionChange} onSelectionChange={onSelectionChange}
deleteKeyCode={[]} deleteKeyCode={[]}

View file

@ -42,7 +42,6 @@ export default function ExtraSidebar(): JSX.Element {
const validApiKey = useStoreStore((state) => state.validApiKey); const validApiKey = useStoreStore((state) => state.validApiKey);
const isBuilt = useFlowStore((state) => state.isBuilt); const isBuilt = useFlowStore((state) => state.isBuilt);
const isPending = useFlowStore((state) => state.isPending);
const setErrorData = useAlertStore((state) => state.setErrorData); const setErrorData = useAlertStore((state) => state.setErrorData);
const [dataFilter, setFilterData] = useState(data); const [dataFilter, setFilterData] = useState(data);
const [search, setSearch] = useState(""); const [search, setSearch] = useState("");
@ -302,39 +301,6 @@ export default function ExtraSidebar(): JSX.Element {
)} )}
</div> </div>
</ShadTooltip> </ShadTooltip>
<div className="side-bar-button" data-testid="save-button">
{currentFlow && currentFlow.data && (
<ShadTooltip content="Save" side="top">
<button
className={
"extra-side-bar-buttons " +
(isPending ? "" : "button-disable")
}
onClick={(event) => {
saveFlow(
{
...currentFlow,
data: {
nodes,
edges,
viewport: reactFlowInstance?.getViewport()!,
},
},
true
);
}}
>
<IconComponent
name="Save"
className={
"side-bar-button-size" +
(isPending ? " " : " extra-side-bar-save-disable")
}
/>
</button>
</ShadTooltip>
)}
</div>
</div> </div>
<Separator /> <Separator />
<div className="side-bar-search-div-placement"> <div className="side-bar-search-div-placement">

View file

@ -42,13 +42,11 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
set({ set({
nodes: applyNodeChanges(changes, get().nodes), nodes: applyNodeChanges(changes, get().nodes),
}); });
if (!get().isPending) set({ isPending: true });
}, },
onEdgesChange: (changes: EdgeChange[]) => { onEdgesChange: (changes: EdgeChange[]) => {
set({ set({
edges: applyEdgeChanges(changes, get().edges), edges: applyEdgeChanges(changes, get().edges),
}); });
if (!get().isPending) set({ isPending: true });
}, },
setNodes: (change) => { setNodes: (change) => {
let newChange = typeof change === "function" ? change(get().nodes) : change; let newChange = typeof change === "function" ? change(get().nodes) : change;
@ -96,29 +94,6 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
getNode: (id: string) => { getNode: (id: string) => {
return get().nodes.find((node) => node.id === id); return get().nodes.find((node) => node.id === id);
}, },
onConnect: (connection: Connection) => {
set({
edges: addEdge(
{
...connection,
data: {
targetHandle: scapeJSONParse(connection.targetHandle!),
sourceHandle: scapeJSONParse(connection.sourceHandle!),
},
style: { stroke: "#555" },
className:
((scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text"
? "stroke-foreground "
: "stroke-foreground ") + " stroke-connection",
animated:
(scapeJSONParse(connection.targetHandle!) as targetHandleType)
.type === "Text",
},
get().edges
),
});
},
deleteNode: (nodeId) => { deleteNode: (nodeId) => {
get().setNodes( get().setNodes(
get().nodes.filter((node) => get().nodes.filter((node) =>
@ -229,10 +204,6 @@ const useFlowStore = create<FlowStoreType>((set, get) => ({
}); });
set({ edges: newEdges }); set({ edges: newEdges });
}, },
isPending: false,
setPending: (pending: boolean) => {
set({ isPending: pending });
},
})); }));
export default useFlowStore; export default useFlowStore;

View file

@ -130,7 +130,6 @@ const useFlowsManagerStore = create<FlowsManagerStoreType>((set, get) => ({
); );
//update tabs state //update tabs state
useFlowStore.setState({ isPending: false });
resolve(); resolve();
} }
}) })

View file

@ -18,7 +18,6 @@ export type FlowStoreType = {
setEdges: (update: Edge[] | ((oldState: Edge[]) => Edge[])) => void; setEdges: (update: Edge[] | ((oldState: Edge[]) => Edge[])) => void;
setNode: (id: string, update: Node | ((oldState: Node) => Node)) => void; setNode: (id: string, update: Node | ((oldState: Node) => Node)) => void;
getNode: (id: string) => Node | undefined; getNode: (id: string) => Node | undefined;
onConnect: OnConnect;
deleteNode: (nodeId: string | Array<string>) => void; deleteNode: (nodeId: string | Array<string>) => void;
deleteEdge: (edgeId: string | Array<string>) => void; deleteEdge: (edgeId: string | Array<string>) => void;
paste: ( paste: (
@ -27,6 +26,4 @@ export type FlowStoreType = {
) => void; ) => void;
isBuilt: boolean; isBuilt: boolean;
setIsBuilt: (isBuilt: boolean) => void; setIsBuilt: (isBuilt: boolean) => void;
isPending: boolean;
setPending: (pending: boolean) => void;
}; };