import React from "react";
import { fetchStats, StatsResponse } from "../lib/api";
import { Card, CardContent, CardHeader, CardTitle } from "./components/ui";
import Link from "next/link";
import { getServerTranslations } from "../lib/i18n/server";
import type { TranslateFunction } from "../lib/i18n/dictionaries";
export const dynamic = "force-dynamic";
function formatBytes(bytes: number): string {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`;
}
function formatNumber(n: number, locale: string): string {
return n.toLocaleString(locale === "fr" ? "fr-FR" : "en-US");
}
// Donut chart via SVG
function DonutChart({ data, colors, noDataLabel, locale = "fr" }: { data: { label: string; value: number; color: string }[]; colors?: string[]; noDataLabel?: string; locale?: string }) {
const total = data.reduce((sum, d) => sum + d.value, 0);
if (total === 0) return
{noDataLabel}
;
const radius = 40;
const circumference = 2 * Math.PI * radius;
let offset = 0;
return (
{data.map((d, i) => (
{d.label}
{d.value}
))}
);
}
// Bar chart via pure CSS
function BarChart({ data, color = "var(--color-primary)", noDataLabel }: { data: { label: string; value: number }[]; color?: string; noDataLabel?: string }) {
const max = Math.max(...data.map((d) => d.value), 1);
if (data.length === 0) return {noDataLabel}
;
return (
{data.map((d, i) => (
{d.value || ""}
{d.label}
))}
);
}
// Horizontal progress bar for library breakdown
function HorizontalBar({ label, value, max, subLabel, color = "var(--color-primary)" }: { label: string; value: number; max: number; subLabel?: string; color?: string }) {
const pct = max > 0 ? (value / max) * 100 : 0;
return (
{label}
{subLabel || value}
);
}
export default async function DashboardPage() {
const { t, locale } = await getServerTranslations();
let stats: StatsResponse | null = null;
try {
stats = await fetchStats();
} catch (e) {
console.error("Failed to fetch stats:", e);
}
if (!stats) {
return (
StripStream Backoffice
{t("dashboard.loadError")}
);
}
const { overview, reading_status, by_format, by_language, by_library, top_series, additions_over_time, metadata } = stats;
const readingColors = ["hsl(220 13% 70%)", "hsl(45 93% 47%)", "hsl(142 60% 45%)"];
const formatColors = [
"hsl(198 78% 37%)", "hsl(142 60% 45%)", "hsl(45 93% 47%)",
"hsl(2 72% 48%)", "hsl(280 60% 50%)", "hsl(32 80% 50%)",
"hsl(170 60% 45%)", "hsl(220 60% 50%)",
];
const maxLibBooks = Math.max(...by_library.map((l) => l.book_count), 1);
const noDataLabel = t("common.noData");
return (
{/* Header */}
{t("dashboard.title")}
{t("dashboard.subtitle")}
{/* Overview stat cards */}
{/* Charts row */}
{/* Reading status donut */}
{t("dashboard.readingStatus")}
{/* By format donut */}
{t("dashboard.byFormat")}
({
label: (f.format || t("dashboard.unknown")).toUpperCase(),
value: f.count,
color: formatColors[i % formatColors.length],
}))}
/>
{/* By library donut */}
{t("dashboard.byLibrary")}
({
label: l.library_name,
value: l.book_count,
color: formatColors[i % formatColors.length],
}))}
/>
{/* Metadata row */}
{/* Series metadata coverage donut */}
{t("dashboard.metadataCoverage")}
{/* By provider donut */}
{t("dashboard.byProvider")}
({
label: p.provider.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase()),
value: p.count,
color: formatColors[i % formatColors.length],
}))}
/>
{/* Book metadata quality */}
{t("dashboard.bookMetadata")}
0 ? `${Math.round((metadata.books_with_summary / overview.total_books) * 100)}%` : "0%"}
color="hsl(198 78% 37%)"
/>
0 ? `${Math.round((metadata.books_with_isbn / overview.total_books) * 100)}%` : "0%"}
color="hsl(280 60% 50%)"
/>
{/* Second row */}
{/* Monthly additions bar chart */}
{t("dashboard.booksAdded")}
({
label: m.month.slice(5), // "MM" from "YYYY-MM"
value: m.books_added,
}))}
color="hsl(198 78% 37%)"
/>
{/* Top series */}
{t("dashboard.popularSeries")}
{top_series.slice(0, 8).map((s, i) => (
))}
{top_series.length === 0 && (
{t("dashboard.noSeries")}
)}
{/* Libraries breakdown */}
{by_library.length > 0 && (
{t("dashboard.libraries")}
{by_library.map((lib, i) => (
{lib.library_name}
{formatBytes(lib.size_bytes)}
{lib.book_count} {t("dashboard.books").toLowerCase()}
{lib.read_count} {t("status.read").toLowerCase()}
{lib.reading_count} {t("status.reading").toLowerCase()}
))}
)}
{/* Quick links */}
);
}
function StatCard({ icon, label, value, color }: { icon: string; label: string; value: string; color: string }) {
const icons: Record = {
book: ,
series: ,
library: ,
pages: ,
author: ,
size: ,
};
const colorClasses: Record = {
primary: "bg-primary/10 text-primary",
success: "bg-success/10 text-success",
warning: "bg-warning/10 text-warning",
};
return (
);
}
function QuickLinks({ t }: { t: TranslateFunction }) {
const links = [
{ href: "/libraries", label: t("nav.libraries"), bg: "bg-primary/10", text: "text-primary", hoverBg: "group-hover:bg-primary", hoverText: "group-hover:text-primary-foreground", icon: },
{ href: "/books", label: t("nav.books"), bg: "bg-success/10", text: "text-success", hoverBg: "group-hover:bg-success", hoverText: "group-hover:text-white", icon: },
{ href: "/series", label: t("nav.series"), bg: "bg-warning/10", text: "text-warning", hoverBg: "group-hover:bg-warning", hoverText: "group-hover:text-white", icon: },
{ href: "/jobs", label: t("nav.jobs"), bg: "bg-destructive/10", text: "text-destructive", hoverBg: "group-hover:bg-destructive", hoverText: "group-hover:text-destructive-foreground", icon: },
];
return (
{links.map((l) => (
{l.label}
))}
);
}