import React, { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { api } from '../lib/api'; import type { PaginatedResponse } from '../lib/api'; import type { AppLog } from '../types'; import { formatDateTime } from '../lib/utils'; import DataTable, { Column } from '../components/ui/DataTable'; import Pagination from '../components/ui/Pagination'; import Modal from '../components/ui/Modal'; import SearchableSelect from '../components/ui/SearchableSelect'; const LEVEL_META: Record = { emergency: { label: 'اضطراری', cls: 'red' }, alert: { label: 'هشدار جدی', cls: 'red' }, critical: { label: 'بحرانی', cls: 'red' }, error: { label: 'خطا', cls: 'red' }, warning: { label: 'اخطار', cls: 'amber' }, notice: { label: 'اطلاع', cls: 'gray' }, info: { label: 'اطلاعات', cls: 'gray' }, debug: { label: 'دیباگ', cls: 'gray' }, }; const LEVEL_FILTER_OPTIONS = [ { value: '', label: 'همه سطوح' }, { value: 'error', label: 'خطا' }, { value: 'warning', label: 'اخطار' }, { value: 'critical', label: 'بحرانی' }, ]; export default function LogsPage() { const [page, setPage] = useState(1); const [level, setLevel] = useState(''); const [search, setSearch] = useState(''); const [viewLog, setViewLog] = useState(null); const limit = 25; const logsQuery = useQuery({ queryKey: ['admin-logs', page, level, search], queryFn: () => api.get>( `/api/v1/admin/logs?page=${page}&limit=${limit}` + (level ? `&level=${level}` : '') + (search ? `&search=${encodeURIComponent(search)}` : ''), ), }); const columns: Column[] = [ { key: 'created_at', header: 'زمان', render: (l) => formatDateTime(l.created_at) }, { key: 'level', header: 'سطح', render: (l) => { const meta = LEVEL_META[l.level] ?? { label: l.level, cls: 'gray' }; return {meta.label}; }, }, { key: 'message', header: 'پیام', render: (l) => ( ), }, { key: 'path', header: 'مسیر', render: (l) => {l.path ?? '—'} }, ]; return (

لاگ‌ها

رویدادهای ثبت‌شده‌ی سیستم (اخطار و بالاتر)
{ setSearch(e.target.value); setPage(1); }} />
{ setLevel(v ? String(v) : ''); setPage(1); }} placeholder="همه سطوح" />
columns={columns} data={logsQuery.data?.data ?? []} loading={logsQuery.isLoading} emptyMessage="لاگی یافت نشد" />
setViewLog(null)}> {viewLog && (
سطح: {LEVEL_META[viewLog.level]?.label ?? viewLog.level}
زمان: {formatDateTime(viewLog.created_at)}
{viewLog.path &&
مسیر: {viewLog.path}
}
{viewLog.message}
{viewLog.context && (
context
                  {viewLog.context}
                
)}
)}
); }