// "Neuer API-Schluessel" as a popup on the shared FormDialog chrome, plus the
// one-time reveal that follows a successful create.
//
// The create route used to stack two OpenModal overlays with the boilerplate
// FormGroup/InputText/InputDate controls inside — a different popup shape from
// every other form in the app. Both steps now wear the same header/body/footer
// bar the task, role, group and member popups wear.
//
// Presentational: the route owns the submit and the navigation back to the list.
// The admin API-key routes keep the shared ApiKeyForm for now.
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherCheck, FeatherCopy, FeatherKey } from "@subframe/core";
import { Alert } from "~/ui/components/Alert";
import { Button } from "~/ui/components/Button";
import { Checkbox } from "~/ui/components/Checkbox";
import { TextInput } from "~/ui/components/TextInput";
import { useCopyToast } from "~/hooks/use-copy-toast";
import { DateField } from "../tasks/DateField";
import { FormDialog } from "../shared/FormDialog";
export type ApiKeyNewInput = {
/** Free-text name the list shows next to the redacted key. */
alias: string;
/** ISO date (yyyy-mm-dd) or "" for a key that does not expire. */
expires: string;
active: boolean;
};
export function ApiKeyNewDialog({
open,
onOpenChange,
submitting,
errorMessage,
onCreate,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
submitting: boolean;
/** Server-side rejection (e.g. the alias is already taken). */
errorMessage: string | null;
onCreate: (input: ApiKeyNewInput) => void;
}): React.JSX.Element {
const { t } = useTranslation();
const [alias, setAlias] = useState("");
const [expires, setExpires] = useState("");
const [active, setActive] = useState(true);
const canSubmit = alias.trim().length > 0 && !submitting;
return (
}
loading={submitting}
disabled={!canSubmit}
onClick={() => onCreate({ alias: alias.trim(), expires, active })}
data-ui-id="ui.settings.apiKeys.newDialog.submit"
>
{t("shared.create")}
>
}
>
{errorMessage ? : null}
setAlias(event.target.value)}
autoFocus
autoComplete="off"
required
data-ui-id="ui.settings.apiKeys.newDialog.alias"
/>
{t("models.apiKey.expiresHint")}
setActive(checked)} />
);
}
/**
* One-time reveal. The raw key exists only in this response, so the dialog does
* not offer a way to dismiss it without the user having seen the copy button.
*/
export function ApiKeyCreatedDialog({
apiKey,
onClose,
action = "created",
}: {
apiKey: { key: string; alias: string };
onClose: () => void;
/** A rotated key replaces a live one, so it says so. */
action?: "created" | "rotated";
}): React.JSX.Element {
const { t } = useTranslation();
const copyText = useCopyToast();
const [copied, setCopied] = useState(false);
async function copyApiKey() {
const didCopy = await copyText(apiKey.key, { successKey: "models.apiKey.copied", errorKey: "models.apiKey.copyFailed" });
if (didCopy) setCopied(true);
}
return (
{
if (!next) onClose();
}}
title={t(action === "rotated" ? "models.apiKey.rotatedTitle" : "models.apiKey.createdTitle")}
subtitle={t("models.apiKey.aliasWithValue", { alias: apiKey.alias })}
uiId="ui.settings.apiKeys.createdDialog"
footer={
<>
: }
onClick={copyApiKey}
data-ui-id="ui.settings.apiKeys.createdDialog.copy"
>
{copied ? t("shared.copied") : t("shared.copy")}
>
}
>
);
}