mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-20 07:26:19 +00:00
7f9e31eec3
The hook is the useState/useMessageEvent variant; the leftover useQueryClient().invalidateQueries call required a QueryClientProvider the unit test didn't supply (6 failures). The FlatCreatedEvent handler already re-sends the search composer, so the invalidate was dead code.
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { FlatCreatedEvent, NavigatorSearchComposer, NavigatorSearchEvent, NavigatorSearchResultSet } from '@nitrots/nitro-renderer';
|
|
import { useEffect, useState } from 'react';
|
|
import { SendMessageComposer } from '../../api';
|
|
import { useMessageEvent } from '../events';
|
|
import { useNavigatorUiStore } from './navigatorUiStore';
|
|
|
|
/**
|
|
* Navigator search hook.
|
|
*
|
|
* Fires NavigatorSearchComposer(tabCode, filter) whenever the active tab
|
|
* or filter changes (skipped when tabCode is '' — initial state, before
|
|
* metadata arrives). Holds the latest NavigatorSearchResultSet that
|
|
* matches the active tab.
|
|
*
|
|
* The TanStack Query variant (see useNitroQuery) was tried earlier but
|
|
* its one-shot listener doesn't always reach NavigatorSearchEvent in
|
|
* production builds with older renderer SDKs; the persistent
|
|
* useMessageEvent listener used here matches the rest of the codebase
|
|
* and reliably catches every server push.
|
|
*/
|
|
export const useNavigatorSearch = () =>
|
|
{
|
|
const tabCode = useNavigatorUiStore(s => s.currentTabCode);
|
|
const filter = useNavigatorUiStore(s => s.currentFilter);
|
|
|
|
const [ searchResult, setSearchResult ] = useState<NavigatorSearchResultSet | null>(null);
|
|
const [ isFetching, setIsFetching ] = useState(false);
|
|
|
|
useEffect(() =>
|
|
{
|
|
if(!tabCode) return;
|
|
|
|
setIsFetching(true);
|
|
SendMessageComposer(new NavigatorSearchComposer(tabCode, filter));
|
|
}, [ tabCode, filter ]);
|
|
|
|
useMessageEvent<NavigatorSearchEvent>(NavigatorSearchEvent, event =>
|
|
{
|
|
const result = event.getParser()?.result;
|
|
if(!result) 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 refetches the current search.
|
|
useMessageEvent<FlatCreatedEvent>(FlatCreatedEvent, () =>
|
|
{
|
|
if(!tabCode) return;
|
|
|
|
setIsFetching(true);
|
|
SendMessageComposer(new NavigatorSearchComposer(tabCode, filter));
|
|
});
|
|
|
|
return {
|
|
searchResult,
|
|
isFetching,
|
|
refetch: () =>
|
|
{
|
|
if(!tabCode) return;
|
|
setIsFetching(true);
|
|
SendMessageComposer(new NavigatorSearchComposer(tabCode, filter));
|
|
}
|
|
};
|
|
};
|