import dayjs from "dayjs"; import cn from "classnames"; import { useCallback, useState } from "react"; import { Alert } from "../Alert"; import { Button } from "../Button"; import { AddSkylinkToAPIKeyForm } from "../forms/AddSkylinkToAPIKeyForm"; import { CogIcon, TrashIcon } from "../Icons"; import { Modal } from "../Modal"; import { useAPIKeyEdit } from "./useAPIKeyEdit"; import { useAPIKeyRemoval } from "./useAPIKeyRemoval"; export const APIKey = ({ apiKey, onRemoved, onEdited, onRemovalError }) => { const { id, name, createdAt, skylinks } = apiKey; const isPublic = apiKey.public === "true"; const [error, setError] = useState(null); const onSkylinkListEdited = useCallback(() => { setError(null); onEdited(); }, [onEdited]); const onSkylinkListEditFailure = (errorMessage) => setError(errorMessage); const { removalError, removalInitiated, prompt: promptRemoval, abort: abortRemoval, confirm: confirmRemoval, } = useAPIKeyRemoval({ key: apiKey, onSuccess: onRemoved, onFailure: onRemovalError, }); const { editInitiated, prompt: promptEdit, abort: abortEdit, addSkylink, removeSkylink, } = useAPIKeyEdit({ key: apiKey, onSkylinkListUpdate: onSkylinkListEdited, onSkylinkListUpdateFailure: onSkylinkListEditFailure, }); const closeEditModal = useCallback(() => { setError(null); abortEdit(); }, [abortEdit]); const skylinksNumber = skylinks?.length ?? 0; const isNotConfigured = isPublic && skylinksNumber === 0; const skylinksPhrasePrefix = skylinksNumber === 0 ? "No" : skylinksNumber; const skylinksPhrase = `${skylinksPhrasePrefix} ${skylinksNumber === 1 ? "skylink" : "skylinks"} configured`; return (
Are you sure you want to delete the following API key?
{name || id}
{skylink}