|
| 1 | +import { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { ObjectStackClient } from '@objectstack/client'; |
| 3 | +import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; |
| 4 | +import { Badge } from "@/components/ui/badge"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Package, Power, PowerOff, Trash2, RefreshCw, AppWindow, Layers } from 'lucide-react'; |
| 7 | + |
| 8 | +interface InstalledPackage { |
| 9 | + manifest: { |
| 10 | + id: string; |
| 11 | + name: string; |
| 12 | + version: string; |
| 13 | + type: string; |
| 14 | + description?: string; |
| 15 | + }; |
| 16 | + status: string; |
| 17 | + enabled: boolean; |
| 18 | + installedAt?: string; |
| 19 | + updatedAt?: string; |
| 20 | +} |
| 21 | + |
| 22 | +interface PackageManagerProps { |
| 23 | + client: ObjectStackClient; |
| 24 | +} |
| 25 | + |
| 26 | +export function PackageManager({ client }: PackageManagerProps) { |
| 27 | + const [packages, setPackages] = useState<InstalledPackage[]>([]); |
| 28 | + const [loading, setLoading] = useState(true); |
| 29 | + const [error, setError] = useState<string | null>(null); |
| 30 | + |
| 31 | + const loadPackages = useCallback(async () => { |
| 32 | + setLoading(true); |
| 33 | + setError(null); |
| 34 | + try { |
| 35 | + const result = await client.packages.list(); |
| 36 | + setPackages(result?.packages || []); |
| 37 | + } catch (err: any) { |
| 38 | + console.error('[PackageManager] Failed to load packages:', err); |
| 39 | + setError(err.message || 'Failed to load packages'); |
| 40 | + } finally { |
| 41 | + setLoading(false); |
| 42 | + } |
| 43 | + }, [client]); |
| 44 | + |
| 45 | + useEffect(() => { loadPackages(); }, [loadPackages]); |
| 46 | + |
| 47 | + async function handleToggle(pkg: InstalledPackage) { |
| 48 | + try { |
| 49 | + if (pkg.enabled) { |
| 50 | + await client.packages.disable(pkg.manifest.id); |
| 51 | + } else { |
| 52 | + await client.packages.enable(pkg.manifest.id); |
| 53 | + } |
| 54 | + await loadPackages(); |
| 55 | + } catch (err: any) { |
| 56 | + console.error('[PackageManager] Toggle failed:', err); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + async function handleUninstall(pkg: InstalledPackage) { |
| 61 | + if (!confirm(`Uninstall "${pkg.manifest.name}"? This cannot be undone.`)) return; |
| 62 | + try { |
| 63 | + await client.packages.uninstall(pkg.manifest.id); |
| 64 | + await loadPackages(); |
| 65 | + } catch (err: any) { |
| 66 | + console.error('[PackageManager] Uninstall failed:', err); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const typeColors: Record<string, string> = { |
| 71 | + app: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200', |
| 72 | + plugin: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200', |
| 73 | + driver: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200', |
| 74 | + server: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200', |
| 75 | + module: 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200', |
| 76 | + }; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="flex flex-1 flex-col gap-6 p-6"> |
| 80 | + {/* Header */} |
| 81 | + <div className="flex items-center justify-between"> |
| 82 | + <div className="flex flex-col gap-1"> |
| 83 | + <h1 className="text-2xl font-bold tracking-tight flex items-center gap-2"> |
| 84 | + <Package className="h-6 w-6" /> |
| 85 | + Package Manager |
| 86 | + </h1> |
| 87 | + <p className="text-sm text-muted-foreground"> |
| 88 | + Manage installed packages. A package may contain apps, objects, actions, and other metadata. |
| 89 | + </p> |
| 90 | + </div> |
| 91 | + <Button variant="outline" size="sm" onClick={loadPackages} disabled={loading}> |
| 92 | + <RefreshCw className={`h-4 w-4 mr-1.5 ${loading ? 'animate-spin' : ''}`} /> |
| 93 | + Refresh |
| 94 | + </Button> |
| 95 | + </div> |
| 96 | + |
| 97 | + {/* Error state */} |
| 98 | + {error && ( |
| 99 | + <Card className="border-destructive"> |
| 100 | + <CardContent className="py-4 text-destructive text-sm"> |
| 101 | + {error} |
| 102 | + </CardContent> |
| 103 | + </Card> |
| 104 | + )} |
| 105 | + |
| 106 | + {/* Package list */} |
| 107 | + {loading ? ( |
| 108 | + <div className="grid gap-4 md:grid-cols-2"> |
| 109 | + {[1, 2].map(i => ( |
| 110 | + <Card key={i} className="animate-pulse"> |
| 111 | + <CardHeader> |
| 112 | + <div className="h-5 w-40 bg-muted rounded" /> |
| 113 | + <div className="h-3 w-60 bg-muted rounded mt-2" /> |
| 114 | + </CardHeader> |
| 115 | + </Card> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + ) : packages.length === 0 ? ( |
| 119 | + <Card className="border-dashed"> |
| 120 | + <CardContent className="py-12 text-center"> |
| 121 | + <Layers className="h-10 w-10 mx-auto mb-3 text-muted-foreground/40" /> |
| 122 | + <p className="text-muted-foreground text-sm">No packages installed</p> |
| 123 | + </CardContent> |
| 124 | + </Card> |
| 125 | + ) : ( |
| 126 | + <div className="grid gap-4 md:grid-cols-2"> |
| 127 | + {packages.map((pkg) => ( |
| 128 | + <Card |
| 129 | + key={pkg.manifest.id} |
| 130 | + className={!pkg.enabled ? 'opacity-60' : ''} |
| 131 | + > |
| 132 | + <CardHeader className="pb-3"> |
| 133 | + <div className="flex items-start justify-between gap-2"> |
| 134 | + <div className="flex items-center gap-2 min-w-0"> |
| 135 | + {pkg.manifest.type === 'app' ? ( |
| 136 | + <AppWindow className="h-5 w-5 shrink-0 text-blue-500" /> |
| 137 | + ) : ( |
| 138 | + <Package className="h-5 w-5 shrink-0 text-purple-500" /> |
| 139 | + )} |
| 140 | + <CardTitle className="text-base truncate"> |
| 141 | + {pkg.manifest.name} |
| 142 | + </CardTitle> |
| 143 | + </div> |
| 144 | + <div className="flex items-center gap-1.5 shrink-0"> |
| 145 | + <Badge variant="outline" className="text-xs font-mono"> |
| 146 | + v{pkg.manifest.version} |
| 147 | + </Badge> |
| 148 | + <Badge className={`text-xs ${typeColors[pkg.manifest.type] || typeColors.module}`}> |
| 149 | + {pkg.manifest.type} |
| 150 | + </Badge> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + <CardDescription className="text-xs mt-1"> |
| 154 | + {pkg.manifest.description || pkg.manifest.id} |
| 155 | + </CardDescription> |
| 156 | + </CardHeader> |
| 157 | + <CardContent className="pt-0"> |
| 158 | + <div className="flex items-center justify-between"> |
| 159 | + <div className="flex items-center gap-2 text-xs text-muted-foreground"> |
| 160 | + <Badge |
| 161 | + variant={pkg.enabled ? "default" : "secondary"} |
| 162 | + className="text-xs" |
| 163 | + > |
| 164 | + {pkg.enabled ? 'Enabled' : 'Disabled'} |
| 165 | + </Badge> |
| 166 | + {pkg.installedAt && ( |
| 167 | + <span>Installed {new Date(pkg.installedAt).toLocaleDateString()}</span> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + <div className="flex items-center gap-1"> |
| 171 | + <Button |
| 172 | + variant="ghost" |
| 173 | + size="icon" |
| 174 | + className="h-7 w-7" |
| 175 | + onClick={() => handleToggle(pkg)} |
| 176 | + title={pkg.enabled ? 'Disable' : 'Enable'} |
| 177 | + > |
| 178 | + {pkg.enabled ? ( |
| 179 | + <PowerOff className="h-3.5 w-3.5" /> |
| 180 | + ) : ( |
| 181 | + <Power className="h-3.5 w-3.5" /> |
| 182 | + )} |
| 183 | + </Button> |
| 184 | + <Button |
| 185 | + variant="ghost" |
| 186 | + size="icon" |
| 187 | + className="h-7 w-7 text-destructive hover:text-destructive" |
| 188 | + onClick={() => handleUninstall(pkg)} |
| 189 | + title="Uninstall" |
| 190 | + > |
| 191 | + <Trash2 className="h-3.5 w-3.5" /> |
| 192 | + </Button> |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + </CardContent> |
| 196 | + </Card> |
| 197 | + ))} |
| 198 | + </div> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + ); |
| 202 | +} |
0 commit comments