|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { |
| 4 | + ArrowClockwiseIcon, |
| 5 | + CheckIcon, |
| 6 | + PlusIcon, |
| 7 | + TrashIcon, |
| 8 | +} from '@phosphor-icons/react'; |
| 9 | +import { Badge } from '@/components/ui/badge'; |
| 10 | +import { Button } from '@/components/ui/button'; |
| 11 | +import { Card, CardContent } from '@/components/ui/card'; |
| 12 | + |
| 13 | +interface Extension { |
| 14 | + name: string; |
| 15 | + description: string; |
| 16 | + version?: string; |
| 17 | + defaultVersion?: string; |
| 18 | + schema?: string; |
| 19 | + hasStatefulData?: boolean; |
| 20 | + requiresRestart?: boolean; |
| 21 | + needsUpdate?: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +interface ExtensionCardProps { |
| 25 | + extension: Extension; |
| 26 | + type: 'installed' | 'available'; |
| 27 | + onInstall?: () => void; |
| 28 | + onUpdate?: () => void; |
| 29 | + onRemove?: () => void; |
| 30 | + onReset?: () => void; |
| 31 | + canManage: boolean; |
| 32 | + isInstalling?: boolean; |
| 33 | + isUpdating?: boolean; |
| 34 | + isRemoving?: boolean; |
| 35 | + isResetting?: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +function ExtensionBadges({ |
| 39 | + extension, |
| 40 | + type, |
| 41 | +}: { |
| 42 | + extension: Extension; |
| 43 | + type: 'installed' | 'available'; |
| 44 | +}) { |
| 45 | + return ( |
| 46 | + <div className="flex flex-wrap gap-1"> |
| 47 | + {type === 'installed' ? ( |
| 48 | + <Badge className="bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400"> |
| 49 | + <CheckIcon className="mr-1 h-3 w-3" /> |
| 50 | + Installed |
| 51 | + </Badge> |
| 52 | + ) : ( |
| 53 | + <Badge variant="outline"> |
| 54 | + <PlusIcon className="mr-1 h-3 w-3" /> |
| 55 | + Available |
| 56 | + </Badge> |
| 57 | + )} |
| 58 | + {extension.needsUpdate && ( |
| 59 | + <Badge className="bg-amber-100 text-amber-800 dark:bg-amber-900/20 dark:text-amber-400"> |
| 60 | + <ArrowClockwiseIcon className="mr-1 h-3 w-3" /> |
| 61 | + Update Available |
| 62 | + </Badge> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function ExtensionMetadata({ |
| 69 | + extension, |
| 70 | + type, |
| 71 | +}: { |
| 72 | + extension: Extension; |
| 73 | + type: 'installed' | 'available'; |
| 74 | +}) { |
| 75 | + return ( |
| 76 | + <div className="flex flex-wrap gap-2"> |
| 77 | + {type === 'installed' && extension.version && ( |
| 78 | + <Badge className="text-xs" variant="secondary"> |
| 79 | + v{extension.version} |
| 80 | + </Badge> |
| 81 | + )} |
| 82 | + {type === 'available' && extension.defaultVersion && ( |
| 83 | + <Badge className="text-xs" variant="secondary"> |
| 84 | + v{extension.defaultVersion} |
| 85 | + </Badge> |
| 86 | + )} |
| 87 | + {extension.schema && ( |
| 88 | + <Badge className="text-xs" variant="outline"> |
| 89 | + {extension.schema} |
| 90 | + </Badge> |
| 91 | + )} |
| 92 | + {extension.hasStatefulData && ( |
| 93 | + <Badge className="text-xs" variant="outline"> |
| 94 | + Stateful |
| 95 | + </Badge> |
| 96 | + )} |
| 97 | + {extension.requiresRestart && ( |
| 98 | + <Badge |
| 99 | + className="border-amber-200 text-amber-700 text-xs dark:border-amber-800 dark:text-amber-300" |
| 100 | + variant="outline" |
| 101 | + > |
| 102 | + Restart Required |
| 103 | + </Badge> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +export function ExtensionCard({ |
| 110 | + extension, |
| 111 | + type, |
| 112 | + onInstall, |
| 113 | + onUpdate, |
| 114 | + onRemove, |
| 115 | + onReset, |
| 116 | + canManage, |
| 117 | + isInstalling, |
| 118 | + isUpdating, |
| 119 | + isRemoving, |
| 120 | + isResetting, |
| 121 | +}: ExtensionCardProps) { |
| 122 | + const isLoading = isInstalling || isUpdating || isRemoving || isResetting; |
| 123 | + |
| 124 | + return ( |
| 125 | + <Card className="group relative overflow-hidden rounded border transition-all duration-200 hover:border-primary/20 hover:shadow-lg"> |
| 126 | + <CardContent className="p-6"> |
| 127 | + <div className="space-y-4"> |
| 128 | + {/* Header */} |
| 129 | + <div className="flex items-start justify-between"> |
| 130 | + <div className="min-w-0 flex-1"> |
| 131 | + <h3 className="truncate font-semibold text-lg leading-tight"> |
| 132 | + {extension.name} |
| 133 | + </h3> |
| 134 | + <p className="mt-1 line-clamp-2 text-muted-foreground text-sm"> |
| 135 | + {extension.description} |
| 136 | + </p> |
| 137 | + </div> |
| 138 | + <div className="ml-3 flex-shrink-0"> |
| 139 | + <ExtensionBadges extension={extension} type={type} /> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + |
| 143 | + {/* Metadata */} |
| 144 | + <ExtensionMetadata extension={extension} type={type} /> |
| 145 | + |
| 146 | + {/* Actions */} |
| 147 | + <div className="flex items-center justify-between pt-2"> |
| 148 | + <div className="flex gap-2"> |
| 149 | + {type === 'installed' && extension.needsUpdate && onUpdate && ( |
| 150 | + <Button |
| 151 | + disabled={!canManage || isLoading} |
| 152 | + onClick={onUpdate} |
| 153 | + size="sm" |
| 154 | + variant="outline" |
| 155 | + > |
| 156 | + <ArrowClockwiseIcon className="mr-1 h-3 w-3" /> |
| 157 | + {isUpdating ? 'Updating...' : 'Update'} |
| 158 | + </Button> |
| 159 | + )} |
| 160 | + {type === 'installed' && extension.hasStatefulData && onReset && ( |
| 161 | + <Button |
| 162 | + disabled={!canManage || isLoading} |
| 163 | + onClick={onReset} |
| 164 | + size="sm" |
| 165 | + variant="outline" |
| 166 | + > |
| 167 | + {isResetting ? 'Resetting...' : 'Reset Stats'} |
| 168 | + </Button> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + |
| 172 | + <div className="flex gap-2"> |
| 173 | + {type === 'available' && onInstall && ( |
| 174 | + <Button |
| 175 | + className="bg-gradient-to-r from-primary to-primary/90 hover:from-primary/90 hover:to-primary" |
| 176 | + disabled={!canManage || isLoading} |
| 177 | + onClick={onInstall} |
| 178 | + size="sm" |
| 179 | + > |
| 180 | + <PlusIcon className="mr-1 h-3 w-3" /> |
| 181 | + {isInstalling ? 'Installing...' : 'Install'} |
| 182 | + </Button> |
| 183 | + )} |
| 184 | + {type === 'installed' && onRemove && ( |
| 185 | + <Button |
| 186 | + disabled={!canManage || isLoading} |
| 187 | + onClick={onRemove} |
| 188 | + size="sm" |
| 189 | + variant="destructive" |
| 190 | + > |
| 191 | + <TrashIcon className="mr-1 h-3 w-3" /> |
| 192 | + {isRemoving ? 'Removing...' : 'Remove'} |
| 193 | + </Button> |
| 194 | + )} |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </CardContent> |
| 199 | + |
| 200 | + {/* Loading overlay */} |
| 201 | + {isLoading && ( |
| 202 | + <div className="absolute inset-0 flex items-center justify-center bg-background/50 backdrop-blur-sm"> |
| 203 | + <div className="flex items-center gap-2 text-muted-foreground text-sm"> |
| 204 | + <ArrowClockwiseIcon className="h-4 w-4 animate-spin" /> |
| 205 | + Processing... |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + )} |
| 209 | + </Card> |
| 210 | + ); |
| 211 | +} |
0 commit comments