Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions apps/dokploy/__test__/utils/copy-input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import copy from "copy-to-clipboard";
import { toast } from "sonner";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { copyToClipboard } from "@/components/shared/copy-input";

vi.mock("copy-to-clipboard", () => ({ default: vi.fn() }));
vi.mock("sonner", () => ({ toast: { success: vi.fn() } }));

describe("copyToClipboard", () => {
beforeEach(() => {
vi.clearAllMocks();
});

test("copies the value to the clipboard", () => {
copyToClipboard("admin");
expect(copy).toHaveBeenCalledWith("admin");
});

test("shows a success toast", () => {
copyToClipboard("admin");
expect(toast.success).toHaveBeenCalledWith("Value is copied to clipboard");
});

test("returns the copied value", () => {
expect(copyToClipboard("admin")).toBe("admin");
});

test("falls back to an empty string for undefined", () => {
expect(copyToClipboard(undefined)).toBe("");
expect(copy).toHaveBeenCalledWith("");
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SelectGroup } from "@radix-ui/react-select";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
Expand Down Expand Up @@ -28,7 +29,7 @@ export const ShowInternalLibsqlCredentials = ({ libsqlId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value={data?.databaseUser} />
<CopyInput disabled value={data?.databaseUser} />
</div>
<div className="flex flex-col gap-2">
<Label>Sqld Node</Label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { UpdateDatabasePassword } from "@/components/shared/update-database-password";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand All @@ -25,11 +26,11 @@ export const ShowInternalMariadbCredentials = ({ mariadbId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value={data?.databaseUser} />
<CopyInput disabled value={data?.databaseUser} />
</div>
<div className="flex flex-col gap-2">
<Label>Database Name</Label>
<Input disabled value={data?.databaseName} />
<CopyInput disabled value={data?.databaseName} />
</div>
<div className="flex flex-col gap-2">
<Label>Password</Label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { UpdateDatabasePassword } from "@/components/shared/update-database-password";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand All @@ -25,7 +26,7 @@ export const ShowInternalMongoCredentials = ({ mongoId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value={data?.databaseUser} />
<CopyInput disabled value={data?.databaseUser} />
</div>

<div className="flex flex-col gap-2">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { UpdateDatabasePassword } from "@/components/shared/update-database-password";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand All @@ -25,11 +26,11 @@ export const ShowInternalMysqlCredentials = ({ mysqlId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value={data?.databaseUser} />
<CopyInput disabled value={data?.databaseUser} />
</div>
<div className="flex flex-col gap-2">
<Label>Database Name</Label>
<Input disabled value={data?.databaseName} />
<CopyInput disabled value={data?.databaseName} />
</div>
<div className="flex flex-col gap-2">
<Label>Password</Label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { UpdateDatabasePassword } from "@/components/shared/update-database-password";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand All @@ -25,11 +26,11 @@ export const ShowInternalPostgresCredentials = ({ postgresId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value={data?.databaseUser} />
<CopyInput disabled value={data?.databaseUser} />
</div>
<div className="flex flex-col gap-2">
<Label>Database Name</Label>
<Input disabled value={data?.databaseName} />
<CopyInput disabled value={data?.databaseName} />
</div>
<div className="flex flex-col gap-2">
<Label>Password</Label>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner";
import { CopyInput } from "@/components/shared/copy-input";
import { ToggleVisibilityInput } from "@/components/shared/toggle-visibility-input";
import { UpdateDatabasePassword } from "@/components/shared/update-database-password";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
Expand All @@ -25,7 +26,7 @@ export const ShowInternalRedisCredentials = ({ redisId }: Props) => {
<div className="grid w-full md:grid-cols-2 gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Label>User</Label>
<Input disabled value="default" />
<CopyInput disabled value="default" />
</div>
<div className="flex flex-col gap-2">
<Label>Password</Label>
Expand Down
27 changes: 27 additions & 0 deletions apps/dokploy/components/shared/copy-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import copy from "copy-to-clipboard";
import { Clipboard } from "lucide-react";
import { toast } from "sonner";
import { Button } from "../ui/button";
import { Input, type InputProps } from "../ui/input";

export const copyToClipboard = (value: string | undefined): string => {
const text = value ?? "";
copy(text);
toast.success("Value is copied to clipboard");
return text;
};

export const CopyInput = ({ ...props }: InputProps) => {
return (
<div className="flex w-full items-center space-x-2">
<Input {...props} />
<Button
type="button"
variant={"secondary"}
onClick={() => copyToClipboard(props.value?.toString())}
>
<Clipboard className="size-4 text-muted-foreground" />
</Button>
</div>
);
};