2021-07-17 19:20:03 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-07-17 20:43:15 +00:00
|
|
|
import { format } from 'date-fns';
|
2021-07-17 19:20:03 +00:00
|
|
|
import tw from 'twin.macro';
|
2021-07-17 20:43:15 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2021-08-08 18:52:05 +00:00
|
|
|
import { faFingerprint, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
|
2021-07-17 19:20:03 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import ContentBox from '@/components/elements/ContentBox';
|
|
|
|
import GreyRowBox from '@/components/elements/GreyRowBox';
|
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
|
|
|
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
|
2022-02-13 21:54:12 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
2021-07-17 20:43:15 +00:00
|
|
|
import ConfirmationModal from '@/components/elements/ConfirmationModal';
|
2022-02-13 21:54:12 +00:00
|
|
|
import { useSecurityKeys, deleteSecurityKey } from '@/api/account/security-keys';
|
|
|
|
import AddSecurityKeyForm from '@/components/dashboard/security/AddSecurityKeyForm';
|
2021-07-17 19:20:03 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-02-13 20:44:19 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlashKey('security_keys');
|
2021-07-17 19:20:03 +00:00
|
|
|
|
2021-08-08 18:52:05 +00:00
|
|
|
const [ deleteId, setDeleteId ] = useState<string | null>(null);
|
2022-02-13 20:44:19 +00:00
|
|
|
const { data, mutate, error } = useSecurityKeys({ revalidateOnFocus: false });
|
2021-07-17 20:43:15 +00:00
|
|
|
|
2022-02-13 20:44:19 +00:00
|
|
|
const doDeletion = () => {
|
|
|
|
const uuid = deleteId;
|
2021-07-17 20:43:15 +00:00
|
|
|
|
2022-02-13 20:44:19 +00:00
|
|
|
setDeleteId(null);
|
|
|
|
clearFlashes();
|
|
|
|
mutate(keys => !keys ? undefined : keys.filter(key => key.uuid !== deleteId));
|
2021-07-17 20:43:15 +00:00
|
|
|
|
2022-02-13 20:44:19 +00:00
|
|
|
if (!uuid) return;
|
|
|
|
|
2022-02-13 21:54:12 +00:00
|
|
|
deleteSecurityKey(uuid).catch(error => {
|
2022-02-13 20:44:19 +00:00
|
|
|
clearAndAddHttpError(error);
|
|
|
|
mutate();
|
|
|
|
});
|
2021-07-17 20:43:15 +00:00
|
|
|
};
|
2021-07-17 19:20:03 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-02-13 20:44:19 +00:00
|
|
|
clearAndAddHttpError(error);
|
|
|
|
}, [ error ]);
|
2021-07-17 19:20:03 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<PageContentBlock title={'Security Keys'}>
|
|
|
|
<FlashMessageRender byKey={'security_keys'}/>
|
|
|
|
<div css={tw`md:flex flex-nowrap my-10`}>
|
2021-08-07 23:24:36 +00:00
|
|
|
<ContentBox title={'Add Security Key'} css={tw`flex-1 md:mr-8`}>
|
2022-02-13 20:44:19 +00:00
|
|
|
<AddSecurityKeyForm onKeyAdded={key => mutate((keys) => (keys || []).concat(key))}/>
|
2021-08-07 23:24:36 +00:00
|
|
|
</ContentBox>
|
|
|
|
<ContentBox title={'Security Keys'} css={tw`flex-none w-full mt-8 md:mt-0 md:w-1/2`}>
|
2021-07-17 20:43:15 +00:00
|
|
|
<ConfirmationModal
|
|
|
|
visible={!!deleteId}
|
|
|
|
title={'Confirm key deletion'}
|
2022-02-13 20:44:19 +00:00
|
|
|
buttonText={'Yes, Delete Key'}
|
|
|
|
onConfirmed={doDeletion}
|
2021-07-17 20:43:15 +00:00
|
|
|
onModalDismissed={() => setDeleteId(null)}
|
|
|
|
>
|
2021-07-17 21:45:46 +00:00
|
|
|
Are you sure you wish to delete this security key?
|
|
|
|
You will no longer be able to authenticate using this key.
|
2021-07-17 20:43:15 +00:00
|
|
|
</ConfirmationModal>
|
2022-02-13 20:44:19 +00:00
|
|
|
{!data ?
|
|
|
|
<SpinnerOverlay visible/>
|
|
|
|
:
|
|
|
|
data?.length === 0 ?
|
2021-07-17 19:20:03 +00:00
|
|
|
<p css={tw`text-center text-sm`}>
|
|
|
|
No security keys have been configured for this account.
|
|
|
|
</p>
|
2022-02-13 20:44:19 +00:00
|
|
|
:
|
|
|
|
data.map((key, index) => (
|
|
|
|
<GreyRowBox
|
|
|
|
key={index}
|
|
|
|
css={[ tw`bg-neutral-600 flex items-center`, index > 0 && tw`mt-2` ]}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon icon={faFingerprint} css={tw`text-neutral-300`}/>
|
|
|
|
<div css={tw`ml-4 flex-1 overflow-hidden`}>
|
|
|
|
<p css={tw`text-sm break-words`}>{key.name}</p>
|
|
|
|
<p css={tw`text-2xs text-neutral-300 uppercase`}>
|
|
|
|
Created at:
|
|
|
|
{key.createdAt ? format(key.createdAt, 'MMM do, yyyy HH:mm') : 'Never'}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<button css={tw`ml-4 p-2 text-sm`} onClick={() => setDeleteId(key.uuid)}>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faTrashAlt}
|
|
|
|
css={tw`text-neutral-400 hover:text-red-400 transition-colors duration-150`}
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</GreyRowBox>
|
|
|
|
))
|
2021-07-17 19:20:03 +00:00
|
|
|
}
|
|
|
|
</ContentBox>
|
|
|
|
</div>
|
|
|
|
</PageContentBlock>
|
|
|
|
);
|
|
|
|
};
|