chore: import clean Agent Desk snapshot (6/7)

This commit is contained in:
2026-10-16 12:02:48 +00:00
parent d215bf6303
commit 756ae33d05
278 changed files with 53808 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
"use client"
import Link from "next/link"
import { ArrowRightIcon, ShieldAlertIcon } from "lucide-react"
import type { DashboardAlert } from "@/lib/api/dashboard"
import { Badge } from "@/components/ui/badge"
import { useI18n } from "@/i18n/provider"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
type AlertListProps = {
alerts: DashboardAlert[]
}
function getAlertBadgeVariant(level: DashboardAlert["level"]) {
if (level === "error") {
return "destructive" as const
}
if (level === "warning") {
return "secondary" as const
}
return "outline" as const
}
export function AlertList({ alerts }: AlertListProps) {
const t = useI18n()
return (
<Card className="rounded-md shadow-none">
<CardHeader>
<CardTitle>{t("dashboardHome.riskAlerts")}</CardTitle>
<CardDescription>{t("dashboardHome.riskAlertsDescription")}</CardDescription>
</CardHeader>
<CardContent className="space-y-3">
{alerts.length === 0 ? (
<div className="rounded-md border border-dashed px-4 py-10 text-center">
<ShieldAlertIcon className="mx-auto mb-3 size-8 text-muted-foreground" />
<div className="text-sm font-medium">{t("dashboardHome.noRiskTitle")}</div>
<div className="mt-1 text-sm text-muted-foreground">
{t("dashboardHome.noRiskDescription")}
</div>
</div>
) : (
alerts.map((item) => (
<Link key={item.id} href={item.link} className="block">
<div className="rounded-md border p-4 transition-colors hover:border-primary/40">
<div className="flex items-start justify-between gap-3">
<div>
<div className="flex items-center gap-2">
<div className="font-medium">{item.title}</div>
<Badge variant={getAlertBadgeVariant(item.level)}>
{item.count}
</Badge>
</div>
<div className="mt-1 text-sm text-muted-foreground">
{item.description}
</div>
</div>
<ArrowRightIcon className="mt-0.5 size-4 text-muted-foreground" />
</div>
</div>
</Link>
))
)}
</CardContent>
</Card>
)
}

View File

