@@ -12,25 +12,51 @@ import { useTypesStore } from "@/stores/typesStore";
1212import { useUtilityStore } from "@/stores/utilityStore" ;
1313import { cn } from "@/utils/utils" ;
1414import { 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