"use client"; import { useAuthQuery } from "@/hooks/use-auth-query"; import { useFormatter } from "next-intl"; import { Activity, Coins } from "lucide-react"; import { useEffect, useState } from "react"; import { Badge } from "@/components/ui/badge"; import { api } from "@/convex/_generated/api"; import { cn } from "@/lib/utils"; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- function statusBadge(status: string) { switch (status) { case "committed": return Abgeschlossen; case "reserved": return ( Reserviert ); case "released": return ( Rückerstattet ); case "failed": return Fehlgeschlagen; default: return Unbekannt; } } function truncatedDescription(text: string, maxLen = 40) { if (text.length <= maxLen) return text; return text.slice(0, maxLen) + "…"; } // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- export function RecentTransactions() { const format = useFormatter(); const [now, setNow] = useState(null); const transactions = useAuthQuery(api.credits.getRecentTransactions, { limit: 10, }); useEffect(() => { const updateNow = () => { setNow(Date.now()); }; const initialTimeout = window.setTimeout(updateNow, 0); const interval = window.setInterval(updateNow, 60_000); return () => { window.clearTimeout(initialTimeout); window.clearInterval(interval); }; }, []); const formatEurFromCents = (cents: number) => format.number(cents / 100, { style: "currency", currency: "EUR" }); const formatRelativeTime = (timestamp: number) => { const referenceNow = now ?? timestamp; const diff = referenceNow - timestamp; const minutes = Math.floor(diff / 60000); const hours = Math.floor(diff / 3600000); const days = Math.floor(diff / 86400000); if (minutes < 1) return "Gerade eben"; if (minutes < 60) return `vor ${minutes} Min.`; if (hours < 24) return `vor ${hours} Std.`; if (days < 7) return days === 1 ? "vor 1 Tag" : `vor ${days} Tagen`; return format.dateTime(timestamp, { day: "numeric", month: "short" }); }; // ── Loading State ────────────────────────────────────────────────────── if (transactions === undefined) { return ( Letzte Aktivität {Array.from({ length: 5 }).map((_, i) => ( ))} ); } // ── Empty State ──────────────────────────────────────────────────────── if (transactions.length === 0) { return ( Letzte Aktivität Noch keine Aktivität Erstelle dein erstes Bild im Canvas (Prompt-Knoten) ); } // ── Transaction List ─────────────────────────────────────────────────── return ( Letzte Aktivität {transactions.map((t: NonNullable[number]) => { const isCredit = t.amount > 0; return ( {/* Status Indicator */} {statusBadge(t.status)} {/* Description */} {truncatedDescription(t.description)} {formatRelativeTime(t._creationTime)} {/* Credits */} {isCredit ? "+" : "−"} {formatEurFromCents(Math.abs(t.amount))} ); })} ); }
Noch keine Aktivität
Erstelle dein erstes Bild im Canvas (Prompt-Knoten)
{truncatedDescription(t.description)}
{formatRelativeTime(t._creationTime)}