2022-06-05 22:35:53 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2022-06-05 23:23:25 +00:00
|
|
|
import { ActivityLogFilters, useActivityLogs } from '@/api/account/activity';
|
2022-06-05 22:35:53 +00:00
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import PaginationFooter from '@/components/elements/table/PaginationFooter';
|
2022-06-05 23:23:25 +00:00
|
|
|
import { DesktopComputerIcon, XCircleIcon } from '@heroicons/react/solid';
|
|
|
|
import { useLocation } from 'react-router';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import { styles as btnStyles } from '@/components/elements/button/index';
|
|
|
|
import classNames from 'classnames';
|
2022-06-12 19:08:26 +00:00
|
|
|
import ActivityLogEntry from '@/components/elements/activity/ActivityLogEntry';
|
|
|
|
import Tooltip from '@/components/elements/tooltip/Tooltip';
|
2022-06-05 22:35:53 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-06-05 23:23:25 +00:00
|
|
|
const location = useLocation();
|
2022-06-11 18:52:41 +00:00
|
|
|
|
2022-06-05 22:35:53 +00:00
|
|
|
const { clearAndAddHttpError } = useFlashKey('account');
|
2022-06-05 23:23:25 +00:00
|
|
|
const [ filters, setFilters ] = useState<ActivityLogFilters>({ page: 1, sorts: { timestamp: -1 } });
|
|
|
|
const { data, isValidating, error } = useActivityLogs(filters, {
|
2022-06-05 22:35:53 +00:00
|
|
|
revalidateOnMount: true,
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
});
|
|
|
|
|
2022-06-05 23:23:25 +00:00
|
|
|
useEffect(() => {
|
|
|
|
const parsed = new URLSearchParams(location.search);
|
|
|
|
|
|
|
|
setFilters(value => ({ ...value, filters: { ip: parsed.get('ip'), event: parsed.get('event') } }));
|
|
|
|
}, [ location.search ]);
|
|
|
|
|
2022-06-05 22:35:53 +00:00
|
|
|
useEffect(() => {
|
|
|
|
clearAndAddHttpError(error);
|
|
|
|
}, [ error ]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PageContentBlock title={'Account Activity Log'}>
|
|
|
|
<FlashMessageRender byKey={'account'}/>
|
2022-06-05 23:23:25 +00:00
|
|
|
{(filters.filters?.event || filters.filters?.ip) &&
|
|
|
|
<div className={'flex justify-end mb-2'}>
|
|
|
|
<Link
|
|
|
|
to={'#'}
|
2022-06-12 13:09:01 +00:00
|
|
|
className={classNames(btnStyles.button, btnStyles.text, 'w-full sm:w-auto')}
|
2022-06-05 23:23:25 +00:00
|
|
|
onClick={() => setFilters(value => ({ ...value, filters: {} }))}
|
2022-06-05 22:35:53 +00:00
|
|
|
>
|
2022-06-05 23:23:25 +00:00
|
|
|
Clear Filters <XCircleIcon className={'w-4 h-4 ml-2'}/>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{!data && isValidating ?
|
|
|
|
<Spinner centered/>
|
|
|
|
:
|
|
|
|
<div className={'bg-gray-700'}>
|
|
|
|
{data?.items.map((activity) => (
|
2022-06-12 19:08:26 +00:00
|
|
|
<ActivityLogEntry key={activity.timestamp.toString() + activity.event} activity={activity}>
|
|
|
|
{typeof activity.properties.useragent === 'string' &&
|
|
|
|
<Tooltip content={activity.properties.useragent} placement={'top'}>
|
|
|
|
<DesktopComputerIcon className={'ml-2 w-4 h-4 cursor-pointer'}/>
|
|
|
|
</Tooltip>
|
|
|
|
}
|
|
|
|
</ActivityLogEntry>
|
2022-06-05 23:23:25 +00:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
{data && <PaginationFooter
|
|
|
|
pagination={data.pagination}
|
|
|
|
onPageSelect={page => setFilters(value => ({ ...value, page }))}
|
|
|
|
/>}
|
2022-06-05 22:35:53 +00:00
|
|
|
</PageContentBlock>
|
|
|
|
);
|
|
|
|
};
|