"use client";
import { useMemo, useState } from "react";
import Link from "next/link";
import { ArrowRight, Coins, Flame, Gift, TrendingUp } from "lucide-react";
import type { EconomyStats, LedgerParty, PointLedgerItem } from "@/lib/api";
import { postTypeLabel } from "@/lib/postMeta";
import { pointReasonLabel } from "@/lib/points";
import { formatRelative } from "@/lib/format";
import { AdminPageHeader, AdminSegmented, AdminStatCard } from "@/components/admin";
import Avatar from "@/components/Avatar";
import {
AdminChartCard,
DeltaBarChart,
NamedBarChart,
NamedPieChart,
SingleAreaChart,
} from "@/components/admin/charts";
const fmt = (n: number) => n.toLocaleString("zh-CN");
function netDeltaLabel(n: number) {
if (n > 0) return `+${fmt(n)}`;
return fmt(n);
}
type LedgerKind = "checkin" | "bounty" | "file" | "typed" | "system";
type LedgerFilter =
| "all"
| "granted"
| "spent"
| "poll"
| "lottery"
| "discussion"
| "question";
const LEDGER_FILTERS: { key: LedgerFilter; label: string }[] = [
{ key: "all", label: "全部" },
{ key: "granted", label: "发放" },
{ key: "spent", label: "消耗" },
{ key: "poll", label: "投票" },
{ key: "lottery", label: "抽奖" },
{ key: "discussion", label: "讨论" },
{ key: "question", label: "问答" },
];
const EMPTY_LEDGER: PointLedgerItem[] = [];
const NOTE_STRIP_EXTRAS = [
"解锁帖子",
"下载附件",
"附件收益",
"正文解锁收益",
"悬赏采纳",
"悬赏截止后采纳扣款",
"悬赏过期退回",
"历史签到积分回填",
"每日签到",
];
function partyName(u: LedgerParty) {
return u.nickname || u.username || `#${u.id}`;
}
function isToday(iso: string) {
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return false;
const n = new Date();
return (
d.getFullYear() === n.getFullYear() &&
d.getMonth() === n.getMonth() &&
d.getDate() === n.getDate()
);
}
function ledgerKind(reason: string): LedgerKind {
switch (reason) {
case "checkin":
case "migrate_checkin":
return "checkin";
case "bounty_escrow":
case "bounty_refund":
case "bounty_award":
return "bounty";
case "download_file":
return "file";
default: {
const blob = pointReasonLabel(reason);
if (/投票|抽奖|问答/.test(blob)) return "typed";
return "system";
}
}
}
function ledgerKindEmoji(kind: LedgerKind) {
if (kind === "checkin") return "📅";
if (kind === "bounty") return "🎯";
if (kind === "file") return "📎";
if (kind === "typed") return "🗳";
return "⚙";
}
function blobOf(it: PointLedgerItem) {
return `${pointReasonLabel(it.reason)} ${it.note || ""} ${it.reason}`;
}
function matchLedgerFilter(it: PointLedgerItem, filter: LedgerFilter) {
if (filter === "all") return true;
const delta = Number(it.delta) || 0;
const transfer = Boolean(it.from && it.to);
if (filter === "granted") return transfer || delta > 0;
if (filter === "spent") return transfer || delta < 0;
const blob = blobOf(it);
if (filter === "poll") return /投票/.test(blob);
if (filter === "lottery") return /抽奖/.test(blob);
if (filter === "question") return /问答/.test(blob);
if (filter === "discussion") return it.reason === "unlock_post" || /讨论/.test(blob);
return true;
}
/** 去掉与类型标签重复的前缀,避免「悬赏托管:悬赏托管:悬赏」 */
function ledgerDesc(reason: string, note: string) {
const label = pointReasonLabel(reason);
let s = (note || "").trim();
const prefixes = [label, ...NOTE_STRIP_EXTRAS].filter(Boolean);
let guard = 0;
let changed = true;
while (changed && s && guard < 8) {
changed = false;
guard += 1;
for (const p of prefixes) {
if (s.startsWith(p)) {
s = s.slice(p.length).replace(/^[\s::·\-—]+/, "");
changed = true;
}
}
}
if (s === label) return "";
return s;
}
function todayDelta(items: PointLedgerItem[]) {
let granted = 0;
let spent = 0;
for (const it of items) {
if (!isToday(it.created_at)) continue;
const n = Math.abs(Number(it.delta) || 0);
if (n === 0) continue;
if (it.from && it.to) {
granted += n;
spent += n;
continue;
}
if ((it.delta || 0) > 0) granted += n;
else spent += n;
}
return { granted, spent };
}
function LedgerUserLink({ user, compact }: { user: LedgerParty; compact?: boolean }) {
const name = partyName(user);
return (
暂无签到数据
) : (暂无流水
) : (暂无数据
) : (暂无数据
) : (最近流水
今日 发放{" "} +{fmt(today.granted)} {" · "} 消耗{" "} -{fmt(today.spent)}
{ledger.length === 0 ? (暂无流水
) : filtered.length === 0 ? (该分类下暂无流水
) : (用户可在{" "} /points {" "} 查看个人流水。