added logs page

This commit is contained in:
Jelaletdin12
2026-08-26 23:48:45 +05:00
parent 1a7dd954e2
commit 7dfac2bd59
3 changed files with 383 additions and 1 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,5 @@
# Logs
logs
/logs
*.log
npm-debug.log*
yarn-debug.log*

229
src/pages/logs/index.tsx Normal file
View File

@@ -0,0 +1,229 @@
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { Terminal } from "lucide-react";
import { DataTable, type ColumnDef } from "@/components/dataTable";
import { TableActions } from "@/components/tableActions";
import { DataTableToolbar, type ColumnMeta } from "@/components/dataTableToolbar";
import { useActivityLogs } from "@/features/activityLogs/hooks/useActivityLogs";
import type { ActivityLog } from "@/features/activityLogs/api/activityLogsApi";
import type { VisibilityState } from "@tanstack/react-table";
const MODULE_OPTIONS = [
"loan-orders",
"loan-order-mobiles",
"loan-paid-off-letters",
"loan-remaining",
"order-new-card",
"card-transactions",
"card-requisites",
"card-balances",
"card-pins",
"visa-master",
"sber-payments",
"operators",
"clients",
"all-users",
"currency-rates",
"visa-master-sber-settings",
"file",
"loans-repayment",
];
const ACTION_OPTIONS = ["create", "update", "delete"];
function MethodBadge({ method }: { method: string }) {
const colors: Record<string, string> = {
GET: "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400",
POST: "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400",
PUT: "bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400",
DELETE: "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400",
PATCH: "bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-400",
};
return (
<span
className={`inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium ${colors[method] ?? "bg-gray-100 text-gray-700"}`}
>
{method}
</span>
);
}
function StatusCodeBadge({ code }: { code: number }) {
const color =
code >= 200 && code < 300
? "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400"
: code >= 400
? "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400"
: "bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-400";
return <span className={`inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium ${color}`}>{code}</span>;
}
export default function ActivityLogsPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const [page, setPage] = useState(1);
const [perPage, setPerPage] = useState(25);
const [moduleFilter, setModuleFilter] = useState("");
const [actionFilter, setActionFilter] = useState("");
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
const [columnOrder, setColumnOrder] = useState<string[]>([]);
const { data, isLoading } = useActivityLogs({
page,
limit: perPage,
module: moduleFilter || undefined,
action: actionFilter || undefined,
});
const logs = data?.logs ?? [];
const totalCount = data?.count ?? 0;
const totalPages = Math.max(1, Math.ceil(totalCount / perPage));
const columns: ColumnDef<ActivityLog>[] = [
{
accessorKey: "id",
header: t("common.id", "ID"),
cell: ({ row }) => (
<span
className="text-primary font-semibold cursor-pointer hover:underline text-sm"
onClick={() => navigate(`/logs/${row.original.id}`)}
>
{row.original.id}
</span>
),
size: 70,
},
{
accessorKey: "user",
header: t("logs.fields.user", "Ulanyjy"),
cell: ({ row }) => (
<div className="flex flex-col">
<span className="text-sm font-medium">{row.original.user.username}</span>
<span className="text-xs text-muted-foreground">{row.original.user.role}</span>
</div>
),
},
{
accessorKey: "method",
header: t("logs.fields.method", "Metod"),
cell: ({ row }) => <MethodBadge method={row.original.method} />,
size: 90,
},
{
accessorKey: "module",
header: t("logs.fields.module", "Modul"),
cell: ({ row }) => <span className="text-sm">{row.original.module}</span>,
},
{
accessorKey: "action",
header: t("logs.fields.action", "Amal"),
cell: ({ row }) => <span className="text-sm capitalize">{row.original.action}</span>,
},
{
accessorKey: "statusCode",
header: t("logs.fields.statusCode", "Status kody"),
cell: ({ row }) => <StatusCodeBadge code={row.original.statusCode} />,
size: 100,
},
{
accessorKey: "ip",
header: t("logs.fields.ip", "IP"),
cell: ({ row }) => <span className="text-sm font-mono">{row.original.ip}</span>,
},
{
accessorKey: "createdAt",
header: t("logs.fields.createdAt", "Wagty"),
cell: ({ row }) => <span className="text-sm text-muted-foreground">{new Date(row.original.createdAt).toLocaleString()}</span>,
size: 170,
},
{
id: "actions",
header: "",
cell: ({ row }) => <TableActions onView={() => navigate(`/logs/${row.original.id}`)} />,
size: 80,
enableSorting: false,
},
];
const columnMeta: ColumnMeta[] = columns
.filter((c) => "accessorKey" in c && c.accessorKey)
.map((c) => ({
id: ("accessorKey" in c ? String(c.accessorKey) : c.id) as string,
label: typeof c.header === "string" ? c.header : (("accessorKey" in c ? String(c.accessorKey) : "") ?? ""),
}));
return (
<div className="space-y-4">
<div className="mb-6">
<h1 className="text-2xl font-semibold text-foreground flex items-center gap-2">
<Terminal className="size-5 text-muted-foreground" />
{t("logs.title", "Loglar")}
</h1>
</div>
<div className="bg-card border border-border rounded-xl p-4">
<DataTableToolbar
searchValue=""
onSearchChange={() => {}}
searchPlaceholder={t("common.search", "Gözlemek")}
columns={columnMeta}
columnVisibility={columnVisibility}
onColumnVisibilityChange={setColumnVisibility}
columnOrder={columnOrder}
onColumnOrderChange={setColumnOrder}
filterFields={[
{
id: "module",
label: t("logs.fields.module", "Modul"),
options: MODULE_OPTIONS.map((m) => ({ value: m, label: m })),
},
{
id: "action",
label: t("logs.fields.action", "Amal"),
options: ACTION_OPTIONS.map((a) => ({ value: a, label: a })),
},
]}
activeFilters={[
{ fieldId: "module", value: moduleFilter },
{ fieldId: "action", value: actionFilter },
]}
onFilterChange={(fieldId, value) => {
if (fieldId === "module") setModuleFilter(value);
if (fieldId === "action") setActionFilter(value);
setPage(1);
}}
onFilterReset={() => {
setModuleFilter("");
setActionFilter("");
setPage(1);
}}
perPageOptions={[10, 25, 50, 100]}
perPage={perPage}
onPerPageChange={(v) => {
setPerPage(v);
setPage(1);
}}
hideSearch
hideAction
/>
<DataTable
columns={columns}
data={logs}
isLoading={isLoading}
columnVisibility={columnVisibility}
onColumnVisibilityChange={setColumnVisibility}
columnOrder={columnOrder}
onColumnOrderChange={setColumnOrder}
getRowId={(row) => String(row.id)}
currentPage={page}
totalPages={totalPages}
totalCount={totalCount}
onPageChange={setPage}
/>
</div>
</div>
);
}