@@ -0,0 +1,169 @@
"use client"
import { useCallback, useEffect, useState } from "react"
import { RefreshCwIcon } from "lucide-react"
import { toast } from "sonner"
import { Button } from "@/components/ui/button"
import { Skeleton } from "@/components/ui/skeleton"
import { Card, CardContent } from "@/components/ui/card"
import { useI18n } from "@/i18n/provider"
import {
fetchDashboardOverview,
type DashboardOverview,
type DashboardRange,
} from "@/lib/api/dashboard"
import { SummaryCards } from "./summary-cards"
import { TrendPanel } from "./trend-panel"
import { TeamLoadPanel } from "./team-load-panel"
import { AlertList } from "./alert-list"
import { SetupReadinessBanner } from "./setup-readiness-banner"
function LoadingCards() {
return (
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-6">
{Array.from({ length: 6 }).map((_, index) => (
<Card key={index} className="rounded-md shadow-none">
<CardContent className="space-y-3 p-4">
<Skeleton className="h-4 w-24" />
<Skeleton className="h-8 w-20" />
<Skeleton className="h-4 w-full" />
</CardContent>
</Card>
))}
</div>
)
}
export function DashboardHome() {
const t = useI18n()
const [range, setRange] = useState<DashboardRange>("7d")
const [data, setData] = useState<DashboardOverview | null>(null)
const [loading, setLoading] = useState(true)
const [refreshing, setRefreshing] = useState(false)
const loadData = useCallback(
async (nextRange: DashboardRange, showRefreshing = false) => {
if (showRefreshing) {
setRefreshing(true)
} else {
setLoading(true)
}
try {
const result = await fetchDashboardOverview(nextRange)
setData(result)
} catch (error) {
toast.error(error instanceof Error ? error.message : t("dashboardHome.loadFailed"))
} finally {
setLoading(false)
setRefreshing(false)
}
},
[t]
)
useEffect(() => {
void loadData(range)
}, [loadData, range])
const rangeOptions: Array<{ value: DashboardRange; label: string }> = [
{ value: "today", label: t("dashboardHome.rangeToday") },
{ value: "7d", label: t("dashboardHome.range7d") },
{ value: "30d", label: t("dashboardHome.range30d") },
]
return (
<div className="flex flex-1 flex-col gap-4 p-4 lg:p-5">
<SetupReadinessBanner onUpdated={() => void loadData(range, true)} />
<div className="flex justify-end">
<div className="flex flex-wrap items-center gap-2">
<div className="rounded-md border bg-background p-1">
{rangeOptions.map((item) => (
<Button
key={item.value}
variant={range === item.value ? "secondary" : "ghost"}
size="sm"
onClick={() => setRange(item.value)}
>
{item.label}
</Button>
))}
</div>
<Button
variant="outline"
size="sm"
onClick={() => void loadData(range, true)}
disabled={loading || refreshing}
>
<RefreshCwIcon className={refreshing ? "mr-2 size-4 animate-spin" : "mr-2 size-4"} />
{t("dashboardHome.refresh")}
</Button>
</div>
</div>
{loading && !data ? (
<LoadingCards />
) : data ? (
<>
<SummaryCards summary={data.summary} />
<TrendPanel
title={t("dashboardHome.conversationTrend")}
description={t("dashboardHome.conversationTrendDescription")}
trend={data.conversationStats.trend}
distribution={data.conversationStats.statusDistribution}
/>
<div className="grid gap-4 xl:grid-cols-[1.15fr_0.85fr]">
<TeamLoadPanel agentStats={data.agentStats} />
<Card className="rounded-md shadow-none">
<CardContent className="grid gap-3 p-4 sm:grid-cols-2">
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.enabledAiAgents")}</div>
<div className="mt-1 text-2xl font-semibold">{data.aiStats.enabledAiAgents}</div>
</div>
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.enabledChannels")}</div>
<div className="mt-1 text-2xl font-semibold">{data.aiStats.enabledChannels}</div>
</div>
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.todayKnowledgeRetrieves")}</div>
<div className="mt-1 text-2xl font-semibold">
{data.aiStats.todayKnowledgeRetrieves}
</div>
</div>
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.todayKnowledgeRetrieveFailRate")}</div>
<div className="mt-1 text-2xl font-semibold">
{data.aiStats.todayKnowledgeRetrieveFailRate.toFixed(1)}%
</div>
</div>
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.todayAgentRunFailCount")}</div>
<div className="mt-1 text-2xl font-semibold">
{data.aiStats.todayAgentRunFailCount}
</div>
</div>
<div className="rounded-md border bg-background px-3 py-2.5">
<div className="text-sm text-muted-foreground">{t("dashboardHome.todayAiHandoffCount")}</div>
<div className="mt-1 text-2xl font-semibold">
{data.aiStats.todayAiHandoffCount}
</div>
</div>
</CardContent>
</Card>
</div>
<AlertList alerts={data.alerts} />
</>
) : (
<Card className="rounded-md shadow-none">
<CardContent className="flex min-h-60 items-center justify-center p-6 text-sm text-muted-foreground">
{t("dashboardHome.empty")}
</CardContent>
</Card>
)}
</div>
)
}

View File

