'use client'; //=========================================== // THIS FILE IS AUTO-GENERATED FROM TEMPLATE. DO NOT EDIT IT DIRECTLY UNLESS YOU ALSO EDIT THE CORRESPONDING FILE IN packages/template //=========================================== import { yupResolver } from "@hookform/resolvers/yup"; import { yupObject, yupString } from '@hexclave/shared/dist/schema-fields'; import { captureError } from '@hexclave/shared/dist/utils/errors'; import { runAsynchronously } from '@hexclave/shared/dist/utils/promises'; import { ActionDialog, Button, CopyField, Input, Label, Typography } from '@hexclave/ui'; import { useState } from "react"; import { useForm } from 'react-hook-form'; import * as yup from "yup"; import { useUser } from '..'; import { FormWarningText } from '../components/elements/form-warning'; import { ApiKey, ApiKeyCreationOptions, ApiKeyType } from "../lib/hexclave-app/api-keys"; import { useTranslation } from "../lib/translations"; // Constants for expiration options export const neverInMs = 1000 * 60 * 60 * 24 * 365 * 200; export const expiresInOptions = { [1000 * 60 * 60 * 24 * 1]: "1 day", [1000 * 60 * 60 * 24 * 7]: "7 days", [1000 * 60 * 60 * 24 * 30]: "30 days", [1000 * 60 * 60 * 24 * 90]: "90 days", [1000 * 60 * 60 * 24 * 365]: "1 year", [neverInMs]: "Never", } as const; /** * Dialog for creating a new API key */ export function CreateApiKeyDialog(props: { open: boolean, onOpenChange: (open: boolean) => void, onKeyCreated?: (key: ApiKey) => void, createApiKey: (data: ApiKeyCreationOptions) => Promise>, mockMode?: boolean, }) { const { t } = useTranslation(); const user = useUser({ or: props.mockMode ? 'return-null' : 'redirect' }); const [loading, setLoading] = useState(false); const apiKeySchema = yupObject({ description: yupString().defined().nonEmpty(t('Description is required')), expiresIn: yupString().defined(), }); const { register, handleSubmit, formState: { errors }, reset } = useForm({ resolver: yupResolver(apiKeySchema), defaultValues: { description: '', expiresIn: Object.keys(expiresInOptions)[2], // Default to 30 days } }); const onSubmit = async (data: yup.InferType) => { setLoading(true); try { const expiresAt = new Date(Date.now() + parseInt(data.expiresIn)); const apiKey = await props.createApiKey({ description: data.description, expiresAt, }); if (props.onKeyCreated) { props.onKeyCreated(apiKey); } reset(); props.onOpenChange(false); } catch (error) { captureError("Failed to create API key", { error }); } finally { setLoading(false); } }; return (
{ e.preventDefault(); runAsynchronously(handleSubmit(onSubmit)); }} className="space-y-4" >
{errors.description && }
{errors.expiresIn && }
); } /** * Dialog for showing the newly created API key */ export function ShowApiKeyDialog(props: { apiKey: ApiKey | null, onClose?: () => void, }) { const { t } = useTranslation(); return (
{t("Here is your API key.")}{" "} {t("Copy it to a safe place. You will not be able to view it again.")}
); }