11import { useEffect, useRef, useState } from 'react'
22import { ArrowLeft, CalendarDays, MoreHorizontal, PenLine, Split, Trash2, Image as ImageIcon, Loader2, Merge, Quote, Search, Star, X } from 'lucide-react'
33import { api, isApp, untitled } from './api.js'
4- import { renderCard } from './shareCard.js'
4+ import { renderCard, renderProgressCard } from './shareCard.js'
55import { isPace, moodEmoji } from './moods.js'
66import ShareSheet from './ShareSheet.jsx'
77import { useNativeBack, useNativeButton } from './tabbar.js'
@@ -115,6 +115,65 @@ export function ClipShare({ session, book, clip, onClose }) {
115115 )
116116}
117117
118+ // What the reading-progress card shows, from the book, its sync activity and its
119+ // device stats (`stats` is /stats/books/:document's answer, or null).
120+ export function progressCard(book, a, stats) {
121+ const c = stats?.combined
122+ const progress = book.percentage ?? 0
123+ const total = a?.page_count ?? book.page_count
124+ // From the shown percentage, not activity's pages_read (the furthest ever reached), so a reread matches.
125+ const read = total ? Math.round(progress * total) : null
126+ // The freshest device's own estimate of the time left.
127+ const eta = [...(stats?.devices ?? [])].sort((x, y) => y.updated_at - x.updated_at).find((d) => d.stats?.eta > 0)?.stats.eta
128+ const started = a?.start_manual ? a.started_at : c?.start_date || a?.started_at
129+ const days = started ? Math.max(1, Math.round((Date.now() / 1000 - started) / 86400)) : null
130+ const tiles = [
131+ c?.seconds > 0 && ['time read', duration(c.seconds)],
132+ days && ['days reading', days.toLocaleString()],
133+ c?.sessions > 0 && ['sessions', c.sessions.toLocaleString()],
134+ c?.avg_fwd > 0 && ['per page', c.avg_fwd < 60 ? `${c.avg_fwd}s` : duration(c.avg_fwd)],
135+ !c?.sessions && started && ['started', date(started, { year: undefined })],
136+ ].filter(Boolean)
137+ return {
138+ title: book.title || book.filename || 'Untitled book',
139+ author: book.author,
140+ series: seriesLabel(book),
141+ progress,
142+ pages: total && read != null ? `Page ${Math.min(read, total).toLocaleString()} of ${total.toLocaleString()}` : null,
143+ left: eta ? `${duration(eta)} left` : null,
144+ tiles,
145+ moods: (book.moods ?? []).filter((m) => !isPace(m)).slice(0, 3),
146+ }
147+ }
148+
149+ // The currently reading book's progress card, straight to the native share sheet where it can.
150+ function ProgressShare({ session, book, activity, onClose }) {
151+ const title = book.title || book.filename || 'Untitled book'
152+ const meta = {
153+ title,
154+ author: book.author,
155+ postTitle: `Currently reading ${title}`,
156+ fileName: `${title} progress.png`,
157+ text: `${pct(book.percentage ?? 0)} through ${title}${book.author ? ` by ${book.author}` : ''}. Tracked with CrossPoint Sync.`,
158+ }
159+ return (
160+ <ShareSheet
161+ heading="Share your progress"
162+ meta={meta}
163+ renderKey={`${book.document}-${book.percentage}`}
164+ onClose={onClose}
165+ autoShare
166+ render={async () => {
167+ const [coverUrl, stats] = await Promise.all([
168+ book.cover_url ?? api.cover(session, book.document).then((r) => r.url, () => null),
169+ api.bookStats(session, book.document).catch(() => null),
170+ ])
171+ return renderProgressCard({ ...progressCard(book, activity, stats), coverUrl })
172+ }}
173+ />
174+ )
175+ }
176+
118177// Touch: press and hold a clipping to delete it (desktop uses ClipMenu). One press at a time, so one timer.
119178let holdTimer
120179export function hold(fn) {
@@ -656,28 +715,41 @@ function EditDetailsSheet({ session, book: current, onDone, onClose }) {
656715
657716function BookMenu({ session, book, books, activity, onChange }) {
658717 const [action, setAction] = useState(null)
659- const [open, setOpen] = useState(null) // 'edit' | 'cover' | 'dates' | 'merge'
718+ const [open, setOpen] = useState(null) // 'share' | ' edit' | 'cover' | 'dates' | 'merge'
660719 const [menu, setMenu] = useState(false)
661- useNativeButton('more', { sfSymbol: 'ellipsis', anchor: 'topTrailing' }, () => setMenu((m) => !m))
662720 const dupes = books.filter((b) => looksLikeSame(book, b)).length
721+ const separate = book.aliases?.length ?? 0
722+ // One list for the iPhone's native menu and the HTML menu elsewhere.
723+ const entries = [
724+ book.status === 'reading' && !book.history_only && { id: 'share', title: 'Share', Icon: ShareIcon, sfSymbol: 'square.and.arrow.up', run: () => setOpen('share') },
725+ { id: 'edit', title: 'Edit details', Icon: PenLine, sfSymbol: 'pencil', run: () => setOpen('edit') },
726+ { id: 'cover', title: 'Change cover', Icon: ImageIcon, sfSymbol: 'photo', run: () => setOpen('cover') },
727+ { id: 'dates', title: 'Reading dates', Icon: CalendarDays, sfSymbol: 'calendar', run: () => setOpen('dates') },
728+ { id: 'merge', title: 'Merge a duplicate', Icon: Merge, sfSymbol: 'arrow.triangle.merge', badge: dupes > 0 ? `${dupes} likely` : null, run: () => setOpen('merge') },
729+ separate > 0 && {
730+ id: 'separate',
731+ title: separate === 1 ? 'Separate merged copy' : `Separate ${separate} merged copies`,
732+ Icon: Split,
733+ sfSymbol: 'arrow.triangle.branch',
734+ run: () => setAction({ kind: 'separate', aliases: [...book.aliases] }),
735+ },
736+ { id: 'remove', title: 'Remove from library', Icon: Trash2, sfSymbol: 'trash', destructive: true, run: () => setAction({ kind: 'remove', document: book.document, title: book.title }) },
737+ ].filter(Boolean)
738+ useNativeButton(
739+ 'more',
740+ {
741+ sfSymbol: 'ellipsis',
742+ anchor: 'topTrailing',
743+ label: 'More',
744+ menu: entries.map(({ id, title, sfSymbol, badge, destructive }) => ({ id, title, sfSymbol, ...(badge ? { subtitle: badge } : {}), ...(destructive ? { destructive } : {}) })),
745+ },
746+ () => setMenu((m) => !m),
747+ (id) => entries.find((e) => e.id === id)?.run()
748+ )
663749 const done = () => {
664750 setOpen(null)
665751 onChange()
666752 }
667- const item = (Icon, label, run, extra = null, danger = false) => (
668- <button
669- type="button"
670- role="menuitem"
671- onClick={() => {
672- setMenu(false)
673- run()
674- }}
675- className={`flex w-full items-center gap-3 px-4 py-3 text-left text-sm font-medium active:bg-stone-50 md:hover:bg-stone-50 ${danger ? 'text-red-600' : 'text-stone-800'}`}
676- >
677- <Icon className={`size-4 ${danger ? '' : 'text-stone-400'}`} /> {label}
678- {extra}
679- </button>
680- )
681753
682754 return (
683755 <div className="relative ml-auto">
@@ -695,18 +767,21 @@ function BookMenu({ session, book, books, activity, onChange }) {
695767 <>
696768 <div className="fixed inset-0 z-30" onClick={() => setMenu(false)} />
697769 <div role="menu" className="native-menu absolute top-12 right-0 z-30 w-64 divide-y divide-stone-100 overflow-hidden rounded-xl bg-surface shadow-lg ring-1 ring-stone-950/10">
698- {item(PenLine, 'Edit details', () => setOpen('edit'))}
699- {item(ImageIcon, 'Change cover', () => setOpen('cover'))}
700- {item(CalendarDays, 'Reading dates', () => setOpen('dates'))}
701- {item(
702- Merge,
703- 'Merge a duplicate',
704- () => setOpen('merge'),
705- dupes > 0 && <span className="ml-auto rounded-full bg-brand-50 px-2 py-0.5 text-[0.65rem] font-semibold text-brand-700">{dupes} likely</span>
706- )}
707- {book.aliases?.length > 0 &&
708- item(Split, book.aliases.length === 1 ? 'Separate merged copy' : `Separate ${book.aliases.length} merged copies`, () => setAction({ kind: 'separate', aliases: [...book.aliases] }))}
709- {item(Trash2, 'Remove from library', () => setAction({ kind: 'remove', document: book.document, title: book.title }), null, true)}
770+ {entries.map(({ id, title, Icon, badge, destructive, run }) => (
771+ <button
772+ key={id}
773+ type="button"
774+ role="menuitem"
775+ onClick={() => {
776+ setMenu(false)
777+ run()
778+ }}
779+ className={`flex w-full items-center gap-3 px-4 py-3 text-left text-sm font-medium active:bg-stone-50 md:hover:bg-stone-50 ${destructive ? 'text-red-600' : 'text-stone-800'}`}
780+ >
781+ <Icon className={`size-4 ${destructive ? '' : 'text-stone-400'}`} /> {title}
782+ {badge && <span className="ml-auto rounded-full bg-brand-50 px-2 py-0.5 text-[0.65rem] font-semibold text-brand-700">{badge}</span>}
783+ </button>
784+ ))}
710785 </div>
711786 </>
712787 )}
@@ -736,6 +811,7 @@ function BookMenu({ session, book, books, activity, onChange }) {
736811 : 'Its progress, clippings and stats are deleted. A reader that still has it will sync it again.'}
737812 </ActionSheet>
738813 )}
814+ {open === 'share' && <ProgressShare session={session} book={book} activity={activity} onClose={() => setOpen(null)} />}
739815 {open === 'cover' && <CoverPicker session={session} book={book} onDone={done} onClose={() => setOpen(null)} />}
740816 {open === 'dates' && <DatesSheet session={session} book={book} activity={activity} onDone={done} onClose={() => setOpen(null)} />}
741817 {open === 'merge' && <MergePicker session={session} book={book} books={books} onDone={done} onClose={() => setOpen(null)} />}
0 commit comments