2024-11-14 08:08:45 +00:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
SelectContent,
|
|
|
|
SelectItem,
|
|
|
|
SelectTrigger,
|
|
|
|
SelectValue,
|
|
|
|
} from "@/components/ui/select";
|
|
|
|
import { PlusCircle, Trash2 } from "lucide-react";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { InstallMethodSchema, ScriptSchema } from "../_schemas/schemas";
|
2024-11-15 17:16:19 +00:00
|
|
|
import { memo, useCallback } from "react";
|
2024-11-14 08:08:45 +00:00
|
|
|
|
|
|
|
type Script = z.infer<typeof ScriptSchema>;
|
|
|
|
|
|
|
|
type InstallMethodProps = {
|
|
|
|
script: Script;
|
|
|
|
setScript: (value: Script | ((prevState: Script) => Script)) => void;
|
|
|
|
setIsValid: (isValid: boolean) => void;
|
|
|
|
setZodErrors: (zodErrors: z.ZodError | null) => void;
|
|
|
|
};
|
|
|
|
|
2024-11-15 17:16:19 +00:00
|
|
|
function InstallMethod({
|
2024-11-14 08:08:45 +00:00
|
|
|
script,
|
|
|
|
setScript,
|
|
|
|
setIsValid,
|
|
|
|
setZodErrors,
|
|
|
|
}: InstallMethodProps) {
|
2024-11-15 17:16:19 +00:00
|
|
|
const addInstallMethod = useCallback(() => {
|
2024-11-14 08:08:45 +00:00
|
|
|
setScript((prev) => {
|
|
|
|
const method = InstallMethodSchema.parse({
|
|
|
|
type: "default",
|
|
|
|
script: `/${prev.type}/${prev.slug}.sh`,
|
|
|
|
resources: {
|
|
|
|
cpu: null,
|
|
|
|
ram: null,
|
|
|
|
hdd: null,
|
|
|
|
os: null,
|
|
|
|
version: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
...prev,
|
|
|
|
install_methods: [...prev.install_methods, method],
|
|
|
|
};
|
|
|
|
});
|
2024-11-15 17:16:19 +00:00
|
|
|
}, [setScript]);
|
2024-11-14 08:08:45 +00:00
|
|
|
|
2024-11-15 17:16:19 +00:00
|
|
|
const updateInstallMethod = useCallback((
|
2024-11-14 08:08:45 +00:00
|
|
|
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") {
|
|
|
|
updatedMethod.script =
|
|
|
|
value === "alpine"
|
|
|
|
? `/${prev.type}/alpine-${prev.slug}.sh`
|
|
|
|
: `/${prev.type}/${prev.slug}.sh`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatedMethod;
|
|
|
|
}
|
|
|
|
return method;
|
|
|
|
});
|
|
|
|
|
|
|
|
const updated = {
|
|
|
|
...prev,
|
|
|
|
install_methods: updatedMethods,
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = ScriptSchema.safeParse(updated);
|
|
|
|
setIsValid(result.success);
|
|
|
|
if (!result.success) {
|
|
|
|
setZodErrors(result.error);
|
|
|
|
} else {
|
|
|
|
setZodErrors(null);
|
|
|
|
}
|
|
|
|
return updated;
|
|
|
|
});
|
2024-11-15 17:16:19 +00:00
|
|
|
}, [setScript, setIsValid, setZodErrors]);
|
2024-11-14 08:08:45 +00:00
|
|
|
|
2024-11-15 17:16:19 +00:00
|
|
|
const removeInstallMethod = useCallback((index: number) => {
|
2024-11-14 08:08:45 +00:00
|
|
|
setScript((prev) => ({
|
|
|
|
...prev,
|
|
|
|
install_methods: prev.install_methods.filter((_, i) => i !== index),
|
|
|
|
}));
|
2024-11-15 17:16:19 +00:00
|
|
|
}, [setScript]);
|
|
|
|
|
|
|
|
const ResourceInput = memo(({
|
|
|
|
placeholder,
|
|
|
|
value,
|
|
|
|
onChange,
|
|
|
|
type = "text"
|
|
|
|
}: {
|
|
|
|
placeholder: string;
|
|
|
|
value: string | number | null;
|
|
|
|
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
|
|
type?: string;
|
|
|
|
}) => (
|
|
|
|
<Input
|
|
|
|
placeholder={placeholder}
|
|
|
|
type={type}
|
|
|
|
value={value || ""}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
));
|
|
|
|
|
|
|
|
ResourceInput.displayName = 'ResourceInput';
|
2024-11-14 08:08:45 +00: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">
|
|
|
|
<Select
|
|
|
|
value={method.type}
|
|
|
|
onValueChange={(value) => updateInstallMethod(index, "type", value)}
|
|
|
|
>
|
|
|
|
<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-15 17:16:19 +00:00
|
|
|
<ResourceInput
|
2024-11-14 08:08:45 +00:00
|
|
|
placeholder="CPU in Cores"
|
|
|
|
type="number"
|
2024-11-15 17:16:19 +00:00
|
|
|
value={method.resources.cpu}
|
|
|
|
onChange={(e) =>
|
2024-11-14 08:08:45 +00:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
...method.resources,
|
|
|
|
cpu: e.target.value ? Number(e.target.value) : null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2024-11-15 17:16:19 +00:00
|
|
|
<ResourceInput
|
2024-11-14 08:08:45 +00:00
|
|
|
placeholder="RAM in MB"
|
|
|
|
type="number"
|
2024-11-15 17:16:19 +00:00
|
|
|
value={method.resources.ram}
|
|
|
|
onChange={(e) =>
|
2024-11-14 08:08:45 +00:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
...method.resources,
|
|
|
|
ram: e.target.value ? Number(e.target.value) : null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2024-11-15 17:16:19 +00:00
|
|
|
<ResourceInput
|
|
|
|
placeholder="HDD in GB"
|
2024-11-14 08:08:45 +00:00
|
|
|
type="number"
|
2024-11-15 17:16:19 +00:00
|
|
|
value={method.resources.hdd}
|
|
|
|
onChange={(e) =>
|
2024-11-14 08:08:45 +00:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
...method.resources,
|
|
|
|
hdd: e.target.value ? Number(e.target.value) : null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex gap-2">
|
2024-11-15 17:16:19 +00:00
|
|
|
<ResourceInput
|
2024-11-14 08:08:45 +00:00
|
|
|
placeholder="OS"
|
2024-11-15 17:16:19 +00:00
|
|
|
value={method.resources.os}
|
|
|
|
onChange={(e) =>
|
2024-11-14 08:08:45 +00:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
...method.resources,
|
|
|
|
os: e.target.value || null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
2024-11-15 17:16:19 +00:00
|
|
|
<ResourceInput
|
2024-11-14 08:08:45 +00:00
|
|
|
placeholder="Version"
|
|
|
|
type="number"
|
2024-11-15 17:16:19 +00:00
|
|
|
value={method.resources.version}
|
|
|
|
onChange={(e) =>
|
2024-11-14 08:08:45 +00:00
|
|
|
updateInstallMethod(index, "resources", {
|
|
|
|
...method.resources,
|
|
|
|
version: e.target.value ? Number(e.target.value) : null,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
variant="destructive"
|
2024-11-15 17:16:19 +00:00
|
|
|
size="sm"
|
2024-11-14 08:08:45 +00:00
|
|
|
type="button"
|
|
|
|
onClick={() => removeInstallMethod(index)}
|
|
|
|
>
|
|
|
|
<Trash2 className="mr-2 h-4 w-4" /> Remove Install Method
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
<Button
|
|
|
|
type="button"
|
2024-11-15 17:16:19 +00:00
|
|
|
size="sm"
|
2024-11-14 08:08:45 +00:00
|
|
|
disabled={script.install_methods.length >= 2}
|
|
|
|
onClick={addInstallMethod}
|
|
|
|
>
|
|
|
|
<PlusCircle className="mr-2 h-4 w-4" /> Add Install Method
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2024-11-15 17:16:19 +00:00
|
|
|
|
|
|
|
export default memo(InstallMethod);
|