2026-07-26 11:27:21 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-07-26 15:13:18 +07:00
|
|
|
import { AlertCircle, RefreshCw } from "lucide-react";
|
2026-07-26 11:27:21 +07:00
|
|
|
import { useSearchParams } from "next/navigation";
|
2026-07-26 14:27:36 +07:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2026-07-26 15:13:18 +07:00
|
|
|
import { Badge } from "@/components/ui/badge";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
|
|
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
2026-07-26 11:27:21 +07:00
|
|
|
import { DashboardPanel } from "@/features/dashboard/dashboard-panel";
|
|
|
|
|
import { LivePanel } from "@/features/live/live-panel";
|
|
|
|
|
import { MessagesPanel } from "@/features/messages/messages-panel";
|
2026-07-26 14:27:36 +07:00
|
|
|
import { voiceApi } from "@/lib/api";
|
|
|
|
|
import { useAppConfig } from "@/lib/hooks/use-config";
|
|
|
|
|
import type { Guild } from "@/lib/types";
|
2026-07-26 11:27:21 +07:00
|
|
|
|
|
|
|
|
export default function DashboardPage() {
|
|
|
|
|
const searchParams = useSearchParams();
|
|
|
|
|
const tab = searchParams.get("tab") ?? "messages";
|
2026-07-26 13:45:05 +07:00
|
|
|
const urlGuildId = searchParams.get("guildId");
|
2026-07-26 11:27:21 +07:00
|
|
|
|
2026-07-26 13:45:05 +07:00
|
|
|
const { config, loading: configLoading } = useAppConfig();
|
|
|
|
|
|
|
|
|
|
const [guilds, setGuilds] = useState<Guild[]>([]);
|
|
|
|
|
const [guildsLoading, setGuildsLoading] = useState(true);
|
|
|
|
|
const [guildsError, setGuildsError] = useState<string | null>(null);
|
|
|
|
|
const [selectedGuildId, setSelectedGuildId] = useState("");
|
|
|
|
|
|
|
|
|
|
// Resolve the active guild ID from:
|
|
|
|
|
// 1. URL param (?guildId=xxx)
|
|
|
|
|
// 2. Config monitorGuildId
|
|
|
|
|
// 3. First available guild from /api/guilds
|
|
|
|
|
// 4. Empty (user needs to select)
|
|
|
|
|
const resolveGuild = useCallback(() => {
|
|
|
|
|
if (urlGuildId) return urlGuildId;
|
|
|
|
|
if (config?.monitorGuildId) return config.monitorGuildId;
|
|
|
|
|
if (guilds.length > 0) return guilds[0].id;
|
|
|
|
|
return "";
|
|
|
|
|
}, [urlGuildId, config?.monitorGuildId, guilds]);
|
|
|
|
|
|
|
|
|
|
// Fetch guilds list from backend
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
setGuildsLoading(true);
|
|
|
|
|
setGuildsError(null);
|
|
|
|
|
voiceApi
|
|
|
|
|
.getGuilds()
|
|
|
|
|
.then((g) => {
|
|
|
|
|
if (!cancelled) setGuilds(g);
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
2026-07-26 14:27:36 +07:00
|
|
|
if (!cancelled)
|
|
|
|
|
setGuildsError(
|
|
|
|
|
err instanceof Error ? err.message : "Failed to load guilds",
|
|
|
|
|
);
|
2026-07-26 13:45:05 +07:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
if (!cancelled) setGuildsLoading(false);
|
|
|
|
|
});
|
2026-07-26 14:27:36 +07:00
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
2026-07-26 13:45:05 +07:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Resolve guild ID once config and guilds are loaded
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (configLoading || guildsLoading) return;
|
|
|
|
|
const resolved = resolveGuild();
|
|
|
|
|
if (resolved && resolved !== selectedGuildId) {
|
|
|
|
|
setSelectedGuildId(resolved);
|
|
|
|
|
}
|
|
|
|
|
}, [configLoading, guildsLoading, resolveGuild, selectedGuildId]);
|
|
|
|
|
|
2026-07-26 15:13:18 +07:00
|
|
|
const handleGuildChange = useCallback((guildId: string | null) => {
|
|
|
|
|
if (guildId) setSelectedGuildId(guildId);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const handleRetry = useCallback(() => {
|
|
|
|
|
setGuildsLoading(true);
|
|
|
|
|
setGuildsError(null);
|
|
|
|
|
voiceApi
|
|
|
|
|
.getGuilds()
|
|
|
|
|
.then(setGuilds)
|
|
|
|
|
.catch((err) =>
|
|
|
|
|
setGuildsError(
|
|
|
|
|
err instanceof Error ? err.message : "Failed to load guilds",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.finally(() => setGuildsLoading(false));
|
2026-07-26 13:45:05 +07:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const isReady = !configLoading && !guildsLoading;
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="space-y-5">
|
2026-07-26 13:45:05 +07:00
|
|
|
{/* Guild selector bar */}
|
|
|
|
|
<GuildBar
|
|
|
|
|
guilds={guilds}
|
|
|
|
|
loading={guildsLoading}
|
|
|
|
|
error={guildsError}
|
|
|
|
|
selectedGuildId={selectedGuildId}
|
|
|
|
|
onChange={handleGuildChange}
|
2026-07-26 15:13:18 +07:00
|
|
|
onRetry={handleRetry}
|
2026-07-26 13:45:05 +07:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{/* Main panel */}
|
|
|
|
|
{isReady ? (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="animate-fade-in-up">
|
2026-07-26 13:45:05 +07:00
|
|
|
{tab === "live" && <LivePanel />}
|
2026-07-26 14:27:36 +07:00
|
|
|
{tab === "dashboard" && <DashboardPanel guildId={selectedGuildId} />}
|
|
|
|
|
{tab === "messages" && <MessagesPanel guildId={selectedGuildId} />}
|
2026-07-26 15:13:18 +07:00
|
|
|
</div>
|
2026-07-26 13:45:05 +07:00
|
|
|
) : (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="flex items-center justify-center py-24">
|
|
|
|
|
<div className="flex flex-col items-center gap-3">
|
|
|
|
|
<div className="size-8 animate-spin rounded-full border-2 border-primary border-t-transparent" />
|
|
|
|
|
<p className="text-sm text-muted-foreground">Loading dashboard…</p>
|
|
|
|
|
</div>
|
2026-07-26 13:45:05 +07:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Guild Bar ────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
function GuildBar({
|
|
|
|
|
guilds,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
selectedGuildId,
|
|
|
|
|
onChange,
|
|
|
|
|
onRetry,
|
|
|
|
|
}: {
|
|
|
|
|
guilds: Guild[];
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error: string | null;
|
|
|
|
|
selectedGuildId: string;
|
2026-07-26 15:13:18 +07:00
|
|
|
onChange: (id: string | null) => void;
|
2026-07-26 13:45:05 +07:00
|
|
|
onRetry: () => void;
|
|
|
|
|
}) {
|
|
|
|
|
// No guild bar if there's only one guild and it's already selected
|
|
|
|
|
if (guilds.length <= 1 && !loading && !error) return null;
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="flex items-center gap-3 rounded-xl border border-border/50 bg-card p-3">
|
|
|
|
|
<Skeleton className="h-8 w-36" />
|
|
|
|
|
<Skeleton className="h-8 w-8 rounded-full" />
|
2026-07-26 13:45:05 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-07-26 11:27:21 +07:00
|
|
|
}
|
2026-07-26 13:45:05 +07:00
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="flex items-center justify-between rounded-xl border border-destructive/20 bg-destructive/5 p-3">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<AlertCircle className="size-4 text-destructive shrink-0" />
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
Could not load guilds: {error}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<Button variant="outline" size="sm" onClick={onRetry}>
|
|
|
|
|
<RefreshCw className="size-3 mr-1" />
|
2026-07-26 13:45:05 +07:00
|
|
|
Retry
|
2026-07-26 15:13:18 +07:00
|
|
|
</Button>
|
2026-07-26 13:45:05 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (guilds.length === 0) {
|
|
|
|
|
return (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="rounded-xl border border-yellow-500/20 bg-yellow-500/5 p-3">
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<AlertCircle className="size-4 text-yellow-500 shrink-0" />
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
No guilds available. Make sure the Discord gateway is connected.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2026-07-26 13:45:05 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-26 15:13:18 +07:00
|
|
|
<div className="flex items-center gap-3 rounded-xl border border-border/50 bg-card p-3">
|
|
|
|
|
<Badge variant="outline" className="shrink-0 text-xs font-normal">
|
|
|
|
|
Guild
|
|
|
|
|
</Badge>
|
|
|
|
|
<Select value={selectedGuildId} onValueChange={onChange}>
|
|
|
|
|
<SelectTrigger className="h-8 w-full max-w-xs">
|
|
|
|
|
<SelectValue placeholder="Select a guild…" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{guilds.map((g) => (
|
|
|
|
|
<SelectItem key={g.id} value={g.id}>
|
|
|
|
|
{g.name}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
2026-07-26 13:45:05 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-07-26 11:27:21 +07:00
|
|
|
}
|