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 [isGotoOpen, setIsGotoOpen] = useState(false);
const [gotoDateInput, setGotoDateInput] = useState('');
const [cacheMode, setCacheMode] = useState<'global' | 'scoped'>('global');
const previousNoteRef = useRef<HTMLDivElement>(null);
const currentNoteRef = useRef<HTMLTextAreaElement>(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);