- Add admin-next/: full Next.js + Prisma + shadcn admin panel (45 API routes, 76 components) - docker-compose: tg_shop_admin service (port 3000, shared db/shop.db, prisma), bot talks to it via ADMIN_CHAT_URL=http://tg_shop_admin:3000/api/chat - tor-proxy now proxies onion admin to tg_shop_admin:3000 (new panel) - chatbotService: ADMIN_CHAT_URL default = http://tg_shop_admin:3000/api/chat (removed localhost:3100 anachronism) - .env admin secrets gitignored (admin-next/.env)
189 lines
7.7 KiB
TypeScript
Executable File
189 lines
7.7 KiB
TypeScript
Executable File
"use client";
|
|
|
|
import { useEffect, useState, useCallback } from "react";
|
|
import {
|
|
LogIn,
|
|
DollarSign,
|
|
UserX,
|
|
KeyRound,
|
|
Package,
|
|
Settings,
|
|
CheckCircle,
|
|
Wallet,
|
|
CreditCard,
|
|
UserPlus,
|
|
FileText,
|
|
ShoppingCart,
|
|
Ban,
|
|
Upload,
|
|
} from "lucide-react";
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|
import { Badge } from "@/components/ui/badge";
|
|
|
|
// ─── Types ───────────────────────────────────────────────
|
|
|
|
interface ActivityItem {
|
|
id: number;
|
|
action: string;
|
|
createdAt: string;
|
|
adminId: string;
|
|
details: string | null;
|
|
}
|
|
|
|
// ─── Icon + color mapping ─────────────────────────────────
|
|
|
|
const ACTION_CONFIG: Record<
|
|
string,
|
|
{ icon: React.ComponentType<{ className?: string }>; color: string; badge: string }
|
|
> = {
|
|
login: { icon: LogIn, color: "#06b6d4", badge: "bg-cyan-500/15 text-cyan-400 border-cyan-500/25" },
|
|
balance_adjust: { icon: DollarSign, color: "#f97316", badge: "bg-orange-500/15 text-orange-400 border-orange-500/25" },
|
|
status_toggle: { icon: UserX, color: "#ef4444", badge: "bg-red-500/15 text-red-400 border-red-500/25" },
|
|
seed_phrase_viewed: { icon: KeyRound, color: "#a855f7", badge: "bg-violet-500/15 text-violet-400 border-violet-500/25" },
|
|
csv_seed_export: { icon: Upload, color: "#a855f7", badge: "bg-violet-500/15 text-violet-400 border-violet-500/25" },
|
|
product_created: { icon: Package, color: "#22c55e", badge: "bg-emerald-500/15 text-emerald-400 border-emerald-500/25" },
|
|
settings_changed: { icon: Settings, color: "#22c55e", badge: "bg-emerald-500/15 text-emerald-400 border-emerald-500/25" },
|
|
purchase_approved: { icon: CheckCircle, color: "#22c55e", badge: "bg-emerald-500/15 text-emerald-400 border-emerald-500/25" },
|
|
purchase_cancelled: { icon: Ban, color: "#ef4444", badge: "bg-red-500/15 text-red-400 border-red-500/25" },
|
|
wallet_added: { icon: Wallet, color: "#06b6d4", badge: "bg-cyan-500/15 text-cyan-400 border-cyan-500/25" },
|
|
commission_paid: { icon: CreditCard, color: "#f59e0b", badge: "bg-yellow-500/15 text-yellow-400 border-yellow-500/25" },
|
|
user_registered: { icon: UserPlus, color: "#14b8a6", badge: "bg-teal-500/15 text-teal-400 border-teal-500/25" },
|
|
user_banned: { icon: Ban, color: "#ef4444", badge: "bg-red-500/15 text-red-400 border-red-500/25" },
|
|
user_unbanned: { icon: CheckCircle, color: "#22c55e", badge: "bg-emerald-500/15 text-emerald-400 border-emerald-500/25" },
|
|
purchase_created: { icon: ShoppingCart, color: "#f59e0b", badge: "bg-yellow-500/15 text-yellow-400 border-yellow-500/25" },
|
|
};
|
|
|
|
const DEFAULT_CONFIG = { icon: FileText, color: "#6b7280", badge: "bg-muted text-muted-foreground border-border" };
|
|
|
|
// ─── Helpers ──────────────────────────────────────────────
|
|
|
|
function relativeTime(dateStr: string): string {
|
|
const now = Date.now();
|
|
const then = new Date(dateStr).getTime();
|
|
const diffMs = now - then;
|
|
const diffSec = Math.floor(diffMs / 1000);
|
|
|
|
if (diffSec < 60) return `${diffSec} second${diffSec !== 1 ? "s" : ""} ago`;
|
|
const diffMin = Math.floor(diffSec / 60);
|
|
if (diffMin < 60) return `${diffMin} minute${diffMin !== 1 ? "s" : ""} ago`;
|
|
const diffHr = Math.floor(diffMin / 60);
|
|
if (diffHr < 24) return `${diffHr} hour${diffHr !== 1 ? "s" : ""} ago`;
|
|
const diffDay = Math.floor(diffHr / 24);
|
|
if (diffDay < 30) return `${diffDay} day${diffDay !== 1 ? "s" : ""} ago`;
|
|
return `${Math.floor(diffDay / 30)} month${Math.floor(diffDay / 30) !== 1 ? "s" : ""} ago`;
|
|
}
|
|
|
|
function actionDescription(action: string, details: string | null): string {
|
|
const label = action.replace(/_/g, " ");
|
|
if (!details) return label;
|
|
try {
|
|
const obj = JSON.parse(details);
|
|
if (obj.username) return `${label} — ${obj.username}`;
|
|
if (obj.target) return `${label} — ${obj.target}`;
|
|
if (obj.userId) return `${label} — user #${obj.userId}`;
|
|
} catch {
|
|
// not JSON
|
|
}
|
|
return label;
|
|
}
|
|
|
|
// ─── Component ────────────────────────────────────────────
|
|
|
|
export function ActivityFeed() {
|
|
const [items, setItems] = useState<ActivityItem[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const fetchFeed = useCallback(async () => {
|
|
try {
|
|
const res = await fetch("/api/stats/dashboard");
|
|
if (!res.ok) return;
|
|
const json = await res.json();
|
|
setItems(json.recentActivity ?? []);
|
|
} catch {
|
|
// silently fail
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
fetchFeed();
|
|
const interval = setInterval(fetchFeed, 30000);
|
|
return () => clearInterval(interval);
|
|
}, [fetchFeed]);
|
|
|
|
return (
|
|
<Card>
|
|
<CardHeader className="p-4 pb-0">
|
|
<CardTitle className="text-sm font-medium">
|
|
Recent Activity
|
|
</CardTitle>
|
|
</CardHeader>
|
|
<CardContent className="p-4">
|
|
{loading ? (
|
|
<div className="max-h-64 animate-pulse space-y-2">
|
|
{Array.from({ length: 5 }).map((_, i) => (
|
|
<div
|
|
key={i}
|
|
className="flex items-center gap-3 rounded-md border p-2"
|
|
>
|
|
<div className="h-6 w-6 rounded-full bg-muted" />
|
|
<div className="flex-1 space-y-1">
|
|
<div className="h-3 w-3/4 rounded bg-muted" />
|
|
<div className="h-2 w-1/3 rounded bg-muted" />
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : items.length > 0 ? (
|
|
<div className="max-h-64 overflow-y-auto space-y-1.5">
|
|
{items.map((item, index) => {
|
|
const config = ACTION_CONFIG[item.action] ?? DEFAULT_CONFIG;
|
|
const Icon = config.icon;
|
|
return (
|
|
<div
|
|
key={item.id}
|
|
className="flex items-center gap-3 rounded-md border border-border/50 px-3 py-2 animate-in fade-in slide-in-from-left-1 duration-300"
|
|
style={{ animationDelay: `${index * 50}ms`, animationFillMode: "both" }}
|
|
>
|
|
<div
|
|
className="flex h-7 w-7 shrink-0 items-center justify-center rounded-full"
|
|
style={{ backgroundColor: `${config.color}15` }}
|
|
>
|
|
<Icon
|
|
className="h-3.5 w-3.5"
|
|
style={{ color: config.color }}
|
|
/>
|
|
</div>
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex items-center gap-2">
|
|
<p className="text-sm truncate leading-tight">
|
|
{actionDescription(item.action, item.details)}
|
|
</p>
|
|
</div>
|
|
<div className="flex items-center gap-2 mt-0.5">
|
|
<Badge
|
|
variant="outline"
|
|
className={`text-[10px] px-1.5 py-0 h-4 font-normal ${config.badge}`}
|
|
>
|
|
{item.action.replace(/_/g, " ")}
|
|
</Badge>
|
|
<span className="text-xs text-muted-foreground">
|
|
{relativeTime(item.createdAt)}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
) : (
|
|
<div className="flex h-48 items-center justify-center text-muted-foreground text-sm">
|
|
No recent activity
|
|
</div>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|