@foresthubai/workflow-builder 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -93
  4. package/dist/components/ui/command.d.ts +2 -2
  5. package/dist/components/ui/input.d.ts +1 -1
  6. package/dist/components/ui/resizable.d.ts +1 -1
  7. package/dist/components/ui/textarea.d.ts +1 -1
  8. package/dist/graph/BaseNode.js +10 -10
  9. package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
  10. package/dist/lib/utils.d.ts +3 -0
  11. package/dist/lib/utils.d.ts.map +1 -0
  12. package/dist/lib/utils.js +6 -0
  13. package/dist/lib/utils.js.map +1 -0
  14. package/dist/toolbars/CanvasTabsToolbar.d.ts +11 -0
  15. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +1 -0
  16. package/dist/toolbars/CanvasTabsToolbar.js +101 -0
  17. package/dist/toolbars/CanvasTabsToolbar.js.map +1 -0
  18. package/package.json +2 -2
  19. package/src/BuilderLayout.tsx +345 -345
  20. package/src/Canvas.tsx +261 -261
  21. package/src/CanvasEditor.tsx +142 -142
  22. package/src/CanvasTabsToolbar.tsx +176 -176
  23. package/src/RightConfigPanel.tsx +266 -266
  24. package/src/WorkflowBuilder.tsx +412 -412
  25. package/src/cn.ts +6 -6
  26. package/src/components/ui/add-button.tsx +39 -39
  27. package/src/components/ui/alert-dialog.tsx +141 -141
  28. package/src/components/ui/alert.tsx +59 -59
  29. package/src/components/ui/badge.tsx +36 -36
  30. package/src/components/ui/button.tsx +85 -85
  31. package/src/components/ui/card.tsx +79 -79
  32. package/src/components/ui/checkbox.tsx +28 -28
  33. package/src/components/ui/collapsible.tsx +9 -9
  34. package/src/components/ui/command.tsx +153 -153
  35. package/src/components/ui/delete-button.tsx +23 -23
  36. package/src/components/ui/dialog.tsx +125 -125
  37. package/src/components/ui/dropdown-menu.tsx +198 -198
  38. package/src/components/ui/input.tsx +55 -55
  39. package/src/components/ui/label.tsx +24 -24
  40. package/src/components/ui/readonly-banner.tsx +15 -15
  41. package/src/components/ui/resizable.tsx +43 -43
  42. package/src/components/ui/scroll-area.tsx +102 -102
  43. package/src/components/ui/select.tsx +160 -160
  44. package/src/components/ui/separator.tsx +29 -29
  45. package/src/components/ui/switch.tsx +27 -27
  46. package/src/components/ui/textarea.tsx +51 -51
  47. package/src/components/ui/toast.tsx +127 -127
  48. package/src/components/ui/toaster.tsx +33 -33
  49. package/src/components/ui/toggle-group.tsx +59 -59
  50. package/src/components/ui/toggle.tsx +43 -43
  51. package/src/components/ui/tooltip.tsx +32 -32
  52. package/src/dialogs/NodePickerDialog.tsx +84 -84
  53. package/src/dialogs/ValidationDialog.tsx +184 -184
  54. package/src/graph/BaseNode.tsx +557 -557
  55. package/src/graph/CustomEdge.tsx +185 -185
  56. package/src/graph/CustomNode.tsx +16 -16
  57. package/src/graph/FunctionCallNode.tsx +30 -30
  58. package/src/graph/PortHandle.tsx +189 -189
  59. package/src/graph/reactFlowRegistry.ts +26 -26
  60. package/src/hooks/use-toast.ts +125 -125
  61. package/src/hooks/useAvailableVariables.ts +20 -20
  62. package/src/hooks/useCanvasHistory.ts +22 -22
  63. package/src/hooks/useCanvasTabs.ts +168 -168
  64. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  65. package/src/hooks/useFunctionRegistry.ts +26 -26
  66. package/src/hooks/useFunctions.ts +44 -44
  67. package/src/hooks/useGraph.ts +161 -161
  68. package/src/hooks/useNodeDefinitions.ts +82 -82
  69. package/src/hooks/useParamErrors.ts +26 -26
  70. package/src/hooks/useResolvedTheme.ts +30 -30
  71. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  72. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  73. package/src/hooks/useWorkflowSerialization.ts +127 -127
  74. package/src/i18n/index.ts +53 -53
  75. package/src/i18n/locales/de.json +501 -501
  76. package/src/i18n/locales/en.json +557 -557
  77. package/src/index.ts +27 -27
  78. package/src/inputs/ExpressionInput.tsx +297 -297
  79. package/src/inputs/ParameterEditor.tsx +515 -515
  80. package/src/inputs/PortSection.tsx +144 -144
  81. package/src/panels/BuilderSidebar.tsx +301 -301
  82. package/src/panels/ChannelConfigPanel.tsx +49 -49
  83. package/src/panels/ChannelsPanel.tsx +28 -28
  84. package/src/panels/DebugConsolePanel.tsx +73 -73
  85. package/src/panels/DebugContextPanel.tsx +77 -77
  86. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  87. package/src/panels/DiagnosticsPanel.tsx +170 -170
  88. package/src/panels/EdgeConfigPanel.tsx +104 -104
  89. package/src/panels/FunctionConfigPanel.tsx +179 -179
  90. package/src/panels/FunctionListPanel.tsx +45 -45
  91. package/src/panels/MemoryConfigPanel.tsx +55 -55
  92. package/src/panels/MemoryPanel.tsx +40 -40
  93. package/src/panels/ModelConfigPanel.tsx +41 -41
  94. package/src/panels/ModelsPanel.tsx +36 -36
  95. package/src/panels/NodeConfigPanel.tsx +630 -630
  96. package/src/panels/NodeLibrary.tsx +288 -288
  97. package/src/panels/ResourceConfigPanel.tsx +132 -132
  98. package/src/panels/ResourceListPanel.tsx +113 -113
  99. package/src/panels/VariableConfigPanel.tsx +161 -161
  100. package/src/panels/VariablesPanel.tsx +145 -145
  101. package/src/stores/canvasStore.test.ts +44 -44
  102. package/src/stores/canvasStore.ts +245 -245
  103. package/src/stores/debugStore.ts +74 -74
  104. package/src/stores/diagnosticsStore.ts +130 -130
  105. package/src/stores/editorStore.ts +202 -202
  106. package/src/styles/index.css +526 -526
  107. package/src/utils/categoryConstants.ts +26 -26
  108. package/src/utils/channelOperations.ts +86 -86
  109. package/src/utils/connectionRules.ts +137 -137
  110. package/src/utils/functionOperations.ts +179 -179
  111. package/src/utils/graphOperations.ts +550 -550
  112. package/src/utils/history.ts +207 -207
  113. package/src/utils/memoryOperations.ts +57 -57
  114. package/src/utils/migrateFunctionNodes.ts +107 -107
  115. package/src/utils/modelOperations.ts +55 -55
  116. package/src/utils/paramDisplay.ts +71 -71
  117. package/src/utils/resourceHelpers.ts +32 -32
  118. package/src/utils/translation.ts +28 -28
  119. package/src/utils/variableOperations.ts +75 -75
  120. package/tailwind-preset.ts +166 -166
