import { useEffect, useState } from 'react';
import useSWR from 'swr';
import useUser from '../../lib/hooks/useUser';
import conditionalClass from '../../lib/utils/conditional-class';
import { Button } from '../common/Button/Button';
import { Container } from '../common/Container/Container';
import { Header } from '../common/Header/Header';
import { Paginator } from '../common/Paginator/Paginator';
import styles from './AuditPage.module.scss';
export const AuditLogDetail = ({ entry }: { entry: any }) => {
const [revealed, setRevealed] = useState(false);
return (
From IP: {entry.actor_ip}
{entry.location && (
Approx. location: {entry.location.city}, {entry.location.country} (
{entry.location.ll.join(', ')})
)}
{entry.actor_ua && (
User Agent: {entry.actor_ua}
)}
{entry.user_agent && (
Browser: {entry.user_agent.browser} {entry.user_agent.version}
{' on '}
{entry.user_agent.platform} ({entry.user_agent.os})
)}
{entry.content && (
Log details:{' '}
{revealed ? (
entry.content
) : (
)}
)}
);
};
export const AuditLog = ({ entry }: { entry: any }) => {
const [expanded, setExpanded] = useState(false);
return (
{entry.action}
{new Date(entry.created_at).toString()}
{entry.actor?.username}
{expanded &&
}
);
};
export const AuditLogs = ({}) => {
const [searchParams, setSearchParams] = useState(new URLSearchParams({}));
const [types, setTypes] = useState([]);
const [pageIndex, setPageIndex] = useState(1);
const { data } = useSWR(
`/api/admin/audit?page=${pageIndex}&pageSize=26&${searchParams.toString()}`
);
const filter = useSWR('/api/admin/audit/filter');
useEffect(() => {
if (!filter?.data || types.length) {
return;
}
setTypes(
Array.from(
{ length: filter.data.length },
(_, k) => k > 0
)
);
}, [types, filter.data]);
useEffect(() => {
if (!filter?.data || !types.length) {
return;
}
const urlparams = new URLSearchParams();
urlparams.set(
'actions',
filter.data
.reduce((array, current, index) => {
return [...array, types[index] ? current : ''];
}, [])
.filter((item) => item)
.join(',')
);
setSearchParams(urlparams);
}, [filter.data, types]);
if (!data || !filter.data || !types.length) {
return <>>;
}
return (
{filter.data?.map((item: string, index: number) => (
{
const newTypes = types.slice();
newTypes[index] = e.target.checked;
setTypes(newTypes);
}}
/>{' '}
))}
Action
Time
Actor
Actions
{data.list.map((entry: any) => (
))}
);
};
export const AuditPage = () => {
const { user } = useUser({ redirectTo: '/login' });
return (
<>
Audit logs
>
);
};