-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathScriptDetailModal.tsx
More file actions
741 lines (703 loc) · 27.9 KB
/
ScriptDetailModal.tsx
File metadata and controls
741 lines (703 loc) · 27.9 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
"use client";
import { useState } from "react";
import Image from "next/image";
import { api } from "~/trpc/react";
import type { Script } from "~/types/script";
import { DiffViewer } from "./DiffViewer";
import { TextViewer } from "./TextViewer";
import { ExecutionModeModal } from "./ExecutionModeModal";
import { TypeBadge, UpdateableBadge, PrivilegedBadge, NoteBadge } from "./Badge";
import { Button } from "./ui/button";
import { useRegisterModal } from './modal/ModalStackProvider';
interface ScriptDetailModalProps {
script: Script | null;
isOpen: boolean;
onClose: () => void;
onInstallScript?: (
scriptPath: string,
scriptName: string,
mode?: "local" | "ssh",
server?: any,
) => void;
}
export function ScriptDetailModal({
script,
isOpen,
onClose,
onInstallScript,
}: ScriptDetailModalProps) {
useRegisterModal(isOpen, { id: 'script-detail-modal', allowEscape: true, onClose });
const [imageError, setImageError] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [loadMessage, setLoadMessage] = useState<string | null>(null);
const [diffViewerOpen, setDiffViewerOpen] = useState(false);
const [selectedDiffFile, setSelectedDiffFile] = useState<string | null>(null);
const [textViewerOpen, setTextViewerOpen] = useState(false);
const [executionModeOpen, setExecutionModeOpen] = useState(false);
// Check if script files exist locally
const {
data: scriptFilesData,
refetch: refetchScriptFiles,
isLoading: scriptFilesLoading,
} = api.scripts.checkScriptFiles.useQuery(
{ slug: script?.slug ?? "" },
{ enabled: !!script && isOpen },
);
// Compare local and remote script content (run in parallel, not dependent on scriptFilesData)
const {
data: comparisonData,
refetch: refetchComparison,
isLoading: comparisonLoading,
} = api.scripts.compareScriptContent.useQuery(
{ slug: script?.slug ?? "" },
{ enabled: !!script && isOpen },
);
// Load script mutation
const loadScriptMutation = api.scripts.loadScript.useMutation({
onSuccess: (data) => {
setIsLoading(false);
if (data.success) {
const message =
"message" in data ? data.message : "Script loaded successfully";
setLoadMessage(`[SUCCESS] ${message}`);
// Refetch script files status and comparison data to update the UI
void refetchScriptFiles();
void refetchComparison();
} else {
const error = "error" in data ? data.error : "Failed to load script";
setLoadMessage(`[ERROR] ${error}`);
}
// Clear message after 5 seconds
setTimeout(() => setLoadMessage(null), 5000);
},
onError: (error) => {
setIsLoading(false);
setLoadMessage(`[ERROR] ${error.message}`);
setTimeout(() => setLoadMessage(null), 5000);
},
});
if (!isOpen || !script) return null;
const handleImageError = () => {
setImageError(true);
};
const handleBackdropClick = (e: React.MouseEvent) => {
if (e.target === e.currentTarget) {
onClose();
}
};
const handleLoadScript = async () => {
if (!script) return;
setIsLoading(true);
setLoadMessage(null);
loadScriptMutation.mutate({ slug: script.slug });
};
const handleInstallScript = () => {
if (!script) return;
setExecutionModeOpen(true);
};
const handleExecuteScript = (mode: "local" | "ssh", server?: any) => {
if (!script || !onInstallScript) return;
// Find the script path (CT or tools)
const scriptMethod = script.install_methods?.find(
(method) => method.script,
);
if (scriptMethod?.script) {
const scriptPath = `scripts/${scriptMethod.script}`;
const scriptName = script.name;
// Pass execution mode and server info to the parent
onInstallScript(scriptPath, scriptName, mode, server);
onClose(); // Close the modal when starting installation
}
};
const handleViewScript = () => {
setTextViewerOpen(true);
};
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center p-4 backdrop-blur-sm bg-black/50"
onClick={handleBackdropClick}
>
<div className="bg-card rounded-lg shadow-xl max-w-6xl w-full max-h-[95vh] min-h-[80vh] overflow-y-auto border border-border mx-2 sm:mx-4 lg:mx-0">
{/* Header */}
<div className="flex items-center justify-between border-b border-border p-4 sm:p-6">
<div className="flex items-center space-x-3 sm:space-x-4 min-w-0 flex-1">
{script.logo && !imageError ? (
<Image
src={script.logo}
alt={`${script.name} logo`}
width={64}
height={64}
className="h-12 w-12 sm:h-16 sm:w-16 rounded-lg object-contain flex-shrink-0"
onError={handleImageError}
/>
) : (
<div className="flex h-12 w-12 sm:h-16 sm:w-16 items-center justify-center rounded-lg bg-muted flex-shrink-0">
<span className="text-lg sm:text-2xl font-semibold text-muted-foreground">
{script.name.charAt(0).toUpperCase()}
</span>
</div>
)}
<div className="min-w-0 flex-1">
<h2 className="text-xl sm:text-2xl font-bold text-foreground truncate">
{script.name}
</h2>
<div className="mt-1 flex flex-wrap items-center gap-1 sm:gap-2">
<TypeBadge type={script.type} />
{script.updateable && <UpdateableBadge />}
{script.privileged && <PrivilegedBadge />}
</div>
</div>
{/* Interface Port*/}
{script.interface_port && (
<div className="ml-3 sm:ml-4 flex-shrink-0">
<div className="bg-primary/10 border border-primary/30 rounded-lg px-3 py-1.5 sm:px-4 sm:py-2">
<span className="text-xs sm:text-sm font-medium text-muted-foreground mr-2">
Port:
</span>
<span className="text-sm sm:text-base font-semibold text-foreground font-mono">
{script.interface_port}
</span>
</div>
</div>
)}
</div>
{/* Close Button */}
<Button
onClick={onClose}
variant="ghost"
size="icon"
className="text-muted-foreground hover:text-foreground flex-shrink-0 ml-4"
>
<svg
className="h-5 w-5 sm:h-6 sm:w-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</Button>
</div>
{/* Action Buttons */}
<div className="flex flex-col sm:flex-row items-stretch sm:items-center space-y-2 sm:space-y-0 sm:space-x-2 p-4 sm:p-6 border-b border-border">
{/* Install Button - only show if script files exist */}
{scriptFilesData?.success &&
scriptFilesData.ctExists &&
onInstallScript && (
<Button
onClick={handleInstallScript}
variant="outline"
size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"
/>
</svg>
<span>Install</span>
</Button>
)}
{/* View Button - only show if script files exist */}
{scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists) && (
<Button
onClick={handleViewScript}
variant="outline"
size="default"
className="w-full sm:w-auto flex items-center justify-center space-x-2"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
/>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"
/>
</svg>
<span>View</span>
</Button>
)}
{/* Load/Update Script Button */}
{(() => {
const hasLocalFiles =
scriptFilesData?.success &&
(scriptFilesData.ctExists || scriptFilesData.installExists);
const hasDifferences =
comparisonData?.success && comparisonData.hasDifferences;
const isUpToDate = hasLocalFiles && !hasDifferences;
if (!hasLocalFiles) {
// No local files - show Load Script button
return (
<button
onClick={handleLoadScript}
disabled={isLoading}
className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
isLoading
? "cursor-not-allowed bg-muted text-muted-foreground"
: "bg-success text-success-foreground hover:bg-success/90"
}`}
>
{isLoading ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Loading...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
<span>Load Script</span>
</>
)}
</button>
);
} else if (isUpToDate) {
// Local files exist and are up to date - show disabled Update button
return (
<button
disabled
className="flex cursor-not-allowed items-center space-x-2 rounded-lg bg-muted px-4 py-2 font-medium text-muted-foreground transition-colors"
>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Up to Date</span>
</button>
);
} else {
// Local files exist but have differences - show Update button
return (
<button
onClick={handleLoadScript}
disabled={isLoading}
className={`flex items-center space-x-2 rounded-lg px-4 py-2 font-medium transition-colors ${
isLoading
? "cursor-not-allowed bg-muted text-muted-foreground"
: "bg-warning text-warning-foreground hover:bg-warning/90"
}`}
>
{isLoading ? (
<>
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-white"></div>
<span>Updating...</span>
</>
) : (
<>
<svg
className="h-4 w-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"
/>
</svg>
<span>Update Script</span>
</>
)}
</button>
);
}
})()}
</div>
{/* Content */}
<div className="space-y-4 sm:space-y-6 p-4 sm:p-6">
{/* Script Files Status */}
{(scriptFilesLoading || comparisonLoading) && (
<div className="mb-4 rounded-lg bg-primary/10 p-3 text-sm text-primary">
<div className="flex items-center space-x-2">
<div className="h-4 w-4 animate-spin rounded-full border-b-2 border-primary"></div>
<span>Loading script status...</span>
</div>
</div>
)}
{scriptFilesData?.success &&
!scriptFilesLoading &&
(() => {
// Determine script type from the first install method
const firstScript = script?.install_methods?.[0]?.script;
let scriptType = "Script";
if (firstScript?.startsWith("ct/")) {
scriptType = "CT Script";
} else if (firstScript?.startsWith("tools/")) {
scriptType = "Tools Script";
} else if (firstScript?.startsWith("vm/")) {
scriptType = "VM Script";
} else if (firstScript?.startsWith("vw/")) {
scriptType = "VW Script";
}
return (
<div className="mb-4 rounded-lg bg-muted p-3 text-sm text-muted-foreground">
<div className="flex flex-col sm:flex-row sm:items-center space-y-2 sm:space-y-0 sm:space-x-4">
<div className="flex items-center space-x-2">
<div
className={`h-2 w-2 rounded-full ${scriptFilesData.ctExists ? "bg-success" : "bg-muted"}`}
></div>
<span>
{scriptType}:{" "}
{scriptFilesData.ctExists ? "Available" : "Not loaded"}
</span>
</div>
<div className="flex items-center space-x-2">
<div
className={`h-2 w-2 rounded-full ${scriptFilesData.installExists ? "bg-success" : "bg-muted"}`}
></div>
<span>
Install Script:{" "}
{scriptFilesData.installExists
? "Available"
: "Not loaded"}
</span>
</div>
{scriptFilesData?.success &&
(scriptFilesData.ctExists ||
scriptFilesData.installExists) &&
comparisonData?.success &&
!comparisonLoading && (
<div className="flex items-center space-x-2">
<div
className={`h-2 w-2 rounded-full ${comparisonData.hasDifferences ? "bg-warning" : "bg-success"}`}
></div>
<span>
Status:{" "}
{comparisonData.hasDifferences
? "Update available"
: "Up to date"}
</span>
</div>
)}
</div>
{scriptFilesData.files.length > 0 && (
<div className="mt-2 text-xs text-muted-foreground break-words">
Files: {scriptFilesData.files.join(", ")}
</div>
)}
</div>
);
})()}
{/* Load Message */}
{loadMessage && (
<div className="mb-4 rounded-lg bg-primary/10 p-3 text-sm text-primary">
{loadMessage}
</div>
)}
{/* Description */}
<div>
<h3 className="mb-2 text-base sm:text-lg font-semibold text-foreground">
Description
</h3>
<p className="text-sm sm:text-base text-muted-foreground">
{script.description}
</p>
</div>
{/* Basic Information */}
<div className="grid grid-cols-1 gap-4 sm:gap-6 lg:grid-cols-2">
<div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground">
Basic Information
</h3>
<dl className="space-y-2">
<div>
<dt className="text-sm font-medium text-muted-foreground">
Slug
</dt>
<dd className="font-mono text-sm text-foreground">
{script.slug}
</dd>
</div>
<div>
<dt className="text-sm font-medium text-muted-foreground">
Date Created
</dt>
<dd className="text-sm text-foreground">
{script.date_created}
</dd>
</div>
<div>
<dt className="text-sm font-medium text-muted-foreground">
Categories
</dt>
<dd className="text-sm text-foreground">
{script.categories.join(", ")}
</dd>
</div>
{script.interface_port && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Interface Port
</dt>
<dd className="text-sm text-foreground">
{script.interface_port}
</dd>
</div>
)}
{script.config_path && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Config Path
</dt>
<dd className="font-mono text-sm text-foreground">
{script.config_path}
</dd>
</div>
)}
</dl>
</div>
<div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground">
Links
</h3>
<dl className="space-y-2">
{script.website && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Website
</dt>
<dd className="text-sm">
<a
href={script.website}
target="_blank"
rel="noopener noreferrer"
className="break-all text-primary hover:text-primary/80"
>
{script.website}
</a>
</dd>
</div>
)}
{script.documentation && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Documentation
</dt>
<dd className="text-sm">
<a
href={script.documentation}
target="_blank"
rel="noopener noreferrer"
className="break-all text-primary hover:text-primary/80"
>
{script.documentation}
</a>
</dd>
</div>
)}
</dl>
</div>
</div>
{/* Install Methods - Hide for PVE and ADDON types as they typically don't have install methods */}
{script.install_methods.length > 0 &&
script.type !== "pve" &&
script.type !== "addon" && (
<div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground">
Install Methods
</h3>
<div className="space-y-4">
{script.install_methods.map((method, index) => (
<div
key={index}
className="rounded-lg border border-border bg-card p-3 sm:p-4"
>
<div className="mb-3 flex flex-col sm:flex-row sm:items-center justify-between space-y-1 sm:space-y-0">
<h4 className="text-sm sm:text-base font-medium text-foreground capitalize">
{method.type}
</h4>
<span className="font-mono text-xs sm:text-sm text-muted-foreground break-all">
{method.script}
</span>
</div>
<div className="grid grid-cols-2 gap-2 sm:gap-4 text-xs sm:text-sm lg:grid-cols-4">
<div>
<dt className="font-medium text-muted-foreground">
CPU
</dt>
<dd className="text-foreground">
{method.resources.cpu} cores
</dd>
</div>
<div>
<dt className="font-medium text-muted-foreground">
RAM
</dt>
<dd className="text-foreground">
{method.resources.ram} MB
</dd>
</div>
<div>
<dt className="font-medium text-muted-foreground">
HDD
</dt>
<dd className="text-foreground">
{method.resources.hdd} GB
</dd>
</div>
<div>
<dt className="font-medium text-muted-foreground">
OS
</dt>
<dd className="text-foreground">
{method.resources.os} {method.resources.version}
</dd>
</div>
</div>
</div>
))}
</div>
</div>
)}
{/* Default Credentials */}
{(script.default_credentials.username ??
script.default_credentials.password) && (
<div>
<h3 className="mb-3 text-base sm:text-lg font-semibold text-foreground">
Default Credentials
</h3>
<dl className="space-y-2">
{script.default_credentials.username && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Username
</dt>
<dd className="font-mono text-sm text-foreground">
{script.default_credentials.username}
</dd>
</div>
)}
{script.default_credentials.password && (
<div>
<dt className="text-sm font-medium text-muted-foreground">
Password
</dt>
<dd className="font-mono text-sm text-foreground">
{script.default_credentials.password}
</dd>
</div>
)}
</dl>
</div>
)}
{/* Notes */}
{script.notes.length > 0 && (
<div>
<h3 className="mb-3 text-lg font-semibold text-foreground">
Notes
</h3>
<ul className="space-y-2">
{script.notes.map((note, index) => {
// Handle both object and string note formats
const noteText = typeof note === "string" ? note : note.text;
const noteType =
typeof note === "string" ? "info" : note.type;
return (
<li
key={index}
className={`rounded-lg p-3 text-sm ${
noteType === "warning"
? "border-l-4 border-warning bg-warning/10 text-warning"
: noteType === "error"
? "border-l-4 border-destructive bg-destructive/10 text-destructive"
: "bg-muted text-muted-foreground"
}`}
>
<div className="flex items-start">
<NoteBadge noteType={noteType as 'info' | 'warning' | 'error'} className="mr-2 flex-shrink-0">
{noteType}
</NoteBadge>
<span>{noteText}</span>
</div>
</li>
);
})}
</ul>
</div>
)}
</div>
</div>
{/* Diff Viewer Modal */}
{selectedDiffFile && (
<DiffViewer
scriptSlug={script.slug}
filePath={selectedDiffFile}
isOpen={diffViewerOpen}
onClose={() => {
setDiffViewerOpen(false);
setSelectedDiffFile(null);
}}
/>
)}
{/* Text Viewer Modal */}
{script && (
<TextViewer
scriptName={
script.install_methods
?.find((method) => method.script?.startsWith("ct/"))
?.script?.split("/")
.pop() ?? `${script.slug}.sh`
}
isOpen={textViewerOpen}
onClose={() => setTextViewerOpen(false)}
/>
)}
{/* Execution Mode Modal */}
{script && (
<ExecutionModeModal
scriptName={script.name}
isOpen={executionModeOpen}
onClose={() => setExecutionModeOpen(false)}
onExecute={handleExecuteScript}
/>
)}
</div>
);
}