From 6aacc0c7a86e36c52a3c2c1d26fe58cefcd3bec4 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Mon, 12 Feb 2024 14:52:00 +0000 Subject: feature: Add support for managing API keys --- .../dashboard/settings/components/AddApiKey.tsx | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 packages/web/app/dashboard/settings/components/AddApiKey.tsx (limited to 'packages/web/app/dashboard/settings/components/AddApiKey.tsx') diff --git a/packages/web/app/dashboard/settings/components/AddApiKey.tsx b/packages/web/app/dashboard/settings/components/AddApiKey.tsx new file mode 100644 index 00000000..f4f2894c --- /dev/null +++ b/packages/web/app/dashboard/settings/components/AddApiKey.tsx @@ -0,0 +1,148 @@ +"use client"; + +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; + +import { + Dialog, + DialogClose, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, + DialogTrigger, +} from "@/components/ui/dialog"; +import { z } from "zod"; +import { useRouter } from "next/navigation"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm, SubmitErrorHandler } from "react-hook-form"; +import { toast } from "@/components/ui/use-toast"; +import { api } from "@/lib/trpc"; +import { useState } from "react"; +import { Copy } from "lucide-react"; + +function ApiKeySuccess({ apiKey }: { apiKey: string }) { + return ( +
+
+ Note: please copy the key and store it somewhere safe. Once you close + the dialog, you won't be able to access it again. +
+
+ + +
+
+ ); +} + +function AddApiKeyForm({ onSuccess }: { onSuccess: (key: string) => void }) { + const formSchema = z.object({ + name: z.string(), + }); + const router = useRouter(); + + const form = useForm>({ + resolver: zodResolver(formSchema), + }); + + async function onSubmit(value: z.infer) { + try { + const resp = await api.apiKeys.create.mutate({ name: value.name }); + onSuccess(resp.key); + } catch (e) { + toast({ description: "Something went wrong", variant: "destructive" }); + return; + } + router.refresh(); + } + + const onError: SubmitErrorHandler> = (errors) => { + toast({ + description: Object.values(errors) + .map((v) => v.message) + .join("\n"), + variant: "destructive", + }); + }; + + return ( +
+ + { + return ( + + Name + + + + + Give your API key a unique name + + + + ); + }} + /> + + + + ); +} + +export default function AddApiKey() { + const [key, setKey] = useState(undefined); + const [dialogOpen, setDialogOpen] = useState(false); + return ( + + + + + + + + {key ? "Key was successfully created" : "Create API key"} + + + {key ? ( + + ) : ( + + )} + + + + + + + + + + ); +} -- cgit v1.2.3-70-g09d2