nomadro/apps/ebook/src/components/BookEnd.tsx
eric 0fc608ea26 Fix ebook /read links to /book/read and add compat redirects.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 18:54:03 -05:00

65 lines
2.3 KiB
TypeScript

'use client';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { ShareToX } from './ShareToX';
import { getReadingProgress } from '@ebook/lib/reading-progress';
import { getBookShareText, getReadShareUrl } from '@ebook/lib/share-text';
import { trackEvent } from '@ebook/lib/analytics';
import { CryptoSupport } from './CryptoSupport';
import { BuyEbook } from './BuyEbook';
import { isEnabled } from '@ebook/lib/features';
import { useT } from '@ebook/components/LocaleProvider';
import type { Book } from '@ebook/lib/book-types';
export function BookEnd({ book }: { book: Book }) {
const t = useT();
const [readCount, setReadCount] = useState(0);
useEffect(() => {
const saved = getReadingProgress();
setReadCount(saved?.readChapters.length ?? 0);
if (saved && saved.readChapters.length >= book.chapters.length) {
trackEvent('book_complete');
}
}, [book.chapters.length]);
const shareText = `${t('cover.finishedNow', { title: book.title })} 📖\n${book.tagline}\n\n${getBookShareText(book).split('\n').slice(2).join('\n')}`;
const done = readCount >= book.chapters.length;
return (
<footer className="book-end">
<p className="book-end-emoji" aria-hidden>
{done ? '🌅' : '📖'}
</p>
<p className="book-end-text">{done ? t('reader.bookFinished') : t('reader.lastChapter')}</p>
<p className="book-end-sub">
{done
? t('cover.shareDone', { tagline: book.tagline })
: t('cover.flipBack', { read: readCount, total: book.chapters.length })}
</p>
<div className="book-end-actions">
<ShareToX text={shareText} url={getReadShareUrl()} label={t('reader.shareThoughts')} />
<button
type="button"
className="book-end-btn"
onClick={() => {
const first = book.chapters[0]?.slug;
if (first) {
document.getElementById(first)?.scrollIntoView({ behavior: 'smooth' });
history.replaceState(null, '', `/book/read#${first}`);
}
}}
>
{t('reader.readFromStart')}
</button>
<Link href="/book" className="book-end-btn">
{t('thanks.backCover')}
</Link>
</div>
{isEnabled.checkoutPay() && <BuyEbook compact />}
{isEnabled.cryptoPay() && <CryptoSupport compact />}
</footer>
);
}