Skip to content

Commit 7c2a9be

Browse files
authored
Merge pull request #205 from dgageot/fix-load-image-progress
Pull if not found
2 parents 5f034c0 + cc02e9b commit 7c2a9be

File tree

2 files changed

+20
-93
lines changed

2 files changed

+20
-93
lines changed

src/extension/ui/src/components/LoadingState.tsx

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import { LinearProgress, Box, Typography, Paper, Stack, Fade, CircularProgress } from "@mui/material";
2-
import { useEffect, useState } from "react";
31
import { createDockerDesktopClient } from '@docker/extension-api-client';
2+
import { Box, CircularProgress, Fade, LinearProgress, Paper, Stack, Typography } from "@mui/material";
3+
import { useEffect, useState } from "react";
44

55
// Initialize the Docker Desktop client
66
const client = createDockerDesktopClient();
77

8-
// Type definition for image state
9-
interface ImageState {
10-
status: 'idle' | 'loading' | 'success' | 'error';
11-
result?: any;
12-
error?: unknown;
13-
}
14-
158
interface LoadingStateProps {
169
appProps: any; // We'll use this to pass all our hook data
1710
}
@@ -28,14 +21,7 @@ const LoadingState: React.FC<LoadingStateProps> = ({ appProps }) => {
2821
const [progress, setProgress] = useState(0);
2922

3023
useEffect(() => {
31-
const imageProgressTotal = (() => {
32-
const total = 5;
33-
// Type assertion to Record<string, ImageState>
34-
const imageStatesMap = imageStates as Record<string, ImageState> || {};
35-
const loaded = Object.values(imageStatesMap).filter(state => state.status === 'success').length;
36-
return Math.round((loaded / total) * 100);
37-
})()
38-
24+
const imageProgressTotal = imagesLoading ? 100 : 0;
3925
const configProgressTotal = configLoading ? 100 : 0;
4026
const secretsProgressTotal = secretsLoading ? 100 : 0;
4127
const catalogProgressTotal = catalogLoading ? 100 : 0;
@@ -57,15 +43,6 @@ const LoadingState: React.FC<LoadingStateProps> = ({ appProps }) => {
5743
return 'Loading...';
5844
}
5945

60-
const getStatusColor = (status: string) => {
61-
switch (status) {
62-
case 'loading': return '#3498db';
63-
case 'success': return '#2ecc71';
64-
case 'error': return '#e74c3c';
65-
default: return '#95a5a6';
66-
}
67-
};
68-
6946
return (
7047
<Fade in={isLoading}>
7148
<Box sx={{
Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,34 @@
11
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";
43
import { BUSYBOX } from "../Constants";
54

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-
165
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);
607

8+
const imageName = BUSYBOX;
9+
const fetchRequiredImage = async () => {
6110
try {
62-
await imageQueries[index].refetch();
11+
await client.docker.cli.exec('inspect', [imageName]);
6312
} catch (error) {
64-
client.desktopUI.toast.error(`Failed to load image ${imageName}: ${error}`);
13+
// Ignore
6514
}
66-
};
6715

68-
// Load all required images
69-
const loadAllImages = async () => {
7016
try {
71-
await Promise.all(imageQueries.map(query => query.refetch()));
17+
await client.docker.cli.exec('pull', [imageName]);
7218
} 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;
7421
}
75-
};
22+
}
23+
24+
useEffect(() => {
25+
setIsLoading(true);
26+
fetchRequiredImage().then(() => {
27+
setIsLoading(false);
28+
});
29+
}, []);
7630

7731
return {
78-
imageStates,
7932
isLoading,
80-
isFetching,
81-
loadAllImages,
82-
loadImage
8333
};
8434
}

0 commit comments

Comments
 (0)