Skip to content

Commit 114c25e

Browse files
authored
feat: Enhance component update system with edge validation alerts (#6716)
✨ (PageComponent/index.tsx): Refactor UpdateAllComponents rendering logic to improve readability and maintainability 🔧 (UpdateAllComponents/index.tsx): Add error messages as constants for better code organization and reusability ♻️ (UpdateAllComponents/index.tsx): Refactor UpdateAllComponents to use useRef and useMemo hooks for better performance and edge case handling
1 parent 02617ff commit 114c25e

File tree

2 files changed

+38
-17
lines changed
  • src/frontend/src/pages/FlowPage/components

2 files changed

+38
-17
lines changed

src/frontend/src/pages/FlowPage/components/PageComponent/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default function Page({ view }: { view?: boolean }): JSX.Element {
8989
const setPositionDictionary = useFlowStore(
9090
(state) => state.setPositionDictionary,
9191
);
92-
9392
const reactFlowInstance = useFlowStore((state) => state.reactFlowInstance);
9493
const setReactFlowInstance = useFlowStore(
9594
(state) => state.setReactFlowInstance,
@@ -612,7 +611,9 @@ export default function Page({ view }: { view?: boolean }): JSX.Element {
612611
<span className="text-foreground">Components</span>
613612
</SidebarTrigger>
614613
</Panel>
615-
{componentsToUpdate.length > 0 && <UpdateAllComponents />}
614+
<div className={cn(componentsToUpdate.length === 0 && "hidden")}>
615+
<UpdateAllComponents />
616+
</div>
616617
<SelectionMenu
617618
lastSelection={lastSelection}
618619
isVisible={selectionMenuVisible}

src/frontend/src/pages/FlowPage/components/UpdateAllComponents/index.tsx

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,51 @@ import { useTypesStore } from "@/stores/typesStore";
1212
import { useUtilityStore } from "@/stores/utilityStore";
1313
import { cn } from "@/utils/utils";
1414
import { useUpdateNodeInternals } from "@xyflow/react";
15-
import { useState } from "react";
15+
import { useEffect, useMemo, useRef, useState } from "react";
1616

17-
export default function UpdateAllComponents() {
17+
const ERROR_MESSAGE_UPDATING_COMPONENTS = "Error updating components";
18+
const ERROR_MESSAGE_UPDATING_COMPONENTS_LIST = [
19+
"There was an error updating the components.",
20+
"If the error persists, please report it on our Discord or GitHub.",
21+
];
22+
const ERROR_MESSAGE_EDGES_LOST =
23+
"Some edges were lost after updating the components. Please review the flow and reconnect them.";
24+
25+
export default function UpdateAllComponents({}: {}) {
1826
const { componentsToUpdate, nodes, edges, setNodes } = useFlowStore();
19-
const updateNodeInternals = useUpdateNodeInternals();
27+
const setDismissAll = useUtilityStore((state) => state.setDismissAll);
2028
const templates = useTypesStore((state) => state.templates);
2129
const setErrorData = useAlertStore((state) => state.setErrorData);
22-
const [loadingUpdate, setLoadingUpdate] = useState(false);
23-
2430
const { mutateAsync: validateComponentCode } = usePostValidateComponentCode();
2531
const takeSnapshot = useFlowsManagerStore((state) => state.takeSnapshot);
2632

33+
const updateNodeInternals = useUpdateNodeInternals();
2734
const updateAllNodes = useUpdateAllNodes(setNodes, updateNodeInternals);
2835

36+
const [loadingUpdate, setLoadingUpdate] = useState(false);
2937
const [dismissed, setDismissed] = useState(false);
3038

31-
const setDismissAll = useUtilityStore((state) => state.setDismissAll);
39+
const numberOfEdgesBeforeUpdate = useRef(0);
40+
41+
useMemo(() => {
42+
if (
43+
numberOfEdgesBeforeUpdate.current > 0 &&
44+
edges.length !== numberOfEdgesBeforeUpdate.current
45+
) {
46+
useAlertStore.getState().setNoticeData({
47+
title: ERROR_MESSAGE_EDGES_LOST,
48+
});
49+
}
50+
}, [edges]);
51+
52+
const getSuccessTitle = (updatedCount: number) => {
53+
return `Successfully updated ${updatedCount} component${
54+
updatedCount > 1 ? "s" : ""
55+
}`;
56+
};
3257

3358
const handleUpdateAllComponents = () => {
59+
numberOfEdgesBeforeUpdate.current = edges.length;
3460
setLoadingUpdate(true);
3561
takeSnapshot();
3662

@@ -77,23 +103,17 @@ export default function UpdateAllComponents() {
77103
Promise.all(updatePromises)
78104
.then(() => {
79105
if (updatedCount > 0) {
80-
// Batch update all nodes at once
81106
updateAllNodes(updates);
82107

83108
useAlertStore.getState().setSuccessData({
84-
title: `Successfully updated ${updatedCount} component${
85-
updatedCount > 1 ? "s" : ""
86-
}`,
109+
title: getSuccessTitle(updatedCount),
87110
});
88111
}
89112
})
90113
.catch((error) => {
91114
setErrorData({
92-
title: "Error updating components",
93-
list: [
94-
"There was an error updating the components.",
95-
"If the error persists, please report it on our Discord or GitHub.",
96-
],
115+
title: ERROR_MESSAGE_UPDATING_COMPONENTS,
116+
list: ERROR_MESSAGE_UPDATING_COMPONENTS_LIST,
97117
});
98118
console.error(error);
99119
})

0 commit comments

Comments
 (0)