Skip to content

Commit d9e36fc

Browse files
author
colinmcneil
committed
Correctly model enriched catalog item in response to mutations
1 parent 605fcb9 commit d9e36fc

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

src/extension/ui/src/Secrets.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// From secrets.yaml
22

33
import { v1 } from "@docker/extension-api-client-types";
4-
import { CatalogItemRichened } from "./types/catalog";
4+
import { CatalogItemRichened, CatalogItemWithName } from "./types/catalog";
55
import { Secret, StoredSecret, Policy } from "./types/secrets";
66

77
namespace Secrets {
@@ -55,12 +55,12 @@ namespace Secrets {
5555
}
5656

5757
// Get all relevant secrets for a given set of catalog items
58-
export function getAllSecretNames(catalogItems: CatalogItemRichened[]): string[] {
58+
export function getAllSecretNames(catalogItems: CatalogItemWithName[]): string[] {
5959
return catalogItems.map((item) => item.secrets || []).flat().map((secret) => secret.name);
6060
}
6161

6262
// Whether or not each secret has been assigned for a given catalog item
63-
export function getSecretsWithAssignment(catalogItem: CatalogItemRichened, secrets: Secret[]): { name: string, assigned: boolean }[] {
63+
export function getSecretsWithAssignment(catalogItem: CatalogItemWithName, secrets: Secret[]): { name: string, assigned: boolean }[] {
6464
return catalogItem.secrets?.map((secret) => ({ name: secret.name, assigned: secrets.some((s) => s.name === secret.name) })) || [];
6565
}
6666
}

src/extension/ui/src/components/tile/Index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const Tile = ({ item, client, unAssignedConfig }: TileProps) => {
9494
} else {
9595
unregisterCatalogItem(item)
9696
}
97-
}} item={item} unAssignedConfig={unAssignedConfig} unAssignedSecrets={unAssignedSecrets} registered={true} />
97+
}} item={item} unAssignedConfig={unAssignedConfig} unAssignedSecrets={unAssignedSecrets} registered={item.registered} />
9898
<Center item={item} />
9999
<Divider sx={{ marginBottom: 1 }} />
100100
<Bottom item={item} needsConfiguration={Boolean(unAssignedSecrets.length || unAssignedConfig.length)} />

src/extension/ui/src/components/tile/Modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const ConfigurationModal = ({
131131
{catalogItem.description}
132132
</Typography>
133133
<Tooltip placement="right" title={!canRegister ? 'You must assign all secrets and configure the item before it can be used.' : ''}>
134-
<FormControlLabel control={<Switch disabled={!canRegister} checked={true} onChange={(e) => registerCatalogItem(catalogItem)} />} label={registered ? 'Disable ' + `${catalogItem.name} tools` : 'Enable ' + `${catalogItem.name} tools`} sx={{ mt: 2 }} />
134+
<FormControlLabel control={<Switch disabled={!canRegister} checked={catalogItem.registered} onChange={(e) => registerCatalogItem(catalogItem)} />} label={catalogItem.registered ? 'Disable ' + `${catalogItem.name} tools` : 'Enable ' + `${catalogItem.name} tools`} sx={{ mt: 2 }} />
135135
</Tooltip>
136136
<Divider sx={{ mt: 2 }} />
137137
<Typography variant="caption" sx={{ mt: 2, color: 'text.secondary' }}>

src/extension/ui/src/hooks/useCatalog.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { v1 } from "@docker/extension-api-client-types";
2-
import { CatalogItemRichened } from '../types/catalog';
2+
import { CatalogItem, CatalogItemRichened, CatalogItemWithName } from '../types/catalog';
33
import { getRegistry } from '../Registry';
44
import Secrets from '../Secrets';
55
import { parse } from 'yaml';
@@ -10,6 +10,7 @@ import { useState } from 'react';
1010
import { escapeJSONForPlatformShell, tryRunImageSync } from '../FileUtils';
1111
import { useConfig } from './useConfig';
1212
import { useRequiredImages } from './useRequiredImages';
13+
import { useSecrets } from "./useSecrets";
1314

1415
// Storage keys for each query type
1516
const STORAGE_KEYS = {
@@ -26,6 +27,30 @@ interface QueryContextWithMeta {
2627

2728
export function useCatalog(client: v1.DockerDesktopClient) {
2829
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+
};
2954

3055
const {
3156
data: catalogItems = [],
@@ -40,11 +65,11 @@ export function useCatalog(client: v1.DockerDesktopClient) {
4065
const response = await fetch(CATALOG_URL);
4166
const catalog = await response.text();
4267
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[];
4469
if (showNotification) {
4570
client.desktopUI.toast.success('Catalog updated successfully.');
4671
}
47-
return itemsWithName.reverse() as CatalogItemRichened[];
72+
return itemsWithName.reverse().map(enrichCatalogItem);
4873
} catch (error) {
4974
client.desktopUI.toast.error('Failed to get latest catalog.' + error);
5075
throw error;
@@ -323,19 +348,12 @@ export function useCatalogOperations(client: v1.DockerDesktopClient) {
323348
}
324349
});
325350

326-
const getCanRegisterCatalogItem = (item: CatalogItemRichened): boolean => {
327-
if (!registryItems) return false;
328-
const isRegistered = !!registryItems[item.name];
329-
return !isRegistered && canRegister;
330-
};
331-
332351
return {
333352
registerCatalogItem: (item: CatalogItemRichened, showNotification = true) =>
334353
registerItemMutation.mutateAsync({ item, showNotification }),
335354
unregisterCatalogItem: (item: CatalogItemRichened) =>
336355
unregisterItemMutation.mutateAsync(item),
337356
startPull: () => startPullMutation.mutateAsync(),
338-
getCanRegisterCatalogItem
339357
};
340358
}
341359

@@ -346,7 +364,6 @@ export function useCatalogAll(client: v1.DockerDesktopClient) {
346364
registerCatalogItem,
347365
unregisterCatalogItem,
348366
startPull,
349-
getCanRegisterCatalogItem
350367
} = useCatalogOperations(client);
351368

352369
return {
@@ -363,6 +380,5 @@ export function useCatalogAll(client: v1.DockerDesktopClient) {
363380
registerCatalogItem,
364381
unregisterCatalogItem,
365382
startPull,
366-
getCanRegisterCatalogItem
367383
};
368384
}

src/extension/ui/src/types/catalog/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export interface CatalogItem {
1616
config?: any; // Configuration type
1717
}
1818

19+
export interface CatalogItemWithName extends CatalogItem {
20+
name: string;
21+
}
22+
1923
/**
2024
* Interface for a catalog item with a name
2125
*/
2226
export interface CatalogItemRichened extends CatalogItem {
2327
name: string;
24-
secrets: Secret[];
25-
config: { [key: string]: any };
28+
secrets: { name: string, assigned: boolean }[];
29+
configValue: { [key: string]: any };
30+
configSchema: any;
2631
registered: boolean;
2732
canRegister: boolean;
2833
}

0 commit comments

Comments
 (0)