2022-02-20 18:07:12 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2020-03-23 01:15:38 +00:00
|
|
|
import ContentBox from '@/components/elements/ContentBox';
|
|
|
|
import CreateApiKeyForm from '@/components/dashboard/forms/CreateApiKeyForm';
|
2020-03-23 01:25:29 +00:00
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2022-02-20 18:07:12 +00:00
|
|
|
import { faKey } from '@fortawesome/free-solid-svg-icons';
|
2020-03-23 02:10:49 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
2020-07-05 01:46:09 +00:00
|
|
|
import { format } from 'date-fns';
|
2020-04-17 18:17:01 +00:00
|
|
|
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';
|
2022-02-20 18:07:12 +00:00
|
|
|
import { useAPIKeys } from '@/api/account/api-keys';
|
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
|
|
|
import DeleteAPIKeyButton from '@/components/dashboard/security/DeleteAPIKeyButton';
|
2020-03-23 01:15:38 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-02-20 18:07:12 +00:00
|
|
|
const { clearAndAddHttpError } = useFlashKey('account');
|
|
|
|
const { data: keys, isValidating, error } = useAPIKeys({
|
|
|
|
revalidateOnMount: true,
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
});
|
2020-03-23 01:25:29 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-20 18:07:12 +00:00
|
|
|
clearAndAddHttpError(error);
|
|
|
|
}, [ error ]);
|
2020-03-23 02:10:49 +00:00
|
|
|
|
2020-03-23 01:15:38 +00:00
|
|
|
return (
|
2020-09-08 03:26:18 +00:00
|
|
|
<PageContentBlock title={'Account API'}>
|
2020-10-11 19:29:00 +00:00
|
|
|
<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-02-20 18:07:12 +00:00
|
|
|
<CreateApiKeyForm/>
|
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`}>
|
2022-02-20 18:07:12 +00:00
|
|
|
<SpinnerOverlay visible={!keys && isValidating}/>
|
2020-03-28 23:06:36 +00:00
|
|
|
{
|
2022-02-20 18:07:12 +00:00
|
|
|
!keys || !keys.length ?
|
2020-07-04 16:28:03 +00:00
|
|
|
<p css={tw`text-center text-sm`}>
|
2022-02-20 18:07:12 +00:00
|
|
|
{!keys ? 'Loading...' : 'No API keys exist for this account.'}
|
2020-03-28 23:06:36 +00:00
|
|
|
</p>
|
|
|
|
:
|
2020-07-04 16:28:03 +00:00
|
|
|
keys.map((key, index) => (
|
|
|
|
<GreyRowBox
|
2020-03-28 23:06:36 +00:00
|
|
|
key={key.identifier}
|
2020-07-04 16:28:03 +00:00
|
|
|
css={[ tw`bg-neutral-600 flex items-center`, index > 0 && tw`mt-2` ]}
|
2020-03-28 23:06:36 +00:00
|
|
|
>
|
2020-07-04 16:28:03 +00:00
|
|
|
<FontAwesomeIcon icon={faKey} css={tw`text-neutral-300`}/>
|
2020-10-11 19:29:00 +00:00
|
|
|
<div css={tw`ml-4 flex-1 overflow-hidden`}>
|
|
|
|
<p css={tw`text-sm break-words`}>{key.description}</p>
|
2020-07-04 16:28:03 +00:00
|
|
|
<p css={tw`text-2xs text-neutral-300 uppercase`}>
|
|
|
|
Last used:
|
|
|
|
{key.lastUsedAt ? format(key.lastUsedAt, 'MMM do, yyyy HH:mm') : 'Never'}
|
2020-03-28 23:06:36 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
2020-10-11 19:29:00 +00:00
|
|
|
<p css={tw`text-sm ml-4 hidden md:block`}>
|
2020-07-04 16:28:03 +00:00
|
|
|
<code css={tw`font-mono py-1 px-2 bg-neutral-900 rounded`}>
|
2020-03-28 23:06:36 +00:00
|
|
|
{key.identifier}
|
|
|
|
</code>
|
2020-03-23 02:10:49 +00:00
|
|
|
</p>
|
2022-02-20 18:07:12 +00:00
|
|
|
<DeleteAPIKeyButton identifier={key.identifier}/>
|
2020-07-04 16:28:03 +00:00
|
|
|
</GreyRowBox>
|
2020-03-28 23:06:36 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
</ContentBox>
|
2020-10-11 19:29:00 +00:00
|
|
|
</div>
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2020-03-23 01:15:38 +00:00
|
|
|
);
|
|
|
|
};
|