misc_pterodactyl-panel/resources/scripts/components/dashboard/AccountApiContainer.tsx

97 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { useEffect, useState } from 'react';
import ContentBox from '@/components/elements/ContentBox';
import CreateApiKeyForm from '@/components/dashboard/forms/CreateApiKeyForm';
2020-03-23 01:25:29 +00:00
import getApiKeys, { ApiKey } from '@/api/account/getApiKeys';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
2020-07-05 01:46:09 +00:00
import { faKey, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
import deleteApiKey from '@/api/account/deleteApiKey';
import FlashMessageRender from '@/components/FlashMessageRender';
2020-07-05 01:46:09 +00:00
import { format } from 'date-fns';
import PageContentBlock from '@/components/elements/PageContentBlock';
2020-07-04 16:28:03 +00:00
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';
export default () => {
const [deleteIdentifier, setDeleteIdentifier] = useState('');
const [keys, setKeys] = useState<ApiKey[]>([]);
const [loading, setLoading] = useState(true);
const { clearAndAddHttpError } = useFlashKey('account');
2020-03-23 01:25:29 +00:00
useEffect(() => {
getApiKeys()
2022-11-25 20:25:03 +00:00
.then(keys => setKeys(keys))
2020-03-23 01:25:29 +00:00
.then(() => setLoading(false))
2022-11-25 20:25:03 +00:00
.catch(error => clearAndAddHttpError(error));
2020-03-23 01:25:29 +00:00
}, []);
const doDeletion = (identifier: string) => {
setLoading(true);
clearAndAddHttpError();
deleteApiKey(identifier)
2022-11-25 20:25:03 +00:00
.then(() => setKeys(s => [...(s || []).filter(key => key.identifier !== identifier)]))
.catch(error => clearAndAddHttpError(error))
.then(() => {
setLoading(false);
setDeleteIdentifier('');
});
};
return (
2020-09-08 03:26:18 +00:00
<PageContentBlock title={'Account API'}>
<FlashMessageRender byKey={'account'} />
2020-12-26 17:50:09 +00:00
<div css={tw`md:flex flex-nowrap my-10`}>
2020-10-11 19:29:00 +00:00
<ContentBox title={'Create API Key'} css={tw`flex-none w-full md:w-1/2`}>
2022-11-25 20:25:03 +00:00
<CreateApiKeyForm onKeyCreated={key => setKeys(s => [...s!, key])} />
2020-03-28 23:06:36 +00:00
</ContentBox>
2020-10-11 19:29:00 +00:00
<ContentBox title={'API Keys'} css={tw`flex-1 overflow-hidden mt-8 md:mt-0 md:ml-8`}>
<SpinnerOverlay visible={loading} />
<Dialog.Confirm
title={'Delete API Key'}
confirm={'Delete Key'}
open={!!deleteIdentifier}
onClose={() => setDeleteIdentifier('')}
onConfirmed={() => doDeletion(deleteIdentifier)}
2020-03-28 23:06:36 +00:00
>
All requests using the <Code>{deleteIdentifier}</Code> key will be invalidated.
</Dialog.Confirm>
{keys.length === 0 ? (
<p css={tw`text-center text-sm`}>
{loading ? 'Loading...' : 'No API keys exist for this account.'}
</p>
) : (
keys.map((key, index) => (
<GreyRowBox
key={key.identifier}
css={[tw`bg-neutral-600 flex items-center`, index > 0 && tw`mt-2`]}
>
<FontAwesomeIcon icon={faKey} css={tw`text-neutral-300`} />
<div css={tw`ml-4 flex-1 overflow-hidden`}>
<p css={tw`text-sm break-words`}>{key.description}</p>
<p css={tw`text-2xs text-neutral-300 uppercase`}>
Last used:&nbsp;
{key.lastUsedAt ? format(key.lastUsedAt, 'MMM do, yyyy HH:mm') : 'Never'}
</p>
</div>
<p css={tw`text-sm ml-4 hidden md:block`}>
<code css={tw`font-mono py-1 px-2 bg-neutral-900 rounded`}>{key.identifier}</code>
</p>
<button css={tw`ml-4 p-2 text-sm`} onClick={() => setDeleteIdentifier(key.identifier)}>
<FontAwesomeIcon
icon={faTrashAlt}
css={tw`text-neutral-400 hover:text-red-400 transition-colors duration-150`}
/>
</button>
</GreyRowBox>
))
)}
2020-03-28 23:06:36 +00:00
</ContentBox>
2020-10-11 19:29:00 +00:00
</div>
</PageContentBlock>
);
};