"use client"; import { SettingsContainer, SettingsSection, SettingsSectionBody, SettingsSectionDescription, SettingsSectionForm, SettingsSectionHeader, SettingsSectionTitle } from "@app/components/Settings"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import HeaderTitle from "@app/components/SettingsSectionTitle"; import { z } from "zod"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { Input } from "@app/components/ui/input"; import { InfoIcon } from "lucide-react"; import { Button } from "@app/components/ui/button"; import { Checkbox, CheckboxWithLabel } from "@app/components/ui/checkbox"; import { Alert, AlertDescription, AlertTitle } from "@app/components/ui/alert"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { toast } from "@app/hooks/useToast"; import { AxiosResponse } from "axios"; import { useParams, useRouter } from "next/navigation"; import { Breadcrumb, BreadcrumbItem, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator } from "@app/components/ui/breadcrumb"; import Link from "next/link"; import { CreateOrgApiKeyBody, CreateOrgApiKeyResponse } from "@server/routers/apiKeys"; import { InfoSection, InfoSectionContent, InfoSections, InfoSectionTitle } from "@app/components/InfoSection"; import CopyToClipboard from "@app/components/CopyToClipboard"; import moment from "moment"; import CopyTextBox from "@app/components/CopyTextBox"; import PermissionsSelectBox from "@app/components/PermissionsSelectBox"; const createFormSchema = z.object({ name: z .string() .min(2, { message: "Name must be at least 2 characters." }) .max(255, { message: "Name must not be longer than 255 characters." }) }); type CreateFormValues = z.infer; const copiedFormSchema = z .object({ copied: z.boolean() }) .refine( (data) => { return data.copied; }, { message: "You must confirm that you have copied the API key.", path: ["copied"] } ); type CopiedFormValues = z.infer; export default function Page() { const { env } = useEnvContext(); const api = createApiClient({ env }); const router = useRouter(); const [loadingPage, setLoadingPage] = useState(true); const [createLoading, setCreateLoading] = useState(false); const [apiKey, setApiKey] = useState(null); const [selectedPermissions, setSelectedPermissions] = useState< Record >({}); const form = useForm({ resolver: zodResolver(createFormSchema), defaultValues: { name: "" } }); const copiedForm = useForm({ resolver: zodResolver(copiedFormSchema), defaultValues: { copied: false } }); async function onSubmit(data: CreateFormValues) { setCreateLoading(true); let payload: CreateOrgApiKeyBody = { name: data.name }; const res = await api .put>(`/api-key`, payload) .catch((e) => { toast({ variant: "destructive", title: "Error creating API key", description: formatAxiosError(e) }); }); if (res && res.status === 201) { const data = res.data.data; console.log({ actionIds: Object.keys(selectedPermissions).filter( (key) => selectedPermissions[key] ) }); const actionsRes = await api .post(`/api-key/${data.apiKeyId}/actions`, { actionIds: Object.keys(selectedPermissions).filter( (key) => selectedPermissions[key] ) }) .catch((e) => { console.error("Error setting permissions", e); toast({ variant: "destructive", title: "Error setting permissions", description: formatAxiosError(e) }); }); if (actionsRes) { setApiKey(data); } } setCreateLoading(false); } async function onCopiedSubmit(data: CopiedFormValues) { if (!data.copied) { return; } router.push(`/admin/api-keys`); } useEffect(() => { const load = async () => { setLoadingPage(false); }; load(); }, []); return ( <>
{!loadingPage && (
{!apiKey && ( <> API Key Information
( Name )} />
Permissions Determine what this API key can do )} {apiKey && ( Your API Key Name Created {moment( apiKey.createdAt ).format("lll")} Save Your API Key You will only be able to see this once. Make sure to copy it to a secure place.

Your API key is:

(
{ copiedForm.setValue( "copied", e as boolean ); }} />
)} />
)}
{!apiKey && ( )} {!apiKey && ( )} {apiKey && ( )}
)} ); }