Replace reverse-proxy/iframe approach with @ebook/@community/@navmini imports under /nav /book /community so localhost:3000 serves all three products directly. Co-authored-by: Cursor <cursoragent@cursor.com>
138 lines
4.2 KiB
TypeScript
138 lines
4.2 KiB
TypeScript
"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<DlFile[]>([]);
|
|
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 (
|
|
<div className="container" style={{ padding: "48px 24px", maxWidth: 560 }}>
|
|
<nav className="detail-nav">
|
|
<Link href="/book">← {t.nav.ebook}</Link>
|
|
</nav>
|
|
<h1>{b.title}</h1>
|
|
{status === "need-login" && (
|
|
<p>
|
|
{b.needLogin}{" "}
|
|
<Link href="/community/login?next=/book/thanks">{t.nav.login}</Link>
|
|
</p>
|
|
)}
|
|
{status === "checking" && <p>{b.checking}</p>}
|
|
{status === "pending" && (
|
|
<div>
|
|
<p>{msg}</p>
|
|
<button type="button" className="btn btn-primary" onClick={() => router.refresh()}>
|
|
{b.refresh}
|
|
</button>
|
|
</div>
|
|
)}
|
|
{hasOnlineOnly && (
|
|
<div>
|
|
<p>{b.confirmedNoFiles}</p>
|
|
<p className="section-desc">{b.confirmedHint}</p>
|
|
<div className="dating-empty-actions">
|
|
<Link href="/book/read" className="btn btn-primary">
|
|
{b.continueRead}
|
|
</Link>
|
|
<Link href="/community/feedback" className="btn btn-ghost">
|
|
{b.contact}
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
)}
|
|
{status === "ok" && downloads.length > 0 && (
|
|
<div>
|
|
<p>{b.ready}</p>
|
|
<ul className="ebook-download-list">
|
|
{downloads.map((f) => (
|
|
<li key={f.format}>
|
|
<a href={f.url} className="btn btn-primary btn-sm" target="_blank" rel="noreferrer">
|
|
{f.label}
|
|
</a>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
<p style={{ marginTop: 16 }}>
|
|
<Link href="/book/read">{b.continueRead} →</Link>
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function BookThanksPage() {
|
|
const { t } = useI18n();
|
|
return (
|
|
<SiteShell>
|
|
<Suspense fallback={<div className="container" style={{ padding: 48 }}>{t.bookThanks.checking}</div>}>
|
|
<BookThanksInner />
|
|
</Suspense>
|
|
</SiteShell>
|
|
);
|
|
}
|