"use client"; import { AlertCircle, ArrowLeft, BarChart3, ChevronRight, Clock, Hash, Search, Shield, Sparkles, Users, } from "lucide-react"; import Image from "next/image"; import { useEffect, useState } from "react"; import { DetailStat, EmptyState, ErrorState, LoadingSkeleton, StatCard, } from "@/components/shared"; import { GuildSelector } from "@/components/shared/guild-selector"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Progress } from "@/components/ui/progress"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { useChannels, useStats, useUsers } from "@/hooks"; import { formatNumber } from "@/lib/format"; import type { DashboardChannelDetail, DashboardUserDetail } from "@/lib/types"; type View = "stats" | "users" | "channels" | "user-detail" | "channel-detail"; export default function DashboardPage() { const [view, setView] = useState("stats"); const [guildId, setGuildId] = useState(""); const [activeUser, setActiveUser] = useState( null, ); const [activeChannel, setActiveChannel] = useState(null); return (
setView(v as View)} > setView("stats")}> Stats setView("users")}> Users setView("channels")}> Channels {view === "stats" && } {view === "users" && ( { try { const { dashboardApi } = await import("@/lib/api"); const detail = await dashboardApi.getUserDetail(userId); setActiveUser(detail); setView("user-detail"); } catch (err) { console.error("dashboard/userDetail:", err); } }} /> )} {view === "channels" && ( { try { const { dashboardApi } = await import("@/lib/api"); const detail = await dashboardApi.getChannelDetail(chId); setActiveChannel(detail); setView("channel-detail"); } catch (err) { console.error("dashboard/channelDetail:", err); } }} /> )} {view === "user-detail" && activeUser && ( setView("users")} /> )} {view === "channel-detail" && activeChannel && ( setView("channels")} /> )}
); } // ── Stats Section ─────────────────────────────── function StatsSection() { const { stats, loading, error, refetch } = useStats(); if (error) return ; if (loading || !stats) { return (
); } return (
Top Channels {stats.top_channels.length === 0 ? (

No channel data yet.

) : (
{stats.top_channels.map((ch) => { const max = stats.top_channels[0].message_count; const pct = max > 0 ? (ch.message_count / max) * 100 : 0; return (
#{ch.channel_name ?? ch.channel_id.slice(0, 8)} {formatNumber(ch.message_count)}
); })}
)}
Moderation Queue
); } function QueueStat({ label, value, variant, }: { label: string; value: number; variant?: "default" | "warning" | "danger"; }) { return (
{value}
{label}
); } // ── Users Section ─────────────────────────────── function UsersSection({ onSelect }: { onSelect: (id: string) => void }) { const { users, loading, search, setSearch, refetch } = useUsers(); useEffect(() => { const timer = setTimeout(refetch, 300); return () => clearTimeout(timer); }, [refetch]); return (
setSearch(e.target.value)} className="pl-9 h-9" />
{loading ? ( ) : users.length === 0 ? ( ) : (
{users.map((user) => ( onSelect(user.user_id)} >
{user.avatar_url ? ( ) : ( (user.username ?? "?").charAt(0).toUpperCase() )}

{user.username ?? "Unknown"}

{user.total_messages} messages {user.flagged_count > 0 && ( {user.flagged_count} flagged )}

))}
)}
); } // ── Channels Section ──────────────────────────── function ChannelsSection({ guildId, onSelect, }: { guildId: string; onSelect: (id: string) => void; }) { const { channels, loading, search, setSearch, refetch } = useChannels(guildId); useEffect(() => { const timer = setTimeout(refetch, 300); return () => clearTimeout(timer); }, [refetch]); return (
setSearch(e.target.value)} className="pl-9 h-9" />
{loading ? ( ) : channels.length === 0 ? ( ) : (
{channels.map((ch) => ( onSelect(ch.channel_id)} >

{ch.channel_name ?? ch.channel_id.slice(0, 8)}

{ch.total_messages} messages {ch.flagged_count > 0 && ( {ch.flagged_count} flagged )}

{ch.culture_summary && (

“{ch.culture_summary}”

)}
))}
)}
); } // ── User Detail View ──────────────────────────── function UserDetailView({ user, onBack, }: { user: DashboardUserDetail; onBack: () => void; }) { return (
{user.avatar_url ? ( ) : ( (user.username ?? "?").charAt(0).toUpperCase() )}

{user.username ?? "Unknown"}

{user.user_id}

{user.profile_summary && (

AI Profile

{user.profile_summary}

)} {user.recent_messages.length > 0 && (

Recent Messages

{user.recent_messages.slice(0, 5).map((msg) => (

{new Date(msg.created_at).toLocaleString()}

{msg.content}

))}
)}
); } // ── Channel Detail View ───────────────────────── function ChannelDetailView({ channel, onBack, }: { channel: DashboardChannelDetail; onBack: () => void; }) { return (

{channel.channel_name ?? channel.channel_id.slice(0, 8)}

{channel.channel_id}

{channel.culture_summary && (

Channel Culture

“{channel.culture_summary}”

)} {channel.recent_messages.length > 0 && (

Recent Messages

{channel.recent_messages.slice(0, 5).map((msg) => (
{msg.username} {new Date(msg.created_at).toLocaleString()}

{msg.content}

))}
)}
); }