1
1
import { v1 } from "@docker/extension-api-client-types" ;
2
- import { ExecResult } from '@docker/extension-api-client-types/dist/v0' ;
3
- import { useQuery } from '@tanstack/react-query' ;
2
+ import { useEffect , useState } from "react" ;
4
3
import { BUSYBOX } from "../Constants" ;
5
4
6
- // List of required images for the extension
7
- const REQUIRED_IMAGES = [ BUSYBOX ] ;
8
-
9
- // Image loading state interface
10
- interface ImageState {
11
- status : 'idle' | 'loading' | 'success' | 'error' ;
12
- result ?: ExecResult ;
13
- error ?: unknown ;
14
- }
15
-
16
5
export function useRequiredImages ( client : v1 . DockerDesktopClient ) {
17
- // Create queries for each required image
18
- const imageQueries = REQUIRED_IMAGES . map ( imageName => {
19
- return useQuery ( {
20
- queryKey : [ 'image' , imageName ] ,
21
- queryFn : async ( ) => {
22
- try {
23
- return await client . docker . cli . exec ( 'pull' , [ imageName ] ) ;
24
- } catch ( error ) {
25
- client . desktopUI . toast . error ( `Failed to pull image ${ imageName } : ${ error } ` ) ;
26
- throw error ;
27
- }
28
- } ,
29
- staleTime : Infinity ,
30
- refetchOnWindowFocus : false ,
31
- refetchOnMount : false ,
32
- } ) ;
33
- } ) ;
34
-
35
- // Construct the image states map
36
- const imageStates : Record < string , ImageState > = { } ;
37
-
38
- REQUIRED_IMAGES . forEach ( ( imageName , index ) => {
39
- const query = imageQueries [ index ] ;
40
- imageStates [ imageName ] = {
41
- status : query . isLoading ? 'loading' :
42
- query . isError ? 'error' :
43
- query . isSuccess ? 'success' : 'idle' ,
44
- result : query . data ,
45
- error : query . error
46
- } ;
47
- } ) ;
48
-
49
- // Calculate overall loading state
50
- const isLoading = imageQueries . some ( query => query . isLoading ) ;
51
- const isFetching = imageQueries . some ( query => query . isFetching ) ;
52
-
53
- // Load a specific image
54
- const loadImage = async ( imageName : string ) => {
55
- const index = REQUIRED_IMAGES . indexOf ( imageName ) ;
56
- if ( index === - 1 ) {
57
- client . desktopUI . toast . error ( `Unknown image: ${ imageName } ` ) ;
58
- return ;
59
- }
6
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
60
7
8
+ const imageName = BUSYBOX ;
9
+ const fetchRequiredImage = async ( ) => {
61
10
try {
62
- await imageQueries [ index ] . refetch ( ) ;
11
+ await client . docker . cli . exec ( 'inspect' , [ imageName ] ) ;
63
12
} catch ( error ) {
64
- client . desktopUI . toast . error ( `Failed to load image ${ imageName } : ${ error } ` ) ;
13
+ // Ignore
65
14
}
66
- } ;
67
15
68
- // Load all required images
69
- const loadAllImages = async ( ) => {
70
16
try {
71
- await Promise . all ( imageQueries . map ( query => query . refetch ( ) ) ) ;
17
+ await client . docker . cli . exec ( 'pull' , [ imageName ] ) ;
72
18
} catch ( error ) {
73
- client . desktopUI . toast . error ( `Failed to load one or more images: ${ error } ` ) ;
19
+ client . desktopUI . toast . error ( `Failed to pull image ${ imageName } : ${ error } ` ) ;
20
+ throw error ;
74
21
}
75
- } ;
22
+ }
23
+
24
+ useEffect ( ( ) => {
25
+ setIsLoading ( true ) ;
26
+ fetchRequiredImage ( ) . then ( ( ) => {
27
+ setIsLoading ( false ) ;
28
+ } ) ;
29
+ } , [ ] ) ;
76
30
77
31
return {
78
- imageStates,
79
32
isLoading,
80
- isFetching,
81
- loadAllImages,
82
- loadImage
83
33
} ;
84
34
}
0 commit comments