"use client";
import { useAuthQuery } from "@/hooks/use-auth-query";
import { useFormatter } from "next-intl";
import { Activity, Coins } from "lucide-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 transactions = useAuthQuery(api.credits.getRecentTransactions, {
limit: 10,
});
const formatEurFromCents = (cents: number) =>
format.number(cents / 100, { style: "currency", currency: "EUR" });
const formatRelativeTime = (timestamp: number) => {
const now = Date.now();
const diff = now - 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 (
{Array.from({ length: 5 }).map((_, i) => (
))}
);
}
// ── Empty State ────────────────────────────────────────────────────────
if (transactions.length === 0) {
return (
Noch keine Aktivität
Erstelle dein erstes Bild im Canvas (Prompt-Knoten)
);
}
// ── Transaction List ───────────────────────────────────────────────────
return (
{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))}
);
})}
);
}