2025-06-28 00:38:09 +02:00
|
|
|
import type { z } from "zod";
|
|
|
|
|
|
2024-11-14 09:08:45 +01:00
|
|
|
import { PlusCircle, Trash2 } from "lucide-react";
|
2024-11-28 15:50:40 +01:00
|
|
|
import { memo, useCallback, useRef } from "react";
|
2025-06-28 00:38:09 +02:00
|
|
|
|
|
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
|
|
|
import { OperatingSystems } from "@/config/site-config";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
|
|
|
|
|
import type { Script } from "../_schemas/schemas";
|
|
|
|
|
|
|
|
|
|
import { InstallMethodSchema, ScriptSchema } from "../_schemas/schemas";
|
2024-11-14 09:08:45 +01:00
|
|
|
|
|
|
|
|
type InstallMethodProps = {
|
|
|
|
|
script: Script;
|
|
|
|
|
setScript: (value: Script | ((prevState: Script) => Script)) => void;
|
|
|
|
|
setIsValid: (isValid: boolean) => void;
|
|
|
|
|
setZodErrors: (zodErrors: z.ZodError | null) => void;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-09 13:10:02 +02:00
|
|
|
function InstallMethod({ script, setScript, setIsValid, setZodErrors }: InstallMethodProps) {
|
2024-11-23 08:14:22 +01:00
|
|
|
const cpuRefs = useRef<(HTMLInputElement | null)[]>([]);
|
|
|
|
|
const ramRefs = useRef<(HTMLInputElement | null)[]>([]);
|
|
|
|
|
const hddRefs = useRef<(HTMLInputElement | null)[]>([]);
|
|
|
|
|
|
2024-11-15 18:16:19 +01:00
|
|
|
const addInstallMethod = useCallback(() => {
|
2024-11-14 09:08:45 +01:00
|
|
|
setScript((prev) => {
|
2025-04-09 13:10:02 +02:00
|
|
|
const { type, slug } = prev;
|
|
|
|
|
const newMethodType = "default";
|
|
|
|
|
|
|
|
|
|
let scriptPath = "";
|
|
|
|
|
|
|
|
|
|
if (type === "pve") {
|
|
|
|
|
scriptPath = `tools/pve/${slug}.sh`;
|
2025-06-28 00:38:09 +02:00
|
|
|
}
|
|
|
|
|
else if (type === "addon") {
|
2025-04-09 13:10:02 +02:00
|
|
|
scriptPath = `tools/addon/${slug}.sh`;
|
2025-06-28 00:38:09 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2025-04-09 13:10:02 +02:00
|
|
|
scriptPath = `${type}/${slug}.sh`;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:08:45 +01:00
|
|
|
const method = InstallMethodSchema.parse({
|
2025-04-09 13:10:02 +02:00
|
|
|
type: newMethodType,
|
|
|
|
|
script: scriptPath,
|
2024-11-14 09:08:45 +01:00
|
|
|
resources: {
|
|
|
|
|
cpu: null,
|
|
|
|
|
ram: null,
|
|
|
|
|
hdd: null,
|
|
|
|
|
os: null,
|
|
|
|
|
version: null,
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-04-09 13:10:02 +02:00
|
|
|
|
2024-11-14 09:08:45 +01:00
|
|
|
return {
|
|
|
|
|
...prev,
|
|
|
|
|
install_methods: [...prev.install_methods, method],
|
|
|
|
|
};
|
|
|
|
|
});
|
2024-11-15 18:16:19 +01:00
|
|
|
}, [setScript]);
|
2024-11-14 09:08:45 +01:00
|
|
|
|
2024-11-23 08:14:22 +01:00
|
|
|
const updateInstallMethod = useCallback(
|
|
|
|
|
(
|
|
|
|
|
index: number,
|
|
|
|
|
key: keyof Script["install_methods"][number],
|
|
|
|
|
value: Script["install_methods"][number][keyof Script["install_methods"][number]],
|
|
|
|
|
) => {
|
|
|
|
|
setScript((prev) => {
|
|
|
|
|
const updatedMethods = prev.install_methods.map((method, i) => {
|
|
|
|
|
if (i === index) {
|
|
|
|
|
const updatedMethod = { ...method, [key]: value };
|
|
|
|
|
|
|
|
|
|
if (key === "type") {
|
2025-06-28 00:38:09 +02:00
|
|
|
updatedMethod.script
|
|
|
|
|
= value === "alpine" ? `${prev.type}/alpine-${prev.slug}.sh` : `${prev.type}/${prev.slug}.sh`;
|
2024-11-14 09:08:45 +01:00
|
|
|
|
2024-11-23 08:14:22 +01:00
|
|
|
// Set OS to Alpine and reset version if type is alpine
|
|
|
|
|
if (value === "alpine") {
|
|
|
|
|
updatedMethod.resources.os = "Alpine";
|
|
|
|
|
updatedMethod.resources.version = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updatedMethod;
|
2024-11-14 09:08:45 +01:00
|
|
|
}
|
2024-11-23 08:14:22 +01:00
|
|
|
return method;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const updated = {
|
|
|
|
|
...prev,
|
|
|
|
|
install_methods: updatedMethods,
|
|
|
|
|
};
|
2024-11-14 09:08:45 +01:00
|
|
|
|
2024-11-23 08:14:22 +01:00
|
|
|
const result = ScriptSchema.safeParse(updated);
|
|
|
|
|
setIsValid(result.success);
|
|
|
|
|
if (!result.success) {
|
|
|
|
|
setZodErrors(result.error);
|
2025-06-28 00:38:09 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2024-11-23 08:14:22 +01:00
|
|
|
setZodErrors(null);
|
2024-11-14 09:08:45 +01:00
|
|
|
}
|
2024-11-23 08:14:22 +01:00
|
|
|
return updated;
|
2024-11-14 09:08:45 +01:00
|
|
|
});
|
2024-11-23 08:14:22 +01:00
|
|
|
},
|
|
|
|
|
[setScript, setIsValid, setZodErrors],
|
|
|
|
|
);
|
2024-11-14 09:08:45 +01:00
|
|
|
|
2024-11-23 08:14:22 +01:00
|
|
|
const removeInstallMethod = useCallback(
|
|
|
|
|
(index: number) => {
|
2025-06-28 00:38:09 +02:00
|
|
|
setScript(prev => ({
|
2024-11-14 09:08:45 +01:00
|
|
|
...prev,
|
2024-11-23 08:14:22 +01:00
|
|
|
install_methods: prev.install_methods.filter((_, i) => i !== index),
|
|
|
|
|
}));
|
|
|
|
|
},
|
|
|
|
|
[setScript],
|
|
|
|
|
);
|
2024-11-14 09:08:45 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<h3 className="text-xl font-semibold">Install Methods</h3>
|
|
|
|
|
{script.install_methods.map((method, index) => (
|
|
|
|
|
<div key={index} className="space-y-2 border p-4 rounded">
|
2025-06-28 00:38:09 +02:00
|
|
|
<Select value={method.type} onValueChange={value => updateInstallMethod(index, "type", value)}>
|
2024-11-14 09:08:45 +01:00
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder="Type" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
<SelectItem value="default">Default</SelectItem>
|
|
|
|
|
<SelectItem value="alpine">Alpine</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
<div className="flex gap-2">
|
2024-11-23 08:14:22 +01:00
|
|
|
<Input
|
|
|
|
|
ref={(el) => {
|
|
|
|
|
cpuRefs.current[index] = el;
|
|
|
|
|
}}
|
2024-11-14 09:08:45 +01:00
|
|
|
placeholder="CPU in Cores"
|
|
|
|
|
type="number"
|
2024-11-23 08:14:22 +01:00
|
|
|
value={method.resources.cpu || ""}
|
2025-06-28 00:38:09 +02:00
|
|
|
onChange={e =>
|
2024-11-14 09:08:45 +01:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
|
...method.resources,
|
|
|
|
|
cpu: e.target.value ? Number(e.target.value) : null,
|
2025-06-28 00:38:09 +02:00
|
|
|
})}
|
2024-11-14 09:08:45 +01:00
|
|
|
/>
|
2024-11-23 08:14:22 +01:00
|
|
|
<Input
|
|
|
|
|
ref={(el) => {
|
|
|
|
|
ramRefs.current[index] = el;
|
|
|
|
|
}}
|
2024-11-14 09:08:45 +01:00
|
|
|
placeholder="RAM in MB"
|
|
|
|
|
type="number"
|
2024-11-23 08:14:22 +01:00
|
|
|
value={method.resources.ram || ""}
|
2025-06-28 00:38:09 +02:00
|
|
|
onChange={e =>
|
2024-11-14 09:08:45 +01:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
|
...method.resources,
|
|
|
|
|
ram: e.target.value ? Number(e.target.value) : null,
|
2025-06-28 00:38:09 +02:00
|
|
|
})}
|
2024-11-14 09:08:45 +01:00
|
|
|
/>
|
2024-11-23 08:14:22 +01:00
|
|
|
<Input
|
|
|
|
|
ref={(el) => {
|
|
|
|
|
hddRefs.current[index] = el;
|
|
|
|
|
}}
|
|
|
|
|
placeholder="HDD in GB"
|
2024-11-14 09:08:45 +01:00
|
|
|
type="number"
|
2024-11-23 08:14:22 +01:00
|
|
|
value={method.resources.hdd || ""}
|
2025-06-28 00:38:09 +02:00
|
|
|
onChange={e =>
|
2024-11-14 09:08:45 +01:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
|
...method.resources,
|
|
|
|
|
hdd: e.target.value ? Number(e.target.value) : null,
|
2025-06-28 00:38:09 +02:00
|
|
|
})}
|
2024-11-14 09:08:45 +01:00
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex gap-2">
|
2024-11-23 08:14:22 +01:00
|
|
|
<Select
|
|
|
|
|
value={method.resources.os || undefined}
|
2025-06-28 00:38:09 +02:00
|
|
|
onValueChange={value =>
|
2024-11-14 09:08:45 +01:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
|
...method.resources,
|
2024-11-23 08:14:22 +01:00
|
|
|
os: value || null,
|
|
|
|
|
version: null, // Reset version when OS changes
|
2025-06-28 00:38:09 +02:00
|
|
|
})}
|
2024-11-23 08:14:22 +01:00
|
|
|
disabled={method.type === "alpine"}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder="OS" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
2025-06-28 00:38:09 +02:00
|
|
|
{OperatingSystems.map(os => (
|
2024-11-23 08:14:22 +01:00
|
|
|
<SelectItem key={os.name} value={os.name}>
|
|
|
|
|
{os.name}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
<Select
|
2024-11-28 15:50:40 +01:00
|
|
|
value={method.resources.version || undefined}
|
2025-06-28 00:38:09 +02:00
|
|
|
onValueChange={value =>
|
2024-11-14 09:08:45 +01:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
|
...method.resources,
|
2024-11-28 15:50:40 +01:00
|
|
|
version: value || null,
|
2025-06-28 00:38:09 +02:00
|
|
|
})}
|
2024-11-23 08:14:22 +01:00
|
|
|
disabled={method.type === "alpine"}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder="Version" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
2025-06-28 00:38:09 +02:00
|
|
|
{OperatingSystems.find(os => os.name === method.resources.os)?.versions.map(version => (
|
2024-11-23 08:14:22 +01:00
|
|
|
<SelectItem key={version.slug} value={version.name}>
|
|
|
|
|
{version.name}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
2024-11-14 09:08:45 +01:00
|
|
|
</div>
|
2025-04-09 13:10:02 +02:00
|
|
|
<Button variant="destructive" size="sm" type="button" onClick={() => removeInstallMethod(index)}>
|
2025-06-28 00:38:09 +02:00
|
|
|
<Trash2 className="mr-2 h-4 w-4" />
|
|
|
|
|
{" "}
|
|
|
|
|
Remove Install Method
|
2024-11-14 09:08:45 +01:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2025-04-09 13:10:02 +02:00
|
|
|
<Button type="button" size="sm" disabled={script.install_methods.length >= 2} onClick={addInstallMethod}>
|
2025-06-28 00:38:09 +02:00
|
|
|
<PlusCircle className="mr-2 h-4 w-4" />
|
|
|
|
|
{" "}
|
|
|
|
|
Add Install Method
|
2024-11-14 09:08:45 +01:00
|
|
|
</Button>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-11-15 18:16:19 +01:00
|
|
|
|
|
|
|
|
export default memo(InstallMethod);
|