mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
* fix: dashboard layout scroll * feat: dashboard nav style animation * chore: code input font * style: format code * pref: alert component extraction * fix: global font var not found * fix: code path beak line * chore: remove framer-motion * fix: status color
153 lines
3.6 KiB
TypeScript
153 lines
3.6 KiB
TypeScript
import { Button } from "@/components/ui/button";
|
|
|
|
import {
|
|
Form,
|
|
FormControl,
|
|
FormDescription,
|
|
FormField,
|
|
FormItem,
|
|
FormLabel,
|
|
FormMessage,
|
|
} from "@/components/ui/form";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { api } from "@/utils/api";
|
|
import { AlertBlock } from "@/components/shared/alert-block";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { AlertTriangle } from "lucide-react";
|
|
import { useEffect, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import { toast } from "sonner";
|
|
import { z } from "zod";
|
|
import { validateAndFormatYAML } from "../application/advanced/traefik/update-traefik-config";
|
|
|
|
const UpdateServerMiddlewareConfigSchema = z.object({
|
|
traefikConfig: z.string(),
|
|
});
|
|
|
|
type UpdateServerMiddlewareConfig = z.infer<
|
|
typeof UpdateServerMiddlewareConfigSchema
|
|
>;
|
|
|
|
interface Props {
|
|
path: string;
|
|
}
|
|
|
|
export const ShowTraefikFile = ({ path }: Props) => {
|
|
const { data, refetch } = api.settings.readTraefikFile.useQuery(
|
|
{
|
|
path,
|
|
},
|
|
{
|
|
enabled: !!path,
|
|
},
|
|
);
|
|
const [canEdit, setCanEdit] = useState(true);
|
|
|
|
const { mutateAsync, isLoading, error, isError } =
|
|
api.settings.updateTraefikFile.useMutation();
|
|
|
|
const form = useForm<UpdateServerMiddlewareConfig>({
|
|
defaultValues: {
|
|
traefikConfig: "",
|
|
},
|
|
disabled: canEdit,
|
|
resolver: zodResolver(UpdateServerMiddlewareConfigSchema),
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (data) {
|
|
form.reset({
|
|
traefikConfig: data || "",
|
|
});
|
|
}
|
|
}, [form, form.reset, data]);
|
|
|
|
const onSubmit = async (data: UpdateServerMiddlewareConfig) => {
|
|
const { valid, error } = validateAndFormatYAML(data.traefikConfig);
|
|
if (!valid) {
|
|
form.setError("traefikConfig", {
|
|
type: "manual",
|
|
message: error || "Invalid YAML",
|
|
});
|
|
return;
|
|
}
|
|
form.clearErrors("traefikConfig");
|
|
await mutateAsync({
|
|
traefikConfig: data.traefikConfig,
|
|
path,
|
|
})
|
|
.then(async () => {
|
|
toast.success("Traefik config Updated");
|
|
refetch();
|
|
})
|
|
.catch(() => {
|
|
toast.error("Error to update the traefik config");
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
|
|
|
|
<Form {...form}>
|
|
<form
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
className="grid w-full relative"
|
|
>
|
|
<div className="flex flex-col">
|
|
<FormField
|
|
control={form.control}
|
|
name="traefikConfig"
|
|
render={({ field }) => (
|
|
<FormItem className="relative">
|
|
<FormLabel>Traefik config</FormLabel>
|
|
<FormDescription className="break-all">
|
|
{path}
|
|
</FormDescription>
|
|
<FormControl>
|
|
<Textarea
|
|
className="h-[35rem] font-mono"
|
|
placeholder={`http:
|
|
routers:
|
|
router-name:
|
|
rule: Host('domain.com')
|
|
service: container-name
|
|
entryPoints:
|
|
- web
|
|
tls: false
|
|
middlewares: []
|
|
`}
|
|
{...field}
|
|
/>
|
|
</FormControl>
|
|
|
|
<pre>
|
|
<FormMessage />
|
|
</pre>
|
|
<div className="flex justify-end absolute z-50 right-6 top-8">
|
|
<Button
|
|
className="shadow-sm"
|
|
variant="secondary"
|
|
type="button"
|
|
onClick={async () => {
|
|
setCanEdit(!canEdit);
|
|
}}
|
|
>
|
|
{canEdit ? "Unlock" : "Lock"}
|
|
</Button>
|
|
</div>
|
|
</FormItem>
|
|
)}
|
|
/>
|
|
</div>
|
|
<div className="flex justify-end">
|
|
<Button isLoading={isLoading} disabled={canEdit} type="submit">
|
|
Update
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
);
|
|
};
|