kcg-ai-monitoring/frontend/src/features/admin/AuditLogs.tsx
htlee 4ee8f05dfd refactor(frontend): admin 계열 PageContainer/PageHeader 적용
- AdminPanel: PageContainer + PageHeader(demo)
- AuditLogs/AccessLogs/LoginHistoryView: size=lg + primary Button
- AccessControl: size=lg + 우측 stats 유지 + ghost 새로고침 Button
- DataHub: PageContainer + demo 배지 + secondary 새로고침
- NoticeManagement: primary '새 알림 등록' Button
- SystemConfig: secondary 2개 액션 Button

인라인 <button>/<div className="p-5 space-y-4"> 패턴을 쇼케이스 공통 컴포넌트로
치환. admin 계열 9개 파일 중 7개 완료 (PermissionsPanel은 서브 컴포넌트라 제외).
UserRoleAssignDialog는 dialog라 제외.
2026-04-08 11:48:41 +09:00

136 lines
5.9 KiB
TypeScript

import { useEffect, useState, useCallback } from 'react';
import { Loader2, RefreshCw, FileSearch } from 'lucide-react';
import { Card, CardContent, CardHeader, CardTitle } from '@shared/components/ui/card';
import { Badge } from '@shared/components/ui/badge';
import { Button } from '@shared/components/ui/button';
import { PageContainer, PageHeader } from '@shared/components/layout';
import { fetchAuditLogs, fetchAuditStats, type AuditLog, type AuditStats } from '@/services/adminApi';
import { formatDateTime } from '@shared/utils/dateFormat';
/**
* 감사 로그 조회 + 메트릭 카드.
* 권한: admin:audit-logs (READ)
*/
export function AuditLogs() {
const [items, setItems] = useState<AuditLog[]>([]);
const [stats, setStats] = useState<AuditStats | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const load = useCallback(async () => {
setLoading(true); setError('');
try {
const [logs, st] = await Promise.all([fetchAuditLogs(0, 100), fetchAuditStats()]);
setItems(logs.content);
setStats(st);
} catch (e: unknown) {
setError(e instanceof Error ? e.message : 'unknown');
} finally {
setLoading(false);
}
}, []);
useEffect(() => { load(); }, [load]);
return (
<PageContainer size="lg">
<PageHeader
icon={FileSearch}
iconColor="text-blue-400"
title="감사 로그"
description="@Auditable AOP가 모든 운영자 의사결정 자동 기록"
actions={
<Button variant="primary" size="sm" onClick={load} icon={<RefreshCw className="w-3.5 h-3.5" />}>
</Button>
}
/>
{/* 통계 카드 */}
{stats && (
<div className="grid grid-cols-4 gap-3">
<MetricCard label="전체 로그" value={stats.total} color="text-heading" />
<MetricCard label="24시간 내" value={stats.last24h} color="text-blue-400" />
<MetricCard label="실패 (24시간)" value={stats.failed24h} color="text-red-400" />
<MetricCard label="액션 종류 (7일)" value={stats.byAction.length} color="text-purple-400" />
</div>
)}
{/* 액션별 분포 */}
{stats && stats.byAction.length > 0 && (
<Card>
<CardHeader className="px-4 pt-3 pb-2"><CardTitle className="text-xs text-label"> ( 7)</CardTitle></CardHeader>
<CardContent className="px-4 pb-4">
<div className="flex flex-wrap gap-2">
{stats.byAction.map((a) => (
<Badge key={a.action} className="bg-surface-overlay text-muted-foreground border border-border text-[10px] px-2 py-1">
{a.action} <span className="text-cyan-400 font-bold ml-1">{a.count}</span>
</Badge>
))}
</div>
</CardContent>
</Card>
)}
{error && <div className="text-xs text-red-400">: {error}</div>}
{loading && <div className="flex items-center justify-center py-12 text-muted-foreground"><Loader2 className="w-5 h-5 animate-spin" /></div>}
{!loading && (
<Card>
<CardContent className="p-0 overflow-x-auto">
<table className="w-full text-xs">
<thead className="bg-surface-overlay text-hint">
<tr>
<th className="px-3 py-2 text-left">SN</th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left"></th>
<th className="px-3 py-2 text-left"> </th>
<th className="px-3 py-2 text-left">IP</th>
<th className="px-3 py-2 text-left"></th>
</tr>
</thead>
<tbody>
{items.length === 0 && <tr><td colSpan={9} className="px-3 py-8 text-center text-hint"> .</td></tr>}
{items.map((it) => (
<tr key={it.auditSn} className="border-t border-border hover:bg-surface-overlay/50">
<td className="px-3 py-2 text-hint font-mono">{it.auditSn}</td>
<td className="px-3 py-2 text-muted-foreground text-[10px]">{formatDateTime(it.createdAt)}</td>
<td className="px-3 py-2 text-cyan-400">{it.userAcnt || '-'}</td>
<td className="px-3 py-2 text-heading font-medium">{it.actionCd}</td>
<td className="px-3 py-2 text-muted-foreground">{it.resourceType ?? '-'} {it.resourceId ? `(${it.resourceId})` : ''}</td>
<td className="px-3 py-2">
<Badge className={`border-0 text-[9px] ${it.result === 'SUCCESS' ? 'bg-green-500/20 text-green-400' : 'bg-red-500/20 text-red-400'}`}>
{it.result || '-'}
</Badge>
</td>
<td className="px-3 py-2 text-red-400 text-[10px]">{it.failReason || '-'}</td>
<td className="px-3 py-2 text-muted-foreground text-[10px]">{it.ipAddress || '-'}</td>
<td className="px-3 py-2 text-muted-foreground text-[10px] max-w-xs truncate">
{it.detail ? JSON.stringify(it.detail) : '-'}
</td>
</tr>
))}
</tbody>
</table>
</CardContent>
</Card>
)}
</PageContainer>
);
}
function MetricCard({ label, value, color }: { label: string; value: number; color: string }) {
return (
<Card>
<CardContent className="p-4">
<div className="text-[10px] text-hint">{label}</div>
<div className={`text-2xl font-bold ${color} mt-1`}>{value.toLocaleString()}</div>
</CardContent>
</Card>
);
}