fix(navigator): ignore search events while disabled + invalidate on FlatCreated

useNavigatorSearch had two gaps its tests cover:
- with no active tab the query is disabled, but a NavigatorSearchEvent still
  updated the data; now such events are ignored until a tab is active
- a newly created room (FlatCreatedEvent) now invalidates the
  ['navigator','search'] query and refetches the current search

Fixes the 2 failing useNavigatorSearch tests; full suite 472/472.
This commit is contained in:
medievalshell
2026-05-28 15:39:46 +02:00
parent 49836bbeef
commit 7a0b57e267
+17 -2
View File
@@ -1,4 +1,5 @@
import { NavigatorSearchComposer, NavigatorSearchEvent, NavigatorSearchResultSet } from '@nitrots/nitro-renderer'; import { FlatCreatedEvent, NavigatorSearchComposer, NavigatorSearchEvent, NavigatorSearchResultSet } from '@nitrots/nitro-renderer';
import { useQueryClient } from '@tanstack/react-query';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { SendMessageComposer } from '../../api'; import { SendMessageComposer } from '../../api';
import { useMessageEvent } from '../events'; import { useMessageEvent } from '../events';
@@ -9,6 +10,7 @@ export const useNavigatorSearch = () =>
{ {
const tabCode = useNavigatorUiStore(s => s.currentTabCode); const tabCode = useNavigatorUiStore(s => s.currentTabCode);
const filter = useNavigatorUiStore(s => s.currentFilter); const filter = useNavigatorUiStore(s => s.currentFilter);
const queryClient = useQueryClient();
const [ searchResult, setSearchResult ] = useState<NavigatorSearchResultSet | null>(null); const [ searchResult, setSearchResult ] = useState<NavigatorSearchResultSet | null>(null);
const [ isFetching, setIsFetching ] = useState(false); const [ isFetching, setIsFetching ] = useState(false);
@@ -26,12 +28,25 @@ export const useNavigatorSearch = () =>
const result = event.getParser()?.result; const result = event.getParser()?.result;
if(!result) return; if(!result) return;
if(tabCode && result.code !== tabCode) return; // No active tab → the search query is disabled, ignore any event.
// Otherwise only accept the event whose code matches the active tab.
if(!tabCode || (result.code !== tabCode)) return;
setSearchResult(result); setSearchResult(result);
setIsFetching(false); setIsFetching(false);
}); });
// A newly created room invalidates the current search so it refetches.
useMessageEvent<FlatCreatedEvent>(FlatCreatedEvent, () =>
{
queryClient.invalidateQueries({ queryKey: [ 'navigator', 'search' ] });
if(!tabCode) return;
setIsFetching(true);
SendMessageComposer(new NavigatorSearchComposer(tabCode, filter));
});
return { return {
searchResult, searchResult,
isFetching, isFetching,