Skip to content

Commit aebefd4

Browse files
committed
Remove dead code
Signed-off-by: Trung Nguyen <[email protected]>
1 parent 1c1013c commit aebefd4

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

src/extension/ui/src/queries/useCatalog.ts

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { v1 } from "@docker/extension-api-client-types";
2-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
3-
import { useCallback, useEffect, useMemo, useState } from "react";
4-
import { parse, stringify } from "yaml";
5-
import { CATALOG_URL, REGISTRY_YAML } from "../Constants";
6-
import { writeToPromptsVolume } from "../utils/Files";
7-
import { getRegistry, syncRegistryWithConfig } from "../Registry";
8-
import Secrets from "../Secrets";
9-
import { CatalogItemRichened, CatalogItemWithName } from "../types/catalog";
10-
import { getTemplateForItem, useConfig } from "./useConfig";
11-
import { useSecrets } from "./useSecrets";
1+
import { v1 } from '@docker/extension-api-client-types';
2+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
3+
import { useCallback, useEffect, useMemo, useState } from 'react';
4+
import { parse, stringify } from 'yaml';
5+
import { CATALOG_URL, REGISTRY_YAML } from '../Constants';
6+
import { getRegistry, syncRegistryWithConfig } from '../Registry';
7+
import Secrets from '../Secrets';
8+
import { CatalogItemRichened, CatalogItemWithName } from '../types/catalog';
9+
import { writeToPromptsVolume } from '../utils/Files';
10+
import { getTemplateForItem, useConfig } from './useConfig';
11+
import { useSecrets } from './useSecrets';
1212

1313
const STORAGE_KEYS = {
14-
catalog: "docker-catalog-catalog",
15-
registry: "docker-catalog-registry",
14+
catalog: 'docker-catalog-catalog',
15+
registry: 'docker-catalog-registry',
1616
};
1717

