diff --git a/Dockerfile.cloud b/Dockerfile.cloud index 8e4bac215..ee42cd2bd 100644 --- a/Dockerfile.cloud +++ b/Dockerfile.cloud @@ -16,11 +16,11 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm --filter=@dokploy/server # Deploy only the dokploy app -ARG NEXT_PUBLIC_UMAMI_HOST -ENV NEXT_PUBLIC_UMAMI_HOST=$NEXT_PUBLIC_UMAMI_HOST +# ARG NEXT_PUBLIC_UMAMI_HOST +# ENV NEXT_PUBLIC_UMAMI_HOST=$NEXT_PUBLIC_UMAMI_HOST -ARG NEXT_PUBLIC_UMAMI_WEBSITE_ID -ENV NEXT_PUBLIC_UMAMI_WEBSITE_ID=$NEXT_PUBLIC_UMAMI_WEBSITE_ID +# ARG NEXT_PUBLIC_UMAMI_WEBSITE_ID +# ENV NEXT_PUBLIC_UMAMI_WEBSITE_ID=$NEXT_PUBLIC_UMAMI_WEBSITE_ID ARG NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY ENV NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=$NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY diff --git a/apps/dokploy/__test__/compose/domain/host-rule-format.test.ts b/apps/dokploy/__test__/compose/domain/host-rule-format.test.ts new file mode 100644 index 000000000..27e696b20 --- /dev/null +++ b/apps/dokploy/__test__/compose/domain/host-rule-format.test.ts @@ -0,0 +1,215 @@ +import type { Domain } from "@dokploy/server"; +import { createDomainLabels } from "@dokploy/server"; +import { parse, stringify } from "yaml"; +import { describe, expect, it } from "vitest"; + +/** + * Regression tests for Traefik Host rule label format. + * + * These tests verify that the Host rule is generated with the correct format: + * - Host(`domain.com`) - with opening and closing parentheses + * - Host(`domain.com`) && PathPrefix(`/path`) - for path-based routing + * + * Issue: https://github.com/Dokploy/dokploy/issues/3161 + * The bug caused Host rules to be malformed as Host`domain.com`) + * (missing opening parenthesis) which broke all domain routing. + */ +describe("Host rule format regression tests", () => { + const baseDomain: Domain = { + host: "example.com", + port: 8080, + https: false, + uniqueConfigKey: 1, + customCertResolver: null, + certificateType: "none", + applicationId: "", + composeId: "", + domainType: "compose", + serviceName: "test-app", + domainId: "", + path: "/", + createdAt: "", + previewDeploymentId: "", + internalPath: "/", + stripPath: false, + }; + + describe("Host rule format validation", () => { + it("should generate Host rule with correct parentheses format", async () => { + const labels = await createDomainLabels("test-app", baseDomain, "web"); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + expect(ruleLabel).toBeDefined(); + // Verify exact format: Host(`domain`) + expect(ruleLabel).toMatch(/Host\(`[^`]+`\)/); + // Ensure opening parenthesis is present after Host + expect(ruleLabel).toContain("Host(`example.com`)"); + // Ensure it does NOT have the malformed format + expect(ruleLabel).not.toMatch(/Host`[^`]+`\)/); + }); + + it("should generate PathPrefix with correct parentheses format", async () => { + const labels = await createDomainLabels( + "test-app", + { ...baseDomain, path: "/api" }, + "web", + ); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + expect(ruleLabel).toBeDefined(); + // Verify PathPrefix format + expect(ruleLabel).toMatch(/PathPrefix\(`[^`]+`\)/); + expect(ruleLabel).toContain("PathPrefix(`/api`)"); + // Ensure opening parenthesis is present + expect(ruleLabel).not.toMatch(/PathPrefix`[^`]+`\)/); + }); + + it("should generate combined Host and PathPrefix with correct format", async () => { + const labels = await createDomainLabels( + "test-app", + { ...baseDomain, path: "/api/v1" }, + "websecure", + ); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + expect(ruleLabel).toBeDefined(); + expect(ruleLabel).toBe( + "traefik.http.routers.test-app-1-websecure.rule=Host(`example.com`) && PathPrefix(`/api/v1`)", + ); + }); + }); + + describe("YAML serialization preserves Host rule format", () => { + it("should preserve Host rule format through YAML stringify/parse", async () => { + const labels = await createDomainLabels("test-app", baseDomain, "web"); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + // Simulate compose file structure + const composeSpec = { + services: { + myapp: { + image: "nginx", + labels: labels, + }, + }, + }; + + // Stringify to YAML + const yamlOutput = stringify(composeSpec, { lineWidth: 1000 }); + + // Parse back + const parsed = parse(yamlOutput) as typeof composeSpec; + const parsedRuleLabel = parsed.services.myapp.labels.find((l: string) => + l.includes(".rule="), + ); + + // Verify format is preserved + expect(parsedRuleLabel).toBe(ruleLabel); + expect(parsedRuleLabel).toContain("Host(`example.com`)"); + expect(parsedRuleLabel).not.toMatch(/Host`[^`]+`\)/); + }); + + it("should preserve complex rule format through YAML serialization", async () => { + const labels = await createDomainLabels( + "test-app", + { ...baseDomain, path: "/api", https: true }, + "websecure", + ); + + const composeSpec = { + services: { + myapp: { + labels: labels, + }, + }, + }; + + const yamlOutput = stringify(composeSpec, { lineWidth: 1000 }); + const parsed = parse(yamlOutput) as typeof composeSpec; + const parsedRuleLabel = parsed.services.myapp.labels.find((l: string) => + l.includes(".rule="), + ); + + expect(parsedRuleLabel).toContain( + "Host(`example.com`) && PathPrefix(`/api`)", + ); + }); + }); + + describe("Edge cases for domain names", () => { + const domainCases = [ + { name: "simple domain", host: "example.com" }, + { name: "subdomain", host: "app.example.com" }, + { name: "deep subdomain", host: "api.v1.app.example.com" }, + { name: "numeric domain", host: "123.example.com" }, + { name: "hyphenated domain", host: "my-app.example-host.com" }, + { name: "localhost", host: "localhost" }, + { name: "IP address style", host: "192.168.1.100" }, + ]; + + for (const { name, host } of domainCases) { + it(`should generate correct Host rule for ${name}: ${host}`, async () => { + const labels = await createDomainLabels( + "test-app", + { ...baseDomain, host }, + "web", + ); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + expect(ruleLabel).toBeDefined(); + expect(ruleLabel).toContain(`Host(\`${host}\`)`); + // Verify parenthesis is present + expect(ruleLabel).toMatch( + new RegExp(`Host\\(\\\`${host.replace(/\./g, "\\.")}\\\`\\)`), + ); + }); + } + }); + + describe("Multiple domains scenario", () => { + it("should generate correct format for both web and websecure entrypoints", async () => { + const webLabels = await createDomainLabels("test-app", baseDomain, "web"); + const websecureLabels = await createDomainLabels( + "test-app", + baseDomain, + "websecure", + ); + + const webRule = webLabels.find((l) => l.includes(".rule=")); + const websecureRule = websecureLabels.find((l) => l.includes(".rule=")); + + // Both should have correct format + expect(webRule).toContain("Host(`example.com`)"); + expect(websecureRule).toContain("Host(`example.com`)"); + + // Neither should have malformed format + expect(webRule).not.toMatch(/Host`[^`]+`\)/); + expect(websecureRule).not.toMatch(/Host`[^`]+`\)/); + }); + }); + + describe("Special characters in paths", () => { + const pathCases = [ + { name: "simple path", path: "/api" }, + { name: "nested path", path: "/api/v1/users" }, + { name: "path with hyphen", path: "/api-v1" }, + { name: "path with underscore", path: "/api_v1" }, + ]; + + for (const { name, path } of pathCases) { + it(`should generate correct PathPrefix for ${name}: ${path}`, async () => { + const labels = await createDomainLabels( + "test-app", + { ...baseDomain, path }, + "web", + ); + const ruleLabel = labels.find((l) => l.includes(".rule=")); + + expect(ruleLabel).toBeDefined(); + expect(ruleLabel).toContain(`PathPrefix(\`${path}\`)`); + // Verify parenthesis is present + expect(ruleLabel).not.toMatch(/PathPrefix`[^`]+`\)/); + }); + } + }); +}); diff --git a/apps/dokploy/__test__/deploy/application.command.test.ts b/apps/dokploy/__test__/deploy/application.command.test.ts index a0c4387c8..be29748eb 100644 --- a/apps/dokploy/__test__/deploy/application.command.test.ts +++ b/apps/dokploy/__test__/deploy/application.command.test.ts @@ -189,7 +189,7 @@ describe("deployApplication - Command Generation Tests", () => { it("should verify nixpacks command is called with correct app", async () => { const mockNixpacksCommand = "nixpacks build /path/to/app --name test-app"; - vi.mocked(builders.getBuildCommand).mockReturnValue(mockNixpacksCommand); + vi.mocked(builders.getBuildCommand).mockResolvedValue(mockNixpacksCommand); await deployApplication({ applicationId: "test-app-id", @@ -220,7 +220,7 @@ describe("deployApplication - Command Generation Tests", () => { ); const mockRailpackCommand = "railpack prepare /path/to/app"; - vi.mocked(builders.getBuildCommand).mockReturnValue(mockRailpackCommand); + vi.mocked(builders.getBuildCommand).mockResolvedValue(mockRailpackCommand); await deployApplication({ applicationId: "test-app-id", @@ -241,7 +241,7 @@ describe("deployApplication - Command Generation Tests", () => { it("should execute commands in correct order", async () => { const mockNixpacksCommand = "nixpacks build"; - vi.mocked(builders.getBuildCommand).mockReturnValue(mockNixpacksCommand); + vi.mocked(builders.getBuildCommand).mockResolvedValue(mockNixpacksCommand); await deployApplication({ applicationId: "test-app-id", @@ -260,7 +260,7 @@ describe("deployApplication - Command Generation Tests", () => { it("should include log redirection in command", async () => { const mockCommand = "nixpacks build"; - vi.mocked(builders.getBuildCommand).mockReturnValue(mockCommand); + vi.mocked(builders.getBuildCommand).mockResolvedValue(mockCommand); await deployApplication({ applicationId: "test-app-id", diff --git a/apps/dokploy/__test__/drop/drop.test.ts b/apps/dokploy/__test__/drop/drop.test.ts index bd2d3c981..cabc77d87 100644 --- a/apps/dokploy/__test__/drop/drop.test.ts +++ b/apps/dokploy/__test__/drop/drop.test.ts @@ -41,6 +41,9 @@ const baseApp: ApplicationNested = { giteaRepository: "", cleanCache: false, watchPaths: [], + rollbackRegistryId: "", + rollbackRegistry: null, + deployments: [], enableSubmodules: false, applicationStatus: "done", triggerType: "push", diff --git a/apps/dokploy/__test__/traefik/server/update-server-config.test.ts b/apps/dokploy/__test__/traefik/server/update-server-config.test.ts index 6858f0f00..f35e8132c 100644 --- a/apps/dokploy/__test__/traefik/server/update-server-config.test.ts +++ b/apps/dokploy/__test__/traefik/server/update-server-config.test.ts @@ -18,6 +18,8 @@ const baseAdmin: User = { enablePaidFeatures: false, allowImpersonation: false, role: "user", + firstName: "", + lastName: "", metricsConfig: { containers: { refreshRate: 20, @@ -61,7 +63,6 @@ const baseAdmin: User = { expirationDate: "", id: "", isRegistered: false, - name: "", createdAt2: new Date().toISOString(), emailVerified: false, image: "", diff --git a/apps/dokploy/__test__/traefik/traefik.test.ts b/apps/dokploy/__test__/traefik/traefik.test.ts index e6b98c3ba..279e74fa5 100644 --- a/apps/dokploy/__test__/traefik/traefik.test.ts +++ b/apps/dokploy/__test__/traefik/traefik.test.ts @@ -17,6 +17,9 @@ const baseApp: ApplicationNested = { giteaBuildPath: "", giteaId: "", args: [], + rollbackRegistryId: "", + rollbackRegistry: null, + deployments: [], cleanCache: false, applicationStatus: "done", endpointSpecSwarm: null, diff --git a/apps/dokploy/components/dashboard/application/deployments/show-deployments.tsx b/apps/dokploy/components/dashboard/application/deployments/show-deployments.tsx index 8abf8cbf1..ca7a93518 100644 --- a/apps/dokploy/components/dashboard/application/deployments/show-deployments.tsx +++ b/apps/dokploy/components/dashboard/application/deployments/show-deployments.tsx @@ -143,7 +143,7 @@ export const ShowDeployments = ({ See the last 10 deployments for this {type} -
+
{(type === "application" || type === "compose") && ( )} @@ -373,7 +373,19 @@ export const ShowDeployments = ({ type === "application" && ( +

+ Are you sure you want to rollback to this + deployment? +

+ + Please wait a few seconds while the image is + pulled from the registry. Your application + should be running shortly. + +
+ } type="default" onClick={async () => { await rollback({ diff --git a/apps/dokploy/components/dashboard/application/rollbacks/show-rollback-settings.tsx b/apps/dokploy/components/dashboard/application/rollbacks/show-rollback-settings.tsx index 2fc7c0522..a06cf5697 100644 --- a/apps/dokploy/components/dashboard/application/rollbacks/show-rollback-settings.tsx +++ b/apps/dokploy/components/dashboard/application/rollbacks/show-rollback-settings.tsx @@ -1,5 +1,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; -import { useState } from "react"; +import Link from "next/link"; +import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; @@ -20,13 +21,37 @@ import { FormField, FormItem, FormLabel, + FormMessage, } from "@/components/ui/form"; +import { + Select, + SelectContent, + SelectGroup, + SelectItem, + SelectLabel, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import { api } from "@/utils/api"; -const formSchema = z.object({ - rollbackActive: z.boolean(), -}); +const formSchema = z + .object({ + rollbackActive: z.boolean(), + rollbackRegistryId: z.string().optional(), + }) + .superRefine((values, ctx) => { + if ( + values.rollbackActive && + (!values.rollbackRegistryId || values.rollbackRegistryId === "none") + ) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ["rollbackRegistryId"], + message: "Registry is required when rollbacks are enabled", + }); + } + }); type FormValues = z.infer; @@ -49,17 +74,33 @@ export const ShowRollbackSettings = ({ applicationId, children }: Props) => { const { mutateAsync: updateApplication, isLoading } = api.application.update.useMutation(); + const { data: registries } = api.registry.all.useQuery(); + const form = useForm({ resolver: zodResolver(formSchema), defaultValues: { rollbackActive: application?.rollbackActive ?? false, + rollbackRegistryId: application?.rollbackRegistryId || "", }, }); + useEffect(() => { + if (application) { + form.reset({ + rollbackActive: application.rollbackActive ?? false, + rollbackRegistryId: application.rollbackRegistryId || "", + }); + } + }, [application, form]); + const onSubmit = async (data: FormValues) => { await updateApplication({ applicationId, rollbackActive: data.rollbackActive, + rollbackRegistryId: + data.rollbackRegistryId === "none" || !data.rollbackRegistryId + ? null + : data.rollbackRegistryId, }) .then(() => { toast.success("Rollback settings updated"); @@ -112,6 +153,65 @@ export const ShowRollbackSettings = ({ applicationId, children }: Props) => { )} /> + {form.watch("rollbackActive") && ( + ( + + Rollback Registry + + {!registries || registries.length === 0 ? ( + + No registries available. Please{" "} + + configure a registry + {" "} + first to enable rollbacks. + + ) : ( + + Select a registry where rollback images will be stored. + + )} + + + )} + /> + )} + diff --git a/apps/dokploy/components/dashboard/impersonation/impersonation-bar.tsx b/apps/dokploy/components/dashboard/impersonation/impersonation-bar.tsx index b96b7c866..f77983996 100644 --- a/apps/dokploy/components/dashboard/impersonation/impersonation-bar.tsx +++ b/apps/dokploy/components/dashboard/impersonation/impersonation-bar.tsx @@ -103,7 +103,7 @@ export const ImpersonationBar = () => { setOpen(false); toast.success("Successfully impersonating user", { - description: `You are now viewing as ${selectedUser.name || selectedUser.email}`, + description: `You are now viewing as ${`${selectedUser.name} ${selectedUser.lastName}`.trim() || selectedUser.email}`, }); window.location.reload(); } catch (error) { @@ -195,7 +195,8 @@ export const ImpersonationBar = () => { - {selectedUser.name || ""} + {`${selectedUser.name} ${selectedUser.lastName}`.trim() || + ""} {selectedUser.email} @@ -242,7 +243,8 @@ export const ImpersonationBar = () => { - {user.name || ""} + {`${user.name} ${user.lastName}`.trim() || + ""} {user.email} • {user.role} @@ -283,10 +285,14 @@ export const ImpersonationBar = () => { - {data?.user?.name?.slice(0, 2).toUpperCase() || "U"} + {`${data?.user?.firstName?.[0] || ""}${data?.user?.lastName?.[0] || ""}`.toUpperCase() || + "U"}
@@ -299,7 +305,8 @@ export const ImpersonationBar = () => { Impersonating - {data?.user?.name || ""} + {`${data?.user?.firstName} ${data?.user?.lastName}`.trim() || + ""}
diff --git a/apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx b/apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx index b28c4d9b6..1dd41e722 100644 --- a/apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx +++ b/apps/dokploy/components/dashboard/monitoring/free/container/show-free-container-monitoring.tsx @@ -10,7 +10,7 @@ import { DockerNetworkChart } from "./docker-network-chart"; const defaultData = { cpu: { - value: 0, + value: "0%", time: "", }, memory: { @@ -46,7 +46,7 @@ interface Props { } export interface DockerStats { cpu: { - value: number; + value: string; time: string; }; memory: { @@ -220,7 +220,13 @@ export const ContainerFreeMonitoring = ({ Used: {currentData.cpu.value} - +
diff --git a/apps/dokploy/components/dashboard/requests/show-requests.tsx b/apps/dokploy/components/dashboard/requests/show-requests.tsx index 3b5315c3e..cc4f1764a 100644 --- a/apps/dokploy/components/dashboard/requests/show-requests.tsx +++ b/apps/dokploy/components/dashboard/requests/show-requests.tsx @@ -65,6 +65,25 @@ export const ShowRequests = () => { to: Date | undefined; }>(getDefaultDateRange()); + // Check if logs exist to determine if traefik has been reloaded + // Only fetch when active to minimize network calls + const { data: statsLogsCheck } = api.settings.readStatsLogs.useQuery( + { + page: { + pageIndex: 0, + pageSize: 1, + }, + }, + { + enabled: !!isActive, + refetchInterval: 5000, // Check every 5 seconds when active + }, + ); + + // Determine if warning should be shown + // Show warning only if active but no logs exist yet + const shouldShowWarning = isActive && (statsLogsCheck?.totalCount ?? 0) === 0; + useEffect(() => { if (logCleanupStatus) { setCronExpression(logCleanupStatus.cronExpression || "0 0 * * *"); @@ -85,16 +104,18 @@ export const ShowRequests = () => { See all the incoming requests that pass trough Traefik - - When you activate, you need to reload traefik to apply the - changes, you can reload traefik in{" "} - - Settings - - + {shouldShowWarning && ( + + When you activate, you need to reload traefik to apply the + changes, you can reload traefik in{" "} + + Settings + + + )}
diff --git a/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx b/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx index f8eb74985..94199c142 100644 --- a/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx +++ b/apps/dokploy/components/dashboard/settings/notifications/handle-notifications.tsx @@ -5,31 +5,31 @@ import { useFieldArray, useForm } from "react-hook-form"; import { toast } from "sonner"; import { z } from "zod"; import { - DiscordIcon, - GotifyIcon, - LarkIcon, - NtfyIcon, - SlackIcon, - TelegramIcon, + DiscordIcon, + GotifyIcon, + LarkIcon, + NtfyIcon, + SlackIcon, + TelegramIcon, } from "@/components/icons/notification-icons"; import { Button } from "@/components/ui/button"; import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle, - DialogTrigger, + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, } from "@/components/ui/dialog"; import { - Form, - FormControl, - FormDescription, - FormField, - FormItem, - FormLabel, - FormMessage, + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; @@ -39,1353 +39,1380 @@ import { api } from "@/utils/api"; import { KeyValueInput } from "./key-value-input"; const notificationBaseSchema = z.object({ - name: z.string().min(1, { - message: "Name is required", - }), - appDeploy: z.boolean().default(false), - appBuildError: z.boolean().default(false), - databaseBackup: z.boolean().default(false), - dokployRestart: z.boolean().default(false), - dockerCleanup: z.boolean().default(false), - serverThreshold: z.boolean().default(false), + name: z.string().min(1, { + message: "Name is required", + }), + appDeploy: z.boolean().default(false), + appBuildError: z.boolean().default(false), + databaseBackup: z.boolean().default(false), + volumeBackup: z.boolean().default(false), + dokployRestart: z.boolean().default(false), + dockerCleanup: z.boolean().default(false), + serverThreshold: z.boolean().default(false), }); export const notificationSchema = z.discriminatedUnion("type", [ - z - .object({ - type: z.literal("slack"), - webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), - channel: z.string(), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("telegram"), - botToken: z.string().min(1, { message: "Bot Token is required" }), - chatId: z.string().min(1, { message: "Chat ID is required" }), - messageThreadId: z.string().optional(), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("discord"), - webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), - decoration: z.boolean().default(true), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("email"), - smtpServer: z.string().min(1, { message: "SMTP Server is required" }), - smtpPort: z.number().min(1, { message: "SMTP Port is required" }), - username: z.string().min(1, { message: "Username is required" }), - password: z.string().min(1, { message: "Password is required" }), - fromAddress: z.string().min(1, { message: "From Address is required" }), - toAddresses: z - .array( - z.string().min(1, { message: "Email is required" }).email({ - message: "Email is invalid", - }) - ) - .min(1, { message: "At least one email is required" }), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("gotify"), - serverUrl: z.string().min(1, { message: "Server URL is required" }), - appToken: z.string().min(1, { message: "App Token is required" }), - priority: z.number().min(1).max(10).default(5), - decoration: z.boolean().default(true), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("ntfy"), - serverUrl: z.string().min(1, { message: "Server URL is required" }), - topic: z.string().min(1, { message: "Topic is required" }), - accessToken: z.string().optional(), - priority: z.number().min(1).max(5).default(3), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("custom"), - endpoint: z.string().min(1, { message: "Endpoint URL is required" }), - headers: z.string().optional(), - }) - .merge(notificationBaseSchema), - z - .object({ - type: z.literal("lark"), - webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), - }) - .merge(notificationBaseSchema), + z + .object({ + type: z.literal("slack"), + webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), + channel: z.string(), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("telegram"), + botToken: z.string().min(1, { message: "Bot Token is required" }), + chatId: z.string().min(1, { message: "Chat ID is required" }), + messageThreadId: z.string().optional(), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("discord"), + webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), + decoration: z.boolean().default(true), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("email"), + smtpServer: z.string().min(1, { message: "SMTP Server is required" }), + smtpPort: z.number().min(1, { message: "SMTP Port is required" }), + username: z.string().min(1, { message: "Username is required" }), + password: z.string().min(1, { message: "Password is required" }), + fromAddress: z.string().min(1, { message: "From Address is required" }), + toAddresses: z + .array( + z.string().min(1, { message: "Email is required" }).email({ + message: "Email is invalid", + }), + ) + .min(1, { message: "At least one email is required" }), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("gotify"), + serverUrl: z.string().min(1, { message: "Server URL is required" }), + appToken: z.string().min(1, { message: "App Token is required" }), + priority: z.number().min(1).max(10).default(5), + decoration: z.boolean().default(true), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("ntfy"), + serverUrl: z.string().min(1, { message: "Server URL is required" }), + topic: z.string().min(1, { message: "Topic is required" }), + accessToken: z.string().optional(), + priority: z.number().min(1).max(5).default(3), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("custom"), + endpoint: z.string().min(1, { message: "Endpoint URL is required" }), + headers: z.string().optional(), + }) + .merge(notificationBaseSchema), + z + .object({ + type: z.literal("lark"), + webhookUrl: z.string().min(1, { message: "Webhook URL is required" }), + }) + .merge(notificationBaseSchema), ]); export const notificationsMap = { - slack: { - icon: , - label: "Slack", - }, - telegram: { - icon: , - label: "Telegram", - }, - discord: { - icon: , - label: "Discord", - }, - lark: { - icon: , - label: "Lark", - }, - email: { - icon: , - label: "Email", - }, - gotify: { - icon: , - label: "Gotify", - }, - ntfy: { - icon: , - label: "ntfy", - }, - custom: { - icon: , - label: "Custom", - }, + slack: { + icon: , + label: "Slack", + }, + telegram: { + icon: , + label: "Telegram", + }, + discord: { + icon: , + label: "Discord", + }, + lark: { + icon: , + label: "Lark", + }, + email: { + icon: , + label: "Email", + }, + gotify: { + icon: , + label: "Gotify", + }, + ntfy: { + icon: , + label: "ntfy", + }, + custom: { + icon: , + label: "Custom", + }, }; export type NotificationSchema = z.infer; interface Props { - notificationId?: string; + notificationId?: string; } export const HandleNotifications = ({ notificationId }: Props) => { - const utils = api.useUtils(); - const [visible, setVisible] = useState(false); - const { data: isCloud } = api.settings.isCloud.useQuery(); + const utils = api.useUtils(); + const [visible, setVisible] = useState(false); + const { data: isCloud } = api.settings.isCloud.useQuery(); - const { data: notification } = api.notification.one.useQuery( - { - notificationId: notificationId || "", - }, - { - enabled: !!notificationId, - } - ); - const { mutateAsync: testSlackConnection, isLoading: isLoadingSlack } = - api.notification.testSlackConnection.useMutation(); - const { mutateAsync: testTelegramConnection, isLoading: isLoadingTelegram } = - api.notification.testTelegramConnection.useMutation(); - const { mutateAsync: testDiscordConnection, isLoading: isLoadingDiscord } = - api.notification.testDiscordConnection.useMutation(); - const { mutateAsync: testEmailConnection, isLoading: isLoadingEmail } = - api.notification.testEmailConnection.useMutation(); - const { mutateAsync: testGotifyConnection, isLoading: isLoadingGotify } = - api.notification.testGotifyConnection.useMutation(); - const { mutateAsync: testNtfyConnection, isLoading: isLoadingNtfy } = - api.notification.testNtfyConnection.useMutation(); - const { mutateAsync: testCustomConnection, isLoading: isLoadingCustom } = - api.notification.testCustomConnection.useMutation(); - const { mutateAsync: testLarkConnection, isLoading: isLoadingLark } = - api.notification.testLarkConnection.useMutation(); - const slackMutation = notificationId - ? api.notification.updateSlack.useMutation() - : api.notification.createSlack.useMutation(); - const telegramMutation = notificationId - ? api.notification.updateTelegram.useMutation() - : api.notification.createTelegram.useMutation(); - const discordMutation = notificationId - ? api.notification.updateDiscord.useMutation() - : api.notification.createDiscord.useMutation(); - const emailMutation = notificationId - ? api.notification.updateEmail.useMutation() - : api.notification.createEmail.useMutation(); - const gotifyMutation = notificationId - ? api.notification.updateGotify.useMutation() - : api.notification.createGotify.useMutation(); - const ntfyMutation = notificationId - ? api.notification.updateNtfy.useMutation() - : api.notification.createNtfy.useMutation(); - const customMutation = notificationId - ? api.notification.updateCustom.useMutation() - : api.notification.createCustom.useMutation(); - const larkMutation = notificationId - ? api.notification.updateLark.useMutation() - : api.notification.createLark.useMutation(); + const { data: notification } = api.notification.one.useQuery( + { + notificationId: notificationId || "", + }, + { + enabled: !!notificationId, + }, + ); + const { mutateAsync: testSlackConnection, isLoading: isLoadingSlack } = + api.notification.testSlackConnection.useMutation(); + const { mutateAsync: testTelegramConnection, isLoading: isLoadingTelegram } = + api.notification.testTelegramConnection.useMutation(); + const { mutateAsync: testDiscordConnection, isLoading: isLoadingDiscord } = + api.notification.testDiscordConnection.useMutation(); + const { mutateAsync: testEmailConnection, isLoading: isLoadingEmail } = + api.notification.testEmailConnection.useMutation(); + const { mutateAsync: testGotifyConnection, isLoading: isLoadingGotify } = + api.notification.testGotifyConnection.useMutation(); + const { mutateAsync: testNtfyConnection, isLoading: isLoadingNtfy } = + api.notification.testNtfyConnection.useMutation(); + const { mutateAsync: testLarkConnection, isLoading: isLoadingLark } = + api.notification.testLarkConnection.useMutation(); - const form = useForm({ - defaultValues: { - type: "slack", - webhookUrl: "", - channel: "", - name: "", - }, - resolver: zodResolver(notificationSchema), - }); - const type = form.watch("type"); + const { mutateAsync: testCustomConnection, isLoading: isLoadingCustom } = + api.notification.testCustomConnection.useMutation(); - const { fields, append, remove } = useFieldArray({ - control: form.control, - name: "toAddresses" as never, - }); + const customMutation = notificationId + ? api.notification.updateCustom.useMutation() + : api.notification.createCustom.useMutation(); + const slackMutation = notificationId + ? api.notification.updateSlack.useMutation() + : api.notification.createSlack.useMutation(); + const telegramMutation = notificationId + ? api.notification.updateTelegram.useMutation() + : api.notification.createTelegram.useMutation(); + const discordMutation = notificationId + ? api.notification.updateDiscord.useMutation() + : api.notification.createDiscord.useMutation(); + const emailMutation = notificationId + ? api.notification.updateEmail.useMutation() + : api.notification.createEmail.useMutation(); + const gotifyMutation = notificationId + ? api.notification.updateGotify.useMutation() + : api.notification.createGotify.useMutation(); + const ntfyMutation = notificationId + ? api.notification.updateNtfy.useMutation() + : api.notification.createNtfy.useMutation(); + const larkMutation = notificationId + ? api.notification.updateLark.useMutation() + : api.notification.createLark.useMutation(); - useEffect(() => { - if (type === "email" && fields.length === 0) { - append(""); - } - }, [type, append, fields.length]); + const form = useForm({ + defaultValues: { + type: "slack", + webhookUrl: "", + channel: "", + name: "", + }, + resolver: zodResolver(notificationSchema), + }); + const type = form.watch("type"); - useEffect(() => { - if (notification) { - if (notification.notificationType === "slack") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - dockerCleanup: notification.dockerCleanup, - webhookUrl: notification.slack?.webhookUrl, - channel: notification.slack?.channel || "", - name: notification.name, - type: notification.notificationType, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "telegram") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - botToken: notification.telegram?.botToken, - messageThreadId: notification.telegram?.messageThreadId || "", - chatId: notification.telegram?.chatId, - type: notification.notificationType, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "discord") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - webhookUrl: notification.discord?.webhookUrl, - decoration: notification.discord?.decoration || undefined, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "email") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - smtpServer: notification.email?.smtpServer, - smtpPort: notification.email?.smtpPort, - username: notification.email?.username, - password: notification.email?.password, - toAddresses: notification.email?.toAddresses, - fromAddress: notification.email?.fromAddress, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "gotify") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - appToken: notification.gotify?.appToken, - decoration: notification.gotify?.decoration || undefined, - priority: notification.gotify?.priority, - serverUrl: notification.gotify?.serverUrl, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - }); - } else if (notification.notificationType === "ntfy") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - accessToken: notification.ntfy?.accessToken || "", - topic: notification.ntfy?.topic, - priority: notification.ntfy?.priority, - serverUrl: notification.ntfy?.serverUrl, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "lark") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - webhookUrl: notification.lark?.webhookUrl, - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } else if (notification.notificationType === "custom") { - form.reset({ - appBuildError: notification.appBuildError, - appDeploy: notification.appDeploy, - dokployRestart: notification.dokployRestart, - databaseBackup: notification.databaseBackup, - type: notification.notificationType, - endpoint: notification.custom?.endpoint || "", - headers: notification.custom?.headers || "", - name: notification.name, - dockerCleanup: notification.dockerCleanup, - serverThreshold: notification.serverThreshold, - }); - } - } else { - form.reset(); - } - }, [form, form.reset, form.formState.isSubmitSuccessful, notification]); + const { fields, append, remove } = useFieldArray({ + control: form.control, + name: "toAddresses" as never, + }); - const activeMutation = { - slack: slackMutation, - telegram: telegramMutation, - discord: discordMutation, - email: emailMutation, - gotify: gotifyMutation, - ntfy: ntfyMutation, - custom: customMutation, - lark: larkMutation, - }; + useEffect(() => { + if (type === "email" && fields.length === 0) { + append(""); + } + }, [type, append, fields.length]); - const onSubmit = async (data: NotificationSchema) => { - const { - appBuildError, - appDeploy, - dokployRestart, - databaseBackup, - dockerCleanup, - serverThreshold, - } = data; - let promise: Promise | null = null; - if (data.type === "slack") { - promise = slackMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - webhookUrl: data.webhookUrl, - channel: data.channel, - name: data.name, - dockerCleanup: dockerCleanup, - slackId: notification?.slackId || "", - notificationId: notificationId || "", - serverThreshold: serverThreshold, - }); - } else if (data.type === "telegram") { - promise = telegramMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - botToken: data.botToken, - messageThreadId: data.messageThreadId || "", - chatId: data.chatId, - name: data.name, - dockerCleanup: dockerCleanup, - notificationId: notificationId || "", - telegramId: notification?.telegramId || "", - serverThreshold: serverThreshold, - }); - } else if (data.type === "discord") { - promise = discordMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - webhookUrl: data.webhookUrl, - decoration: data.decoration, - name: data.name, - dockerCleanup: dockerCleanup, - notificationId: notificationId || "", - discordId: notification?.discordId || "", - serverThreshold: serverThreshold, - }); - } else if (data.type === "email") { - promise = emailMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - smtpServer: data.smtpServer, - smtpPort: data.smtpPort, - username: data.username, - password: data.password, - fromAddress: data.fromAddress, - toAddresses: data.toAddresses, - name: data.name, - dockerCleanup: dockerCleanup, - notificationId: notificationId || "", - emailId: notification?.emailId || "", - serverThreshold: serverThreshold, - }); - } else if (data.type === "gotify") { - promise = gotifyMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - serverUrl: data.serverUrl, - appToken: data.appToken, - priority: data.priority, - name: data.name, - dockerCleanup: dockerCleanup, - decoration: data.decoration, - notificationId: notificationId || "", - gotifyId: notification?.gotifyId || "", - }); - } else if (data.type === "ntfy") { - promise = ntfyMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - serverUrl: data.serverUrl, - accessToken: data.accessToken || "", - topic: data.topic, - priority: data.priority, - name: data.name, - dockerCleanup: dockerCleanup, - notificationId: notificationId || "", - ntfyId: notification?.ntfyId || "", - }); - } else if (data.type === "custom") { - promise = customMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - endpoint: data.endpoint, - headers: data.headers || "", - name: data.name, - dockerCleanup: dockerCleanup, - serverThreshold: serverThreshold, - notificationId: notificationId || "", - customId: notification?.customId || "", - }); - } else if (data.type === "lark") { - promise = larkMutation.mutateAsync({ - appBuildError: appBuildError, - appDeploy: appDeploy, - dokployRestart: dokployRestart, - databaseBackup: databaseBackup, - webhookUrl: data.webhookUrl, - name: data.name, - dockerCleanup: dockerCleanup, - notificationId: notificationId || "", - larkId: notification?.larkId || "", - serverThreshold: serverThreshold, - }); - } + useEffect(() => { + if (notification) { + if (notification.notificationType === "slack") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + dockerCleanup: notification.dockerCleanup, + webhookUrl: notification.slack?.webhookUrl, + channel: notification.slack?.channel || "", + name: notification.name, + type: notification.notificationType, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "telegram") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + botToken: notification.telegram?.botToken, + messageThreadId: notification.telegram?.messageThreadId || "", + chatId: notification.telegram?.chatId, + type: notification.notificationType, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "discord") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + type: notification.notificationType, + webhookUrl: notification.discord?.webhookUrl, + decoration: notification.discord?.decoration || undefined, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "email") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + type: notification.notificationType, + smtpServer: notification.email?.smtpServer, + smtpPort: notification.email?.smtpPort, + username: notification.email?.username, + password: notification.email?.password, + toAddresses: notification.email?.toAddresses, + fromAddress: notification.email?.fromAddress, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "gotify") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + type: notification.notificationType, + appToken: notification.gotify?.appToken, + decoration: notification.gotify?.decoration || undefined, + priority: notification.gotify?.priority, + serverUrl: notification.gotify?.serverUrl, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + }); + } else if (notification.notificationType === "ntfy") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + volumeBackup: notification.volumeBackup, + type: notification.notificationType, + accessToken: notification.ntfy?.accessToken || "", + topic: notification.ntfy?.topic, + priority: notification.ntfy?.priority, + serverUrl: notification.ntfy?.serverUrl, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "lark") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + type: notification.notificationType, + webhookUrl: notification.lark?.webhookUrl, + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } else if (notification.notificationType === "custom") { + form.reset({ + appBuildError: notification.appBuildError, + appDeploy: notification.appDeploy, + dokployRestart: notification.dokployRestart, + databaseBackup: notification.databaseBackup, + type: notification.notificationType, + endpoint: notification.custom?.endpoint || "", + headers: notification.custom?.headers || "", + name: notification.name, + dockerCleanup: notification.dockerCleanup, + serverThreshold: notification.serverThreshold, + }); + } + } else { + form.reset(); + } + }, [form, form.reset, form.formState.isSubmitSuccessful, notification]); - if (promise) { - await promise - .then(async () => { - toast.success( - notificationId ? "Notification Updated" : "Notification Created" - ); - form.reset({ - type: "slack", - webhookUrl: "", - }); - setVisible(false); - await utils.notification.all.invalidate(); - }) - .catch(() => { - toast.error( - notificationId - ? "Error updating a notification" - : "Error creating a notification" - ); - }); - } - }; - return ( - - - {notificationId ? ( - - ) : ( - - )} - - - - - {notificationId ? "Update" : "Add"} Notification - - - {notificationId - ? "Update your notification providers for multiple channels." - : "Create new notification providers for multiple channels."} - - -
- - ( - - - Select a provider - - - - {Object.entries(notificationsMap).map(([key, value]) => ( - - -
- - -
-
-
- ))} -
-
- - {activeMutation[field.value].isError && ( -
- - - {activeMutation[field.value].error?.message} - -
- )} -
- )} - /> + const activeMutation = { + slack: slackMutation, + telegram: telegramMutation, + discord: discordMutation, + email: emailMutation, + gotify: gotifyMutation, + ntfy: ntfyMutation, + lark: larkMutation, + custom: customMutation, + }; -
- - Fill the next fields. - -
- ( - - Name - - - + const onSubmit = async (data: NotificationSchema) => { + const { + appBuildError, + appDeploy, + dokployRestart, + databaseBackup, + volumeBackup, + dockerCleanup, + serverThreshold, + } = data; + let promise: Promise | null = null; + if (data.type === "slack") { + promise = slackMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + webhookUrl: data.webhookUrl, + channel: data.channel, + name: data.name, + dockerCleanup: dockerCleanup, + slackId: notification?.slackId || "", + notificationId: notificationId || "", + serverThreshold: serverThreshold, + }); + } else if (data.type === "telegram") { + promise = telegramMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + botToken: data.botToken, + messageThreadId: data.messageThreadId || "", + chatId: data.chatId, + name: data.name, + dockerCleanup: dockerCleanup, + notificationId: notificationId || "", + telegramId: notification?.telegramId || "", + serverThreshold: serverThreshold, + }); + } else if (data.type === "discord") { + promise = discordMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + webhookUrl: data.webhookUrl, + decoration: data.decoration, + name: data.name, + dockerCleanup: dockerCleanup, + notificationId: notificationId || "", + discordId: notification?.discordId || "", + serverThreshold: serverThreshold, + }); + } else if (data.type === "email") { + promise = emailMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + smtpServer: data.smtpServer, + smtpPort: data.smtpPort, + username: data.username, + password: data.password, + fromAddress: data.fromAddress, + toAddresses: data.toAddresses, + name: data.name, + dockerCleanup: dockerCleanup, + notificationId: notificationId || "", + emailId: notification?.emailId || "", + serverThreshold: serverThreshold, + }); + } else if (data.type === "gotify") { + promise = gotifyMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + serverUrl: data.serverUrl, + appToken: data.appToken, + priority: data.priority, + name: data.name, + dockerCleanup: dockerCleanup, + decoration: data.decoration, + notificationId: notificationId || "", + gotifyId: notification?.gotifyId || "", + }); + } else if (data.type === "ntfy") { + promise = ntfyMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + volumeBackup: volumeBackup, + serverUrl: data.serverUrl, + accessToken: data.accessToken || "", + topic: data.topic, + priority: data.priority, + name: data.name, + dockerCleanup: dockerCleanup, + notificationId: notificationId || "", + ntfyId: notification?.ntfyId || "", + }); + } else if (data.type === "lark") { + promise = larkMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + webhookUrl: data.webhookUrl, + name: data.name, + dockerCleanup: dockerCleanup, + notificationId: notificationId || "", + larkId: notification?.larkId || "", + serverThreshold: serverThreshold, + }); + } else if (data.type === "custom") { + promise = customMutation.mutateAsync({ + appBuildError: appBuildError, + appDeploy: appDeploy, + dokployRestart: dokployRestart, + databaseBackup: databaseBackup, + endpoint: data.endpoint, + headers: data.headers || "", + name: data.name, + dockerCleanup: dockerCleanup, + serverThreshold: serverThreshold, + notificationId: notificationId || "", + customId: notification?.customId || "", + }); + } - - - )} - /> + if (promise) { + await promise + .then(async () => { + toast.success( + notificationId ? "Notification Updated" : "Notification Created", + ); + form.reset({ + type: "slack", + webhookUrl: "", + }); + setVisible(false); + await utils.notification.all.invalidate(); + }) + .catch(() => { + toast.error( + notificationId + ? "Error updating a notification" + : "Error creating a notification", + ); + }); + } + }; + return ( + + + {notificationId ? ( + + ) : ( + + )} + + + + + {notificationId ? "Update" : "Add"} Notification + + + {notificationId + ? "Update your notification providers for multiple channels." + : "Create new notification providers for multiple channels."} + + + + + ( + + + Select a provider + + + + {Object.entries(notificationsMap).map(([key, value]) => ( + + +
+ + +
+
+
+ ))} +
+
+ + {activeMutation[field.value].isError && ( +
+ + + {activeMutation[field.value].error?.message} + +
+ )} +
+ )} + /> - {type === "slack" && ( - <> - ( - - Webhook URL - - - +
+ + Fill the next fields. + +
+ ( + + Name + + + - - - )} - /> + + + )} + /> - ( - - Channel - - - + {type === "slack" && ( + <> + ( + + Webhook URL + + + - - - )} - /> - - )} + + + )} + /> - {type === "telegram" && ( - <> - ( - - Bot Token - - - + ( + + Channel + + + - - - )} - /> + + + )} + /> + + )} - ( - - Chat ID - - - - - - )} - /> + {type === "telegram" && ( + <> + ( + + Bot Token + + + - ( - - Message Thread ID - - - + + + )} + /> - - - Optional. Use it when you want to send notifications - to a specific topic in a group. - - - )} - /> - - )} + ( + + Chat ID + + + + + + )} + /> - {type === "discord" && ( - <> - ( - - Webhook URL - - - + ( + + Message Thread ID + + + - - - )} - /> + + + Optional. Use it when you want to send notifications + to a specific topic in a group. + + + )} + /> + + )} - ( - -
- Decoration - - Decorate the notification with emojis. - -
- - - -
- )} - /> - - )} + {type === "discord" && ( + <> + ( + + Webhook URL + + + - {type === "email" && ( - <> -
- ( - - SMTP Server - - - + + + )} + /> - - - )} - /> - ( - - SMTP Port - - { - const value = e.target.value; - if (value === "") { - field.onChange(undefined); - } else { - const port = Number.parseInt(value); - if (port > 0 && port < 65536) { - field.onChange(port); - } - } - }} - value={field.value || ""} - type="number" - /> - + ( + +
+ Decoration + + Decorate the notification with emojis. + +
+ + + +
+ )} + /> + + )} - -
- )} - /> -
+ {type === "email" && ( + <> +
+ ( + + SMTP Server + + + -
- ( - - Username - - - + + + )} + /> + ( + + SMTP Port + + { + const value = e.target.value; + if (value === "") { + field.onChange(undefined); + } else { + const port = Number.parseInt(value); + if (port > 0 && port < 65536) { + field.onChange(port); + } + } + }} + value={field.value || ""} + type="number" + /> + - - - )} - /> + + + )} + /> +
- ( - - Password - - - +
+ ( + + Username + + + - - - )} - /> -
+ +
+ )} + /> - ( - - From Address - - - - - - )} - /> -
- To Addresses + ( + + Password + + + - {fields.map((field, index) => ( -
- ( - - - - + + + )} + /> +
- -
- )} - /> - -
- ))} - {type === "email" && - "toAddresses" in form.formState.errors && ( -
- {form.formState?.errors?.toAddresses?.root?.message} -
- )} -
+ ( + + From Address + + + + + + )} + /> +
+ To Addresses - - - )} + {fields.map((field, index) => ( +
+ ( + + + + - {type === "gotify" && ( - <> - ( - - Server URL - - - - - - )} - /> - ( - - App Token - - - - - - )} - /> - ( - - Priority - - { - const value = e.target.value; - if (value) { - const port = Number.parseInt(value); - if (port > 0 && port < 10) { - field.onChange(port); - } - } - }} - type="number" - /> - - - Message priority (1-10, default: 5) - - - - )} - /> - ( - -
- Decoration - - Decorate the notification with emojis. - -
- - - -
- )} - /> - - )} + +
+ )} + /> + +
+ ))} + {type === "email" && + "toAddresses" in form.formState.errors && ( +
+ {form.formState?.errors?.toAddresses?.root?.message} +
+ )} +
- {type === "ntfy" && ( - <> - ( - - Server URL - - - - - - )} - /> - ( - - Topic - - - - - - )} - /> - ( - - Access Token - - - - - Optional. Leave blank for public topics. - - - - )} - /> - ( - - Priority - - { - const value = e.target.value; - if (value) { - const port = Number.parseInt(value); - if (port > 0 && port <= 5) { - field.onChange(port); - } - } - }} - type="number" - /> - - - Message priority (1-5, default: 3) - - - - )} - /> - - )} + + + )} - {type === "custom" && ( -
- ( - - Webhook URL - - - - - The URL where POST requests will be sent with - notification data. - - - - )} - /> + {type === "gotify" && ( + <> + ( + + Server URL + + + + + + )} + /> + ( + + App Token + + + + + + )} + /> + ( + + Priority + + { + const value = e.target.value; + if (value) { + const port = Number.parseInt(value); + if (port > 0 && port < 10) { + field.onChange(port); + } + } + }} + type="number" + /> + + + Message priority (1-10, default: 5) + + + + )} + /> + ( + +
+ Decoration + + Decorate the notification with emojis. + +
+ + + +
+ )} + /> + + )} - ( - - - - - - - )} - /> -
- )} + {type === "ntfy" && ( + <> + ( + + Server URL + + + + + + )} + /> + ( + + Topic + + + + + + )} + /> + ( + + Access Token + + + + + Optional. Leave blank for public topics. + + + + )} + /> + ( + + Priority + + { + const value = e.target.value; + if (value) { + const port = Number.parseInt(value); + if (port > 0 && port <= 5) { + field.onChange(port); + } + } + }} + type="number" + /> + + + Message priority (1-5, default: 3) + + + + )} + /> + + )} + {type === "custom" && ( +
+ ( + + Webhook URL + + + + + The URL where POST requests will be sent with + notification data. + + + + )} + /> - {type === "lark" && ( - <> - ( - - Webhook URL - - - - - - )} - /> - - )} -
-
-
- - Select the actions. - + ( + + + + + + + )} + /> +
+ )} + {type === "lark" && ( + <> + ( + + Webhook URL + + + + + + )} + /> + + )} +
+
+
+ + Select the actions. + -
- ( - -
- App Deploy - - Trigger the action when a app is deployed. - -
- - - -
- )} - /> - ( - -
- App Build Error - - Trigger the action when the build fails. - -
- - - -
- )} - /> +
+ ( + +
+ App Deploy + + Trigger the action when a app is deployed. + +
+ + + +
+ )} + /> + ( + +
+ App Build Error + + Trigger the action when the build fails. + +
+ + + +
+ )} + /> - ( - -
- Database Backup - - Trigger the action when a database backup is created. - -
- - - -
- )} - /> + ( + +
+ Database Backup + + Trigger the action when a database backup is created. + +
+ + + +
+ )} + /> - ( - -
- Docker Cleanup - - Trigger the action when the docker cleanup is - performed. - -
- - - -
- )} - /> + ( + +
+ Volume Backup + + Trigger the action when a volume backup is created. + +
+ + + +
+ )} + /> - {!isCloud && ( - ( - -
- Dokploy Restart - - Trigger the action when dokploy is restarted. - -
- - - -
- )} - /> - )} + ( + +
+ Docker Cleanup + + Trigger the action when the docker cleanup is + performed. + +
+ + + +
+ )} + /> - {isCloud && ( - ( - -
- Server Threshold - - Trigger the action when the server threshold is - reached. - -
- - - -
- )} - /> - )} -
-
- + {!isCloud && ( + ( + +
+ Dokploy Restart + + Trigger the action when dokploy is restarted. + +
+ + + +
+ )} + /> + )} - -
+
+ - const data = form.getValues(); + + - - - -
-
- ); + const data = form.getValues(); + + try { + if (data.type === "slack") { + await testSlackConnection({ + webhookUrl: data.webhookUrl, + channel: data.channel, + }); + } else if (data.type === "telegram") { + await testTelegramConnection({ + botToken: data.botToken, + chatId: data.chatId, + messageThreadId: data.messageThreadId || "", + }); + } else if (data.type === "discord") { + await testDiscordConnection({ + webhookUrl: data.webhookUrl, + decoration: data.decoration, + }); + } else if (data.type === "email") { + await testEmailConnection({ + smtpServer: data.smtpServer, + smtpPort: data.smtpPort, + username: data.username, + password: data.password, + fromAddress: data.fromAddress, + toAddresses: data.toAddresses, + }); + } else if (data.type === "gotify") { + await testGotifyConnection({ + serverUrl: data.serverUrl, + appToken: data.appToken, + priority: data.priority, + decoration: data.decoration, + }); + } else if (data.type === "ntfy") { + await testNtfyConnection({ + serverUrl: data.serverUrl, + topic: data.topic, + accessToken: data.accessToken || "", + priority: data.priority, + }); + } else if (data.type === "lark") { + await testLarkConnection({ + webhookUrl: data.webhookUrl, + }); + } + toast.success("Connection Success"); + } catch (error) { + toast.error( + `Error testing the provider: ${error instanceof Error ? error.message : "Unknown error"}`, + ); + } + }} + > + Test Notification + + + + + + + ); }; diff --git a/apps/dokploy/components/dashboard/settings/profile/profile-form.tsx b/apps/dokploy/components/dashboard/settings/profile/profile-form.tsx index 583f3fefe..47219620f 100644 --- a/apps/dokploy/components/dashboard/settings/profile/profile-form.tsx +++ b/apps/dokploy/components/dashboard/settings/profile/profile-form.tsx @@ -41,6 +41,7 @@ const profileSchema = z.object({ currentPassword: z.string().nullable(), image: z.string().optional(), name: z.string().optional(), + lastName: z.string().optional(), allowImpersonation: z.boolean().optional().default(false), }); @@ -88,7 +89,8 @@ export const ProfileForm = () => { image: data?.user?.image || "", currentPassword: "", allowImpersonation: data?.user?.allowImpersonation || false, - name: data?.user?.name || "", + name: data?.user?.firstName || "", + lastName: data?.user?.lastName || "", }, resolver: zodResolver(profileSchema), }); @@ -102,7 +104,8 @@ export const ProfileForm = () => { image: data?.user?.image || "", currentPassword: form.getValues("currentPassword") || "", allowImpersonation: data?.user?.allowImpersonation, - name: data?.user?.name || "", + name: data?.user?.firstName || "", + lastName: data?.user?.lastName || "", }, { keepValues: true, @@ -127,6 +130,7 @@ export const ProfileForm = () => { currentPassword: values.currentPassword || undefined, allowImpersonation: values.allowImpersonation, name: values.name || undefined, + lastName: values.lastName || undefined, }); await refetch(); toast.success("Profile Updated"); @@ -136,6 +140,7 @@ export const ProfileForm = () => { image: values.image, currentPassword: "", name: values.name || "", + lastName: values.lastName || "", }); } catch (error) { toast.error("Error updating the profile"); @@ -180,9 +185,22 @@ export const ProfileForm = () => { name="name" render={({ field }) => ( - Name + First Name - + + + + + )} + /> + ( + + Last Name + + @@ -280,7 +298,7 @@ export const ProfileForm = () => { {getFallbackAvatarInitials( - data?.user?.name, + `${data?.user?.firstName} ${data?.user?.lastName}`.trim(), )} diff --git a/apps/dokploy/components/dashboard/settings/servers/actions/show-traefik-actions.tsx b/apps/dokploy/components/dashboard/settings/servers/actions/show-traefik-actions.tsx index d9573ca74..aebba8877 100644 --- a/apps/dokploy/components/dashboard/settings/servers/actions/show-traefik-actions.tsx +++ b/apps/dokploy/components/dashboard/settings/servers/actions/show-traefik-actions.tsx @@ -1,5 +1,7 @@ import { useTranslation } from "next-i18next"; import { toast } from "sonner"; +import { AlertBlock } from "@/components/shared/alert-block"; +import { DialogAction } from "@/components/shared/dialog-action"; import { Button } from "@/components/ui/button"; import { DropdownMenu, @@ -85,7 +87,26 @@ export const ShowTraefikActions = ({ serverId }: Props) => { - + + The Traefik container will be recreated from scratch. This + means the container will be deleted and created again, which + may cause downtime in your applications. + +

+ Are you sure you want to{" "} + {haveTraefikDashboardPortEnabled ? "disable" : "enable"} the + Traefik dashboard? +

+
+ } onClick={async () => { await toggleDashboard({ enableDashboard: !haveTraefikDashboardPortEnabled, @@ -97,14 +118,26 @@ export const ShowTraefikActions = ({ serverId }: Props) => { ); refetchDashboard(); }) - .catch(() => {}); + .catch((error) => { + const errorMessage = + error?.message || + "Failed to toggle dashboard. Please check if port 8080 is available."; + toast.error(errorMessage); + }); }} - className="w-full cursor-pointer space-x-3" + disabled={toggleDashboardIsLoading} + type="default" > - - {haveTraefikDashboardPortEnabled ? "Disable" : "Enable"} Dashboard - - + e.preventDefault()} + className="w-full cursor-pointer space-x-3" + > + + {haveTraefikDashboardPortEnabled ? "Disable" : "Enable"}{" "} + Dashboard + + + e.preventDefault()} diff --git a/apps/dokploy/components/dashboard/settings/users/add-invitation.tsx b/apps/dokploy/components/dashboard/settings/users/add-invitation.tsx index 6e0384554..e778f2e96 100644 --- a/apps/dokploy/components/dashboard/settings/users/add-invitation.tsx +++ b/apps/dokploy/components/dashboard/settings/users/add-invitation.tsx @@ -158,6 +158,7 @@ export const AddInvitation = () => { Member + Admin diff --git a/apps/dokploy/components/dashboard/settings/users/change-role.tsx b/apps/dokploy/components/dashboard/settings/users/change-role.tsx new file mode 100644 index 000000000..93dc4dfc0 --- /dev/null +++ b/apps/dokploy/components/dashboard/settings/users/change-role.tsx @@ -0,0 +1,159 @@ +import { zodResolver } from "@hookform/resolvers/zod"; +import { useEffect, useState } from "react"; +import { useForm } from "react-hook-form"; +import { toast } from "sonner"; +import { z } from "zod"; +import { AlertBlock } from "@/components/shared/alert-block"; +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; +import { api } from "@/utils/api"; + +const changeRoleSchema = z.object({ + role: z.enum(["admin", "member"]), +}); + +type ChangeRoleSchema = z.infer; + +interface Props { + memberId: string; + currentRole: "admin" | "member"; + userEmail: string; +} + +export const ChangeRole = ({ memberId, currentRole, userEmail }: Props) => { + const [isOpen, setIsOpen] = useState(false); + const utils = api.useUtils(); + + const { mutateAsync, isError, error, isLoading } = + api.organization.updateMemberRole.useMutation(); + + const form = useForm({ + defaultValues: { + role: currentRole, + }, + resolver: zodResolver(changeRoleSchema), + }); + + useEffect(() => { + if (isOpen) { + form.reset({ + role: currentRole, + }); + } + }, [form, currentRole, isOpen]); + + const onSubmit = async (data: ChangeRoleSchema) => { + await mutateAsync({ + memberId, + role: data.role, + }) + .then(async () => { + toast.success("Role updated successfully"); + await utils.user.all.invalidate(); + setIsOpen(false); + }) + .catch((error) => { + toast.error(error?.message || "Error updating role"); + }); + }; + + return ( + + + e.preventDefault()} + > + Change Role + + + + + Change User Role + + Change the role for {userEmail} + + + {isError && {error?.message}} + +
+ + ( + + Role + + + Admin: Can manage users and settings. +
+ Member: Limited permissions, can be + customized. +
+ + Note: Owner role is intransferible. + +
+ +
+ )} + /> + + + + + + +
+
+ ); +}; diff --git a/apps/dokploy/components/dashboard/settings/users/show-users.tsx b/apps/dokploy/components/dashboard/settings/users/show-users.tsx index 06c94416b..a52cfda6d 100644 --- a/apps/dokploy/components/dashboard/settings/users/show-users.tsx +++ b/apps/dokploy/components/dashboard/settings/users/show-users.tsx @@ -29,12 +29,15 @@ import { import { authClient } from "@/lib/auth-client"; import { api } from "@/utils/api"; import { AddUserPermissions } from "./add-permissions"; +import { ChangeRole } from "./change-role"; export const ShowUsers = () => { const { data: isCloud } = api.settings.isCloud.useQuery(); const { data, isLoading, refetch } = api.user.all.useQuery(); const { mutateAsync } = api.user.remove.useMutation(); + const utils = api.useUtils(); + const { data: session } = authClient.useSession(); return (
@@ -81,6 +84,52 @@ export const ShowUsers = () => { {data?.map((member) => { + const currentUserRole = data?.find( + (m) => m.user.id === session?.user?.id, + )?.role; + + // Owner never has "Edit Permissions" (they're absolute owner) + // Other users can edit permissions if target is not themselves and target is a member + const canEditPermissions = + member.role !== "owner" && + member.role === "member" && + member.user.id !== session?.user?.id; + + // Can change role based on hierarchy: + // - Owner: Can change anyone's role (except themselves and other owners) + // - Admin: Can only change member roles (not other admins or owners) + // - Owner role is intransferible + const canChangeRole = + member.role !== "owner" && + member.user.id !== session?.user?.id && + (currentUserRole === "owner" || + (currentUserRole === "admin" && + member.role === "member")); + + // Delete/Unlink follow same hierarchy as role changes + // - Owner: Can delete/unlink anyone (except themselves and owner can't be deleted) + // - Admin: Can only delete/unlink members (not other admins or owner) + const canDelete = + member.role !== "owner" && + !isCloud && + member.user.id !== session?.user?.id && + (currentUserRole === "owner" || + (currentUserRole === "admin" && + member.role === "member")); + + const canUnlink = + member.role !== "owner" && + member.user.id !== session?.user?.id && + (currentUserRole === "owner" || + (currentUserRole === "admin" && + member.role === "member")); + + const hasAnyAction = + canEditPermissions || + canChangeRole || + canDelete || + canUnlink; + return ( @@ -109,7 +158,7 @@ export const ShowUsers = () => { - {member.role !== "owner" && ( + {hasAnyAction ? ( )} diff --git a/apps/dokploy/components/dashboard/settings/web-server/manage-traefik-ports.tsx b/apps/dokploy/components/dashboard/settings/web-server/manage-traefik-ports.tsx index 282f1fddd..3ce95aa1f 100644 --- a/apps/dokploy/components/dashboard/settings/web-server/manage-traefik-ports.tsx +++ b/apps/dokploy/components/dashboard/settings/web-server/manage-traefik-ports.tsx @@ -105,7 +105,9 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => { }); toast.success(t("settings.server.webServer.traefik.portsUpdated")); setOpen(false); - } catch {} + } catch (error) { + toast.error((error as Error).message || "Error updating Traefik ports"); + } }; return ( @@ -156,11 +158,11 @@ export const ManageTraefikPorts = ({ children, serverId }: Props) => {

) : ( - +
{fields.map((field, index) => ( - + {
)} + + + The Traefik container will be recreated from scratch. This + means the container will be deleted and created again, which + may cause downtime in your applications. +