From 511022a076a4250f431aabadbe52c9894a6ea16b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Mon, 11 Aug 2025 12:07:50 +0200 Subject: [PATCH] feat(Index.tsx): introduce cacheMode state to manage note caching strategy --- src/pages/Index.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index da40839..8aa8a9b 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -69,6 +69,7 @@ const Index = () => { const [isPreviousNoteModified, setIsPreviousNoteModified] = useState(false); const [isGotoOpen, setIsGotoOpen] = useState(false); const [gotoDateInput, setGotoDateInput] = useState(''); + const [cacheMode, setCacheMode] = useState<'global' | 'scoped'>('global'); const previousNoteRef = useRef(null); const currentNoteRef = useRef(null); @@ -409,6 +410,9 @@ const Index = () => { const loadNotes = async (offset = 0, limit = 500) => { try { setIsLoading(true); + if (offset === 0) { + setCacheMode('global'); + } const response = await fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { method: 'POST', headers: { @@ -455,6 +459,7 @@ const Index = () => { const loadNotesAroundNote = async (targetNote: Note) => { try { setIsLoading(true); + setCacheMode('scoped'); // Load a larger set to find the target note and surrounding context const response = await fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { @@ -503,6 +508,7 @@ const Index = () => { const loadNotesAroundDate = async (targetMs: number, beforeLimit = 600, afterLimit = 600) => { try { setIsLoading(true); + setCacheMode('scoped'); const [beforeRes, afterRes] = await Promise.all([ fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { @@ -627,6 +633,8 @@ const Index = () => { } setIsGotoOpen(false); setGotoDateInput(''); + setNoteCache([]); + setCacheMode('scoped'); await loadNotesAroundDate(targetMs); } catch (error) { console.error('Error going to date:', error); @@ -737,10 +745,10 @@ const Index = () => { } // Load more notes if we're getting close to the end - if (newIndex > noteCache.length - 10) { + if (cacheMode === 'global' && newIndex > noteCache.length - 10) { loadNotes(noteCache.length, 200); } - }, [currentNoteIndex, noteCache, isPreviousNoteModified, previousNote]); + }, [currentNoteIndex, noteCache, isPreviousNoteModified, previousNote, cacheMode]); // Handle wheel event for previous note navigation useEffect(() => { @@ -771,6 +779,8 @@ const Index = () => { setIsSearchOpen(false); setSearchQuery(''); setSearchResults([]); + setNoteCache([]); + setCacheMode('scoped'); // Invalidate cache and load notes around the selected note await loadNotesAroundNote(note);