1818
function useCatalog(client: v1.DockerDesktopClient) {
@@ -25,11 +25,11 @@ function useCatalog(client: v1.DockerDesktopClient) {
2525
(item: CatalogItemWithName): CatalogItemRichened => {
2626
const secretsWithAssignment = Secrets.getSecretsWithAssignment(
2727
item,
28-
secrets || []
28+
secrets || [],
2929
);
3030
const itemConfigValue = config?.[item.name] || {};
3131
const neverOnceConfigured = Boolean(
32-
item.config && Object.keys(itemConfigValue).length === 0
32+
item.config && Object.keys(itemConfigValue).length === 0,
3333
);
3434
const configTemplate = getTemplateForItem(item, itemConfigValue);
3535
const baseConfigTemplate = getTemplateForItem(item, {});
@@ -40,7 +40,7 @@ function useCatalog(client: v1.DockerDesktopClient) {
4040
JSON.stringify(baseConfigTemplate));
4141

4242
const missingASecret = secretsWithAssignment.some(
43-
(secret) => !secret.assigned
43+
(secret) => !secret.assigned,
4444
);
4545
const enrichedItem: CatalogItemRichened = {
4646
...item,
@@ -56,21 +56,18 @@ function useCatalog(client: v1.DockerDesktopClient) {
5656
};
5757
return enrichedItem;
5858
},
59-
[secrets, config, registryItems]
59+
[secrets, config, registryItems],
6060
);
6161

62-
const {
63-
data: catalogItems = [],
64-
isLoading: catalogLoading,
65-
refetch: refetchCatalog,
66-
} = useQuery({
67-
queryKey: ["catalog"],
62+
const { data: catalogItems = [], isLoading: catalogLoading } = useQuery({
63+
queryKey: ['catalog'],
6864
queryFn: async () => {
65+
console.log('Fetching catalog items');
6966
const response = await fetch(
70-
localStorage.getItem("catalogUrl") || CATALOG_URL
67+
localStorage.getItem('catalogUrl') || CATALOG_URL,
7168
);
7269
const catalog = await response.text();
73-
const items = parse(catalog)["registry"] as { [key: string]: any };
70+
const items = parse(catalog)['registry'] as { [key: string]: any };
7471
const enrichedItems = Object.entries(items).map(([name, item]) => ({
7572
name,
7673
...item,
@@ -93,7 +90,7 @@ function useCatalog(client: v1.DockerDesktopClient) {
9390
// Use deep comparison for determining if updates are needed
9491
if (JSON.stringify(enrichedItems) !== JSON.stringify(catalogItems)) {
9592
// Use a stable reference for query data updates
96-
queryClient.setQueryData(["catalog"], [...enrichedItems]);
93+
queryClient.setQueryData(['catalog'], [...enrichedItems]);
9794
}
9895
}
9996
}, [
@@ -107,12 +104,12 @@ function useCatalog(client: v1.DockerDesktopClient) {
107104

108105
// Persist catalog to localStorage when it changes (for fallback only)
109106
useQuery({
110-
queryKey: ["catalog", "persist", catalogItems],
107+
queryKey: ['catalog', 'persist', catalogItems],
111108
queryFn: async () => {
112109
if (catalogItems && catalogItems.length > 0) {
113110
localStorage.setItem(
114111
STORAGE_KEYS.catalog,
115-
JSON.stringify(catalogItems)
112+
JSON.stringify(catalogItems),
116113
);
117114
}
118115
return null;
@@ -121,17 +118,9 @@ function useCatalog(client: v1.DockerDesktopClient) {
121118
gcTime: 0,
122119
});
123120

124-
const tryLoadCatalog = async () => {
125-
return await refetchCatalog({
126-
cancelRefetch: false,
127-
});
128-
};
129-
130121
return {
131122
catalogItems,
132123
catalogLoading,
133-
tryLoadCatalog,
134-
refetchCatalog,
135124
};
136125
}
137126

@@ -145,8 +134,8 @@ function useRegistry(client: v1.DockerDesktopClient) {
145134
refetch: refetchRegistry,
146135
isLoading: registryLoading,
147136
} = useQuery({
148-
queryKey: ["registry"],
149-
networkMode: "always",
137+
queryKey: ['registry'],
138+
networkMode: 'always',
150139
queryFn: async () => {
151140
setCanRegister(false);
152141
try {
@@ -156,11 +145,11 @@ function useRegistry(client: v1.DockerDesktopClient) {
156145
} catch (error) {
157146
if (error instanceof Error) {
158147
client.desktopUI.toast.error(
159-
"Failed to get prompt registry: " + error.message
148+
'Failed to get prompt registry: ' + error.message,
160149
);
161150
} else {
162151
client.desktopUI.toast.error(
163-
"Failed to get prompt registry: " + JSON.stringify(error)
152+
'Failed to get prompt registry: ' + JSON.stringify(error),
164153
);
165154
}
166155
setCanRegister(true);
@@ -170,15 +159,15 @@ function useRegistry(client: v1.DockerDesktopClient) {
170159
});
171160

172161
useQuery({
173-
queryKey: ["registry", "init"],
162+
queryKey: ['registry', 'init'],
174163
queryFn: async () => {
175164
const cachedRegistry = localStorage.getItem(STORAGE_KEYS.registry);
176165
if (cachedRegistry && queryClient && !registryItems) {
177166
try {
178167
const parsedRegistry = JSON.parse(cachedRegistry);
179-
queryClient.setQueryData(["registry"], parsedRegistry);
168+
queryClient.setQueryData(['registry'], parsedRegistry);
180169
} catch (e) {
181-
console.error("Failed to parse cached registry:", e);
170+
console.error('Failed to parse cached registry:', e);
182171
}
183172
}
184173
return null;
@@ -189,11 +178,11 @@ function useRegistry(client: v1.DockerDesktopClient) {
189178

190179
const registryItemsString = useMemo(
191180
() => (registryItems ? JSON.stringify(registryItems) : null),
192-
[registryItems]
181+
[registryItems],
193182
);
194183

195184
useQuery({
196-
queryKey: ["registry", "persist"],
185+
queryKey: ['registry', 'persist'],
197186
queryFn: async () => {
198187
if (registryItemsString) {
199188
localStorage.setItem(STORAGE_KEYS.registry, registryItemsString);
@@ -212,7 +201,7 @@ function useRegistry(client: v1.DockerDesktopClient) {
212201
await writeToPromptsVolume(
213202
client,
214203
REGISTRY_YAML,
215-
stringify({ registry: newRegistry })
204+
stringify({ registry: newRegistry }),
216205
);
217206

218207
return newRegistry;
@@ -254,12 +243,12 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
254243
await writeToPromptsVolume(
255244
client,
256245
REGISTRY_YAML,
257-
stringify({ registry: newRegistry })
246+
stringify({ registry: newRegistry }),
258247
);
259248
return { success: true, newRegistry };
260249
} catch (error) {
261250
client.desktopUI.toast.error(
262-
"Failed to register catalog item: " + error
251+
'Failed to register catalog item: ' + error,
263252
);
264253
// Treat YAML file write failures as fatal, no rollback
265254
throw error;
@@ -268,7 +257,7 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
268257
// Only need one update of registry data, not both onMutate and onSuccess
269258
onSuccess: async (data) => {
270259
// Update the registry data after successful registration
271-
queryClient.setQueryData(["registry"], data.newRegistry);
260+
queryClient.setQueryData(['registry'], data.newRegistry);
272261
},
273262
});
274263

@@ -287,13 +276,13 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
287276
await writeToPromptsVolume(
288277
client,
289278
REGISTRY_YAML,
290-
stringify({ registry: currentRegistry })
279+
stringify({ registry: currentRegistry }),
291280
);
292281

293282
return { success: true, newRegistry: currentRegistry };
294283
} catch (error) {
295284
client.desktopUI.toast.error(
296-
"Failed to unregister catalog item: " + error
285+
'Failed to unregister catalog item: ' + error,
297286
);
298287
// Treat YAML file write failures as fatal, no rollback
299288
throw error;
@@ -302,7 +291,7 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
302291
// Only need one update of registry data, not both onMutate and onSuccess
303292
onSuccess: async (data) => {
304293
// Update the registry data after successful unregistration
305-
queryClient.setQueryData(["registry"], data.newRegistry);
294+
queryClient.setQueryData(['registry'], data.newRegistry);
306295
},
307296
});
308297

@@ -316,7 +305,7 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
316305

317306
// This hook represents the catalog query. The registry is not part of the UI and does not need to be exported.
318307
export function useCatalogAll(client: v1.DockerDesktopClient) {
319-
const { catalogItems, catalogLoading, tryLoadCatalog } = useCatalog(client);
308+
const { catalogItems, catalogLoading } = useCatalog(client);
320309
const {
321310
registryItems,
322311
registryLoading,
@@ -336,7 +325,6 @@ export function useCatalogAll(client: v1.DockerDesktopClient) {
336325
registryLoading,
337326

338327
// Actions
339-
tryLoadCatalog,
340328
tryLoadRegistry,
341329
registerCatalogItem,
342330
unregisterCatalogItem,

0 commit comments

Comments
 (0)