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,51 @@ 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 ( )
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+ )
2242
23- const { updatePreviewToolCallState, clearPreviewYaml, currentChat, messages } = useCopilotStore ( )
24- const { activeWorkflowId } = useWorkflowRegistry ( )
43+ const { activeWorkflowId } = useWorkflowRegistry (
44+ useCallback ( ( state ) => ( { activeWorkflowId : state . activeWorkflowId } ) , [ ] )
45+ )
2546
2647 // Don't show anything if no diff is available or diff is not ready
2748 if ( ! diffWorkflow || ! isDiffReady ) {
2849 return null
2950 }
3051
31- const handleToggleDiff = ( ) => {
52+ const handleToggleDiff = useCallback ( ( ) => {
3253 logger . info ( 'Toggling diff view' , { currentState : isShowingDiff } )
3354 toggleDiffView ( )
34- }
55+ } , [ isShowingDiff , toggleDiffView ] )
3556
36- const createCheckpoint = async ( ) => {
57+ const createCheckpoint = useCallback ( async ( ) => {
3758 if ( ! activeWorkflowId || ! currentChat ?. id ) {
3859 logger . warn ( 'Cannot create checkpoint: missing workflowId or chatId' , {
3960 workflowId : activeWorkflowId ,
@@ -184,9 +205,9 @@ export function DiffControls() {
184205 logger . error ( 'Failed to create checkpoint:' , error )
185206 return false
186207 }
187- }
208+ } , [ activeWorkflowId , currentChat , messages ] )
188209
189- const handleAccept = async ( ) => {
210+ const handleAccept = useCallback ( async ( ) => {
190211 logger . info ( 'Accepting proposed changes with backup protection' )
191212
192213 try {
@@ -239,9 +260,9 @@ export function DiffControls() {
239260 console . error ( 'Workflow update failed:' , errorMessage )
240261 alert ( `Failed to save workflow changes: ${ errorMessage } ` )
241262 }
242- }
263+ } , [ createCheckpoint , clearPreviewYaml , updatePreviewToolCallState , acceptChanges ] )
243264
244- const handleReject = ( ) => {
265+ const handleReject = useCallback ( ( ) => {
245266 logger . info ( 'Rejecting proposed changes (optimistic)' )
246267
247268 // Clear preview YAML immediately
@@ -279,7 +300,7 @@ export function DiffControls() {
279300 rejectChanges ( ) . catch ( ( error ) => {
280301 logger . error ( 'Failed to reject changes (background):' , error )
281302 } )
282- }
303+ } , [ clearPreviewYaml , updatePreviewToolCallState , rejectChanges ] )
283304
284305 return (
285306 < div className = '-translate-x-1/2 fixed bottom-20 left-1/2 z-30' >
@@ -319,4 +340,4 @@ export function DiffControls() {
319340 </ div >
320341 </ div >
321342 )
322- }
343+ } )
0 commit comments