"use client"; import { useState } from "react"; import { Card, CardContent, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { RefreshCw } from "lucide-react"; import { refetchPlatform } from "./actions"; import { toast } from "sonner"; interface Platform { name: string; cachedCount: number | null; } function formatCount(n: number | null): string { if (n === null) return "No data"; if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`; return n.toString(); } export function BackendClient({ platforms }: { platforms: Platform[] }) { const [data, setData] = useState(platforms); const [loading, setLoading] = useState(null); const handleRefetch = async (platform: string) => { setLoading(platform); try { const result = await refetchPlatform(platform); setData((prev) => prev.map((p) => p.name === platform ? { ...p, cachedCount: result.count } : p ) ); toast.success(`${platform} refetched successfully`); } catch { toast.error(`Failed to refetch ${platform}`); } finally { setLoading(null); } }; return (
{data.map((p) => ( {p.name} {formatCount(p.cachedCount)} ))}
); }