1
1
import { v1 } from "@docker/extension-api-client-types" ;
2
- import { CatalogItemRichened } from '../types/catalog' ;
2
+ import { CatalogItem , CatalogItemRichened , CatalogItemWithName } from '../types/catalog' ;
3
3
import { getRegistry } from '../Registry' ;
4
4
import Secrets from '../Secrets' ;
5
5
import { parse } from 'yaml' ;
@@ -10,6 +10,7 @@ import { useState } from 'react';
10
10
import { escapeJSONForPlatformShell , tryRunImageSync } from '../FileUtils' ;
11
11
import { useConfig } from './useConfig' ;
12
12
import { useRequiredImages } from './useRequiredImages' ;
13
+ import { useSecrets } from "./useSecrets" ;
13
14
14
15
// Storage keys for each query type
15
16
const STORAGE_KEYS = {
@@ -26,6 +27,30 @@ interface QueryContextWithMeta {
26
27
27
28
export function useCatalog ( client : v1 . DockerDesktopClient ) {
28
29
const queryClient = useQueryClient ( ) ;
30
+ const { data : secrets } = useSecrets ( client ) ;
31
+ const { registryItems } = useRegistry ( client ) ;
32
+ const { config } = useConfig ( client ) ;
33
+
34
+ const enrichCatalogItem = ( item : CatalogItemWithName ) : CatalogItemRichened => {
35
+ const secretsWithAssignment = Secrets . getSecretsWithAssignment ( item , secrets || [ ] ) ;
36
+ const itemConfigValue = config ?. [ item . name ] || { } ;
37
+ const unConfigured = Object . keys ( itemConfigValue ) . length === 0 ;
38
+ const missingASecret = secretsWithAssignment . some ( ( secret ) => ! secret . assigned ) ;
39
+ const enrichedItem = {
40
+ ...item ,
41
+ secrets : secretsWithAssignment ,
42
+ configValue : itemConfigValue ,
43
+ configSchema : item . config ,
44
+ registered : ! ! registryItems ?. [ item . name ] ,
45
+ canRegister : ! registryItems ?. [ item . name ] && ! missingASecret && ! unConfigured ,
46
+ name : item . name ,
47
+ } ;
48
+ delete enrichedItem . config ;
49
+ if ( item . name === 'atlassian' ) {
50
+ console . log ( enrichedItem ) ;
51
+ }
52
+ return enrichedItem ;
53
+ } ;
29
54
30
55
const {
31
56
data : catalogItems = [ ] ,
@@ -40,11 +65,11 @@ export function useCatalog(client: v1.DockerDesktopClient) {
40
65
const response = await fetch ( CATALOG_URL ) ;
41
66
const catalog = await response . text ( ) ;
42
67
const items = parse ( catalog ) [ 'registry' ] as { [ key : string ] : any } ;
43
- const itemsWithName = Object . entries ( items ) . map ( ( [ name , item ] ) => ( { name, ...item } ) ) ;
68
+ const itemsWithName = Object . entries ( items ) . map ( ( [ name , item ] ) => ( { name, ...item } ) ) as CatalogItemWithName [ ] ;
44
69
if ( showNotification ) {
45
70
client . desktopUI . toast . success ( 'Catalog updated successfully.' ) ;
46
71
}
47
- return itemsWithName . reverse ( ) as CatalogItemRichened [ ] ;
72
+ return itemsWithName . reverse ( ) . map ( enrichCatalogItem ) ;
48
73
} catch ( error ) {
49
74
client . desktopUI . toast . error ( 'Failed to get latest catalog.' + error ) ;
50
75
throw error ;
@@ -323,19 +348,12 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
323
348
}
324
349
} ) ;
325
350
326
- const getCanRegisterCatalogItem = ( item : CatalogItemRichened ) : boolean => {
327
- if ( ! registryItems ) return false ;
328
- const isRegistered = ! ! registryItems [ item . name ] ;
329
- return ! isRegistered && canRegister ;
330
- } ;
331
-
332
351
return {
333
352
registerCatalogItem : ( item : CatalogItemRichened , showNotification = true ) =>
334
353
registerItemMutation . mutateAsync ( { item, showNotification } ) ,
335
354
unregisterCatalogItem : ( item : CatalogItemRichened ) =>
336
355
unregisterItemMutation . mutateAsync ( item ) ,
337
356
startPull : ( ) => startPullMutation . mutateAsync ( ) ,
338
- getCanRegisterCatalogItem
339
357
} ;
340
358
}
341
359
@@ -346,7 +364,6 @@ export function useCatalogAll(client: v1.DockerDesktopClient) {
346
364
registerCatalogItem,
347
365
unregisterCatalogItem,
348
366
startPull,
349
- getCanRegisterCatalogItem
350
367
} = useCatalogOperations ( client ) ;
351
368
352
369
return {
@@ -363,6 +380,5 @@ export function useCatalogAll(client: v1.DockerDesktopClient) {
363
380
registerCatalogItem,
364
381
unregisterCatalogItem,
365
382
startPull,
366
- getCanRegisterCatalogItem
367
383
} ;
368
384
}
0 commit comments