@@ -0,0 +1,173 @@
"use client"
import {
AlertTriangleIcon,
ArrowRightIcon,
CheckCircle2Icon,
Loader2Icon,
SparklesIcon,
} from "lucide-react"
import Link from "next/link"
import { useCallback, useEffect, useState } from "react"
import { toast } from "sonner"
import {
notifySetupReadinessUpdated,
SETUP_READINESS_UPDATED_EVENT,
} from "@/components/operator-readiness-indicator"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { useI18n } from "@/i18n/provider"
import {
applySetupQuickFix,
fetchSetupReadiness,
type SetupReadinessResponse,
} from "@/lib/api/setup-readiness"
export function SetupReadinessBanner({
onUpdated,
}: {
onUpdated?: () => void
}) {
const t = useI18n()
const [readiness, setReadiness] = useState<SetupReadinessResponse | null>(null)
const [fixing, setFixing] = useState(false)
const loadReadiness = useCallback(async () => {
try {
const data = await fetchSetupReadiness()
setReadiness(data)
} catch {
// Ignore in banner if failed
}
}, [])
useEffect(() => {
void loadReadiness()
const handleUpdated = () => {
void loadReadiness()
}
window.addEventListener(SETUP_READINESS_UPDATED_EVENT, handleUpdated)
return () => {
window.removeEventListener(SETUP_READINESS_UPDATED_EVENT, handleUpdated)
}
}, [loadReadiness])
async function handleQuickSetupAll() {
setFixing(true)
try {
const res = await applySetupQuickFix({ action: "quick_setup_all" })
setReadiness(res.readiness)
notifySetupReadinessUpdated()
onUpdated?.()
toast.success(t("setupWizard.quickFixSuccess"))
} catch (error) {
toast.error(
error instanceof Error ? error.message : t("common.operationFailed"),
)
} finally {
setFixing(false)
}
}
if (!readiness) {
return null
}
const allReady =
readiness.can_receive_requests_now &&
readiness.current_operator.can_receive_now &&
readiness.overall_status === "ok"
if (allReady) {
return null
}
const canQuickSetupAll = readiness.steps.some((s) =>
s.quick_fix_actions.includes("quick_setup_all"),
)
const incompleteSteps = readiness.steps.filter((s) => s.status !== "ok")
return (
<div className="rounded-md border border-amber-500/40 bg-amber-500/5 p-4">
<div className="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
<div className="space-y-1.5">
<div className="flex flex-wrap items-center gap-2">
<AlertTriangleIcon className="size-4 shrink-0 text-amber-600 dark:text-amber-400" />
<h3 className="text-sm font-semibold text-foreground">
{t("setupWizard.banner.title")}
</h3>
<Badge
variant="outline"
className="rounded-md border-amber-500/40 bg-amber-500/10 text-xs text-amber-700 dark:text-amber-300"
>
{t("setupWizard.stepsProgress", {
ready: readiness.ready_steps,
total: readiness.total_steps,
})}
</Badge>
</div>
<p className="text-xs text-muted-foreground">
{t("setupWizard.banner.description")}
</p>
<div className="flex flex-wrap items-center gap-1.5 pt-1">
{incompleteSteps.map((step) => (
<Badge
key={step.key}
variant="secondary"
className="rounded-md text-xs font-normal"
>
{step.step_number}. {t(`setupWizard.steps.${step.key}.title`)}
</Badge>
))}
{!readiness.current_operator.can_receive_now
? readiness.current_operator.blocker_codes.map((code) => (
<Badge
key={code}
variant="outline"
className="rounded-md border-amber-500/30 text-xs font-normal text-amber-700 dark:text-amber-300"
>
{t(`setupWizard.operatorBlockers.${code}`)}
</Badge>
))
: null}
</div>
</div>
<div className="flex shrink-0 flex-wrap items-center gap-2">
{canQuickSetupAll ? (
<Button
size="sm"
className="gap-1.5 rounded-md"
disabled={fixing}
onClick={() => void handleQuickSetupAll()}
>
{fixing ? (
<Loader2Icon className="size-4 animate-spin" />
) : (
<SparklesIcon className="size-4" />
)}
<span>
{fixing
? t("setupWizard.quickSetupRunning")
: t("setupWizard.quickSetupAll")}
</span>
</Button>
) : null}
<Button
variant="outline"
size="sm"
className="gap-1.5 rounded-md"
render={<Link href="/dashboard/setup" />}
>
<span>{t("setupWizard.openSetupWizard")}</span>
<ArrowRightIcon className="size-4" />
</Button>
</div>
</div>
</div>
)
}
export { CheckCircle2Icon }

View File