@@ -1,125 +1,125 @@
1
- // Standard shadcn toast hook + dispatcher
2
- import * as React from "react";
3
-
4
- import type { ToastActionElement, ToastProps } from "../components/ui/toast";
5
-
6
- const TOAST_LIMIT = 1;
7
- const TOAST_REMOVE_DELAY = 1_000_000;
8
-
9
- type ToasterToast = ToastProps & {
10
- id: string;
11
- title?: React.ReactNode;
12
- description?: React.ReactNode;
13
- action?: ToastActionElement;
14
- };
15
-
16
- const actionTypes = {
17
- ADD_TOAST: "ADD_TOAST",
18
- UPDATE_TOAST: "UPDATE_TOAST",
19
- DISMISS_TOAST: "DISMISS_TOAST",
20
- REMOVE_TOAST: "REMOVE_TOAST",
21
- } as const;
22
-
23
- let count = 0;
24
- function genId(): string {
25
- count = (count + 1) % Number.MAX_SAFE_INTEGER;
26
- return count.toString();
27
- }
28
-
29
- type ActionType = typeof actionTypes;
30
-
31
- type Action =
32
- | { type: ActionType["ADD_TOAST"]; toast: ToasterToast }
33
- | { type: ActionType["UPDATE_TOAST"]; toast: Partial<ToasterToast> & { id: string } }
34
- | { type: ActionType["DISMISS_TOAST"]; toastId?: ToasterToast["id"] }
35
- | { type: ActionType["REMOVE_TOAST"]; toastId?: ToasterToast["id"] };
36
-
37
- interface State {
38
- toasts: ToasterToast[];
39
- }
40
-
41
- const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
42
-
43
- function addToRemoveQueue(toastId: string) {
44
- if (toastTimeouts.has(toastId)) return;
45
- const timeout = setTimeout(() => {
46
- toastTimeouts.delete(toastId);
47
- dispatch({ type: "REMOVE_TOAST", toastId });
48
- }, TOAST_REMOVE_DELAY);
49
- toastTimeouts.set(toastId, timeout);
50
- }
51
-
52
- export const reducer = (state: State, action: Action): State => {
53
- switch (action.type) {
54
- case "ADD_TOAST":
55
- return { ...state, toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT) };
56
- case "UPDATE_TOAST":
57
- return {
58
- ...state,
59
- toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)),
60
- };
61
- case "DISMISS_TOAST": {
62
- const { toastId } = action;
63
- if (toastId) {
64
- addToRemoveQueue(toastId);
65
- } else {
66
- state.toasts.forEach((t) => addToRemoveQueue(t.id));
67
- }
68
- return {
69
- ...state,
70
- toasts: state.toasts.map((t) => (t.id === toastId || toastId === undefined ? { ...t, open: false } : t)),
71
- };
72
- }
73
- case "REMOVE_TOAST":
74
- if (action.toastId === undefined) return { ...state, toasts: [] };
75
- return { ...state, toasts: state.toasts.filter((t) => t.id !== action.toastId) };
76
- }
77
- };
78
-
79
- const listeners: Array<(state: State) => void> = [];
80
- let memoryState: State = { toasts: [] };
81
-
82
- function dispatch(action: Action) {
83
- memoryState = reducer(memoryState, action);
84
- listeners.forEach((listener) => listener(memoryState));
85
- }
86
-
87
- type Toast = Omit<ToasterToast, "id">;
88
-
89
- function toast({ ...props }: Toast) {
90
- const id = genId();
91
- const update = (next: ToasterToast) => dispatch({ type: "UPDATE_TOAST", toast: { ...next, id } });
92
- const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });
93
-
94
- dispatch({
95
- type: "ADD_TOAST",
96
- toast: {
97
- ...props,
98
- id,
99
- open: true,
100
- onOpenChange: (open) => {
101
- if (!open) dismiss();
102
- },
103
- },
104
- });
105
- return { id, dismiss, update };
106
- }
107
-
108
- function useToast() {
109
- const [state, setState] = React.useState<State>(memoryState);
110
- React.useEffect(() => {
111
- listeners.push(setState);
112
- return () => {
113
- const index = listeners.indexOf(setState);
114
- if (index > -1) listeners.splice(index, 1);
115
- };
116
- }, []);
117
-
118
- return {
119
- ...state,
120
- toast,
121
- dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
122
- };
123
- }
124
-
125
- export { useToast, toast };
1
+ // Standard shadcn toast hook + dispatcher
2
+ import * as React from "react";
3
+
4
+ import type { ToastActionElement, ToastProps } from "../components/ui/toast";
5
+
6
+ const TOAST_LIMIT = 1;
7
+ const TOAST_REMOVE_DELAY = 1_000_000;
8
+
9
+ type ToasterToast = ToastProps & {
10
+ id: string;
11
+ title?: React.ReactNode;
12
+ description?: React.ReactNode;
13
+ action?: ToastActionElement;
14
+ };
15
+
16
+ const actionTypes = {
17
+ ADD_TOAST: "ADD_TOAST",
18
+ UPDATE_TOAST: "UPDATE_TOAST",
19
+ DISMISS_TOAST: "DISMISS_TOAST",
20
+ REMOVE_TOAST: "REMOVE_TOAST",
21
+ } as const;
22
+
23
+ let count = 0;
24
+ function genId(): string {
25
+ count = (count + 1) % Number.MAX_SAFE_INTEGER;
26
+ return count.toString();
27
+ }
28
+
29
+ type ActionType = typeof actionTypes;
30
+
31
+ type Action =
32
+ | { type: ActionType["ADD_TOAST"]; toast: ToasterToast }
33
+ | { type: ActionType["UPDATE_TOAST"]; toast: Partial<ToasterToast> & { id: string } }
34
+ | { type: ActionType["DISMISS_TOAST"]; toastId?: ToasterToast["id"] }
35
+ | { type: ActionType["REMOVE_TOAST"]; toastId?: ToasterToast["id"] };
36
+
37
+ interface State {
38
+ toasts: ToasterToast[];
39
+ }
40
+
41
+ const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
42
+
43
+ function addToRemoveQueue(toastId: string) {
44
+ if (toastTimeouts.has(toastId)) return;
45
+ const timeout = setTimeout(() => {
46
+ toastTimeouts.delete(toastId);
47
+ dispatch({ type: "REMOVE_TOAST", toastId });
48
+ }, TOAST_REMOVE_DELAY);
49
+ toastTimeouts.set(toastId, timeout);
50
+ }
51
+
52
+ export const reducer = (state: State, action: Action): State => {
53
+ switch (action.type) {
54
+ case "ADD_TOAST":
55
+ return { ...state, toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT) };
56
+ case "UPDATE_TOAST":
57
+ return {
58
+ ...state,
59
+ toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)),
60
+ };
61
+ case "DISMISS_TOAST": {
62
+ const { toastId } = action;
63
+ if (toastId) {
64
+ addToRemoveQueue(toastId);
65
+ } else {
66
+ state.toasts.forEach((t) => addToRemoveQueue(t.id));
67
+ }
68
+ return {
69
+ ...state,
70
+ toasts: state.toasts.map((t) => (t.id === toastId || toastId === undefined ? { ...t, open: false } : t)),
71
+ };
72
+ }
73
+ case "REMOVE_TOAST":
74
+ if (action.toastId === undefined) return { ...state, toasts: [] };
75
+ return { ...state, toasts: state.toasts.filter((t) => t.id !== action.toastId) };
76
+ }
77
+ };
78
+
79
+ const listeners: Array<(state: State) => void> = [];
80
+ let memoryState: State = { toasts: [] };
81
+
82
+ function dispatch(action: Action) {
83
+ memoryState = reducer(memoryState, action);
84
+ listeners.forEach((listener) => listener(memoryState));
85
+ }
86
+
87
+ type Toast = Omit<ToasterToast, "id">;
88
+
89
+ function toast({ ...props }: Toast) {
90
+ const id = genId();
91
+ const update = (next: ToasterToast) => dispatch({ type: "UPDATE_TOAST", toast: { ...next, id } });
92
+ const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });
93
+
94
+ dispatch({
95
+ type: "ADD_TOAST",
96
+ toast: {
97
+ ...props,
98
+ id,
99
+ open: true,
100
+ onOpenChange: (open) => {
101
+ if (!open) dismiss();
102
+ },
103
+ },
104
+ });
105
+ return { id, dismiss, update };
106
+ }
107
+
108
+ function useToast() {
109
+ const [state, setState] = React.useState<State>(memoryState);
110
+ React.useEffect(() => {
111
+ listeners.push(setState);
112
+ return () => {
113
+ const index = listeners.indexOf(setState);
114
+ if (index > -1) listeners.splice(index, 1);
115
+ };
116
+ }, []);
117
+
118
+ return {
119
+ ...state,
120
+ toast,
121
+ dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
122
+ };
123
+ }
124
+
125
+ export { useToast, toast };
@@ -1,20 +1,20 @@
1
- import { useMemo } from "react";
2
- import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
3
- import { computeAvailableVariables } from "@foresthubai/workflow-core/variable";
4
-
5
- // Re-export types for consumers
6
- export type { Variable as AvailableVariable } from "@foresthubai/workflow-core/variable";
7
-
8
- /**
9
- * Hook that provides access to all available variables for a specific canvas.
10
- * Each canvas is self-contained — main and function canvases do not share scope.
11
- *
12
- * Returns both an array (for iteration/UI) and a record (for O(1) lookup).
13
- */
14
- export const useAvailableVariables = (canvasId: string = MAIN_CANVAS_ID) => {
15
- const store = getOrCreateCanvasStore(canvasId);
16
- const variables = store((s) => s.variables);
17
- const edges = store((s) => s.edges);
18
-
19
- return useMemo(() => computeAvailableVariables(variables, edges), [variables, edges]);
20
- };
1
+ import { useMemo } from "react";
2
+ import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
3
+ import { computeAvailableVariables } from "@foresthubai/workflow-core/variable";
4
+
5
+ // Re-export types for consumers
6
+ export type { Variable as AvailableVariable } from "@foresthubai/workflow-core/variable";
7
+
8
+ /**
9
+ * Hook that provides access to all available variables for a specific canvas.
10
+ * Each canvas is self-contained — main and function canvases do not share scope.
11
+ *
12
+ * Returns both an array (for iteration/UI) and a record (for O(1) lookup).
13
+ */
14
+ export const useAvailableVariables = (canvasId: string = MAIN_CANVAS_ID) => {
15
+ const store = getOrCreateCanvasStore(canvasId);
16
+ const variables = store((s) => s.variables);
17
+ const edges = store((s) => s.edges);
18
+
19
+ return useMemo(() => computeAvailableVariables(variables, edges), [variables, edges]);
20
+ };
@@ -1,22 +1,22 @@
1
- import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
2
-
3
- /**
4
- * Hook that exposes all history functions from a canvas store.
5
- * Each canvas has its own independent undo/redo history.
6
- */
7
- export const useCanvasHistory = (canvasId: string = MAIN_CANVAS_ID) => {
8
- const canvasStore = getOrCreateCanvasStore(canvasId);
9
-
10
- return {
11
- // History actions
12
- undo: canvasStore.undo,
13
- redo: canvasStore.redo,
14
- takeCheckpoint: canvasStore.takeCheckpoint,
15
- withCheckpoint: canvasStore.withCheckpoint,
16
- clearHistory: canvasStore.clearHistory,
17
-
18
- // History state checks
19
- canUndo: canvasStore.canUndo,
20
- canRedo: canvasStore.canRedo,
21
- };
22
- };
1
+ import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
2
+
3
+ /**
4
+ * Hook that exposes all history functions from a canvas store.
5
+ * Each canvas has its own independent undo/redo history.
6
+ */
7
+ export const useCanvasHistory = (canvasId: string = MAIN_CANVAS_ID) => {
8
+ const canvasStore = getOrCreateCanvasStore(canvasId);
9
+
10
+ return {
11
+ // History actions
12
+ undo: canvasStore.undo,
13
+ redo: canvasStore.redo,
14
+ takeCheckpoint: canvasStore.takeCheckpoint,
15
+ withCheckpoint: canvasStore.withCheckpoint,
16
+ clearHistory: canvasStore.clearHistory,
17
+
18
+ // History state checks
19
+ canUndo: canvasStore.canUndo,
20
+ canRedo: canvasStore.canRedo,
21
+ };
22
+ };
@@ -1,168 +1,168 @@
1
- import { useState, useCallback, useEffect } from "react";
2
- import { MAIN_CANVAS_ID } from "../stores/canvasStore";
3
- import { useEditorStore } from "../stores/editorStore";
4
-
5
- // Holds info for a canvas tab. id is identical to canvasId.
6
- export interface CanvasTab {
7
- id: string;
8
- label: string;
9
- }
10
-
11
- /**
12
- * Hook for managing canvas tabs UI.
13
- * Tab switching directly updates the UI store's activeCanvasId.
14
- */
15
- export const useCanvasTabs = () => {
16
- const [tabs, setTabs] = useState<CanvasTab[]>([{ id: MAIN_CANVAS_ID, label: "Main" }]);
17
-
18
- // Get active canvas ID and setter from editor store
19
- const activeCanvasId = useEditorStore((state) => state.activeCanvasId);
20
- const setActiveCanvas = useEditorStore((state) => state.setActiveCanvas);
21
- const selectFunction = useEditorStore((state) => state.selectFunction);
22
-
23
- // Tabs are a projection of the function declarations: a deleted function drops its
24
- // tab (falling back to Main if it was active), a renamed one relabels. Open/close
25
- // of an existing function's tab is still explicit (openTab/closeTab) — this only
26
- // reconciles against the source of truth so the strip can't show a stale function.
27
- const functions = useEditorStore((state) => state.functions);
28
- useEffect(() => {
29
- setTabs((prev) => {
30
- let changed = false;
31
- const next = prev.flatMap<CanvasTab>((t) => {
32
- if (t.id === MAIN_CANVAS_ID) return [t];
33
- const fn = functions[t.id];
34
- if (!fn) {
35
- changed = true;
36
- return [];
37
- }
38
- if (fn.name !== t.label) {
39
- changed = true;
40
- return [{ ...t, label: fn.name }];
41
- }
42
- return [t];
43
- });
44
- return changed ? next : prev;
45
- });
46
- if (activeCanvasId !== MAIN_CANVAS_ID && !functions[activeCanvasId]) {
47
- setActiveCanvas(MAIN_CANVAS_ID);
48
- }
49
- }, [functions, activeCanvasId, setActiveCanvas]);
50
-
51
- // Switch to a tab's canvas. A function tab focuses its declaration (selectFunction
52
- // switches the canvas AND selects the function so its config panel opens) — matching
53
- // the dropdown/sidebar open path; any other tab just switches the canvas.
54
- const setActiveTabId = useCallback(
55
- (tabId: string) => {
56
- if (tabId !== MAIN_CANVAS_ID && functions[tabId]) {
57
- selectFunction(tabId);
58
- } else {
59
- setActiveCanvas(tabId);
60
- }
61
- },
62
- [functions, selectFunction, setActiveCanvas],
63
- );
64
-
65
- // Open a tab for a function (add if not exists, switch to it)
66
- const openTab = useCallback(
67
- (id: string, label: string) => {
68
- setTabs((prev) => {
69
- const existing = prev.find((t) => t.id === id);
70
- if (existing) {
71
- return prev; // Already exists
72
- }
73
- return [...prev, { id, label }];
74
- });
75
- setActiveCanvas(id);
76
- return id;
77
- },
78
- [setActiveCanvas],
79
- );
80
-
81
- // Close a tab (just removes from visible tabs, does NOT delete canvas data)
82
- const closeTab = useCallback(
83
- (tabId: string) => {
84
- // Cannot close main tab
85
- if (tabId === MAIN_CANVAS_ID) return;
86
-
87
- setTabs((prev) => {
88
- const filtered = prev.filter((t) => t.id !== tabId);
89
- // If closing active tab, switch to main
90
- if (tabId === activeCanvasId) {
91
- setActiveCanvas(MAIN_CANVAS_ID);
92
- }
93
- return filtered;
94
- });
95
- },
96
- [activeCanvasId, setActiveCanvas],
97
- );
98
-
99
- // Remove a tab completely (called when function is deleted)
100
- const removeTab = useCallback(
101
- (tabId: string) => {
102
- if (tabId === MAIN_CANVAS_ID) return;
103
-
104
- setTabs((prev) => {
105
- const filtered = prev.filter((t) => t.id !== tabId);
106
- // Switch to main canvas if removing active tab
107
- if (tabId === activeCanvasId) {
108
- setActiveCanvas(MAIN_CANVAS_ID);
109
- }
110
- return filtered;
111
- });
112
- },
113
- [activeCanvasId, setActiveCanvas],
114
- );
115
-
116
- const renameTab = useCallback((tabId: string, newLabel: string) => {
117
- setTabs((prev) => prev.map((t) => (t.id === tabId ? { ...t, label: newLabel } : t)));
118
- }, []);
119
-
120
- const reorderTabs = useCallback((fromIndex: number, toIndex: number) => {
121
- if (fromIndex === 0 || toIndex === 0) return;
122
- setTabs((prev) => {
123
- const updated = [...prev];
124
- const [moved] = updated.splice(fromIndex, 1);
125
- if (!moved) return prev;
126
- updated.splice(toIndex, 0, moved);
127
- return updated;
128
- });
129
- }, []);
130
-
131
- // Reset to main canvas only (closes all function tabs)
132
- const resetToMain = useCallback(() => {
133
- setTabs([{ id: MAIN_CANVAS_ID, label: "Main" }]);
134
- setActiveCanvas(MAIN_CANVAS_ID);
135
- }, [setActiveCanvas]);
136
-
137
- // Restore a previously saved tab state
138
- const restoreTabState = useCallback(
139
- (savedTabs: CanvasTab[], savedActiveId: string) => {
140
- setTabs(savedTabs);
141
- setActiveCanvas(savedActiveId);
142
- },
143
- [setActiveCanvas],
144
- );
145
-
146
- // Get label for a tab
147
- const getTabLabel = useCallback(
148
- (tabId: string) => {
149
- return tabs.find((t) => t.id === tabId)?.label || "Unknown";
150
- },
151
- [tabs],
152
- );
153
-
154
- return {
155
- tabs,
156
- activeTabId: activeCanvasId,
157
- setActiveTabId,
158
- openTab,
159
- closeTab,
160
- removeTab,
161
- renameTab,
162
- reorderTabs,
163
- resetToMain,
164
- restoreTabState,
165
- getTabLabel,
166
- isMainCanvas: activeCanvasId === MAIN_CANVAS_ID,
167
- };
168
- };
1
+ import { useState, useCallback, useEffect } from "react";
2
+ import { MAIN_CANVAS_ID } from "../stores/canvasStore";
3
+ import { useEditorStore } from "../stores/editorStore";
4
+
5
+ // Holds info for a canvas tab. id is identical to canvasId.
6
+ export interface CanvasTab {
7
+ id: string;
8
+ label: string;
9
+ }
10
+
11
+ /**
12
+ * Hook for managing canvas tabs UI.
13
+ * Tab switching directly updates the UI store's activeCanvasId.
14
+ */
15
+ export const useCanvasTabs = () => {
16
+ const [tabs, setTabs] = useState<CanvasTab[]>([{ id: MAIN_CANVAS_ID, label: "Main" }]);
17
+
18
+ // Get active canvas ID and setter from editor store
19
+ const activeCanvasId = useEditorStore((state) => state.activeCanvasId);
20
+ const setActiveCanvas = useEditorStore((state) => state.setActiveCanvas);
21
+ const selectFunction = useEditorStore((state) => state.selectFunction);
22
+
23
+ // Tabs are a projection of the function declarations: a deleted function drops its
24
+ // tab (falling back to Main if it was active), a renamed one relabels. Open/close
25
+ // of an existing function's tab is still explicit (openTab/closeTab) — this only
26
+ // reconciles against the source of truth so the strip can't show a stale function.
27
+ const functions = useEditorStore((state) => state.functions);
28
+ useEffect(() => {
29
+ setTabs((prev) => {
30
+ let changed = false;
31
+ const next = prev.flatMap<CanvasTab>((t) => {
32
+ if (t.id === MAIN_CANVAS_ID) return [t];
33
+ const fn = functions[t.id];
34
+ if (!fn) {
35
+ changed = true;
36
+ return [];
37
+ }
38
+ if (fn.name !== t.label) {
39
+ changed = true;
40
+ return [{ ...t, label: fn.name }];
41
+ }
42
+ return [t];
43
+ });
44
+ return changed ? next : prev;
45
+ });
46
+ if (activeCanvasId !== MAIN_CANVAS_ID && !functions[activeCanvasId]) {
47
+ setActiveCanvas(MAIN_CANVAS_ID);
48
+ }
49
+ }, [functions, activeCanvasId, setActiveCanvas]);
50
+
51
+ // Switch to a tab's canvas. A function tab focuses its declaration (selectFunction
52
+ // switches the canvas AND selects the function so its config panel opens) — matching
53
+ // the dropdown/sidebar open path; any other tab just switches the canvas.
54
+ const setActiveTabId = useCallback(
55
+ (tabId: string) => {
56
+ if (tabId !== MAIN_CANVAS_ID && functions[tabId]) {
57
+ selectFunction(tabId);
58
+ } else {
59
+ setActiveCanvas(tabId);
60
+ }
61
+ },
62
+ [functions, selectFunction, setActiveCanvas],
63
+ );
64
+
65
+ // Open a tab for a function (add if not exists, switch to it)
66
+ const openTab = useCallback(
67
+ (id: string, label: string) => {
68
+ setTabs((prev) => {
69
+ const existing = prev.find((t) => t.id === id);
70
+ if (existing) {
71
+ return prev; // Already exists
72
+ }
73
+ return [...prev, { id, label }];
74
+ });
75
+ setActiveCanvas(id);
76
+ return id;
77
+ },
78
+ [setActiveCanvas],
79
+ );
80
+
81
+ // Close a tab (just removes from visible tabs, does NOT delete canvas data)
82
+ const closeTab = useCallback(
83
+ (tabId: string) => {
84
+ // Cannot close main tab
85
+ if (tabId === MAIN_CANVAS_ID) return;
86
+
87
+ setTabs((prev) => {
88
+ const filtered = prev.filter((t) => t.id !== tabId);
89
+ // If closing active tab, switch to main
90
+ if (tabId === activeCanvasId) {
91
+ setActiveCanvas(MAIN_CANVAS_ID);
92
+ }
93
+ return filtered;
94
+ });
95
+ },
96
+ [activeCanvasId, setActiveCanvas],
97
+ );
98
+
99
+ // Remove a tab completely (called when function is deleted)
100
+ const removeTab = useCallback(
101
+ (tabId: string) => {
102
+ if (tabId === MAIN_CANVAS_ID) return;
103
+
104
+ setTabs((prev) => {
105
+ const filtered = prev.filter((t) => t.id !== tabId);
106
+ // Switch to main canvas if removing active tab
107
+ if (tabId === activeCanvasId) {
108
+ setActiveCanvas(MAIN_CANVAS_ID);
109
+ }
110
+ return filtered;
111
+ });
112
+ },
113
+ [activeCanvasId, setActiveCanvas],
114
+ );
115
+
116
+ const renameTab = useCallback((tabId: string, newLabel: string) => {
117
+ setTabs((prev) => prev.map((t) => (t.id === tabId ? { ...t, label: newLabel } : t)));
118
+ }, []);
119
+
120
+ const reorderTabs = useCallback((fromIndex: number, toIndex: number) => {
121
+ if (fromIndex === 0 || toIndex === 0) return;
122
+ setTabs((prev) => {
123
+ const updated = [...prev];
124
+ const [moved] = updated.splice(fromIndex, 1);
125
+ if (!moved) return prev;
126
+ updated.splice(toIndex, 0, moved);
127
+ return updated;
128
+ });
129
+ }, []);
130
+
131
+ // Reset to main canvas only (closes all function tabs)
132
+ const resetToMain = useCallback(() => {
133
+ setTabs([{ id: MAIN_CANVAS_ID, label: "Main" }]);
134
+ setActiveCanvas(MAIN_CANVAS_ID);
135
+ }, [setActiveCanvas]);
136
+
137
+ // Restore a previously saved tab state
138
+ const restoreTabState = useCallback(
139
+ (savedTabs: CanvasTab[], savedActiveId: string) => {
140
+ setTabs(savedTabs);
141
+ setActiveCanvas(savedActiveId);
142
+ },
143
+ [setActiveCanvas],
144
+ );
145
+
146
+ // Get label for a tab
147
+ const getTabLabel = useCallback(
148
+ (tabId: string) => {
149
+ return tabs.find((t) => t.id === tabId)?.label || "Unknown";
150
+ },
151
+ [tabs],
152
+ );
153
+
154
+ return {
155
+ tabs,
156
+ activeTabId: activeCanvasId,
157
+ setActiveTabId,
158
+ openTab,
159
+ closeTab,
160
+ removeTab,
161
+ renameTab,
162
+ reorderTabs,
163
+ resetToMain,
164
+ restoreTabState,
165
+ getTabLabel,
166
+ isMainCanvas: activeCanvasId === MAIN_CANVAS_ID,
167
+ };
168
+ };