Files
dokploy/apps/dokploy/components/dashboard/settings/ssh-keys/handle-ssh-keys.tsx
T
a104867ed2 Feat/add sidebar (#1084)
* refactor: add sidebar

* chore: add deps

* refactor: update sidebar

* refactor: another layout

* refactor: update variant

* refactor: change layout

* refactor: change variant

* refactor: enhance sidebar navigation with active state management

* feat: add project button to dashboard

* Merge branch 'canary' into feat/add-sidebar

* refactor: add loader

* refactor: update destinations and refactor

* refactor: ui refactor certificates

* refactor: delete unused files

* refactor: remove unused files and duplicate registry

* refactor: update style registry

* refactor: add new design registry

* refactor: enhance git providers

* refactor: remove duplicate files

* refactor: update

* refactor: update users

* refactor: delete unused files

* refactor: update profile

* refactor: apply changes

* refactor: update UI

* refactor: enhance Docker monitoring UI layout

* refactor: add theme toggle and language selection to user navigation (#1083)

* refactor: remove unused files

* feat: add filter to services

* refactor: add active items

* refactor: remove tab prop

* refactor: remove unused files

* refactor: remove duplicated files

* refactor: remove unused files

* refactor: remove duplicate files

* refactor: remove unused files

* refactor: delete unused files

* refactor: remove unsued files

* refactor: delete unused files

* refactor: lint

* refactor: remove unused secuirty

* refactor: delete unused files

* refactor: delete unused files

* remove imports

* refactor: add update button

* refactor: delete unused files

* refactor: remove unused code

* refactor: remove unused files

* refactor: update login page

* refactor: update login UI

* refactor: update ui reset password

* refactor: add justify end

* feat: add suscriptions

* feat: add sheet

* feat: add logs for postgres

* feat: add logs for all databases

* feat: add server logs with drawer logs

* refactor: remove unused files

* refactor: add refetch when closing

* refactor: fix linter

* chore: bump node-20

* revert

* refactor: fix conflicts

* refactor: update

* refactor: add missing deps

* refactor: delete duplicate files

* refactor: delete unsued files

* chore: lint

* refactor: remove unsued file

* refactor: add refetch

* refactor: remove duplicated files

* refactor: delete unused files

* refactor: update setup onboarding

* refactor: add breadcrumb

* refactor: apply updates

* refactor: add faker

* refactor: use 0 in validation

* refactor: show correct state

* refactor: update

---------

Co-authored-by: vishalkadam47 <[email protected]>
Co-authored-by: Vishal kadam <[email protected]>
2025-01-12 14:29:43 -06:00

259 lines
6.0 KiB
TypeScript

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 {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { sshKeyCreate, type sshKeyType } from "@/server/db/validations";
import { api } from "@/utils/api";
import { zodResolver } from "@hookform/resolvers/zod";
import { PenBoxIcon, PlusIcon } from "lucide-react";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import type { z } from "zod";
type SSHKey = z.infer<typeof sshKeyCreate>;
interface Props {
sshKeyId?: string;
}
export const HandleSSHKeys = ({ sshKeyId }: Props) => {
const utils = api.useUtils();
const [isOpen, setIsOpen] = useState(false);
const { data } = api.sshKey.one.useQuery(
{
sshKeyId: sshKeyId || "",
},
{
enabled: !!sshKeyId,
},
);
const { mutateAsync, isError, error, isLoading } = sshKeyId
? api.sshKey.update.useMutation()
: api.sshKey.create.useMutation();
const generateMutation = api.sshKey.generate.useMutation();
const form = useForm<SSHKey>({
resolver: zodResolver(sshKeyCreate),
defaultValues: {
name: "",
description: "",
publicKey: "",
privateKey: "",
},
});
useEffect(() => {
if (data) {
form.reset({
...data,
description: data.description || undefined,
});
} else {
form.reset();
}
}, [data, form, form.reset]);
const onSubmit = async (data: SSHKey) => {
await mutateAsync({
...data,
sshKeyId: sshKeyId || "",
})
.then(async () => {
toast.success(
sshKeyId
? "SSH key updated successfully"
: "SSH key created successfully",
);
await utils.sshKey.all.invalidate();
form.reset();
setIsOpen(false);
})
.catch(() => {
toast.error(
sshKeyId
? "Error updating the SSH key"
: "Error creating the SSH key",
);
});
};
const onGenerateSSHKey = (type: z.infer<typeof sshKeyType>) =>
generateMutation
.mutateAsync(type)
.then(async (data) => {
toast.success("SSH Key Generated");
form.setValue("privateKey", data.privateKey);
form.setValue("publicKey", data.publicKey);
})
.catch(() => {
toast.error("Error generating the SSH Key");
});
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger className="" asChild>
{sshKeyId ? (
<Button
variant="ghost"
size="icon"
className="group hover:bg-blue-500/10 "
>
<PenBoxIcon className="size-3.5 text-primary group-hover:text-blue-500" />
</Button>
) : (
<Button className="cursor-pointer space-x-3">
<PlusIcon className="h-4 w-4" />
Add SSH Key
</Button>
)}
{/* {children} */}
</DialogTrigger>
<DialogContent className="max-h-screen overflow-y-auto sm:max-w-2xl">
<DialogHeader>
<DialogTitle>SSH Key</DialogTitle>
<DialogDescription className="space-y-4">
<div>
In this section you can add one of your keys or generate a new
one.
</div>
{!sshKeyId && (
<div className="flex gap-4">
<Button
variant={"secondary"}
disabled={generateMutation.isLoading}
className="max-sm:w-full"
onClick={() =>
onGenerateSSHKey({
type: "rsa",
})
}
type="button"
>
Generate RSA SSH Key
</Button>
<Button
variant={"secondary"}
disabled={generateMutation.isLoading}
className="max-sm:w-full"
onClick={() =>
onGenerateSSHKey({
type: "ed25519",
})
}
type="button"
>
Generate ED25519 SSH Key
</Button>
</div>
)}
</DialogDescription>
</DialogHeader>
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
<Form {...form}>
<form
className="grid w-full gap-4 "
onSubmit={form.handleSubmit(onSubmit)}
>
<FormField
control={form.control}
name="name"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder={"Personal projects"} {...field} />
</FormControl>
<FormMessage />
</FormItem>
);
}}
/>
<FormField
control={form.control}
name="description"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Description</FormLabel>
<FormControl>
<Input
placeholder={"Used on my personal Hetzner VPS"}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
);
}}
/>
<FormField
control={form.control}
name="privateKey"
render={({ field }) => (
<FormItem>
<div className="space-y-0.5">
<FormLabel>Private Key</FormLabel>
</div>
<FormControl>
<Textarea
placeholder={"-----BEGIN RSA PRIVATE KEY-----"}
rows={5}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="publicKey"
render={({ field }) => (
<FormItem>
<div className="space-y-0.5">
<FormLabel>Public Key</FormLabel>
</div>
<FormControl>
<Input placeholder={"ssh-rsa AAAAB3NzaC1yc2E"} {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<DialogFooter>
<Button isLoading={isLoading} type="submit">
{sshKeyId ? "Update" : "Create"}
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
};