@@ -18,6 +18,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
1818import { useServerHealth } from '@/hooks/useServerHealth'
1919import { parseJsonc , hasJsoncComments } from '@/lib/jsonc'
2020import { showToast } from '@/lib/toast'
21+ import { invalidateConfigCaches } from '@/lib/queryInvalidation'
2122import type { OpenCodeConfig } from '@/api/types/settings'
2223
2324interface Command {
@@ -86,7 +87,7 @@ export function OpenCodeConfigManager() {
8687 return await settingsApi . reloadOpenCodeConfig ( )
8788 } ,
8889 onSuccess : ( ) => {
89- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
90+ invalidateConfigCaches ( queryClient )
9091 } ,
9192 } )
9293
@@ -95,7 +96,7 @@ export function OpenCodeConfigManager() {
9596 return await settingsApi . restartOpenCodeServer ( )
9697 } ,
9798 onSuccess : ( ) => {
98- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
99+ invalidateConfigCaches ( queryClient )
99100 } ,
100101 } )
101102
@@ -104,17 +105,15 @@ export function OpenCodeConfigManager() {
104105 return await settingsApi . upgradeOpenCode ( )
105106 } ,
106107 onSuccess : ( data ) => {
107- queryClient . invalidateQueries ( { queryKey : [ 'health' ] } )
108- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
108+ invalidateConfigCaches ( queryClient )
109109 if ( data . upgraded ) {
110110 showToast . success ( `Upgraded to v${ data . newVersion } and server restarted` , { id : 'upgrade-opencode' } )
111111 } else {
112112 showToast . success ( 'OpenCode is already up to date' , { id : 'upgrade-opencode' } )
113113 }
114114 } ,
115115 onError : ( error ) => {
116- queryClient . invalidateQueries ( { queryKey : [ 'health' ] } )
117- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
116+ invalidateConfigCaches ( queryClient )
118117
119118 const defaultMessage = 'Failed to upgrade OpenCode'
120119
@@ -186,9 +185,8 @@ export function OpenCodeConfigManager() {
186185 }
187186 } else {
188187 showToast . success ( 'Configuration updated' )
188+ invalidateConfigCaches ( queryClient )
189189 }
190-
191- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
192190 } catch ( error ) {
193191 console . error ( 'Failed to update config:' , error )
194192 showToast . error ( getApiErrorMessage ( error , 'Failed to update config' ) , { id : 'update-restart' } )
@@ -242,7 +240,7 @@ export function OpenCodeConfigManager() {
242240 showToast . success ( 'Configuration created' , { id : 'create-config' } )
243241 }
244242
245- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
243+ invalidateConfigCaches ( queryClient )
246244 } catch ( error ) {
247245 console . error ( 'Failed to create config:' , error )
248246 showToast . error ( getApiErrorMessage ( error , 'Failed to create configuration' ) , { id : 'create-config' } )
@@ -259,9 +257,11 @@ export function OpenCodeConfigManager() {
259257 setIsUpdating ( true )
260258 await settingsApi . deleteOpenCodeConfig ( config . name )
261259 setDeleteConfirmConfig ( null )
260+ if ( selectedConfig ?. id === config . id ) {
261+ setSelectedConfig ( null )
262+ }
262263 fetchConfigs ( )
263- // Invalidate agents cache in case deleted config had agents
264- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
264+ invalidateConfigCaches ( queryClient )
265265 } catch ( error ) {
266266 console . error ( 'Failed to delete config:' , error )
267267 } finally {
@@ -501,7 +501,7 @@ export function OpenCodeConfigManager() {
501501 ? 'Configuration saved and server reloaded'
502502 : 'Configuration saved'
503503 showToast . success ( successMsg , { id : 'edit-config' } )
504- queryClient . invalidateQueries ( { queryKey : [ 'opencode' , 'agents' ] } )
504+ invalidateConfigCaches ( queryClient )
505505 } catch ( error ) {
506506 showToast . error ( getApiErrorMessage ( error , 'Failed to save configuration' ) , { id : 'edit-config' } )
507507 throw error
@@ -586,15 +586,15 @@ export function OpenCodeConfigManager() {
586586 < div className = "flex items-center gap-3 min-w-0" >
587587 < h4 className = "text-sm font-medium truncate" > Commands</ h4 >
588588 < span className = "text-xs text-muted-foreground" >
589- { Object . keys ( selectedConfig . content . command as Record < string , Command > || { } ) . length } configured
589+ { Object . keys ( ( selectedConfig . content ? .command as Record < string , Command > | undefined ) ?? { } ) . length } configured
590590 </ span >
591591 </ div >
592592 < Edit className = { `h-4 w-4 transition-transform ${ expandedSections . commands ? 'rotate-90' : '' } ` } />
593593 </ button >
594594 < div className = { `${ expandedSections . commands ? 'block' : 'hidden' } border-t border-border` } >
595595 < div className = "p-1 sm:p-4 max-h-[50vh] overflow-y-auto" >
596596 < CommandsEditor
597- commands = { ( selectedConfig . content . command as Record < string , Command > ) || { } }
597+ commands = { ( selectedConfig . content ? .command as Record < string , Command > | undefined ) ?? { } }
598598 onChange = { ( commands ) => {
599599 const updatedContent = {
600600 ...selectedConfig . content ,
@@ -623,15 +623,15 @@ export function OpenCodeConfigManager() {
623623 < div className = "flex items-center gap-3 min-w-0" >
624624 < h4 className = "text-sm font-medium truncate" > Agents</ h4 >
625625 < span className = "text-xs text-muted-foreground" >
626- { Object . keys ( selectedConfig . content . agent as Record < string , Agent > || { } ) . length } configured
626+ { Object . keys ( ( selectedConfig . content ? .agent as Record < string , Agent > | undefined ) ?? { } ) . length } configured
627627 </ span >
628628 </ div >
629629 < Edit className = { `h-4 w-4 transition-transform ${ expandedSections . agents ? 'rotate-90' : '' } ` } />
630630 </ button >
631631 < div className = { `${ expandedSections . agents ? 'block' : 'hidden' } border-t border-border` } >
632632 < div className = "p-4 max-h-[50vh] overflow-y-auto" >
633633 < AgentsEditor
634- agents = { ( selectedConfig . content . agent as Record < string , Agent > ) || { } }
634+ agents = { ( selectedConfig . content ? .agent as Record < string , Agent > | undefined ) ?? { } }
635635 onChange = { ( agents ) => {
636636 const updatedContent = {
637637 ...selectedConfig . content ,
@@ -660,7 +660,7 @@ export function OpenCodeConfigManager() {
660660 < div className = "flex items-center gap-3 min-w-0" >
661661 < h4 className = "text-sm font-medium truncate" > MCP Servers</ h4 >
662662 < span className = "text-xs text-muted-foreground" >
663- { Object . keys ( ( selectedConfig . content . mcp as Record < string , unknown > ) || { } ) . length } configured
663+ { Object . keys ( ( selectedConfig . content ? .mcp as Record < string , unknown > | undefined ) ?? { } ) . length } configured
664664 </ span >
665665 </ div >
666666 < Edit className = { `h-4 w-4 transition-transform ${ expandedSections . mcp ? 'rotate-90' : '' } ` } />
0 commit comments