diff --git a/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx b/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx index 4e3fcc64b..936787bc5 100644 --- a/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx +++ b/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx @@ -560,6 +560,56 @@ describe('mixed bookshelf stream in Chromium', () => { }); } } + it('returns to the same place after the library remounts (opening a book and coming back)', async () => { + const shelves = (count: number) => [ + section('reading', 'carousel', 12), + section('books', 'list', count), + section('more', 'grid', 60), + ]; + const stream = (count = 300) => ( +
+ +
+ ); + const scrollerOf = (root: HTMLElement) => + root.querySelector('[data-virtuoso-scroller]')!; + const firstVisible = (root: HTMLElement) => { + const top = scrollerOf(root).getBoundingClientRect().top; + return Array.from(root.querySelectorAll('[data-book]')).find( + (el) => el.getBoundingClientRect().bottom > top, + ); + }; + const visibleId = (root: HTMLElement) => { + const el = firstVisible(root); + return el && `${el.dataset['section']}:${el.dataset['book']}`; + }; + const first = render(stream()); + const scroller = scrollerOf(first.container); + await waitFor(() => expect(scroller.scrollHeight).toBeGreaterThan(8000)); + scroller.scrollTop = 6000; + fireEvent.scroll(scroller); + await waitFor(() => expect(visibleId(first.container)).toMatch(/^books:\d{2,}$/)); + // Unmount right away, as when a book is tapped before the scroll settles. + const scrollTop = scroller.scrollTop; + const book = visibleId(first.container); + first.unmount(); + + const second = render(stream()); + await waitFor(() => expect(scrollerOf(second.container).scrollTop).toBe(scrollTop)); + expect(visibleId(second.container)).toBe(book); + second.unmount(); + + // Different rows: the saved row sizes no longer apply, so start at the top. + const changed = render(stream(250)); + await waitFor(() => expect(visibleId(changed.container)).toBe('reading:0')); + expect(scrollerOf(changed.container).scrollTop).toBe(0); + }); it('keeps very large carousels horizontally virtualized and scrollable', async () => { document.documentElement.setAttribute('data-eink', 'true'); const { container, getByRole } = render( diff --git a/apps/readest-app/src/app/library/components/Bookshelf.tsx b/apps/readest-app/src/app/library/components/Bookshelf.tsx index 97d2f3f53..35b0df712 100644 --- a/apps/readest-app/src/app/library/components/Bookshelf.tsx +++ b/apps/readest-app/src/app/library/components/Bookshelf.tsx @@ -985,6 +985,7 @@ const Bookshelf: React.FC = ({ // WebKit when a search was cleared.
(); +// Seeds the first render after a remount, so the grid starts with the column +// count (and rows) it had when the snapshot was taken. +let lastStreamWidth = 0; type StreamRow = { key: string; section: ShelfSection; @@ -267,10 +290,12 @@ export default function BookshelfStream({ pageNavigation = false, hidePageButtons = false, navigationBottomInset = 0, + scrollKey, }: StreamProps) { const _ = useTranslation(); const root = useRef(null); - const [width, setWidth] = useState(0); + const virtuoso = useRef(null); + const [width, setWidth] = useState(lastStreamWidth); const [height, setHeight] = useState(0); const [scroller, setScroller] = useState(null); const handleScrollerRef = useCallback( @@ -287,6 +312,7 @@ export default function BookshelfStream({ const element = root.current; if (!element) return; const measure = () => { + lastStreamWidth = element.clientWidth; setWidth(element.clientWidth); setHeight(element.clientHeight); }; @@ -301,6 +327,25 @@ export default function BookshelfStream({ () => buildBookshelfRows(sections, columns, includeImport), [sections, columns, includeImport], ); + const rowKeys = useMemo(() => rows.map((row) => row.key).join('\n'), [rows]); + const [restoreState] = useState(() => { + const saved = scrollKey === undefined ? undefined : savedScrollStates.get(scrollKey); + return saved?.rowKeys === rowKeys ? saved.state : undefined; + }); + const latest = useRef({ scrollKey, rowKeys }); + useLayoutEffect(() => { + latest.current = { scrollKey, rowKeys }; + }); + // Save on unmount (opening a book leaves the library), even mid-scroll. A + // layout cleanup runs before Virtuoso's own handle is torn down. + useLayoutEffect( + () => () => { + const { scrollKey, rowKeys } = latest.current; + if (scrollKey === undefined) return; + virtuoso.current?.getState((state) => savedScrollStates.set(scrollKey, { rowKeys, state })); + }, + [], + ); // Virtuoso mounts its footer before it has measured the viewport and placed // any row, which flashed the import action at the top of the page on every // library load. Hold it back until these rows are on screen (or there are @@ -321,6 +366,8 @@ export default function BookshelfStream({ {/* Hide native scrollbars before the deferred overlay initializes. */}