import { revalidatePath } from "next/cache"; import Link from "next/link"; import { listFolders, createLibrary, deleteLibrary, fetchLibraries, fetchSeries, getBookCoverUrl, LibraryDto, FolderItem } from "../../lib/api"; import type { TranslationKey } from "../../lib/i18n/fr"; import { getServerTranslations } from "../../lib/i18n/server"; import { LibraryActions } from "../components/LibraryActions"; import { LibraryForm } from "../components/LibraryForm"; import { ProviderIcon } from "../components/ProviderIcon"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, Button, Badge } from "../components/ui"; export const dynamic = "force-dynamic"; function formatNextScan(nextScanAt: string | null, imminentLabel: string): string { if (!nextScanAt) return "-"; const date = new Date(nextScanAt); const now = new Date(); const diff = date.getTime() - now.getTime(); if (diff < 0) return imminentLabel; if (diff < 60000) return "< 1 min"; if (diff < 3600000) return `${Math.floor(diff / 60000)}m`; if (diff < 86400000) return `${Math.floor(diff / 3600000)}h`; return `${Math.floor(diff / 86400000)}d`; } export default async function LibrariesPage() { const { t } = await getServerTranslations(); const [libraries, folders] = await Promise.all([ fetchLibraries().catch(() => [] as LibraryDto[]), listFolders().catch(() => [] as FolderItem[]) ]); const seriesData = await Promise.all( libraries.map(async (lib) => { try { const seriesPage = await fetchSeries(lib.id, 1, 6); return { id: lib.id, count: seriesPage.total, thumbnails: seriesPage.items .map(s => s.first_book_id) .filter(Boolean) .slice(0, 4) .map(bookId => getBookCoverUrl(bookId)), }; } catch { return { id: lib.id, count: 0, thumbnails: [] as string[] }; } }) ); const seriesMap = new Map(seriesData.map(s => [s.id, s])); async function addLibrary(formData: FormData) { "use server"; const name = formData.get("name") as string; const rootPath = formData.get("root_path") as string; if (name && rootPath) { await createLibrary(name, rootPath); revalidatePath("/libraries"); } } async function removeLibrary(formData: FormData) { "use server"; const id = formData.get("id") as string; await deleteLibrary(id); revalidatePath("/libraries"); } return ( <>
{lib.root_path}