import { useState } from 'react'; import { Server, Wrench, Loader2, RefreshCw, Trash2 } from 'lucide-react'; import { toast } from 'sonner'; import { useGlobal } from 'hooks/useGlobal'; import { usePanelChannel } from 'hooks/usePanelChannel'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { useResources, getResourceCategory, type Resource, type PingResult } from '@/state/useResources'; import { RUN_COMMAND_CHANNEL, type RunCommandState } from './run-command-channel'; type ConnectionSectionProps = { resource: Resource; }; const ConnectionSection = ({ resource }: ConnectionSectionProps) => { const { saveConnectionConfig, pingResource } = useResources(); const [url, setUrl] = useState(resource.connectionConfig?.url ?? ''); const [apiKey, setApiKey] = useState(resource.connectionConfig?.credentials?.apiKey ?? ''); const [username, setUsername] = useState(resource.connectionConfig?.credentials?.username ?? ''); const [password, setPassword] = useState(resource.connectionConfig?.credentials?.password ?? ''); const [pinging, setPinging] = useState(false); const [pingResult, setPingResult] = useState(null); const [saving, setSaving] = useState(false); const handlePing = async () => { setPinging(true); setPingResult(null); try { const result = await pingResource(resource.id, url); setPingResult(result); } catch { setPingResult({ reachable: false, latencyMs: null }); } finally { setPinging(false); } }; const handleSave = async () => { setSaving(true); try { const credentials = apiKey || username || password ? { apiKey: apiKey || undefined, username: username || undefined, password: password || undefined } : undefined; await saveConnectionConfig(resource.id, { url, credentials }); } finally { setSaving(false); } }; const hasCredentials = !!( resource.connectionConfig?.credentials?.apiKey || resource.connectionConfig?.credentials?.username ); return (

Connection

setUrl(ev.target.value)} placeholder="http://127.0.0.1:64202" className="h-8 text-xs" />
{(hasCredentials || apiKey) && (
setApiKey(ev.target.value)} placeholder="Optional" type="password" className="h-8 text-xs" />
)} {(hasCredentials || username || password) && (
setUsername(ev.target.value)} placeholder="Optional" className="h-8 text-xs" />
setPassword(ev.target.value)} placeholder="Optional" type="password" className="h-8 text-xs" />
)}
{pingResult && ( {pingResult.reachable ? `Reachable (${pingResult.latencyMs}ms)` : 'Unreachable'} )}
); }; type LocalAvailabilitySectionProps = { resource: Resource; onRun: (command: string) => void; }; type ResourceAction = 'install' | 'uninstall' | 'verify' | 'update' | 'manage'; const LocalAvailabilitySection = ({ resource, onRun }: LocalAvailabilitySectionProps) => { const { runCommand } = useResources(); const [runningAction, setRunningAction] = useState(null); const isSudo = (cmd: string) => cmd.trimStart().startsWith('sudo'); const handleAction = async (action: ResourceAction, command: string) => { if (isSudo(command)) { onRun(command); return; } setRunningAction(action); try { const result = await runCommand(resource.id, action); if (result.exitCode === 0) { toast.success('Command completed successfully'); } else { toast.error(result.output || `Command failed (exit code ${result.exitCode})`, { duration: 8000 }); } } catch { toast.error('Failed to run command'); } finally { setRunningAction(null); } }; return (
{resource.installed ? ( <>
Installed
{resource.version && (
{resource.updateCommand && ( )} {resource.version}
)}
{!resource.version && resource.updateCommand && ( )} {resource.verifyCommand && ( )} {resource.manageCommand && ( )}
{resource.uninstallCommand && (
)} ) : (
Not installed {resource.installCommand && ( )}
)}
); }; type CatalogCardProps = { resource: Resource; onSelect: (id: string) => void; }; const CatalogCard = ({ resource: r, onSelect }: CatalogCardProps) => { const category = getResourceCategory(r); return ( ); }; const ResourceCatalog = () => { const { resources, isLoading } = useResources(); const [, setSelectedId] = useGlobal('RESOURCE_SELECTED', null); const [, setShowCatalog] = useGlobal('RESOURCE_CATALOG', false); const [search, setSearch] = useState(''); const query = search.toLowerCase(); const filtered = resources?.filter( (r: Resource) => !r.installed && (r.name.toLowerCase().includes(query) || r.subtitle.toLowerCase().includes(query) || r.description.toLowerCase().includes(query)), ) ?? []; const apiBased = filtered.filter((r: Resource) => getResourceCategory(r) === 'api-based'); const localCli = filtered.filter((r: Resource) => getResourceCategory(r) === 'local-cli'); const handleSelect = (id: string) => { setShowCatalog(false); setSelectedId(id); }; return (

Resource Catalog

All available resources. Select one to configure.

setSearch(ev.target.value)} className="h-8 text-xs mb-4 max-w-xs" /> {isLoading &&

Loading...

} {apiBased.length > 0 && (
API Based
{apiBased.map((r: Resource) => ( ))}
)} {localCli.length > 0 && (
Local CLI
{localCli.map((r: Resource) => ( ))}
)}
); }; const ResourceDetail = ({ resource }: { resource: Resource }) => { const category = getResourceCategory(resource); const [, setRunCommand] = usePanelChannel(RUN_COMMAND_CHANNEL, null); const [confirmCommand, setConfirmCommand] = useState(null); const handleRun = (command: string) => { const isSudo = command.trimStart().startsWith('sudo'); if (isSudo) { setConfirmCommand(command); } else { setRunCommand({ command }); } }; return ( <>
{resource.port ? ( ) : ( )}

{resource.name}

{resource.subtitle}
{resource.type} {category === 'api-based' ? 'API Based' : 'Local CLI'} {resource.port && ( :{resource.port} )}

{resource.description}

{category === 'api-based' && }
!open && setConfirmCommand(null)}> Run with elevated privileges

For this operation the script must be run with elevated privileges (sudo) on the host machine.
Not to worry, though, we wrote it and battle tested it ourselves.

{confirmCommand} Cancel { if (confirmCommand) setRunCommand({ command: confirmCommand }); setConfirmCommand(null); }} > Run
); }; export const Resources = () => { const { resources } = useResources(); const [selectedId] = useGlobal('RESOURCE_SELECTED', null); const [showCatalog] = useGlobal('RESOURCE_CATALOG', false); const resource = selectedId ? resources?.find((r: Resource) => r.id === selectedId) : null; if (showCatalog || !resource) return ; return ; };