-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlugins.tsx
More file actions
388 lines (365 loc) · 11.4 KB
/
Plugins.tsx
File metadata and controls
388 lines (365 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { useState, type FormEvent } from "react";
import { useQuery, useMutation } from "urql";
import { toast } from "sonner";
import { DataTable, type Column } from "@/components/DataTable";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { PlusIcon, RefreshCwIcon, DownloadIcon, Trash2Icon, ArrowUpCircleIcon, LoaderIcon } from "lucide-react";
const AVAILABLE_PLUGINS = `
query {
availablePlugins {
name
packageName
version
activeVersion
active
installed
pendingRestart
description
registryVersion
}
}
`;
const INSTALL_PLUGIN = `
mutation InstallPlugin($packageSpec: String!) {
installPlugin(packageSpec: $packageSpec) {
success
error
results
}
}
`;
const UNINSTALL_PLUGIN = `
mutation UninstallPlugin($packageName: String!) {
uninstallPlugin(packageName: $packageName) {
success
error
}
}
`;
const UPDATE_PLUGIN = `
mutation UpdatePlugin($packageName: String!, $version: String) {
updatePlugin(packageName: $packageName, version: $version) {
success
error
results
}
}
`;
interface PluginRow {
name: string;
packageName?: string;
version: string | null;
activeVersion: string | null;
active: boolean;
installed: boolean;
pendingRestart: boolean;
description?: string;
registryVersion?: string;
}
type StatusInfo = { label: string; variant: "default" | "secondary" | "destructive" | "outline" };
function getStatus(row: PluginRow): StatusInfo {
if (row.active && row.installed && !row.pendingRestart) {
return { label: "Active", variant: "default" };
}
if (row.active && row.installed && row.pendingRestart) {
return { label: "Pending Restart", variant: "secondary" };
}
if (row.installed && !row.active) {
return { label: "Installed", variant: "secondary" };
}
if (!row.installed && row.active) {
return { label: "Removed", variant: "destructive" };
}
return { label: "Available", variant: "outline" };
}
export function Plugins() {
const [search, setSearch] = useState("");
const [installOpen, setInstallOpen] = useState(false);
const [installSpec, setInstallSpec] = useState("");
const [submitting, setSubmitting] = useState(false);
const [needsRestart, setNeedsRestart] = useState(false);
const [busyPlugins, setBusyPlugins] = useState<Record<string, string>>({});
const [result, reexecuteQuery] = useQuery({ query: AVAILABLE_PLUGINS });
const [, installPlugin] = useMutation(INSTALL_PLUGIN);
const [, uninstallPlugin] = useMutation(UNINSTALL_PLUGIN);
const [, updatePlugin] = useMutation(UPDATE_PLUGIN);
const plugins: PluginRow[] = result.data?.availablePlugins || [];
const loading = result.fetching;
function refetch() {
reexecuteQuery({ requestPolicy: "network-only" });
}
function setBusy(name: string, action: string) {
setBusyPlugins((prev) => ({ ...prev, [name]: action }));
}
function clearBusy(name: string) {
setBusyPlugins((prev) => {
const next = { ...prev };
delete next[name];
return next;
});
}
const filtered = search
? plugins.filter((p) => (p.packageName || p.name).toLowerCase().includes(search.toLowerCase()))
: plugins;
async function handleInstall(e: FormEvent) {
e.preventDefault();
if (!installSpec.trim()) return;
setSubmitting(true);
try {
const res = await installPlugin({ packageSpec: installSpec.trim() });
if (res.error) {
toast.error(res.error.message);
return;
}
const data = res.data?.installPlugin;
if (!data?.success) {
toast.error(data?.error || "Install failed");
return;
}
toast.success(`Installed ${installSpec.trim()}`);
setInstallOpen(false);
setInstallSpec("");
setNeedsRestart(true);
refetch();
} catch (err: any) {
toast.error(err.message || "Install failed");
} finally {
setSubmitting(false);
}
}
async function handleUninstall(name: string) {
setBusy(name, "Uninstalling...");
try {
const res = await uninstallPlugin({ packageName: name });
if (res.error) {
toast.error(res.error.message);
return;
}
const data = res.data?.uninstallPlugin;
if (!data?.success) {
toast.error(data?.error || "Uninstall failed");
return;
}
toast.success(`Uninstalled ${name}`);
setNeedsRestart(true);
refetch();
} catch (err: any) {
toast.error(err.message || "Uninstall failed");
} finally {
clearBusy(name);
}
}
async function handleUpdate(name: string, packageName?: string) {
setBusy(name, "Updating...");
try {
const res = await updatePlugin({ packageName: packageName || name });
if (res.error) {
toast.error(res.error.message);
return;
}
const data = res.data?.updatePlugin;
if (!data?.success) {
toast.error(data?.error || "Update failed");
return;
}
toast.success(`Updated ${name}`);
setNeedsRestart(true);
refetch();
} catch (err: any) {
toast.error(err.message || "Update failed");
} finally {
clearBusy(name);
}
}
async function handleInstallFromRegistry(name: string, packageName?: string) {
setBusy(name, "Installing...");
try {
const res = await installPlugin({ packageSpec: packageName || name });
if (res.error) {
toast.error(res.error.message);
return;
}
const data = res.data?.installPlugin;
if (!data?.success) {
toast.error(data?.error || "Install failed");
return;
}
toast.success(`Installed ${packageName || name}`);
setNeedsRestart(true);
refetch();
} catch (err: any) {
toast.error(err.message || "Install failed");
} finally {
clearBusy(name);
}
}
const columns: Column<PluginRow>[] = [
{
header: "Name",
cell: (row) => (
<div>
<code className="text-xs font-mono">{row.packageName || row.name}</code>
{row.description && (
<p className="text-xs text-muted-foreground mt-0.5 max-w-xs truncate" title={row.description}>{row.description}</p>
)}
</div>
),
},
{
header: "Version",
cell: (row) => {
if (!row.version) return <span className="text-sm">—</span>;
const display = row.version.length > 16 ? row.version.slice(0, 16) + "..." : row.version;
return <span className="text-sm" title={row.version}>{display}</span>;
},
},
{
header: "Registry Version",
cell: (row) => {
if (!row.registryVersion) return <span className="text-sm">—</span>;
const display = row.registryVersion.length > 16 ? row.registryVersion.slice(0, 16) + "..." : row.registryVersion;
return (
<span className="text-sm" title={row.registryVersion}>
{display}
{row.installed && row.version && row.registryVersion !== row.version && (
<Badge variant="outline" className="ml-1 text-[10px] px-1 py-0">new</Badge>
)}
</span>
);
},
},
{
header: "Status",
cell: (row) => {
const busy = busyPlugins[row.name];
if (busy) {
return (
<span className="flex items-center gap-1.5 text-sm text-muted-foreground">
<LoaderIcon className="h-3 w-3 animate-spin" />
{busy}
</span>
);
}
const status = getStatus(row);
return <Badge variant={status.variant}>{status.label}</Badge>;
},
},
{
header: "Actions",
cell: (row) => {
const busy = !!busyPlugins[row.name];
return (
<div className="flex gap-1" onClick={(e) => e.stopPropagation()}>
{!row.installed && row.registryVersion && (
<Button variant="outline" size="sm" disabled={busy} onClick={() => handleInstallFromRegistry(row.name, row.packageName)}>
<DownloadIcon className="h-3 w-3 mr-1" />
Install
</Button>
)}
{row.installed && row.registryVersion && row.registryVersion > (row.version || "") && (
<Button variant="outline" size="sm" disabled={busy} onClick={() => handleUpdate(row.name, row.packageName)}>
<ArrowUpCircleIcon className="h-3 w-3 mr-1" />
Update
</Button>
)}
{row.installed && (
<Button variant="outline" size="sm" disabled={busy} onClick={() => handleUninstall(row.name)}>
<Trash2Icon className="h-3 w-3 mr-1" />
Uninstall
</Button>
)}
</div>
);
},
},
];
return (
<div className="space-y-4">
<div>
<h2 className="text-2xl font-bold">Plugins</h2>
<p className="text-muted-foreground">
Manage installed plugins ({plugins.length} total)
</p>
</div>
{needsRestart && (
<div className="rounded-md border border-blue-200 bg-blue-50 dark:border-blue-900 dark:bg-blue-950 px-4 py-3 text-sm text-blue-800 dark:text-blue-200">
A server restart is needed for plugin changes to take effect.
</div>
)}
<DataTable
columns={columns}
data={filtered}
loading={loading}
searchPlaceholder="Filter plugins..."
searchValue={search}
onSearchChange={setSearch}
emptyMessage="No plugins found."
actions={
<>
<Button variant="outline" onClick={refetch}>
<RefreshCwIcon className="h-4 w-4" />
</Button>
<Button onClick={() => setInstallOpen(true)}>
<PlusIcon />
Install Plugin
</Button>
</>
}
/>
<Dialog
open={installOpen}
onOpenChange={(open) => {
setInstallOpen(open);
if (!open) setInstallSpec("");
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>Install Plugin</DialogTitle>
<DialogDescription>
Enter a package name or spec (e.g. @trex/etl or @trex/etl@1.0.0).
</DialogDescription>
</DialogHeader>
<form onSubmit={handleInstall} className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<Label htmlFor="pkg-spec">Package</Label>
<Input
id="pkg-spec"
placeholder="@trex/my-plugin@1.0.0"
value={installSpec}
onChange={(e) => setInstallSpec(e.target.value)}
required
/>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => {
setInstallOpen(false);
setInstallSpec("");
}}
>
Cancel
</Button>
<Button type="submit" disabled={submitting}>
{submitting ? "Installing..." : "Install"}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</div>
);
}