import React, { useEffect, useState } from 'react'; import ContentBox from '@/components/elements/ContentBox'; import CreateApiKeyForm from '@/components/dashboard/forms/CreateApiKeyForm'; import getApiKeys, { ApiKey } from '@/api/account/getApiKeys'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faKey, faTrashAlt } from '@fortawesome/free-solid-svg-icons'; import deleteApiKey from '@/api/account/deleteApiKey'; import FlashMessageRender from '@/components/FlashMessageRender'; import { format } from 'date-fns'; import PageContentBlock from '@/components/elements/PageContentBlock'; import tw from 'twin.macro'; import GreyRowBox from '@/components/elements/GreyRowBox'; import { Dialog } from '@/components/elements/dialog'; import { useFlashKey } from '@/plugins/useFlash'; import Code from '@/components/elements/Code'; import BeforeContent from '@/blueprint/components/Account/API/BeforeContent'; import AfterContent from '@/blueprint/components/Account/API/AfterContent'; export default () => { const [deleteIdentifier, setDeleteIdentifier] = useState(''); const [keys, setKeys] = useState([]); const [loading, setLoading] = useState(true); const { clearAndAddHttpError } = useFlashKey('account'); useEffect(() => { getApiKeys() .then((keys) => setKeys(keys)) .then(() => setLoading(false)) .catch((error) => clearAndAddHttpError(error)); }, []); const doDeletion = (identifier: string) => { setLoading(true); clearAndAddHttpError(); deleteApiKey(identifier) .then(() => setKeys((s) => [...(s || []).filter((key) => key.identifier !== identifier)])) .catch((error) => clearAndAddHttpError(error)) .then(() => { setLoading(false); setDeleteIdentifier(''); }); }; return (
setKeys((s) => [...s!, key])} /> setDeleteIdentifier('')} onConfirmed={() => doDeletion(deleteIdentifier)} > All requests using the {deleteIdentifier} key will be invalidated. {keys.length === 0 ? (

{loading ? 'Loading...' : 'No API keys exist for this account.'}

) : ( keys.map((key, index) => ( 0 && tw`mt-2`]} >

{key.description}

Last used:  {key.lastUsedAt ? format(key.lastUsedAt, 'MMM do, yyyy HH:mm') : 'Never'}

)) )}
); };