mirror of
https://github.com/Dokploy/dokploy.git
synced 2026-06-15 20:25:23 +02:00
514 lines
15 KiB
TypeScript
514 lines
15 KiB
TypeScript
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { HelpCircle, Plus, Settings2, X } from "lucide-react";
|
|
import { useEffect, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { toast } from "sonner";
|
|
import { z } from "zod";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogTrigger,
|
|
} from "@/components/ui/dialog";
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/ui/form";
|
|
import { Input, NumberInput } from "@/components/ui/input";
|
|
import { Secrets } from "@/components/ui/secrets";
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import {
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipProvider,
|
|
TooltipTrigger,
|
|
} from "@/components/ui/tooltip";
|
|
import { api } from "@/utils/api";
|
|
|
|
const schema = z
|
|
.object({
|
|
env: z.string(),
|
|
buildArgs: z.string(),
|
|
wildcardDomain: z.string(),
|
|
port: z.number(),
|
|
previewLimit: z.number(),
|
|
previewLabels: z.array(z.string()).optional(),
|
|
previewHttps: z.boolean(),
|
|
previewPath: z.string(),
|
|
previewCertificateType: z.enum(["letsencrypt", "none", "custom"]),
|
|
previewCustomCertResolver: z.string().optional(),
|
|
previewRequireCollaboratorPermissions: z.boolean(),
|
|
})
|
|
.superRefine((input, ctx) => {
|
|
if (
|
|
input.previewCertificateType === "custom" &&
|
|
!input.previewCustomCertResolver
|
|
) {
|
|
ctx.addIssue({
|
|
code: z.ZodIssueCode.custom,
|
|
path: ["previewCustomCertResolver"],
|
|
message: "Required",
|
|
});
|
|
}
|
|
});
|
|
|
|
type Schema = z.infer<typeof schema>;
|
|
|
|
interface Props {
|
|
applicationId: string;
|
|
}
|
|
|
|
export const ShowPreviewSettings = ({ applicationId }: Props) => {
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const [isEnabled, setIsEnabled] = useState(false);
|
|
const { mutateAsync: updateApplication, isLoading } =
|
|
api.application.update.useMutation();
|
|
|
|
const { data, refetch } = api.application.one.useQuery({ applicationId });
|
|
|
|
const form = useForm<Schema>({
|
|
defaultValues: {
|
|
env: "",
|
|
wildcardDomain: "*.traefik.me",
|
|
port: 3000,
|
|
previewLimit: 3,
|
|
previewLabels: [],
|
|
previewHttps: false,
|
|
previewPath: "/",
|
|
previewCertificateType: "none",
|
|
previewRequireCollaboratorPermissions: true,
|
|
},
|
|
resolver: zodResolver(schema),
|
|
});
|
|
|
|
const previewHttps = form.watch("previewHttps");
|
|
|
|
useEffect(() => {
|
|
setIsEnabled(data?.isPreviewDeploymentsActive || false);
|
|
}, [data?.isPreviewDeploymentsActive]);
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.reset({
|
|
env: data.previewEnv || "",
|
|
buildArgs: data.previewBuildArgs || "",
|
|
wildcardDomain: data.previewWildcard || "*.traefik.me",
|
|
port: data.previewPort || 3000,
|
|
previewLabels: data.previewLabels || [],
|
|
previewLimit: data.previewLimit || 3,
|
|
previewHttps: data.previewHttps || false,
|
|
previewPath: data.previewPath || "/",
|
|
previewCertificateType: data.previewCertificateType || "none",
|
|
previewCustomCertResolver: data.previewCustomCertResolver || "",
|
|
previewRequireCollaboratorPermissions:
|
|
data.previewRequireCollaboratorPermissions || true,
|
|
});
|
|
}
|
|
}, [data]);
|
|
|
|
const onSubmit = async (formData: Schema) => {
|
|
updateApplication({
|
|
previewEnv: formData.env,
|
|
previewBuildArgs: formData.buildArgs,
|
|
previewWildcard: formData.wildcardDomain,
|
|
previewPort: formData.port,
|
|
previewLabels: formData.previewLabels,
|
|
applicationId,
|
|
previewLimit: formData.previewLimit,
|
|
previewHttps: formData.previewHttps,
|
|
previewPath: formData.previewPath,
|
|
previewCertificateType: formData.previewCertificateType,
|
|
previewCustomCertResolver: formData.previewCustomCertResolver,
|
|
previewRequireCollaboratorPermissions:
|
|
formData.previewRequireCollaboratorPermissions,
|
|
})
|
|
.then(() => {
|
|
toast.success("Preview Deployments settings updated");
|
|
})
|
|
.catch((error) => {
|
|
toast.error(error.message);
|
|
});
|
|
};
|
|
return (
|
|
<div>
|
|
<Dialog open={isOpen} onOpenChange={setIsOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button variant="outline">
|
|
<Settings2 className="size-4" />
|
|
Configure
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-5xl w-full">
|
|
<DialogHeader>
|
|
<DialogTitle>Preview Deployment Settings</DialogTitle>
|
|
<DialogDescription>
|
|
Adjust the settings for preview deployments of this application,
|
|
including environment variables, build options, and deployment
|
|
rules.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="grid gap-4">
|
|
<Form {...form}>
|
|
<form
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
id="hook-form-delete-application"
|
|
className="grid w-full gap-4"
|
|
>
|
|
<div className="grid gap-4 lg:grid-cols-2">
|
|
<FormField
|
|
control={form.control}
|
|
name="wildcardDomain"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Wildcard Domain</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="*.traefik.me" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="previewPath"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Preview Path</FormLabel>
|
|
<FormControl>
|
|
<Input placeholder="/" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="port"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Port</FormLabel>
|
|
<FormControl>
|
|
<NumberInput placeholder="3000" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="previewLabels"
|
|
render={({ field }) => (
|
|
<FormItem className="md:col-span-2">
|
|
<div className="flex items-center gap-2">
|
|
<FormLabel>Preview Labels</FormLabel>
|
|
<TooltipProvider>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<HelpCircle className="size-4 text-muted-foreground hover:text-foreground transition-colors cursor-pointer" />
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
<p>
|
|
Add a labels that will trigger a preview
|
|
deployment for a pull request. If no labels
|
|
are specified, all pull requests will trigger
|
|
a preview deployment.
|
|
</p>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</TooltipProvider>
|
|
</div>
|
|
<div className="flex flex-wrap gap-2 mb-2">
|
|
{field.value?.map((label, index) => (
|
|
<Badge
|
|
key={index}
|
|
variant="secondary"
|
|
className="flex items-center gap-1"
|
|
>
|
|
{label}
|
|
<X
|
|
className="size-3 cursor-pointer hover:text-destructive"
|
|
onClick={() => {
|
|
const newLabels = [...(field.value || [])];
|
|
newLabels.splice(index, 1);
|
|
field.onChange(newLabels);
|
|
}}
|
|
/>
|
|
</Badge>
|
|
))}
|
|
</div>
|
|
<div className="flex gap-2">
|
|
<FormControl>
|
|
<Input
|
|
placeholder="Enter a label (e.g. enhancements, needs-review)"
|
|
onKeyDown={(e) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
const input = e.currentTarget;
|
|
const label = input.value.trim();
|
|
if (label) {
|
|
field.onChange([
|
|
...(field.value || []),
|
|
label,
|
|
]);
|
|
input.value = "";
|
|
}
|
|
}
|
|
}}
|
|
/>
|
|
</FormControl>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="icon"
|
|
onClick={() => {
|
|
const input = document.querySelector(
|
|
'input[placeholder*="Enter a label"]',
|
|
) as HTMLInputElement;
|
|
const label = input.value.trim();
|
|
if (label) {
|
|
field.onChange([...(field.value || []), label]);
|
|
input.value = "";
|
|
}
|
|
}}
|
|
>
|
|
<Plus className="size-4" />
|
|
</Button>
|
|
</div>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="previewLimit"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Preview Limit</FormLabel>
|
|
<FormControl>
|
|
<NumberInput placeholder="3000" {...field} />
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
<FormField
|
|
control={form.control}
|
|
name="previewHttps"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-row items-center justify-between p-3 mt-4 border rounded-lg shadow-sm">
|
|
<div className="space-y-0.5">
|
|
<FormLabel>HTTPS</FormLabel>
|
|
<FormDescription>
|
|
Automatically provision SSL Certificate.
|
|
</FormDescription>
|
|
<FormMessage />
|
|
</div>
|
|
<FormControl>
|
|
<Switch
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
</FormControl>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{previewHttps && (
|
|
<FormField
|
|
control={form.control}
|
|
name="previewCertificateType"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Certificate Provider</FormLabel>
|
|
<Select
|
|
onValueChange={field.onChange}
|
|
defaultValue={field.value || ""}
|
|
>
|
|
<FormControl>
|
|
<SelectTrigger>
|
|
<SelectValue placeholder="Select a certificate provider" />
|
|
</SelectTrigger>
|
|
</FormControl>
|
|
|
|
<SelectContent>
|
|
<SelectItem value="none">None</SelectItem>
|
|
<SelectItem value={"letsencrypt"}>
|
|
Let's Encrypt
|
|
</SelectItem>
|
|
<SelectItem value={"custom"}>Custom</SelectItem>
|
|
</SelectContent>
|
|
</Select>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
)}
|
|
|
|
{form.watch("previewCertificateType") === "custom" && (
|
|
<FormField
|
|
control={form.control}
|
|
name="previewCustomCertResolver"
|
|
render={({ field }) => (
|
|
<FormItem>
|
|
<FormLabel>Certificate Provider</FormLabel>
|
|
<FormControl>
|
|
<Input
|
|
placeholder="my-custom-resolver"
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
)}
|
|
</div>
|
|
<div className="grid gap-4 lg:grid-cols-2">
|
|
<div className="flex flex-row items-center justify-between rounded-lg border p-4 col-span-2">
|
|
<div className="space-y-0.5">
|
|
<FormLabel className="text-base">
|
|
Enable preview deployments
|
|
</FormLabel>
|
|
<FormDescription>
|
|
Enable or disable preview deployments for this
|
|
application.
|
|
</FormDescription>
|
|
</div>
|
|
<Switch
|
|
checked={isEnabled}
|
|
onCheckedChange={(checked) => {
|
|
updateApplication({
|
|
isPreviewDeploymentsActive: checked,
|
|
applicationId,
|
|
})
|
|
.then(() => {
|
|
refetch();
|
|
toast.success(
|
|
checked
|
|
? "Preview deployments enabled"
|
|
: "Preview deployments disabled",
|
|
);
|
|
})
|
|
.catch((error) => {
|
|
toast.error(error.message);
|
|
});
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="grid gap-4 lg:grid-cols-2">
|
|
<FormField
|
|
control={form.control}
|
|
name="previewRequireCollaboratorPermissions"
|
|
render={({ field }) => (
|
|
<FormItem className="flex flex-row items-center justify-between p-3 mt-4 border rounded-lg shadow-sm col-span-2">
|
|
<div className="space-y-0.5">
|
|
<FormLabel>
|
|
Require Collaborator Permissions
|
|
</FormLabel>
|
|
<FormDescription>
|
|
Require collaborator permissions to preview
|
|
deployments, valid roles are:
|
|
<ul>
|
|
<li>Admin</li>
|
|
<li>Maintain</li>
|
|
<li>Write</li>
|
|
</ul>
|
|
</FormDescription>
|
|
</div>
|
|
<FormControl>
|
|
<Switch
|
|
checked={field.value}
|
|
onCheckedChange={field.onChange}
|
|
/>
|
|
</FormControl>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</div>
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="env"
|
|
render={() => (
|
|
<FormItem>
|
|
<FormControl>
|
|
<Secrets
|
|
name="env"
|
|
title="Environment Settings"
|
|
description="You can add environment variables to your resource."
|
|
placeholder={[
|
|
"NODE_ENV=production",
|
|
"PORT=3000",
|
|
].join("\n")}
|
|
/>
|
|
</FormControl>
|
|
<FormMessage />
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
{data?.buildType === "dockerfile" && (
|
|
<Secrets
|
|
name="buildArgs"
|
|
title="Build-time Variables"
|
|
description={
|
|
<span>
|
|
Available only at build-time. See documentation
|
|
<a
|
|
className="text-primary"
|
|
href="https://docs.docker.com/build/guide/build-args/"
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
here
|
|
</a>
|
|
.
|
|
</span>
|
|
}
|
|
placeholder="NPM_TOKEN=xyz"
|
|
/>
|
|
)}
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button
|
|
variant="secondary"
|
|
onClick={() => {
|
|
setIsOpen(false);
|
|
}}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
isLoading={isLoading}
|
|
form="hook-form-delete-application"
|
|
type="submit"
|
|
>
|
|
Save
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
{/* */}
|
|
</div>
|
|
);
|
|
};
|