"use client"; import { useCallback, useState } from "react"; import { useExportLabels } from "@/hooks/use-export-labels"; import { useTranslation } from "react-i18next"; import { useAsyncEffect } from "@/hooks/use-async-effect"; import { useTranslationRef } from "@/hooks/use-translation-ref"; import { getAdminRiskPoolLockLogs } from "@/api/admin-risk"; import { AdminListPaginationFooter } from "@/components/admin/admin-list-pagination-footer"; import { AdminTableExportButton } from "@/components/admin/admin-table-export-button"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { AdminLoadingState, AdminLoadingInline, AdminTableLoadingRow } from "@/components/admin/admin-loading-state"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useAdminCurrencyCatalog } from "@/hooks/use-admin-currency-catalog"; import { useAdminPlayCodeLabel } from "@/hooks/use-admin-play-type-catalog"; import { useAdminDateTimeFormatter } from "@/hooks/use-admin-datetime-formatter"; import { riskActionTypeLabel, riskSourceReasonLabel } from "@/modules/risk/risk-display"; import { formatAdminMinorUnits } from "@/lib/money"; import { LotteryApiBizError } from "@/types/api/errors"; import type { AdminRiskLockLogListData, AdminRiskLockLogRow } from "@/types/api/admin-risk"; const ACTION_ALL = "__all__"; function riskActionFilterLabel( value: string, t: (key: string) => string, ): string { if (value === ACTION_ALL) { return t("noLimit"); } return riskActionTypeLabel(value, t); } export function RiskLockLogsConsole({ drawId }: { drawId: number }) { const { t } = useTranslation(["risk", "common"]); const tRef = useTranslationRef(["risk", "common"]); const exportLabels = useExportLabels("riskLockLogs"); useAdminCurrencyCatalog(); const playCodeLabel = useAdminPlayCodeLabel(); const formatDt = useAdminDateTimeFormatter(); const [page, setPage] = useState(1); const [perPage, setPerPage] = useState(10); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [draftNumber, setDraftNumber] = useState(""); const [appliedNumber, setAppliedNumber] = useState(""); const [draftAction, setDraftAction] = useState(ACTION_ALL); const [appliedAction, setAppliedAction] = useState(ACTION_ALL); const load = useCallback(async () => { setLoading(true); setError(null); try { const d = await getAdminRiskPoolLockLogs(drawId, { page, per_page: perPage, normalized_number: appliedNumber.trim() === "" ? undefined : appliedNumber.trim(), action_type: appliedAction === ACTION_ALL ? undefined : (appliedAction as "lock" | "release"), }); setData(d); } catch (e) { const msg = e instanceof LotteryApiBizError ? e.message : tRef.current("loadLogsFailed"); setError(msg); setData(null); } finally { setLoading(false); } }, [drawId, page, perPage, appliedAction, appliedNumber]); useAsyncEffect(() => { void load(); }, [drawId, page, perPage, appliedAction, appliedNumber]); return ( {t("lockLogsTitle")}
setDraftNumber(e.target.value.replace(/\D/g, "").slice(0, 4))} placeholder={t("optional")} />
{error ?

{error}

: null} <>
{t("time")} {t("searchNumber")} {t("action")} {t("amount")} {t("source")} {t("ticketNo")} {t("playCode")} {loading && !data ? : null} {(data?.items ?? []).map((row: AdminRiskLockLogRow) => ( {row.created_at ? formatDt(row.created_at) : "—"} {row.normalized_number} {riskActionTypeLabel(row.action_type, t)} {formatAdminMinorUnits(row.amount, data?.currency_code ?? "NPR")} {riskSourceReasonLabel(row.source_reason, t)} {row.ticket_no ?? "—"} {playCodeLabel(row.play_code)} ))}
{data ? ( { setPerPage(n); setPage(1); }} onPageChange={setPage} /> ) : null}
); }