@@ -0,0 +1,108 @@
"use client"
import Link from "next/link"
import {
BotMessageSquareIcon,
CircleDashedIcon,
HeadsetIcon,
SparklesIcon,
WavesIcon,
} from "lucide-react"
import type { DashboardOverview } from "@/lib/api/dashboard"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { useI18n } from "@/i18n/provider"
type SummaryCardsProps = {
summary: DashboardOverview["summary"]
}
type SummaryCardItem = {
key: keyof DashboardOverview["summary"]
titleKey: string
descriptionKey: string
link: string
icon: typeof BotMessageSquareIcon
format?: (value: number) => string
}
const cards: SummaryCardItem[] = [
{
key: "todayNewConversations",
titleKey: "dashboardHome.summaryTodayNewConversations",
descriptionKey: "dashboardHome.summaryTodayNewConversationsDescription",
link: "/dashboard/conversations",
icon: BotMessageSquareIcon,
},
{
key: "processingConversations",
titleKey: "dashboardHome.summaryProcessingConversations",
descriptionKey: "dashboardHome.summaryProcessingConversationsDescription",
link: "/dashboard/conversations",
icon: WavesIcon,
},
{
key: "pendingDispatchConversations",
titleKey: "dashboardHome.summaryPendingDispatchConversations",
descriptionKey: "dashboardHome.summaryPendingDispatchConversationsDescription",
link: "/dashboard/conversations",
icon: CircleDashedIcon,
},
{
key: "onlineAgents",
titleKey: "dashboardHome.summaryOnlineAgents",
descriptionKey: "dashboardHome.summaryOnlineAgentsDescription",
link: "/dashboard/agents",
icon: HeadsetIcon,
},
{
key: "aiServiceRate",
titleKey: "dashboardHome.summaryAiServiceRate",
descriptionKey: "dashboardHome.summaryAiServiceRateDescription",
link: "/dashboard/ai-agents",
icon: SparklesIcon,
format: (value: number) => `${value.toFixed(1)}%`,
},
]
export function SummaryCards({ summary }: SummaryCardsProps) {
const t = useI18n()
return (
<div className="grid gap-3 md:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-5">
{cards.map((item) => {
const Icon = item.icon
const rawValue = summary[item.key]
const value =
typeof item.format === "function"
? item.format(Number(rawValue))
: Number(rawValue).toLocaleString()
return (
<Link key={item.key} href={item.link}>
<Card className="h-full rounded-md shadow-none transition-colors hover:border-primary/40">
<CardHeader className="flex flex-row items-start justify-between space-y-0 p-4 pb-2">
<div className="space-y-1">
<CardTitle className="text-sm font-medium">{t(item.titleKey)}</CardTitle>
<CardDescription>{t(item.descriptionKey)}</CardDescription>
</div>
<div className="rounded-md bg-muted p-2 text-muted-foreground">
<Icon className="size-4" />
</div>
</CardHeader>
<CardContent className="px-4 pb-4">
<div className="text-2xl font-semibold tracking-tight">{value}</div>
</CardContent>
</Card>
</Link>
)
})}
</div>
)
}

View File

@@ -0,0 +1,112 @@
"use client"
import { Badge } from "@/components/ui/badge"
import { useI18n } from "@/i18n/provider"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import type { DashboardOverview } from "@/lib/api/dashboard"
type TeamLoadPanelProps = {
agentStats: DashboardOverview["agentStats"]
}
function getLoadTone(loadRate: number) {
if (loadRate >= 85) {
return "bg-red-500"
}
if (loadRate >= 60) {
return "bg-amber-500"
}
return "bg-emerald-500"
}
export function TeamLoadPanel({ agentStats }: TeamLoadPanelProps) {
const t = useI18n()
return (
<Card className="rounded-md shadow-none">
<CardHeader>
<CardTitle>{t("dashboardHome.teamLoad")}</CardTitle>
<CardDescription>
{t("dashboardHome.teamLoadDescription", {
online: agentStats.onlineAgents,
busy: agentStats.busyAgents,
offline: agentStats.offlineAgents,
})}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{agentStats.teamLoads.length === 0 ? (
<div className="rounded-md border border-dashed px-4 py-8 text-center text-sm text-muted-foreground">
{t("dashboardHome.noTeamData")}
</div>
) : (
agentStats.teamLoads.map((item) => (
<div key={item.teamId} className="rounded-md border p-4">
<div className="flex flex-wrap items-center justify-between gap-3">
<div>
<div className="flex items-center gap-2">
<div className="font-medium">{item.teamName}</div>
{item.hasScheduleNow ? (
<Badge variant="secondary">{t("dashboardHome.scheduled")}</Badge>
) : (
<Badge variant="outline">{t("dashboardHome.notScheduled")}</Badge>
)}
</div>
<div className="mt-1 text-sm text-muted-foreground">
{t("dashboardHome.teamStats", {
total: item.totalAgents,
online: item.onlineAgents,
busy: item.busyAgents,
offline: item.offlineAgents,
})}
</div>
</div>
<div className="text-right">
<div className="text-2xl font-semibold">{item.loadRate.toFixed(1)}%</div>
<div className="text-xs text-muted-foreground">
{t("dashboardHome.load", {
processing: item.processingConversations,
capacity: item.maxConcurrentCapacity || 0,
})}
</div>
</div>
</div>
<div className="mt-4 h-2 rounded-full bg-muted">
<div
className={`h-2 rounded-full ${getLoadTone(item.loadRate)}`}
style={{ width: `${Math.min(item.loadRate, 100)}%` }}
/>
</div>
<div className="mt-4 grid gap-3 text-sm md:grid-cols-2 xl:grid-cols-4">
<div className="rounded-md bg-muted/35 px-3 py-2">
<div className="text-muted-foreground">{t("dashboardHome.waiting")}</div>
<div className="mt-1 text-lg font-semibold">{item.waitingConversations}</div>
</div>
<div className="rounded-md bg-muted/35 px-3 py-2">
<div className="text-muted-foreground">{t("dashboardHome.processing")}</div>
<div className="mt-1 text-lg font-semibold">{item.processingConversations}</div>
</div>
<div className="rounded-md bg-muted/35 px-3 py-2">
<div className="text-muted-foreground">{t("dashboardHome.capacity")}</div>
<div className="mt-1 text-lg font-semibold">{item.maxConcurrentCapacity}</div>
</div>
<div className="rounded-md bg-muted/35 px-3 py-2">
<div className="text-muted-foreground">{t("dashboardHome.busyAgents")}</div>
<div className="mt-1 text-lg font-semibold">{item.busyAgents}</div>
</div>
</div>
</div>
))
)}
</CardContent>
</Card>
)
}

