2022-11-25 20:25:03 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-06-12 19:16:48 +00:00
|
|
|
import { useActivityLogs } from '@/api/server/activity';
|
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
|
|
|
import { useFlashKey } from '@/plugins/useFlash';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
|
|
|
import ActivityLogEntry from '@/components/elements/activity/ActivityLogEntry';
|
|
|
|
import PaginationFooter from '@/components/elements/table/PaginationFooter';
|
|
|
|
import { ActivityLogFilters } from '@/api/account/activity';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import classNames from 'classnames';
|
2022-06-12 19:17:14 +00:00
|
|
|
import { styles as btnStyles } from '@/components/elements/button/index';
|
2022-06-12 19:16:48 +00:00
|
|
|
import { XCircleIcon } from '@heroicons/react/solid';
|
2022-06-20 17:19:40 +00:00
|
|
|
import useLocationHash from '@/plugins/useLocationHash';
|
2022-06-12 19:16:48 +00:00
|
|
|
|
|
|
|
export default () => {
|
2022-06-20 17:19:40 +00:00
|
|
|
const { hash } = useLocationHash();
|
2022-06-12 19:16:48 +00:00
|
|
|
const { clearAndAddHttpError } = useFlashKey('server:activity');
|
2022-06-26 19:13:52 +00:00
|
|
|
const [filters, setFilters] = useState<ActivityLogFilters>({ page: 1, sorts: { timestamp: -1 } });
|
2022-06-12 19:16:48 +00:00
|
|
|
|
|
|
|
const { data, isValidating, error } = useActivityLogs(filters, {
|
|
|
|
revalidateOnMount: true,
|
|
|
|
revalidateOnFocus: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-25 20:25:03 +00:00
|
|
|
setFilters(value => ({ ...value, filters: { ip: hash.ip, event: hash.event } }));
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [hash]);
|
2022-06-12 19:16:48 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
clearAndAddHttpError(error);
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [error]);
|
2022-06-12 19:16:48 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ServerContentBlock title={'Activity Log'}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender byKey={'server:activity'} />
|
|
|
|
{(filters.filters?.event || filters.filters?.ip) && (
|
2022-06-12 19:16:48 +00:00
|
|
|
<div className={'flex justify-end mb-2'}>
|
|
|
|
<Link
|
|
|
|
to={'#'}
|
|
|
|
className={classNames(btnStyles.button, btnStyles.text, 'w-full sm:w-auto')}
|
2022-11-25 20:25:03 +00:00
|
|
|
onClick={() => setFilters(value => ({ ...value, filters: {} }))}
|
2022-06-12 19:16:48 +00:00
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
Clear Filters <XCircleIcon className={'w-4 h-4 ml-2'} />
|
2022-06-12 19:16:48 +00:00
|
|
|
</Link>
|
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
)}
|
|
|
|
{!data && isValidating ? (
|
|
|
|
<Spinner centered />
|
|
|
|
) : !data?.items.length ? (
|
|
|
|
<p className={'text-sm text-center text-gray-400'}>No activity logs available for this server.</p>
|
|
|
|
) : (
|
|
|
|
<div className={'bg-gray-700'}>
|
2022-11-25 20:25:03 +00:00
|
|
|
{data?.items.map(activity => (
|
2022-07-10 18:53:29 +00:00
|
|
|
<ActivityLogEntry key={activity.id} activity={activity}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<span />
|
|
|
|
</ActivityLogEntry>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{data && (
|
|
|
|
<PaginationFooter
|
|
|
|
pagination={data.pagination}
|
2022-11-25 20:25:03 +00:00
|
|
|
onPageSelect={page => setFilters(value => ({ ...value, page }))}
|
2022-06-26 19:13:52 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-06-12 19:16:48 +00:00
|
|
|
</ServerContentBlock>
|
|
|
|
);
|
|
|
|
};
|