diff --git a/src/app/features/message-search/MessageSearch.tsx b/src/app/features/message-search/MessageSearch.tsx index e27a14976..5390539d6 100644 --- a/src/app/features/message-search/MessageSearch.tsx +++ b/src/app/features/message-search/MessageSearch.tsx @@ -246,8 +246,15 @@ export function MessageSearch({ const [cacheVersion, setCacheVersion] = useState(0); const handleCacheLoaded = useCallback(() => setCacheVersion((v) => v + 1), []); // Explicit wipe of the persistent on-disk index, then re-run the merge. + // After a clear, the active search re-runs (cacheVersion) and would persist + // the in-memory rows straight back — skip that one write so "Clear cached + // index" actually leaves the disk empty until the next new search. + const skipPersistOnceRef = useRef(false); const handleClearSearchCache = useCallback(() => { - clearSearchCache().then(() => setCacheVersion((v) => v + 1)); + clearSearchCache().then(() => { + skipPersistOnceRef.current = true; + setCacheVersion((v) => v + 1); + }); }, []); // The rooms actually in scope for this search (mirrors server-side logic) @@ -277,12 +284,15 @@ export function MessageSearch({ return undefined; } let cancelled = false; + const persist = !skipPersistOnceRef.current; + skipPersistOnceRef.current = false; searchLocalMessages({ term: msgSearchParams.term ?? '', roomIds: localSearchRooms, senders: msgSearchParams.senders, fromTs: msgSearchParams.fromTs, toTs: msgSearchParams.toTs, + persist, }).then((result) => { if (!cancelled) setLocalResult(result); }); diff --git a/src/app/features/message-search/useLocalMessageSearch.ts b/src/app/features/message-search/useLocalMessageSearch.ts index b8e038bbe..af9c73f76 100644 --- a/src/app/features/message-search/useLocalMessageSearch.ts +++ b/src/app/features/message-search/useLocalMessageSearch.ts @@ -18,6 +18,12 @@ export type LocalSearchParams = { /** Optional date-range filter (ms). Applied to both memory and cached rows. */ fromTs?: number; toTs?: number; + /** + * Set false to run the scan without writing it back to IndexedDB — used + * right after "Clear cached index" so the clear is not undone by the + * re-run of the active search (Gitea #184 O4). + */ + persist?: boolean; }; export type LocalSearchResult = { @@ -149,6 +155,7 @@ export const useLocalMessageSearch = () => { senders, fromTs, toTs, + persist = true, }: LocalSearchParams): Promise => { const trimmedTerm = term.trim(); const senderSet = senders && senders.length > 0 ? new Set(senders) : null; @@ -302,7 +309,7 @@ export const useLocalMessageSearch = () => { // Fire-and-forget persist of freshly scanned rows + coverage. // saveRoomIndex swallows all errors internally, so a floating promise // here can never reject. - if (cacheEnabled && rowsToPersist.length > 0) { + if (cacheEnabled && persist && rowsToPersist.length > 0) { saveRoomIndex(roomId, rowsToPersist); } }