feat: add audit log clear feature

This commit is contained in:
moonrailgun 2024-11-10 06:08:22 +08:00
parent 843a581d42
commit 3bf86b3e6e
2 changed files with 61 additions and 5 deletions

View File

@ -5,8 +5,12 @@ import { CommonWrapper } from '@/components/CommonWrapper';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Empty, List } from 'antd';
import { useMemo, useRef } from 'react';
import { trpc } from '../../api/trpc';
import { useCurrentWorkspaceId } from '../../store/user';
import {
defaultErrorHandler,
defaultSuccessHandler,
trpc,
} from '../../api/trpc';
import { useCurrentWorkspaceId, useHasAdminPermission } from '../../store/user';
import { CommonHeader } from '@/components/CommonHeader';
import { last } from 'lodash-es';
import { useVirtualizer } from '@tanstack/react-virtual';
@ -14,6 +18,9 @@ import { useWatch } from '@/hooks/useWatch';
import dayjs from 'dayjs';
import { ColorTag } from '@/components/ColorTag';
import { SimpleVirtualList } from '@/components/SimpleVirtualList';
import { Button } from '@/components/ui/button';
import { LuTrash2 } from 'react-icons/lu';
import { AlertConfirm } from '@/components/AlertConfirm';
export const Route = createFileRoute('/settings/auditLog')({
beforeLoad: routeAuthBeforeLoad,
@ -24,8 +31,9 @@ function PageComponent() {
const { t } = useTranslation();
const workspaceId = useCurrentWorkspaceId();
const parentRef = useRef<HTMLDivElement>(null);
const hasAdminPermission = useHasAdminPermission();
const { data, hasNextPage, fetchNextPage, isFetchingNextPage } =
const { data, hasNextPage, fetchNextPage, isFetchingNextPage, refetch } =
trpc.auditLog.fetchByCursor.useInfiniteQuery(
{
workspaceId,
@ -35,6 +43,11 @@ function PageComponent() {
}
);
const clearMutation = trpc.auditLog.clear.useMutation({
onSuccess: defaultSuccessHandler,
onError: defaultErrorHandler,
});
const allData = useMemo(() => {
if (!data) {
return [];
@ -69,7 +82,27 @@ function PageComponent() {
});
return (
<CommonWrapper header={<CommonHeader title={t('Audit Log')} />}>
<CommonWrapper
header={
<CommonHeader
title={t('Audit Log')}
actions={
<>
{hasAdminPermission && (
<AlertConfirm
onConfirm={() => {
clearMutation.mutateAsync({ workspaceId });
refetch();
}}
>
<Button variant="outline" size="icon" Icon={LuTrash2} />
</AlertConfirm>
)}
</>
}
/>
}
>
<div className="h-full overflow-hidden p-4">
<SimpleVirtualList
allData={allData}

View File

@ -1,5 +1,10 @@
import { z } from 'zod';
import { OpenApiMetaInfo, router, workspaceProcedure } from '../trpc.js';
import {
OpenApiMetaInfo,
router,
workspaceAdminProcedure,
workspaceProcedure,
} from '../trpc.js';
import { OPENAPI_TAG } from '../../utils/const.js';
import { WorkspaceAuditLogModelSchema } from '../../prisma/zod/index.js';
import { prisma } from '../../model/_client.js';
@ -46,6 +51,24 @@ export const auditLogRouter = router({
nextCursor,
};
}),
clear: workspaceAdminProcedure
.meta(
buildAuditLogOpenapi({
method: 'DELETE',
path: '/clear',
description: 'clear all workspace audit log',
})
)
.output(z.void())
.mutation(async ({ input }) => {
const { workspaceId } = input;
await prisma.workspaceAuditLog.deleteMany({
where: {
workspaceId,
},
});
}),
});
function buildAuditLogOpenapi(meta: OpenApiMetaInfo): OpenApiMeta {