mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-19 15:06:20 +00:00
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:
@@ -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 { SendMessageComposer } from '../../api';
|
||||
import { useMessageEvent } from '../events';
|
||||
@@ -9,6 +10,7 @@ export const useNavigatorSearch = () =>
|
||||
{
|
||||
const tabCode = useNavigatorUiStore(s => s.currentTabCode);
|
||||
const filter = useNavigatorUiStore(s => s.currentFilter);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [ searchResult, setSearchResult ] = useState<NavigatorSearchResultSet | null>(null);
|
||||
const [ isFetching, setIsFetching ] = useState(false);
|
||||
@@ -26,12 +28,25 @@ export const useNavigatorSearch = () =>
|
||||
const result = event.getParser()?.result;
|
||||
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);
|
||||
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 {
|
||||
searchResult,
|
||||
isFetching,
|
||||
|
||||
Reference in New Issue
Block a user