1+ import { memo , useCallback } from 'react'
12import { Eye , EyeOff } from 'lucide-react'
23import { Button } from '@/components/ui/button'
34import { createLogger } from '@/lib/logs/console/logger'
@@ -9,31 +10,46 @@ import { useWorkflowStore } from '@/stores/workflows/workflow/store'
910
1011const logger = createLogger ( 'DiffControls' )
1112
12- export function DiffControls ( ) {
13- const {
14- isShowingDiff,
15- isDiffReady,
16- diffWorkflow,
17- toggleDiffView,
18- acceptChanges,
19- rejectChanges,
20- diffMetadata,
21- } = useWorkflowDiffStore ( )
22-
23- const { updatePreviewToolCallState, clearPreviewYaml, currentChat, messages } = useCopilotStore ( )
24- const { activeWorkflowId } = useWorkflowRegistry ( )
13+ export const DiffControls = memo ( function DiffControls ( ) {
14+ // Optimized: Single diff store subscription
15+ const { isShowingDiff, isDiffReady, diffWorkflow, toggleDiffView, acceptChanges, rejectChanges } =
16+ useWorkflowDiffStore (
17+ useCallback (
18+ ( state ) => ( {
19+ isShowingDiff : state . isShowingDiff ,
20+ isDiffReady : state . isDiffReady ,
21+ diffWorkflow : state . diffWorkflow ,
22+ toggleDiffView : state . toggleDiffView ,
23+ acceptChanges : state . acceptChanges ,
24+ rejectChanges : state . rejectChanges ,
25+ } ) ,
26+ [ ]
27+ )
28+ )
29+
30+ // Optimized: Single copilot store subscription for needed values
31+ const { updatePreviewToolCallState, clearPreviewYaml, currentChat, messages } = useCopilotStore (
32+ useCallback (
33+ ( state ) => ( {
34+ updatePreviewToolCallState : state . updatePreviewToolCallState ,
35+ clearPreviewYaml : state . clearPreviewYaml ,
36+ currentChat : state . currentChat ,
37+ messages : state . messages ,
38+ } ) ,
39+ [ ]
40+ )
41+ )
2542
26- // Don't show anything if no diff is available or diff is not ready
27- if ( ! diffWorkflow || ! isDiffReady ) {
28- return null
29- }
43+ const { activeWorkflowId } = useWorkflowRegistry (
44+ useCallback ( ( state ) => ( { activeWorkflowId : state . activeWorkflowId } ) , [ ] )
45+ )
3046
31- const handleToggleDiff = ( ) => {
47+ const handleToggleDiff = useCallback ( ( ) => {
3248 logger . info ( 'Toggling diff view' , { currentState : isShowingDiff } )
3349 toggleDiffView ( )
34- }
50+ } , [ isShowingDiff , toggleDiffView ] )
3551
36- const createCheckpoint = async ( ) => {
52+ const createCheckpoint = useCallback ( async ( ) => {
3753 if ( ! activeWorkflowId || ! currentChat ?. id ) {
3854 logger . warn ( 'Cannot create checkpoint: missing workflowId or chatId' , {
3955 workflowId : activeWorkflowId ,
@@ -184,9 +200,9 @@ export function DiffControls() {
184200 logger . error ( 'Failed to create checkpoint:' , error )
185201 return false
186202 }
187- }
203+ } , [ activeWorkflowId , currentChat , messages ] )
188204
189- const handleAccept = async ( ) => {
205+ const handleAccept = useCallback ( async ( ) => {
190206 logger . info ( 'Accepting proposed changes with backup protection' )
191207
192208 try {
@@ -239,9 +255,9 @@ export function DiffControls() {
239255 console . error ( 'Workflow update failed:' , errorMessage )
240256 alert ( `Failed to save workflow changes: ${ errorMessage } ` )
241257 }
242- }
258+ } , [ createCheckpoint , clearPreviewYaml , updatePreviewToolCallState , acceptChanges ] )
243259
244- const handleReject = ( ) => {
260+ const handleReject = useCallback ( ( ) => {
245261 logger . info ( 'Rejecting proposed changes (optimistic)' )
246262
247263 // Clear preview YAML immediately
@@ -279,6 +295,11 @@ export function DiffControls() {
279295 rejectChanges ( ) . catch ( ( error ) => {
280296 logger . error ( 'Failed to reject changes (background):' , error )
281297 } )
298+ } , [ clearPreviewYaml , updatePreviewToolCallState , rejectChanges ] )
299+
300+ // Don't show anything if no diff is available or diff is not ready
301+ if ( ! diffWorkflow || ! isDiffReady ) {
302+ return null
282303 }
283304
284305 return (
@@ -319,4 +340,4 @@ export function DiffControls() {
319340 </ div >
320341 </ div >
321342 )
322- }
343+ } )
0 commit comments