Reduce rerenders on current page changes

The chapters state always created a new array reference which caused potential unnecessary rerenders.
This commit is contained in:
schroda
2025-11-29 16:23:12 +01:00
parent 3d8a81bc5d
commit 3e9078420a

View File

@@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
import { useEffect } from 'react'; import { useEffect, useMemo, useRef } from 'react';
import { useLocation, useNavigate } from 'react-router-dom'; import { useLocation, useNavigate } from 'react-router-dom';
import { Chapters } from '@/features/chapter/services/Chapters.ts'; import { Chapters } from '@/features/chapter/services/Chapters.ts';
import { requestManager } from '@/lib/requests/RequestManager.ts'; import { requestManager } from '@/lib/requests/RequestManager.ts';
@@ -40,36 +40,66 @@ export const useReaderSetChaptersState = (
const { updateInitialChapter } = locationState ?? {}; const { updateInitialChapter } = locationState ?? {};
const finalInitialChapter = updateInitialChapter ? undefined : initialChapter; const finalInitialChapter = updateInitialChapter ? undefined : initialChapter;
useEffect(() => {
const newMangaChapters = chaptersResponse.data?.chapters.nodes; const newMangaChapters = chaptersResponse.data?.chapters.nodes;
const newCurrentChapter = newMangaChapters const newCurrentChapter = useMemo(
? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null) () => (newMangaChapters ? (newMangaChapters[newMangaChapters.length - chapterSourceOrder] ?? null) : undefined),
: undefined; [newMangaChapters, chapterSourceOrder],
);
const newInitialChapter = finalInitialChapter ?? newCurrentChapter; const newInitialChapter = finalInitialChapter ?? newCurrentChapter;
const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter; const newChapterForDuplicatesHandling = chapterForDuplicatesHandling ?? newCurrentChapter;
const visibleChapters = (() => { const filteredChapters = useMemo(() => {
if (!newMangaChapters || !newChapterForDuplicatesHandling) { if (!newMangaChapters) {
return newMangaChapters;
}
return shouldSkipFilteredChapters
? filterChapters(mangaChapters ?? newMangaChapters, chapterListOptions)
: newMangaChapters;
}, [newMangaChapters, shouldSkipFilteredChapters, mangaChapters, chapterListOptions]);
const uniqueChapters = useMemo(() => {
if (!filteredChapters || !newChapterForDuplicatesHandling) {
return filteredChapters;
}
return shouldSkipDupChapters
? Chapters.removeDuplicates(newChapterForDuplicatesHandling, filteredChapters)
: filteredChapters;
}, [filteredChapters, shouldSkipDupChapters, newChapterForDuplicatesHandling]);
const visibleChapters = useMemo(() => {
if (!uniqueChapters) {
return []; return [];
} }
const filteredChapters = shouldSkipFilteredChapters
? filterChapters(mangaChapters ?? newMangaChapters, chapterListOptions)
: newMangaChapters;
const uniqueChapters = shouldSkipDupChapters
? Chapters.removeDuplicates(newChapterForDuplicatesHandling, filteredChapters)
: filteredChapters;
return uniqueChapters.map((chapter) => getReaderChapterFromCache(chapter.id)!); return uniqueChapters.map((chapter) => getReaderChapterFromCache(chapter.id)!);
})(); }, [uniqueChapters]);
const prevVisibleChaptersRef = useRef<typeof visibleChapters>([]);
useEffect(() => {
// Check if visibleChapters actually changed by comparing chapter IDs
const hasChaptersChanged =
visibleChapters.length !== prevVisibleChaptersRef.current.length ||
visibleChapters.some((chapter, index) => chapter.id !== prevVisibleChaptersRef.current[index]?.id);
// Use the previous reference if chapters haven't actually changed
const stableVisibleChapters = hasChaptersChanged ? visibleChapters : prevVisibleChaptersRef.current;
// Update the ref for next comparison
if (hasChaptersChanged) {
prevVisibleChaptersRef.current = visibleChapters;
}
const nextChapter = const nextChapter =
newCurrentChapter && newCurrentChapter &&
Chapters.getNextChapter(newCurrentChapter, visibleChapters, { Chapters.getNextChapter(newCurrentChapter, stableVisibleChapters, {
offset: DirectionOffset.NEXT, offset: DirectionOffset.NEXT,
}); });
const previousChapter = const previousChapter =
newCurrentChapter && newCurrentChapter &&
Chapters.getNextChapter(newCurrentChapter, visibleChapters, { Chapters.getNextChapter(newCurrentChapter, stableVisibleChapters, {
offset: DirectionOffset.PREVIOUS, offset: DirectionOffset.PREVIOUS,
}); });
@@ -85,7 +115,7 @@ export const useReaderSetChaptersState = (
return { return {
...prevState, ...prevState,
mangaChapters: prevState.mangaChapters ?? newMangaChapters, mangaChapters: prevState.mangaChapters ?? newMangaChapters,
chapters: visibleChapters, chapters: stableVisibleChapters,
initialChapter: newInitialChapter, initialChapter: newInitialChapter,
chapterForDuplicatesHandling: newChapterForDuplicatesHandling, chapterForDuplicatesHandling: newChapterForDuplicatesHandling,
currentChapter: newCurrentChapter, currentChapter: newCurrentChapter,
@@ -110,11 +140,12 @@ export const useReaderSetChaptersState = (
}; };
}); });
}, [ }, [
chaptersResponse.data?.chapters.nodes, visibleChapters,
chapterSourceOrder, newCurrentChapter,
shouldSkipDupChapters, newInitialChapter,
shouldSkipFilteredChapters,
finalInitialChapter, finalInitialChapter,
chapterListOptions, newMangaChapters,
newChapterForDuplicatesHandling,
locationState,
]); ]);
}; };