View File

@@ -0,0 +1,141 @@
"use client"
import { Area, AreaChart, Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"
import type { DashboardStatusDistributionItem, DashboardTrendItem } from "@/lib/api/dashboard"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
import { useI18n } from "@/i18n/provider"
type TrendPanelProps = {
title: string
description: string
trend: DashboardTrendItem[]
distribution: DashboardStatusDistributionItem[]
}
export function TrendPanel({
title,
description,
trend,
distribution,
}: TrendPanelProps) {
const t = useI18n()
const trendConfig = {
newCount: {
label: t("dashboardHome.chartNew"),
color: "hsl(24 95% 53%)",
},
closedCount: {
label: t("dashboardHome.chartClosed"),
color: "hsl(190 95% 39%)",
},
} satisfies ChartConfig
const distributionConfig = {
count: {
label: t("dashboardHome.chartCount"),
theme: {
light: "hsl(222 47% 11%)",
dark: "hsl(210 40% 98%)",
},
},
} satisfies ChartConfig
const localizedDistribution = distribution.map((item) => ({
...item,
label: getStatusLabel(item.status, item.label, t),
}))
return (
<div className="grid gap-4 xl:grid-cols-[1.5fr_0.9fr]">
<Card className="rounded-md shadow-none">
<CardHeader>
<CardTitle>{title}</CardTitle>
<CardDescription>{description}</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={trendConfig} className="h-72 w-full">
<AreaChart data={trend}>
<CartesianGrid vertical={false} />
<XAxis dataKey="date" tickLine={false} axisLine={false} tickMargin={8} />
<ChartTooltip content={<ChartTooltipContent />} />
<Area
type="monotone"
dataKey="newCount"
stroke="var(--color-newCount)"
fill="var(--color-newCount)"
fillOpacity={0.18}
strokeWidth={2}
/>
<Area
type="monotone"
dataKey="closedCount"
stroke="var(--color-closedCount)"
fill="var(--color-closedCount)"
fillOpacity={0.08}
strokeWidth={2}
/>
</AreaChart>
</ChartContainer>
</CardContent>
</Card>
<Card className="rounded-md shadow-none">
<CardHeader>
<CardTitle>{t("dashboardHome.statusDistribution")}</CardTitle>
<CardDescription>{t("dashboardHome.statusDistributionDescription")}</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
<ChartContainer config={distributionConfig} className="h-72 w-full">
<BarChart data={localizedDistribution} layout="vertical" margin={{ left: 20 }}>
<CartesianGrid horizontal={false} />
<YAxis
type="category"
dataKey="label"
tickLine={false}
axisLine={false}
width={64}
/>
<XAxis type="number" hide />
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<Bar
dataKey="count"
fill="var(--color-count)"
radius={[0, 8, 8, 0]}
/>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
</div>
)
}
function getStatusLabel(
status: number,
fallback: string,
t: (key: string) => string
) {
switch (status) {
case 1:
return t("dashboardHome.statusAiServing")
case 2:
return t("dashboardHome.statusPending")
case 3:
return t("dashboardHome.statusActive")
case 4:
return t("dashboardHome.statusClosed")
default:
return fallback
}
}