feat(traefik/ports): improved UI

This commit is contained in:
Nicholas Penree
2024-12-23 21:05:43 -05:00
parent 96cdffb5b9
commit 0db98c0b92
3 changed files with 192 additions and 105 deletions

View File

@@ -80,8 +80,10 @@ export const EditTraefikEnv = ({ children, serverId }: Props) => {
<DialogTrigger asChild>{children}</DialogTrigger> <DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-4xl"> <DialogContent className="max-h-screen overflow-y-auto sm:max-w-4xl">
<DialogHeader> <DialogHeader>
<DialogTitle>Update Traefik Env</DialogTitle> <DialogTitle>Update Traefik Environment</DialogTitle>
<DialogDescription>Update the traefik env</DialogDescription> <DialogDescription>
Update the traefik environment variables
</DialogDescription>
</DialogHeader> </DialogHeader>
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>} {isError && <AlertBlock type="error">{error?.message}</AlertBlock>}

View File

@@ -1,11 +1,15 @@
import { AlertBlock } from "@/components/shared/alert-block";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Card, CardContent } from "@/components/ui/card";
import { import {
Dialog, Dialog,
DialogContent, DialogContent,
DialogDescription, DialogDescription,
DialogFooter,
DialogHeader, DialogHeader,
DialogTitle, DialogTitle,
} from "@/components/ui/dialog"; } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { import {
Select, Select,
@@ -15,6 +19,7 @@ import {
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import { api } from "@/utils/api"; import { api } from "@/utils/api";
import { ArrowRightLeft, Plus, Trash2 } from "lucide-react";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import type React from "react"; import type React from "react";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
@@ -67,6 +72,7 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
const { t } = useTranslation("settings"); const { t } = useTranslation("settings");
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [additionalPorts, setAdditionalPorts] = useState<AdditionalPort[]>([]); const [additionalPorts, setAdditionalPorts] = useState<AdditionalPort[]>([]);
const [isDirty, setIsDirty] = useState(false);
const { data: currentPorts, refetch: refetchPorts } = const { data: currentPorts, refetch: refetchPorts } =
api.settings.getTraefikPorts.useQuery({ api.settings.getTraefikPorts.useQuery({
@@ -91,6 +97,7 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
...additionalPorts, ...additionalPorts,
{ targetPort: 0, publishedPort: 0, publishMode: "host" }, { targetPort: 0, publishedPort: 0, publishMode: "host" },
]); ]);
setIsDirty(true);
}; };
const handleUpdatePorts = async () => { const handleUpdatePorts = async () => {
@@ -101,59 +108,93 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
}); });
toast.success(t("settings.server.webServer.traefik.portsUpdated")); toast.success(t("settings.server.webServer.traefik.portsUpdated"));
setOpen(false); setOpen(false);
setIsDirty(false);
} catch (error) { } catch (error) {
toast.error(t("settings.server.webServer.traefik.portsUpdateError")); toast.error(t("settings.server.webServer.traefik.portsUpdateError"));
} }
}; };
const handleRemovePort = (index: number) => {
const newPorts = additionalPorts.filter((_, i) => i !== index);
setAdditionalPorts(newPorts);
setIsDirty(true);
};
return ( return (
<> <>
<div onClick={() => setOpen(true)}>{children}</div> <div onClick={() => setOpen(true)}>{children}</div>
<Dialog open={open} onOpenChange={setOpen}> <Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-2xl"> <DialogContent className="sm:max-w-3xl">
<DialogHeader> <DialogHeader>
<DialogTitle> <DialogTitle className="flex items-center gap-2 text-xl">
{t("settings.server.webServer.traefik.managePorts")} {t("settings.server.webServer.traefik.managePorts")}
</DialogTitle> </DialogTitle>
<DialogDescription> <DialogDescription className="text-base w-full">
<div className="flex items-center justify-between">
{t("settings.server.webServer.traefik.managePortsDescription")} {t("settings.server.webServer.traefik.managePortsDescription")}
<Button
onClick={handleAddPort}
variant="default"
className="gap-2"
>
<Plus className="h-4 w-4" />
Add Mapping
</Button>
</div>
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid gap-6 py-4">
{additionalPorts.length === 0 ? (
<div className="flex w-full flex-col items-center justify-center gap-3 pt-10">
<ArrowRightLeft className="size-8 text-muted-foreground" />
<span className="text-base text-muted-foreground text-center">
No port mappings configured
</span>
<p className="text-sm text-muted-foreground text-center">
Add one to get started
</p>
</div>
) : (
<div className="grid gap-4">
{additionalPorts.map((port, index) => ( {additionalPorts.map((port, index) => (
<div <Card key={index}>
key={index} <CardContent className="grid grid-cols-[1fr_1fr_1.5fr_auto] gap-4 p-4 transparent">
className="grid grid-cols-[120px_120px_minmax(120px,1fr)_80px] gap-4 items-end"
>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor={`target-port-${index}`}> <Label
htmlFor={`target-port-${index}`}
className="text-sm font-medium text-muted-foreground"
>
{t("settings.server.webServer.traefik.targetPort")} {t("settings.server.webServer.traefik.targetPort")}
</Label> </Label>
<input <Input
id={`target-port-${index}`} id={`target-port-${index}`}
type="number" type="number"
value={port.targetPort} value={port.targetPort}
onChange={(e) => { onChange={(e) => {
const newPorts = [...additionalPorts]; const newPorts = [...additionalPorts];
if (newPorts[index]) { if (newPorts[index]) {
newPorts[index].targetPort = Number.parseInt( newPorts[index].targetPort = Number.parseInt(
e.target.value, e.target.value,
); );
} }
setAdditionalPorts(newPorts); setAdditionalPorts(newPorts);
}} }}
className="w-full rounded border p-2" className="w-full dark:bg-black"
placeholder="e.g. 8080"
/> />
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor={`published-port-${index}`}> <Label
htmlFor={`published-port-${index}`}
className="text-sm font-medium text-muted-foreground"
>
{t("settings.server.webServer.traefik.publishedPort")} {t("settings.server.webServer.traefik.publishedPort")}
</Label> </Label>
<input <Input
id={`published-port-${index}`} id={`published-port-${index}`}
type="number" type="text"
value={port.publishedPort} value={port.publishedPort}
onChange={(e) => { onChange={(e) => {
const newPorts = [...additionalPorts]; const newPorts = [...additionalPorts];
@@ -164,18 +205,22 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
} }
setAdditionalPorts(newPorts); setAdditionalPorts(newPorts);
}} }}
className="w-full rounded border p-2" className="w-full dark:bg-black"
placeholder="e.g. 80"
/> />
</div> </div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor={`publish-mode-${index}`}> <Label
htmlFor={`publish-mode-${index}`}
className="text-sm font-medium text-muted-foreground"
>
{t("settings.server.webServer.traefik.publishMode")} {t("settings.server.webServer.traefik.publishMode")}
</Label> </Label>
<Select <Select
value={port.publishMode} value={port.publishMode}
onValueChange={(value: "ingress" | "host") => { onValueChange={(value: "ingress" | "host") => {
const newPorts = [...additionalPorts]; const newPorts = [...additionalPorts];
if (newPorts[index]) { if (newPorts[index]) {
newPorts[index].publishMode = value; newPorts[index].publishMode = value;
} }
@@ -184,47 +229,87 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {
> >
<SelectTrigger <SelectTrigger
id={`publish-mode-${index}`} id={`publish-mode-${index}`}
className="w-full" className="dark:bg-black"
> >
<SelectValue /> <SelectValue />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent>
<SelectItem value="host">Host</SelectItem> <SelectItem value="host">Host Mode</SelectItem>
<SelectItem value="ingress">Ingress</SelectItem> <SelectItem value="ingress">
Ingress Mode
</SelectItem>
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<div>
<div className="flex items-end">
<Button <Button
onClick={() => { onClick={() => handleRemovePort(index)}
const newPorts = additionalPorts.filter( variant="ghost"
(_, i) => i !== index, size="icon"
); className="text-muted-foreground hover:text-destructive"
setAdditionalPorts(newPorts);
}}
variant="destructive"
size="sm"
> >
Remove <Trash2 className="h-4 w-4" />
</Button> </Button>
</div> </div>
</div> </CardContent>
</Card>
))} ))}
<div className="mt-4 flex justify-between"> </div>
<Button onClick={handleAddPort} variant="outline" size="sm"> )}
{t("settings.server.webServer.traefik.addPort")}
</Button> {additionalPorts.length > 0 && (
<AlertBlock type="info">
<div className="flex flex-col gap-2">
<span className="text-sm">
<strong>
Each port mapping defines how external traffic reaches
your containers.
</strong>
<ul className="pt-2">
<li>
<strong>Host Mode:</strong> Directly binds the port to
the host machine.
<ul className="p-2 list-inside list-disc">
<li>
Best for single-node deployments or when you need
guaranteed port availability.
</li>
</ul>
</li>
<li>
<strong>Ingress Mode:</strong> Routes through Docker
Swarm's load balancer.
<ul className="p-2 list-inside list-disc">
<li>
Recommended for multi-node deployments and better
scalability.
</li>
</ul>
</li>
</ul>
</span>
</div>
</AlertBlock>
)}
</div>
<DialogFooter className="">
{(additionalPorts.length > 0 || isDirty) && (
<Button <Button
variant="default"
className="text-sm"
onClick={handleUpdatePorts} onClick={handleUpdatePorts}
size="sm" disabled={isLoading || !isDirty}
disabled={isLoading}
> >
Save Save
</Button> </Button>
</div> )}
</div> </DialogFooter>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</> </>
); );
}; };
export default ManageTraefikPorts;

View File

@@ -17,8 +17,8 @@
"settings.server.webServer.updateServerIp": "Update Server IP", "settings.server.webServer.updateServerIp": "Update Server IP",
"settings.server.webServer.server.label": "Server", "settings.server.webServer.server.label": "Server",
"settings.server.webServer.traefik.label": "Traefik", "settings.server.webServer.traefik.label": "Traefik",
"settings.server.webServer.traefik.modifyEnv": "Modify Env", "settings.server.webServer.traefik.modifyEnv": "Modify Environment",
"settings.server.webServer.traefik.managePorts": "Additional Ports", "settings.server.webServer.traefik.managePorts": "Additional Port Mappings",
"settings.server.webServer.traefik.managePortsDescription": "Add or remove additional ports for Traefik", "settings.server.webServer.traefik.managePortsDescription": "Add or remove additional ports for Traefik",
"settings.server.webServer.traefik.targetPort": "Target Port", "settings.server.webServer.traefik.targetPort": "Target Port",
"settings.server.webServer.traefik.publishedPort": "Published Port", "settings.server.webServer.traefik.publishedPort": "Published Port",