|
1 | | -import { useEffect, useMemo, useState } from 'react' |
2 | | -import { createLogger } from '@/lib/logs/console/logger' |
| 1 | +import { useMemo } from 'react' |
| 2 | +import { hasWorkflowChanged } from '@/lib/workflows/comparison' |
3 | 3 | import { useDebounce } from '@/hooks/use-debounce' |
4 | | -import { useOperationQueueStore } from '@/stores/operation-queue/store' |
5 | 4 | import { useSubBlockStore } from '@/stores/workflows/subblock/store' |
6 | 5 | import { useWorkflowStore } from '@/stores/workflows/workflow/store' |
7 | 6 | import type { WorkflowState } from '@/stores/workflows/workflow/types' |
8 | 7 |
|
9 | | -const logger = createLogger('useChangeDetection') |
10 | | - |
11 | 8 | interface UseChangeDetectionProps { |
12 | 9 | workflowId: string | null |
13 | 10 | deployedState: WorkflowState | null |
14 | 11 | isLoadingDeployedState: boolean |
15 | 12 | } |
16 | 13 |
|
17 | 14 | /** |
18 | | - * Hook to detect changes between current workflow state and deployed state |
19 | | - * Uses API-based change detection for accuracy |
| 15 | + * Detects meaningful changes between current workflow state and deployed state. |
| 16 | + * Performs comparison entirely on the client - no API calls needed. |
20 | 17 | */ |
21 | 18 | export function useChangeDetection({ |
22 | 19 | workflowId, |
23 | 20 | deployedState, |
24 | 21 | isLoadingDeployedState, |
25 | 22 | }: UseChangeDetectionProps) { |
26 | | - const [changeDetected, setChangeDetected] = useState(false) |
27 | | - const [blockStructureVersion, setBlockStructureVersion] = useState(0) |
28 | | - const [edgeStructureVersion, setEdgeStructureVersion] = useState(0) |
29 | | - const [subBlockStructureVersion, setSubBlockStructureVersion] = useState(0) |
30 | | - |
31 | | - // Get current store state for change detection |
32 | | - const currentBlocks = useWorkflowStore((state) => state.blocks) |
33 | | - const currentEdges = useWorkflowStore((state) => state.edges) |
34 | | - const lastSaved = useWorkflowStore((state) => state.lastSaved) |
| 23 | + const blocks = useWorkflowStore((state) => state.blocks) |
| 24 | + const edges = useWorkflowStore((state) => state.edges) |
| 25 | + const loops = useWorkflowStore((state) => state.loops) |
| 26 | + const parallels = useWorkflowStore((state) => state.parallels) |
35 | 27 | const subBlockValues = useSubBlockStore((state) => |
36 | 28 | workflowId ? state.workflowValues[workflowId] : null |
37 | 29 | ) |
38 | 30 |
|
39 | | - // Track structure changes |
40 | | - useEffect(() => { |
41 | | - setBlockStructureVersion((version) => version + 1) |
42 | | - }, [currentBlocks]) |
43 | | - |
44 | | - useEffect(() => { |
45 | | - setEdgeStructureVersion((version) => version + 1) |
46 | | - }, [currentEdges]) |
| 31 | + // Build current state with subblock values merged into blocks |
| 32 | + const currentState = useMemo((): WorkflowState | null => { |
| 33 | + if (!workflowId) return null |
47 | 34 |
|
48 | | - useEffect(() => { |
49 | | - setSubBlockStructureVersion((version) => version + 1) |
50 | | - }, [subBlockValues]) |
| 35 | + const blocksWithSubBlocks: WorkflowState['blocks'] = {} |
| 36 | + for (const [blockId, block] of Object.entries(blocks)) { |
| 37 | + const blockSubValues = subBlockValues?.[blockId] || {} |
| 38 | + const subBlocks: Record<string, any> = {} |
51 | 39 |
|
52 | | - // Reset version counters when workflow changes |
53 | | - useEffect(() => { |
54 | | - setBlockStructureVersion(0) |
55 | | - setEdgeStructureVersion(0) |
56 | | - setSubBlockStructureVersion(0) |
57 | | - }, [workflowId]) |
58 | | - |
59 | | - // Create trigger for status check |
60 | | - const statusCheckTrigger = useMemo(() => { |
61 | | - return JSON.stringify({ |
62 | | - lastSaved: lastSaved ?? 0, |
63 | | - blockVersion: blockStructureVersion, |
64 | | - edgeVersion: edgeStructureVersion, |
65 | | - subBlockVersion: subBlockStructureVersion, |
66 | | - }) |
67 | | - }, [lastSaved, blockStructureVersion, edgeStructureVersion, subBlockStructureVersion]) |
68 | | - |
69 | | - const debouncedStatusCheckTrigger = useDebounce(statusCheckTrigger, 500) |
| 40 | + // Merge subblock values into the block's subBlocks structure |
| 41 | + for (const [subId, value] of Object.entries(blockSubValues)) { |
| 42 | + subBlocks[subId] = { value } |
| 43 | + } |
70 | 44 |
|
71 | | - useEffect(() => { |
72 | | - // Avoid off-by-one false positives: wait until operation queue is idle |
73 | | - const { operations, isProcessing } = useOperationQueueStore.getState() |
74 | | - const hasPendingOps = |
75 | | - isProcessing || operations.some((op) => op.status === 'pending' || op.status === 'processing') |
| 45 | + // Also include existing subBlocks from the block itself |
| 46 | + if (block.subBlocks) { |
| 47 | + for (const [subId, subBlock] of Object.entries(block.subBlocks)) { |
| 48 | + if (!subBlocks[subId]) { |
| 49 | + subBlocks[subId] = subBlock |
| 50 | + } else { |
| 51 | + subBlocks[subId] = { ...subBlock, value: subBlocks[subId].value } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
76 | 55 |
|
77 | | - if (!workflowId || !deployedState) { |
78 | | - setChangeDetected(false) |
79 | | - return |
| 56 | + blocksWithSubBlocks[blockId] = { |
| 57 | + ...block, |
| 58 | + subBlocks, |
| 59 | + } |
80 | 60 | } |
81 | 61 |
|
82 | | - if (isLoadingDeployedState || hasPendingOps) { |
83 | | - return |
| 62 | + return { |
| 63 | + blocks: blocksWithSubBlocks, |
| 64 | + edges, |
| 65 | + loops, |
| 66 | + parallels, |
84 | 67 | } |
| 68 | + }, [workflowId, blocks, edges, loops, parallels, subBlockValues]) |
85 | 69 |
|
86 | | - // Use the workflow status API to get accurate change detection |
87 | | - // This uses the same logic as the deployment API (reading from normalized tables) |
88 | | - const checkForChanges = async () => { |
89 | | - try { |
90 | | - const response = await fetch(`/api/workflows/${workflowId}/status`) |
91 | | - if (response.ok) { |
92 | | - const data = await response.json() |
93 | | - setChangeDetected(data.needsRedeployment || false) |
94 | | - } else { |
95 | | - logger.error('Failed to fetch workflow status:', response.status, response.statusText) |
96 | | - setChangeDetected(false) |
97 | | - } |
98 | | - } catch (error) { |
99 | | - logger.error('Error fetching workflow status:', error) |
100 | | - setChangeDetected(false) |
101 | | - } |
| 70 | + // Compute change detection with debouncing for performance |
| 71 | + const rawChangeDetected = useMemo(() => { |
| 72 | + if (!currentState || !deployedState || isLoadingDeployedState) { |
| 73 | + return false |
102 | 74 | } |
| 75 | + return hasWorkflowChanged(currentState, deployedState) |
| 76 | + }, [currentState, deployedState, isLoadingDeployedState]) |
103 | 77 |
|
104 | | - checkForChanges() |
105 | | - }, [workflowId, deployedState, debouncedStatusCheckTrigger, isLoadingDeployedState]) |
| 78 | + // Debounce to avoid UI flicker during rapid edits |
| 79 | + const changeDetected = useDebounce(rawChangeDetected, 300) |
106 | 80 |
|
107 | | - return { |
108 | | - changeDetected, |
109 | | - setChangeDetected, |
| 81 | + const setChangeDetected = () => { |
| 82 | + // No-op: change detection is now computed, not stateful |
| 83 | + // Kept for API compatibility |
110 | 84 | } |
| 85 | + |
| 86 | + return { changeDetected, setChangeDetected } |
111 | 87 | } |
0 commit comments