"use client"; import { useState } from "react"; import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Field, FieldDescription, FieldGroup, FieldLabel, } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Spinner } from "@/components/ui/spinner"; import { Bomb, RefreshCw } from "lucide-react"; import { forceRefresh, 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 [pathPrefix, setPathPrefix] = useState(""); const [reloadTarget, setReloadTarget] = useState<"all" | "path" | null>(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); } }; const handleForceRefresh = async ( target: "all" | "path", prefix: string | null, ) => { setReloadTarget(target); try { await forceRefresh(prefix); toast.success( prefix ? `Reload sent for ${prefix}` : "Reload sent to all clients", ); } catch (error) { toast.error(error instanceof Error ? error.message : "Failed to send reload"); } finally { setReloadTarget(null); } }; const normalizedPathPrefix = pathPrefix.trim(); const pathIsValid = normalizedPathPrefix.startsWith("/"); return (
{data.map((p) => ( {p.name} {formatCount(p.cachedCount)} ))}
Client reload Reload every connected client or only clients under a pathname prefix. 0 && !pathIsValid}> Path prefix setPathPrefix(event.target.value)} placeholder="/admin" aria-invalid={pathPrefix.length > 0 && !pathIsValid} /> Reloads clients whose pathname starts with this value.
); }