153
src/pages/logs/view.tsx Normal file
View File

@@ -0,0 +1,153 @@
import { useParams, useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { ArrowLeft, Terminal, User, Hash, Route, FileText } from "lucide-react";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
import { Spinner } from "@/components/ui/spinner";
import { useActivityLog } from "@/features/activityLogs/hooks/useActivityLogs";
export default function ActivityLogViewPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const { id } = useParams<{ id: string }>();
const { data: log, isLoading } = useActivityLog(Number(id));
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-100">
<Spinner className="size-6" />
</div>
);
}
if (!log) {
return <div className="text-center text-muted-foreground py-10">{t("logs.notFound", "Log tapylmady")}</div>;
}
const methodColors: Record<string, string> = {
GET: "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400",
POST: "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400",
PUT: "bg-amber-100 text-amber-700 dark:bg-amber-900/30 dark:text-amber-400",
DELETE: "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400",
PATCH: "bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-400",
};
const statusCodeColor =
log.statusCode >= 200 && log.statusCode < 300
? "bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400"
: "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400";
return (
<div className="space-y-6">
<div className="flex items-center gap-3">
<Button variant="ghost" size="icon" onClick={() => navigate("/logs")}>
<ArrowLeft className="size-4" />
</Button>
<h1 className="text-2xl font-semibold text-foreground flex items-center gap-2">
<Terminal className="size-5 text-muted-foreground" />
{t("logs.viewTitle", "Log giňişleýin")}
</h1>
</div>
<div className="grid gap-4 md:grid-cols-2">
<Card>
<CardHeader>
<CardTitle className="text-base flex items-center gap-2">
<Hash className="size-4" />
{t("logs.fields.general", "Umumi")}
</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">ID</span>
<span className="text-sm font-medium">{log.id}</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.method", "Metod")}</span>
<span className={`inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium ${methodColors[log.method] ?? ""}`}>
{log.method}
</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.statusCode", "Status kody")}</span>
<span className={`inline-flex items-center rounded-md px-2 py-0.5 text-xs font-medium ${statusCodeColor}`}>
{log.statusCode}
</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.module", "Modul")}</span>
<span className="text-sm font-medium">{log.module}</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.action", "Amal")}</span>
<span className="text-sm font-medium capitalize">{log.action}</span>
</div>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-base flex items-center gap-2">
<User className="size-4" />
{t("logs.fields.user", "Ulanyjy")}
</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.username", "Ulanyjy ady")}</span>
<span className="text-sm font-medium">{log.user.username}</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.role", "Roli")}</span>
<span className="text-sm font-medium capitalize">{log.user.role}</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.ip", "IP")}</span>
<span className="text-sm font-mono">{log.ip}</span>
</div>
<Separator />
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">{t("logs.fields.createdAt", "Wagty")}</span>
<span className="text-sm">{new Date(log.createdAt).toLocaleString()}</span>
</div>
</CardContent>
</Card>
</div>
<Card>
<CardHeader>
<CardTitle className="text-base flex items-center gap-2">
<Route className="size-4" />
{t("logs.fields.path", "Salgylama")}
</CardTitle>
</CardHeader>
<CardContent>
<code className="block bg-muted rounded-md p-3 text-sm font-mono break-all">{log.path}</code>
</CardContent>
</Card>
{log.requestBody && Object.keys(log.requestBody).length > 0 && (
<Card>
<CardHeader>
<CardTitle className="text-base flex items-center gap-2">
<FileText className="size-4" />
{t("logs.fields.requestBody", "Request body")}
</CardTitle>
</CardHeader>
<CardContent>
<pre className="bg-muted rounded-md p-4 text-sm font-mono overflow-x-auto max-h-[500px] overflow-y-auto">
{JSON.stringify(log.requestBody, null, 2)}
</pre>
</CardContent>
</Card>
)}
</div>
);
}