"use client"; import { Suspense, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import SiteShell from "@community/components/SiteShell"; import { useAuth } from "@community/lib/auth"; import { useI18n } from "@community/lib/i18n"; import { api } from "@community/lib/api"; type DlFile = { format: string; label: string; url: string }; function isRealDownload(f: DlFile) { const fmt = (f.format || "").toLowerCase(); return fmt === "pdf" || fmt === "epub" || (Boolean(f.url) && !f.url.startsWith("/book/read")); } function BookThanksInner() { const { token } = useAuth(); const { t } = useI18n(); const b = t.bookThanks; const router = useRouter(); const params = useSearchParams(); const [status, setStatus] = useState<"checking" | "ok" | "pending" | "need-login">("checking"); const [files, setFiles] = useState([]); const [msg, setMsg] = useState(""); useEffect(() => { if (!token) { setStatus("need-login"); return; } const orderId = params.get("order_id") || localStorage.getItem("nomadro-ebook-order") || ""; let cancelled = false; (async () => { try { if (orderId) { await api.completePayment(token, orderId).catch(() => null); } for (let i = 0; i < 5; i++) { if (cancelled) return; const st = await api.ebookStatus(token); if (st.owned) { const dl = await api.ebookDownloads(token); if (cancelled) return; setFiles(dl.files || []); setStatus("ok"); localStorage.removeItem("nomadro-ebook-order"); return; } await new Promise((r) => setTimeout(r, 1000)); } if (!cancelled) { setStatus("pending"); setMsg(b.pending); } } catch (e) { if (!cancelled) { setStatus("pending"); setMsg(e instanceof Error ? e.message : b.statusFail); } } })(); return () => { cancelled = true; }; }, [token, params, b.pending, b.statusFail]); const downloads = useMemo(() => files.filter(isRealDownload), [files]); const hasOnlineOnly = status === "ok" && downloads.length === 0; return (

{b.title}

{status === "need-login" && (

{b.needLogin}{" "} {t.nav.login}

)} {status === "checking" &&

{b.checking}

} {status === "pending" && (

{msg}

)} {hasOnlineOnly && (

{b.confirmedNoFiles}

{b.confirmedHint}

{b.continueRead} {b.contact}
)} {status === "ok" && downloads.length > 0 && (

{b.ready}

{b.continueRead} →

)}
); } export default function BookThanksPage() { const { t } = useI18n(); return ( {t.bookThanks.checking}}> ); }