import dayjs from "dayjs"; 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, removeSkylink, } = useAPIKeyEdit({ key: apiKey, onSkylinkRemoved: onSkylinkListEdited, onSkylinkRemovalFailure: onSkylinkListEditFailure, }); const closeEditModal = useCallback(() => { setError(null); abortEdit(); }, [abortEdit]); return (
  • {name || id} {dayjs(createdAt).format("MMM DD, YYYY")} {isPublic && ( )} {removalInitiated && (

    Delete API key

    Are you sure you want to delete the following API key?

    {name || id}

    {removalError && {removalError}}
    )} {editInitiated && (

    Covered Skylinks

    {skylinks?.length > 0 ? (
      {skylinks.map((skylink) => (
    • {skylink}
    • ))}
    ) : ( No skylinks here yet. You can add the first one below 🙃 )}
    {error && {error}}
    )}
  • ); };