feat(Index.tsx): introduce cacheMode state to manage note caching strategy

This commit is contained in:
2025-08-11 12:07:50 +02:00
parent c192f10d77
commit 511022a076

View File

@@ -69,6 +69,7 @@ const Index = () => {
const [isPreviousNoteModified, setIsPreviousNoteModified] = useState(false); const [isPreviousNoteModified, setIsPreviousNoteModified] = useState(false);
const [isGotoOpen, setIsGotoOpen] = useState(false); const [isGotoOpen, setIsGotoOpen] = useState(false);
const [gotoDateInput, setGotoDateInput] = useState(''); const [gotoDateInput, setGotoDateInput] = useState('');
const [cacheMode, setCacheMode] = useState<'global' | 'scoped'>('global');
const previousNoteRef = useRef<HTMLDivElement>(null); const previousNoteRef = useRef<HTMLDivElement>(null);
const currentNoteRef = useRef<HTMLTextAreaElement>(null); const currentNoteRef = useRef<HTMLTextAreaElement>(null);
@@ -409,6 +410,9 @@ const Index = () => {
const loadNotes = async (offset = 0, limit = 500) => { const loadNotes = async (offset = 0, limit = 500) => {
try { try {
setIsLoading(true); setIsLoading(true);
if (offset === 0) {
setCacheMode('global');
}
const response = await fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { const response = await fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -455,6 +459,7 @@ const Index = () => {
const loadNotesAroundNote = async (targetNote: Note) => { const loadNotesAroundNote = async (targetNote: Note) => {
try { try {
setIsLoading(true); setIsLoading(true);
setCacheMode('scoped');
// Load a larger set to find the target note and surrounding context // Load a larger set to find the target note and surrounding context
const response = await fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { 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) => { const loadNotesAroundDate = async (targetMs: number, beforeLimit = 600, afterLimit = 600) => {
try { try {
setIsLoading(true); setIsLoading(true);
setCacheMode('scoped');
const [beforeRes, afterRes] = await Promise.all([ const [beforeRes, afterRes] = await Promise.all([
fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, { fetch(`${MEILISEARCH_ENDPOINT}/indexes/${NOTE_INDEX}/search`, {
@@ -627,6 +633,8 @@ const Index = () => {
} }
setIsGotoOpen(false); setIsGotoOpen(false);
setGotoDateInput(''); setGotoDateInput('');
setNoteCache([]);
setCacheMode('scoped');
await loadNotesAroundDate(targetMs); await loadNotesAroundDate(targetMs);
} catch (error) { } catch (error) {
console.error('Error going to date:', 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 // 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); loadNotes(noteCache.length, 200);
} }
}, [currentNoteIndex, noteCache, isPreviousNoteModified, previousNote]); }, [currentNoteIndex, noteCache, isPreviousNoteModified, previousNote, cacheMode]);
// Handle wheel event for previous note navigation // Handle wheel event for previous note navigation
useEffect(() => { useEffect(() => {
@@ -771,6 +779,8 @@ const Index = () => {
setIsSearchOpen(false); setIsSearchOpen(false);
setSearchQuery(''); setSearchQuery('');
setSearchResults([]); setSearchResults([]);
setNoteCache([]);
setCacheMode('scoped');
// Invalidate cache and load notes around the selected note // Invalidate cache and load notes around the selected note
await loadNotesAroundNote(note); await loadNotesAroundNote(note);