|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { Grid, Card, CardContent, IconButton, CircularProgress, Alert, Stack, Button, Typography } from '@mui/material'; |
| 3 | +import { CatalogItemWithName, CatalogItemCard, CatalogItem } from './PromptCard'; |
| 4 | +import AddIcon from '@mui/icons-material/Add'; |
| 5 | +import { Ref } from '../Refs'; |
| 6 | +import { v1 } from "@docker/extension-api-client-types"; |
| 7 | +import { parse, stringify } from 'yaml'; |
| 8 | +import { getRegistry } from '../Registry'; |
| 9 | +import { FolderOpenRounded } from '@mui/icons-material'; |
| 10 | + |
| 11 | +interface CatalogGridProps { |
| 12 | + registryItems: { [key: string]: { ref: string } }; |
| 13 | + canRegister: boolean; |
| 14 | + client: v1.DockerDesktopClient; |
| 15 | + onRegistryChange: () => void; |
| 16 | +} |
| 17 | + |
| 18 | +const CATALOG_URL = 'https://raw.githubusercontent.com/docker/labs-ai-tools-for-devs/refs/heads/main/prompts/catalog.yaml' |
| 19 | + |
| 20 | + |
| 21 | +export const CatalogGrid: React.FC<CatalogGridProps> = ({ |
| 22 | + registryItems, |
| 23 | + canRegister, |
| 24 | + client, |
| 25 | + onRegistryChange, |
| 26 | +}) => { |
| 27 | + const [catalogItems, setCatalogItems] = useState<CatalogItemWithName[]>([]); |
| 28 | + const [loading, setLoading] = useState<boolean>(true); |
| 29 | + const [error, setError] = useState<string | null>(null); |
| 30 | + |
| 31 | + const loadCatalog = async (showNotification = true) => { |
| 32 | + const cachedCatalog = localStorage.getItem('catalog'); |
| 33 | + try { |
| 34 | + const response = await fetch(CATALOG_URL); |
| 35 | + const catalog = await response.text(); |
| 36 | + const items = parse(catalog)['registry'] as { [key: string]: CatalogItem } |
| 37 | + const itemsWithName = Object.entries(items).map(([name, item]) => ({ name, ...item })); |
| 38 | + setCatalogItems(itemsWithName); |
| 39 | + localStorage.setItem('catalog', JSON.stringify(itemsWithName)); |
| 40 | + if (showNotification) { |
| 41 | + client.desktopUI.toast.success('Catalog updated successfully.'); |
| 42 | + } |
| 43 | + } |
| 44 | + catch (error) { |
| 45 | + if (cachedCatalog) { |
| 46 | + setCatalogItems(JSON.parse(cachedCatalog)); |
| 47 | + } |
| 48 | + if (showNotification) { |
| 49 | + client.desktopUI.toast.error(`Failed to get latest catalog.${cachedCatalog ? ' Using cached catalog.' : ''}` + error); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const registerCatalogItem = async (item: CatalogItemWithName) => { |
| 55 | + try { |
| 56 | + const currentRegistry = await getRegistry(client); |
| 57 | + const newRegistry = { ...currentRegistry, [item.name]: { ref: item.ref } }; |
| 58 | + const payload = JSON.stringify({ |
| 59 | + files: [{ |
| 60 | + path: 'registry.yaml', |
| 61 | + content: stringify({ registry: newRegistry }) |
| 62 | + }] |
| 63 | + }) |
| 64 | + await client.docker.cli.exec('run', ['--rm', '-v', 'docker-prompts:/docker-prompts', '--workdir', '/docker-prompts', 'vonwig/function_write_files:latest', `'${payload}'`]) |
| 65 | + client.desktopUI.toast.success('Prompt registered successfully. Restart Claude Desktop to apply.'); |
| 66 | + onRegistryChange(); |
| 67 | + |
| 68 | + } |
| 69 | + catch (error) { |
| 70 | + client.desktopUI.toast.error('Failed to register prompt: ' + error); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + const unregisterCatalogItem = async (item: CatalogItemWithName) => { |
| 75 | + try { |
| 76 | + const currentRegistry = await getRegistry(client); |
| 77 | + delete currentRegistry[item.name]; |
| 78 | + const payload = JSON.stringify({ |
| 79 | + files: [{ |
| 80 | + path: 'registry.yaml', |
| 81 | + content: stringify({ registry: currentRegistry }) |
| 82 | + }] |
| 83 | + }) |
| 84 | + await client.docker.cli.exec('run', ['--rm', '-v', 'docker-prompts:/docker-prompts', '--workdir', '/docker-prompts', 'vonwig/function_write_files:latest', `'${payload}'`]) |
| 85 | + client.desktopUI.toast.success('Prompt unregistered successfully. Restart Claude Desktop to apply.'); |
| 86 | + onRegistryChange(); |
| 87 | + } |
| 88 | + catch (error) { |
| 89 | + client.desktopUI.toast.error('Failed to unregister prompt: ' + error) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + useEffect(() => { |
| 94 | + const interval = setInterval(loadCatalog, 1000 * 30); |
| 95 | + loadCatalog(false); |
| 96 | + return () => { |
| 97 | + clearInterval(interval); |
| 98 | + } |
| 99 | + }, []); |
| 100 | + |
| 101 | + const hasOutOfCatalog = catalogItems.length > 0 && Object.keys(registryItems).length > 0 && !Object.keys(registryItems).every((i) => |
| 102 | + catalogItems.some((c) => c.name === i) |
| 103 | + ) |
| 104 | + |
| 105 | + return ( |
| 106 | + <Stack spacing={2}> |
| 107 | + {hasOutOfCatalog && <Alert action={<Button startIcon={<FolderOpenRounded />} variant='outlined' color='secondary' onClick={() => { |
| 108 | + client.desktopUI.navigate.viewVolume('docker-prompts') |
| 109 | + }}>registry.yaml</Button>} severity="info"> |
| 110 | + <Typography sx={{ width: '100%' }}>You have some prompts registered which are not available in the catalog.</Typography> |
| 111 | + </Alert>} |
| 112 | + <Grid container spacing={2}> |
| 113 | + {catalogItems.map((item) => ( |
| 114 | + <Grid item xs={12} sm={6} md={4} key={item.name} flex="1 1 0"> |
| 115 | + <CatalogItemCard |
| 116 | + openUrl={() => { |
| 117 | + client.host.openExternal(Ref.fromRef(item.ref).toURL(true)); |
| 118 | + }} |
| 119 | + item={item} |
| 120 | + canRegister={canRegister} |
| 121 | + registered={Object.keys(registryItems).some((i) => i === item.name)} |
| 122 | + register={registerCatalogItem} |
| 123 | + unregister={unregisterCatalogItem} |
| 124 | + /> |
| 125 | + </Grid> |
| 126 | + ))} |
| 127 | + <Grid item xs={12} sm={6} md={4} flex="1 1 0"> |
| 128 | + <Card sx={{ height: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}> |
| 129 | + <CardContent> |
| 130 | + <IconButton sx={{ height: '100%' }} onClick={() => { |
| 131 | + client.host.openExternal('https://vonwig.github.io/prompts.docs/tools/docs/'); |
| 132 | + }}> |
| 133 | + <AddIcon sx={{ width: '100%', height: 100 }} /> |
| 134 | + </IconButton> |
| 135 | + </CardContent> |
| 136 | + </Card> |
| 137 | + </Grid> |
| 138 | + </Grid> |
| 139 | + </Stack> |
| 140 | + ); |
| 141 | +}; |
0 commit comments