11import { v1 } from "@docker/extension-api-client-types" ;
2- import { getStoredConfig , syncRegistryWithConfig } from '../Registry' ;
2+ import { getStoredConfig , syncConfigWithRegistry } from '../Registry' ;
33import { POLL_INTERVAL } from '../Constants' ;
44import { useQuery , useQueryClient , useMutation } from '@tanstack/react-query' ;
55import { CatalogItemRichened , CatalogItemWithName } from '../types/catalog' ;
@@ -20,7 +20,25 @@ export function useConfig(client: v1.DockerDesktopClient) {
2020 const queryClient = useQueryClient ( ) ;
2121 const configRef = useRef < any > ( null ) ;
2222
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+
2442 const {
2543 data : config = undefined ,
2644 refetch : refetchConfig ,
@@ -40,11 +58,10 @@ export function useConfig(client: v1.DockerDesktopClient) {
4058 }
4159 } ,
4260 refetchInterval : POLL_INTERVAL ,
43- staleTime : 30000 , // Data remains fresh for 30 seconds
61+ staleTime : 30000 ,
4462 gcTime : 300000 ,
4563 } ) ;
4664
47- // Save config mutation
4865 const saveConfigMutation = useMutation ( {
4966 mutationFn : async ( { itemName, newConfig } : { itemName : string , newConfig : { [ key : string ] : any } } ) => {
5067 try {
@@ -60,22 +77,17 @@ export function useConfig(client: v1.DockerDesktopClient) {
6077 } , client . host . platform ) ;
6178
6279 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
6580 const updatedConfigRef = JSON . parse ( JSON . stringify ( updatedConfig ) ) ;
6681 configRef . current = updatedConfigRef ;
6782 return { itemName, updatedConfig : updatedConfigRef } ;
6883 } catch ( error ) {
6984 client . desktopUI . toast . error ( 'Failed to update config: ' + error ) ;
70- // Treat YAML file write failures as fatal, no rollback
7185 throw error ;
7286 }
7387 } ,
7488 onMutate : async ( { itemName, newConfig } ) => {
75- // Cancel any outgoing refetches
7689 await queryClient . cancelQueries ( { queryKey : [ 'config' ] } ) ;
7790
78- // Optimistically update to the new value
7991 const updatedConfig = {
8092 ...( queryClient . getQueryData ( [ 'config' ] ) as Record < string , any > || { } ) ,
8193 [ itemName ] : newConfig
@@ -85,29 +97,10 @@ export function useConfig(client: v1.DockerDesktopClient) {
8597 } ,
8698 onSuccess : ( data ) => {
8799 client . desktopUI . toast . success ( 'Config saved successfully.' ) ;
88- // Update the cached data with the new config
89100 queryClient . setQueryData ( [ 'config' ] , data . updatedConfig ) ;
90101 }
91102 } ) ;
92103
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-
111104 const tryLoadConfig = async ( ) => {
112105 await refetchConfig ( ) ;
113106 } ;
@@ -123,27 +116,11 @@ export function useConfig(client: v1.DockerDesktopClient) {
123116 }
124117 } ;
125118
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-
142119 return {
143120 config,
144121 configLoading,
145122 tryLoadConfig,
146123 saveConfig,
147- syncConfigWithRegistry
124+ syncConfigWithRegistry : syncConfigWithRegistryMutation . mutateAsync
148125 } ;
149126}
0 commit comments