1
1
import { v1 } from "@docker/extension-api-client-types" ;
2
- import { getStoredConfig , syncRegistryWithConfig } from '../Registry' ;
2
+ import { getStoredConfig , syncConfigWithRegistry } from '../Registry' ;
3
3
import { POLL_INTERVAL } from '../Constants' ;
4
4
import { useQuery , useQueryClient , useMutation } from '@tanstack/react-query' ;
5
5
import { CatalogItemRichened , CatalogItemWithName } from '../types/catalog' ;
@@ -20,7 +20,25 @@ export function useConfig(client: v1.DockerDesktopClient) {
20
20
const queryClient = useQueryClient ( ) ;
21
21
const configRef = useRef < any > ( null ) ;
22
22
23
- // Load config with React Query
23
+
24
+ // Sync config with registry - use the exact types from Registry.ts
25
+ const syncConfigWithRegistryMutation = useMutation ( {
26
+ mutationFn : async ( registryItems : { [ key : string ] : { ref : string ; config : any } } ) => {
27
+ try {
28
+ if ( ! config ) return { success : false } ;
29
+ await syncConfigWithRegistry ( client , registryItems , config ) ;
30
+ return { success : true } ;
31
+ } catch ( error ) {
32
+ console . error ( 'Failed to sync config with registry:' , error ) ;
33
+ throw error ;
34
+ }
35
+ } ,
36
+ onSuccess : async ( ) => {
37
+ // Refetch config to ensure UI is in sync after registry sync
38
+ await refetchConfig ( ) ;
39
+ }
40
+ } ) ;
41
+
24
42
const {
25
43
data : config = undefined ,
26
44
refetch : refetchConfig ,
@@ -40,11 +58,10 @@ export function useConfig(client: v1.DockerDesktopClient) {
40
58
}
41
59
} ,
42
60
refetchInterval : POLL_INTERVAL ,
43
- staleTime : 30000 , // Data remains fresh for 30 seconds
61
+ staleTime : 30000 ,
44
62
gcTime : 300000 ,
45
63
} ) ;
46
64
47
- // Save config mutation
48
65
const saveConfigMutation = useMutation ( {
49
66
mutationFn : async ( { itemName, newConfig } : { itemName : string , newConfig : { [ key : string ] : any } } ) => {
50
67
try {
@@ -60,22 +77,17 @@ export function useConfig(client: v1.DockerDesktopClient) {
60
77
} , client . host . platform ) ;
61
78
62
79
await tryRunImageSync ( client , [ '--rm' , '-v' , 'docker-prompts:/docker-prompts' , '--workdir' , '/docker-prompts' , 'vonwig/function_write_files:latest' , payload ] ) ;
63
-
64
- // Update our ref with the new state after successful save
65
80
const updatedConfigRef = JSON . parse ( JSON . stringify ( updatedConfig ) ) ;
66
81
configRef . current = updatedConfigRef ;
67
82
return { itemName, updatedConfig : updatedConfigRef } ;
68
83
} catch ( error ) {
69
84
client . desktopUI . toast . error ( 'Failed to update config: ' + error ) ;
70
- // Treat YAML file write failures as fatal, no rollback
71
85
throw error ;
72
86
}
73
87
} ,
74
88
onMutate : async ( { itemName, newConfig } ) => {
75
- // Cancel any outgoing refetches
76
89
await queryClient . cancelQueries ( { queryKey : [ 'config' ] } ) ;
77
90
78
- // Optimistically update to the new value
79
91
const updatedConfig = {
80
92
...( queryClient . getQueryData ( [ 'config' ] ) as Record < string , any > || { } ) ,
81
93
[ itemName ] : newConfig
@@ -85,29 +97,10 @@ export function useConfig(client: v1.DockerDesktopClient) {
85
97
} ,
86
98
onSuccess : ( data ) => {
87
99
client . desktopUI . toast . success ( 'Config saved successfully.' ) ;
88
- // Update the cached data with the new config
89
100
queryClient . setQueryData ( [ 'config' ] , data . updatedConfig ) ;
90
101
}
91
102
} ) ;
92
103
93
- // Sync config with registry - use the exact types from Registry.ts
94
- const syncRegistryMutation = useMutation ( {
95
- mutationFn : async ( registryItems : { [ key : string ] : { ref : string ; config : any } } ) => {
96
- try {
97
- if ( ! config ) return { success : false } ;
98
- await syncRegistryWithConfig ( client , registryItems , config ) ;
99
- return { success : true } ;
100
- } catch ( error ) {
101
- console . error ( 'Failed to sync config with registry:' , error ) ;
102
- throw error ;
103
- }
104
- } ,
105
- onSuccess : async ( ) => {
106
- // Refetch config to ensure UI is in sync after registry sync
107
- await refetchConfig ( ) ;
108
- }
109
- } ) ;
110
-
111
104
const tryLoadConfig = async ( ) => {
112
105
await refetchConfig ( ) ;
113
106
} ;
@@ -123,27 +116,11 @@ export function useConfig(client: v1.DockerDesktopClient) {
123
116
}
124
117
} ;
125
118
126
- // Make this function handle both with and without config, by ensuring config is present
127
- const syncConfigWithRegistry = async ( registryItems : { [ key : string ] : { ref : string ; config ?: any } } ) => {
128
- // Convert to the format expected by syncRegistryWithConfig
129
- const formattedRegistry : { [ key : string ] : { ref : string ; config : any } } = { } ;
130
-
131
- // Ensure all registry items have a config property, even if empty
132
- for ( const [ key , item ] of Object . entries ( registryItems ) ) {
133
- formattedRegistry [ key ] = {
134
- ref : item . ref ,
135
- config : item . config || { }
136
- } ;
137
- }
138
-
139
- await syncRegistryMutation . mutateAsync ( formattedRegistry ) ;
140
- } ;
141
-
142
119
return {
143
120
config,
144
121
configLoading,
145
122
tryLoadConfig,
146
123
saveConfig,
147
- syncConfigWithRegistry
124
+ syncConfigWithRegistry : syncConfigWithRegistryMutation . mutateAsync
148
125
} ;
149
126
}
0 commit comments