"use client" import { zodResolver } from "@hookform/resolvers/zod" import type { Organization } from "better-auth/plugins/organization" import { Loader2 } from "lucide-react" import { type ComponentProps, useContext } from "react" import { useForm } from "react-hook-form" import * as z from "zod" import { useLang } from "../../../hooks/use-lang" import { AuthUIContext } from "../../../lib/auth-ui-provider" import { cn, getLocalizedError } from "../../../lib/utils" import type { AuthLocalization } from "../../../localization/auth-localization" import type { Refetch } from "../../../types/refetch" import { OrganizationCellView } from "../../organization/organization-cell-view" import { PersonalAccountView } from "../../organization/personal-account-view" import { Button } from "../../ui/button" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "../../ui/dialog" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "../../ui/form" import { Input } from "../../ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../ui/select" import type { SettingsCardClassNames } from "../shared/settings-card" interface CreateApiKeyDialogProps extends ComponentProps { classNames?: SettingsCardClassNames localization?: AuthLocalization onSuccess: (key: string) => void refetch?: Refetch organizationId?: string } export function CreateApiKeyDialog({ classNames, localization, onSuccess, refetch, organizationId, onOpenChange, ...props }: CreateApiKeyDialogProps) { const { authClient, apiKey, hooks: { useListOrganizations, useSession }, localization: contextLocalization, organization: contextOrganization, toast, localizeErrors } = useContext(AuthUIContext) localization = { ...contextLocalization, ...localization } const { lang } = useLang() let organizations: Organization[] | null | undefined if (contextOrganization) { const { data } = useListOrganizations() organizations = data } const { data: sessionData } = useSession() const user = sessionData?.user const showOrganizationSelect = contextOrganization?.apiKey const formSchema = z.object({ name: z .string() .min(1, `${localization.NAME} ${localization.IS_REQUIRED}`), expiresInDays: z.string().optional(), organizationId: showOrganizationSelect ? z .string() .min( 1, `${localization.ORGANIZATION} ${localization.IS_REQUIRED}` ) : z.string().optional() }) const form = useForm({ resolver: zodResolver(formSchema), values: { name: "", expiresInDays: "none", organizationId: organizationId ?? "personal" } }) const { isSubmitting } = form.formState const onSubmit = async (values: z.infer) => { try { const expiresIn = values.expiresInDays && values.expiresInDays !== "none" ? Number.parseInt(values.expiresInDays) * 60 * 60 * 24 : undefined const selectedOrgId = values.organizationId === "personal" ? undefined : values.organizationId const metadata = { ...(typeof apiKey === "object" ? apiKey.metadata : {}), ...(contextOrganization && selectedOrgId ? { organizationId: selectedOrgId } : {}) } const result = await authClient.apiKey.create({ name: values.name, expiresIn, prefix: typeof apiKey === "object" ? apiKey.prefix : undefined, metadata: Object.keys(metadata).length > 0 ? metadata : undefined, fetchOptions: { throw: true } }) await refetch?.() onSuccess(result.key) onOpenChange?.(false) form.reset() } catch (error) { toast({ variant: "error", message: getLocalizedError({ error, localization, localizeErrors }) }) } } const rtf = new Intl.RelativeTimeFormat(lang ?? "en") return ( e.preventDefault()} className={classNames?.dialog?.content} > {localization.CREATE_API_KEY} {localization.CREATE_API_KEY_DESCRIPTION}
{showOrganizationSelect && ( ( {localization.ORGANIZATION} )} /> )}
( {localization.NAME} )} /> ( {localization.EXPIRES} )} />
) }