inogen/app/admin/pages/AuditPage.tsx

25 lines
4.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Filter } from "lucide-react";
import { useEffect, useState, type FormEvent } from "react";
import { Button, Card, EmptyState, ErrorBanner, Field, Input, PageTitle, Select } from "../components/Ui";
import { api } from "../lib/api";
import { persianToGregorianIso } from "../lib/persianDate";
import type { AdminUser, AuditPage as AuditResult } from "../types";
import { PaginationControls } from "../components/ListControls";
const actionLabels:Record<string,string>={Added:"ایجاد",Modified:"ویرایش",Deleted:"حذف",UserCreated:"ایجاد کاربر",UserUpdated:"ویرایش کاربر",PasswordReset:"بازنشانی رمز"};
export function AuditPage(){
const[data,setData]=useState<AuditResult|null>(null);const[users,setUsers]=useState<AdminUser[]>([]);const[error,setError]=useState("");const[query,setQuery]=useState("");const[page,setPage]=useState(1);const[pageSize,setPageSize]=useState(25);
async function load(q=query,p=page,size=pageSize){const suffix=[q,`page=${p}`,`pageSize=${size}`].filter(Boolean).join("&");setError("");setData(await api(`/api/v1/administration/audit?${suffix}`));}
useEffect(()=>{void Promise.all([api<AdminUser[]>("/api/v1/administration/users").then(setUsers),load("",1)]).catch(x=>setError(x.message));},[]);
async function filter(e:FormEvent<HTMLFormElement>){e.preventDefault();const f=new FormData(e.currentTarget);const p=new URLSearchParams();for(const key of ["actorId","entityType","action","fromDate","toDate"]){let value=String(f.get(key)??"").trim();if(value&&(key==="fromDate"||key==="toDate"))value=persianToGregorianIso(value);if(value)p.set(key,value)}const q=p.toString();await load(q,1);setQuery(q);setPage(1)}
async function move(next:number){try{await load(query,next);setPage(next)}catch(x){setError(x instanceof Error?x.message:String(x))}}
async function resize(next:number){try{await load(query,1,next);setPageSize(next);setPage(1)}catch(x){setError(x instanceof Error?x.message:String(x))}}
return <><PageTitle title="تاریخچه عملیات" description="ردیابی تغییرات داده و عملیات مدیریتی بدون نمایش اطلاعات محرمانه"/>{error&&<ErrorBanner message={error}/>}<Card><form className="form-grid cols-3" onSubmit={e=>void filter(e).catch(x=>setError(x.message))}>
<Field label="کاربر"><Select name="actorId"><option value="">همه کاربران</option>{users.map(x=><option key={x.id} value={x.id}>{x.displayName}</option>)}</Select></Field>
<Field label="نوع رکورد"><Input name="entityType" placeholder="Project، ApplicationUser و…"/></Field><Field label="عملیات"><Select name="action"><option value="">همه عملیات</option>{Object.entries(actionLabels).map(([v,l])=><option key={v} value={v}>{l}</option>)}</Select></Field>
<Field label="از تاریخ"><Input name="fromDate" type="date"/></Field><Field label="تا تاریخ"><Input name="toDate" type="date"/></Field><div className="form-actions"><Button><Filter size={16}/>اعمال فیلتر</Button></div>
</form></Card><Card>{!data?<div className="loading-line">در حال دریافت تاریخچه…</div>:data.items.length===0?<EmptyState title="رویدادی یافت نشد" description="فیلترها را تغییر دهید."/>:<><div className="table-wrap"><table><thead><tr><th>زمان</th><th>کاربر</th><th>عملیات</th><th>نوع</th><th>شناسه رکورد</th><th>تغییرات</th></tr></thead><tbody>{data.items.map(x=><tr key={x.id}><td className="latin">{new Date(x.occurredAtUtc).toLocaleString("fa-IR")}</td><td>{x.actorDisplayName}</td><td>{actionLabels[x.action]??x.action}</td><td className="latin">{x.entityType}</td><td className="latin">{x.entityId??"—"}</td><td>{x.changesJson?<details><summary>مشاهده</summary><pre className="audit-json">{formatJson(x.changesJson)}</pre></details>:"—"}</td></tr>)}</tbody></table></div><PaginationControls page={data.page} pageSize={data.pageSize} total={data.totalCount} loading={false} onPage={next=>void move(next)} onPageSize={next=>void resize(next)}/></>}</Card></>
}
function formatJson(value:string){try{return JSON.stringify(JSON.parse(value),null,2)}catch{return value}}