2026-02-23 11:10:28 -06:00
|
|
|
import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema";
|
2025-08-16 20:18:08 -06:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { toast } from "sonner";
|
2025-09-21 02:20:20 -06:00
|
|
|
import { parse, stringify, YAMLParseError } from "yaml";
|
2025-08-16 20:18:08 -06:00
|
|
|
import { z } from "zod";
|
2024-07-15 01:08:18 +02:00
|
|
|
import { AlertBlock } from "@/components/shared/alert-block";
|
|
|
|
|
import { CodeEditor } from "@/components/shared/code-editor";
|
2024-04-28 23:57:52 -06:00
|
|
|
import { Button } from "@/components/ui/button";
|
2026-02-23 11:10:28 -06:00
|
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
2024-04-28 23:57:52 -06:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogFooter,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
} from "@/components/ui/dialog";
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from "@/components/ui/form";
|
2026-02-07 02:15:17 -06:00
|
|
|
import { Label } from "@/components/ui/label";
|
2024-04-28 23:57:52 -06:00
|
|
|
import { api } from "@/utils/api";
|
|
|
|
|
|
|
|
|
|
const UpdateTraefikConfigSchema = z.object({
|
|
|
|
|
traefikConfig: z.string(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
type UpdateTraefikConfig = z.infer<typeof UpdateTraefikConfigSchema>;
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
applicationId: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const validateAndFormatYAML = (yamlText: string) => {
|
|
|
|
|
try {
|
2025-09-21 02:20:20 -06:00
|
|
|
const obj = parse(yamlText);
|
|
|
|
|
const formattedYaml = stringify(obj, { indent: 4 });
|
2024-04-28 23:57:52 -06:00
|
|
|
return { valid: true, formattedYaml, error: null };
|
|
|
|
|
} catch (error) {
|
2025-09-21 02:20:20 -06:00
|
|
|
if (error instanceof YAMLParseError) {
|
2024-04-28 23:57:52 -06:00
|
|
|
return {
|
|
|
|
|
valid: false,
|
|
|
|
|
formattedYaml: yamlText,
|
|
|
|
|
error: error.message,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
valid: false,
|
|
|
|
|
formattedYaml: yamlText,
|
|
|
|
|
error: "An unexpected error occurred while processing the YAML.",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const UpdateTraefikConfig = ({ applicationId }: Props) => {
|
2026-03-15 16:42:48 -06:00
|
|
|
const { data: permissions } = api.user.getPermissions.useQuery();
|
|
|
|
|
const canWrite = permissions?.traefikFiles.write ?? false;
|
2024-06-13 20:42:46 +08:00
|
|
|
const [open, setOpen] = useState(false);
|
2026-02-07 02:15:17 -06:00
|
|
|
const [skipYamlValidation, setSkipYamlValidation] = useState(false);
|
2024-04-28 23:57:52 -06:00
|
|
|
const { data, refetch } = api.application.readTraefikConfig.useQuery(
|
|
|
|
|
{
|
|
|
|
|
applicationId,
|
|
|
|
|
},
|
|
|
|
|
{ enabled: !!applicationId },
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-23 23:50:55 -06:00
|
|
|
const { mutateAsync, isPending, error, isError } =
|
2024-04-28 23:57:52 -06:00
|
|
|
api.application.updateTraefikConfig.useMutation();
|
|
|
|
|
|
|
|
|
|
const form = useForm<UpdateTraefikConfig>({
|
|
|
|
|
defaultValues: {
|
|
|
|
|
traefikConfig: "",
|
|
|
|
|
},
|
|
|
|
|
resolver: zodResolver(UpdateTraefikConfigSchema),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (data) {
|
|
|
|
|
form.reset({
|
|
|
|
|
traefikConfig: data || "",
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-06-13 20:42:46 +08:00
|
|
|
}, [data]);
|
2024-04-28 23:57:52 -06:00
|
|
|
|
|
|
|
|
const onSubmit = async (data: UpdateTraefikConfig) => {
|
2026-02-07 02:15:17 -06:00
|
|
|
if (!skipYamlValidation) {
|
|
|
|
|
const { valid, error } = validateAndFormatYAML(data.traefikConfig);
|
|
|
|
|
if (!valid) {
|
|
|
|
|
form.setError("traefikConfig", {
|
|
|
|
|
type: "manual",
|
|
|
|
|
message: (error as string) || "Invalid YAML",
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-28 23:57:52 -06:00
|
|
|
}
|
|
|
|
|
form.clearErrors("traefikConfig");
|
|
|
|
|
await mutateAsync({
|
|
|
|
|
applicationId,
|
|
|
|
|
traefikConfig: data.traefikConfig,
|
|
|
|
|
})
|
|
|
|
|
.then(async () => {
|
|
|
|
|
toast.success("Traefik config Updated");
|
|
|
|
|
refetch();
|
2024-06-13 20:42:46 +08:00
|
|
|
setOpen(false);
|
|
|
|
|
form.reset();
|
2024-04-28 23:57:52 -06:00
|
|
|
})
|
|
|
|
|
.catch(() => {
|
2024-12-29 22:34:57 +10:00
|
|
|
toast.error("Error updating the Traefik config");
|
2024-04-28 23:57:52 -06:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2024-07-15 01:08:18 +02:00
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
|
|
|
|
onOpenChange={(open) => {
|
|
|
|
|
setOpen(open);
|
|
|
|
|
if (!open) {
|
|
|
|
|
form.reset();
|
2026-02-07 02:15:17 -06:00
|
|
|
setSkipYamlValidation(false);
|
2024-07-15 01:08:18 +02:00
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-03-15 16:42:48 -06:00
|
|
|
{canWrite && (
|
|
|
|
|
<DialogTrigger asChild>
|
|
|
|
|
<Button isLoading={isPending}>Modify</Button>
|
|
|
|
|
</DialogTrigger>
|
|
|
|
|
)}
|
2025-07-13 13:58:25 -03:00
|
|
|
<DialogContent className="sm:max-w-4xl">
|
2024-04-28 23:57:52 -06:00
|
|
|
<DialogHeader>
|
|
|
|
|
<DialogTitle>Update traefik config</DialogTitle>
|
|
|
|
|
<DialogDescription>Update the traefik config</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
2024-05-18 11:43:52 +08:00
|
|
|
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
|
2024-04-28 23:57:52 -06:00
|
|
|
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form
|
|
|
|
|
id="hook-form-update-traefik-config"
|
|
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
2024-06-13 20:42:46 +08:00
|
|
|
className="w-full space-y-4 overflow-auto"
|
2024-04-28 23:57:52 -06:00
|
|
|
>
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="traefikConfig"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
|
|
|
|
<FormLabel>Traefik config</FormLabel>
|
|
|
|
|
<FormControl>
|
2024-05-27 05:32:24 +08:00
|
|
|
<CodeEditor
|
2024-08-13 20:51:50 +08:00
|
|
|
lineWrapping
|
2024-05-27 05:32:24 +08:00
|
|
|
wrapperClassName="h-[35rem] font-mono"
|
2024-04-28 23:57:52 -06:00
|
|
|
placeholder={`http:
|
|
|
|
|
routers:
|
|
|
|
|
router-name:
|
|
|
|
|
rule: Host('domain.com')
|
|
|
|
|
service: container-name
|
|
|
|
|
entryPoints:
|
|
|
|
|
- web
|
|
|
|
|
tls: false
|
|
|
|
|
middlewares: []
|
|
|
|
|
`}
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</pre>
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
|
2026-02-07 02:15:17 -06:00
|
|
|
<DialogFooter className="flex-col sm:flex-row gap-4">
|
|
|
|
|
<div className="flex flex-col gap-1 w-full sm:w-auto sm:mr-auto">
|
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
|
<Checkbox
|
|
|
|
|
id="skip-yaml-validation-app"
|
|
|
|
|
checked={skipYamlValidation}
|
|
|
|
|
onCheckedChange={(checked) =>
|
|
|
|
|
setSkipYamlValidation(checked === true)
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor="skip-yaml-validation-app"
|
|
|
|
|
className="text-sm font-normal cursor-pointer"
|
|
|
|
|
>
|
|
|
|
|
Skip YAML validation (for Go templating)
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Check to save configs with Go templating (e.g.{" "}
|
|
|
|
|
<code className="text-xs">{"{{range}}"}</code>).
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2024-04-28 23:57:52 -06:00
|
|
|
<Button
|
2026-02-23 23:50:55 -06:00
|
|
|
isLoading={isPending}
|
2024-04-28 23:57:52 -06:00
|
|
|
form="hook-form-update-traefik-config"
|
|
|
|
|
type="submit"
|
|
|
|
|
>
|
|
|
|
|
Update
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</Form>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
};
|