import { Link } from 'react-router-dom'; import React, { useEffect, useState } from 'react'; import type { IconType } from 'react-icons'; import { FaArrowDown, FaArrowRight, FaArrowUp, FaChartLine, FaMousePointer, FaPercent, FaRegCommentDots, FaRegThumbsUp, FaShoppingCart, FaUsers, } from 'react-icons/fa'; import { formatNumber, formatPercentage, getDateRangeFromPreset, } from '../agent-analytics/helpers'; import type { IAgentDashboardData, IAgentOverview, IAgentOverviewTrends, } from '../../service/agent-analytics/agent-analytics.interface'; import { fetchAgentDashboard } from '../../service/agent-analytics/agent-analytics.service'; import InfoTooltip from './InfoTooltip'; interface CardMeta { key: keyof IAgentOverview & keyof IAgentOverviewTrends; label: string; icon: IconType; format: 'number' | 'percent'; tooltip: string; iconClass: string; chipClass: string; } const CARD_META: CardMeta[] = [ { key: 'total_conversations', label: 'Conversations', icon: FaRegCommentDots, format: 'number', tooltip: 'Total chat conversations started by visitors in the last 7 days.', iconClass: 'text-pink-500', chipClass: 'bg-pink-50 ring-pink-100', }, { key: 'unique_visitors', label: 'Unique Visitors', icon: FaUsers, format: 'number', tooltip: 'Distinct users who interacted with the AI agent.', iconClass: 'text-sky-500', chipClass: 'bg-sky-50 ring-sky-100', }, { key: 'real_conversion_rate', label: 'Conversion Rate', icon: FaPercent, format: 'percent', tooltip: 'Share of conversations that led to a purchase.', iconClass: 'text-emerald-500', chipClass: 'bg-emerald-50 ring-emerald-100', }, { key: 'satisfaction_rate', label: 'Satisfaction', icon: FaRegThumbsUp, format: 'percent', tooltip: 'Likes divided by total feedback votes on agent messages. A handful of votes can swing it.', iconClass: 'text-amber-500', chipClass: 'bg-amber-50 ring-amber-100', }, { key: 'product_clicks', label: 'Product Clicks', icon: FaMousePointer, format: 'number', tooltip: 'Number of product recommendations clicked by users.', iconClass: 'text-violet-500', chipClass: 'bg-violet-50 ring-violet-100', }, { key: 'add_to_cart_count', label: 'Add to Cart', icon: FaShoppingCart, format: 'number', tooltip: 'Products added to cart directly from agent recommendations.', iconClass: 'text-fuchsia-500', chipClass: 'bg-fuchsia-50 ring-fuchsia-100', }, ]; /** * Last-7-day at-a-glance stat cards for the dashboard. Fetches the agent * dashboard client-side (same service the Agent Analytics tab uses). * * @param {object} props - Component props. * @param {string} props.token - Recomaze JWT. * @return {React.ReactElement} The stat-card section. */ export function OverviewStatCards({ token }: { token: string }) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!token) return; let cancelled = false; const range = getDateRangeFromPreset('7d'); setLoading(true); void (async () => { try { const dashboard = await fetchAgentDashboard(range.start, range.end); if (!cancelled) setData(dashboard); } catch { // Benign: the store may have no agent analytics yet. } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, [token]); const overview = data?.overview ?? null; const trends = data?.overview_trends ?? {}; return (

Performance at a glance

Last 7 days ยท vs the previous 7

View analytics
{CARD_META.map(meta => ( ))}
); } function StatCard({ meta, overview, delta, loading, }: { meta: CardMeta; overview: IAgentOverview | null; delta: number | null; loading: boolean; }) { const Icon = meta.icon; const raw = overview ? overview[meta.key] : 0; const value = meta.format === 'percent' ? formatPercentage(raw) : formatNumber(raw); return (
{!loading && }

{meta.label}

{loading ? (
) : (

{value}

)}
); } function DeltaBadge({ delta }: { delta: number | null }) { if (delta == null || delta === 0) { return ( -- ); } const isUp = delta > 0; return ( {isUp ? ( ) : ( )} {isUp ? '+' : '-'} {Math.abs(delta).toFixed(1)}% ); }