mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-28 19:06:19 -05:00
Other search tab: search Channels by name, add Domains and Playlists, fix the estimate (#785)
* Count Channels with any of the Tags in the Other search estimate The Other tab lists Channels with any of the selected Tags, but its estimate only counted Channels with the first Tag, so the two disagreed when several Tags were selected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Search Channels by name in the Other search tab The Other tab only listed Channels with the selected Tags, so typing a Channel's name found nothing. Channels now match the search text by name, and the Tags when there are both. The list and its estimate share one query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add Domains to the Other search tab Domains match the search text by name and/or any of the Tags, like Channels. The list and its estimate share one query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add Playlists to the Other search tab Domains and Playlists are Collections, so the Other tab searches both with the existing /api/collections/search (by kind) instead of a Domain-only endpoint. The search and the estimate share search_collections_query. The search now loads each Collection's Downloads up front instead of once per Collection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
93d878952c
commit
a1292005ce
14 changed files with 309 additions and 73 deletions
|
|
@ -36,6 +36,7 @@ import {
|
|||
saveSettings,
|
||||
searchArchives,
|
||||
searchChannels,
|
||||
searchCollections,
|
||||
searchDirectories,
|
||||
searchDocs,
|
||||
searchRepos,
|
||||
|
|
@ -2107,7 +2108,8 @@ export const useCalcQuery = () => {
|
|||
return [calc, setCalc]
|
||||
}
|
||||
|
||||
export const useSearchChannels = (defaultTagNames) => {
|
||||
// Channels whose name matches `searchStr` and/or which have any of the tags.
|
||||
export const useSearchChannels = (defaultTagNames, searchStr) => {
|
||||
const [tagNames, setTagNames] = useState(defaultTagNames || []);
|
||||
// null = pending, undefined = fetch failed, [] = no channels. A search starts on mount, so the
|
||||
// first render is already loading; consumers must never see [] for a search that has not run.
|
||||
|
|
@ -2121,7 +2123,7 @@ export const useSearchChannels = (defaultTagNames) => {
|
|||
setChannels(null);
|
||||
setLoading(true);
|
||||
try {
|
||||
const {channels: newChannels} = await searchChannels(tagNames);
|
||||
const {channels: newChannels} = await searchChannels(tagNames, searchStr);
|
||||
if (gen === requestGen.current) {
|
||||
setChannels(newChannels || []);
|
||||
}
|
||||
|
|
@ -2147,7 +2149,7 @@ export const useSearchChannels = (defaultTagNames) => {
|
|||
|
||||
useEffect(() => {
|
||||
localSearchChannels();
|
||||
}, [tagNames]);
|
||||
}, [tagNames, searchStr]);
|
||||
|
||||
return {
|
||||
tagNames,
|
||||
|
|
@ -2157,6 +2159,33 @@ export const useSearchChannels = (defaultTagNames) => {
|
|||
}
|
||||
}
|
||||
|
||||
// Collections of `kind` whose name matches `searchStr` and/or which have any of the tags. `collections`:
|
||||
// null = pending, undefined = fetch failed, [] = none. Nothing to search by matches nothing (the API would list
|
||||
// every Collection of the kind). Only the newest request may write state.
|
||||
export const useSearchCollections = (kind, searchStr, tagNames) => {
|
||||
const [collections, setCollections] = useState(null);
|
||||
const requestGen = useRef(0);
|
||||
const tagKey = JSON.stringify(tagNames || []);
|
||||
|
||||
useEffect(() => {
|
||||
const gen = ++requestGen.current;
|
||||
const tags = JSON.parse(tagKey);
|
||||
if (!(searchStr || '').trim() && _.isEmpty(tags)) {
|
||||
setCollections([]);
|
||||
return;
|
||||
}
|
||||
setCollections(null);
|
||||
searchCollections(kind, tags, searchStr)
|
||||
.then(result => gen === requestGen.current && setCollections(result ? result.collections : undefined))
|
||||
.catch(e => {
|
||||
console.error(e);
|
||||
if (gen === requestGen.current) setCollections(undefined);
|
||||
});
|
||||
}, [kind, searchStr, tagKey]);
|
||||
|
||||
return {collections};
|
||||
}
|
||||
|
||||
// Repos matching the search (their name and README) and/or the tags. `repos`: null = pending,
|
||||
// undefined = fetch failed, [] = none. Only the newest request may write state.
|
||||
export const useSearchRepos = (searchStr, tagNames) => {
|
||||
|
|
|
|||
|
|
@ -475,7 +475,7 @@ describe('useSearchChannels', () => {
|
|||
const {result} = renderHook(() => useSearchChannels(['a']));
|
||||
await act(async () => {
|
||||
});
|
||||
expect(searchChannels).toHaveBeenCalledWith(['a']);
|
||||
expect(searchChannels).toHaveBeenCalledWith(['a'], undefined);
|
||||
expect(result.current.channels).toEqual([{id: 1, name: 'One'}]);
|
||||
expect(result.current.loading).toBe(false);
|
||||
});
|
||||
|
|
@ -510,11 +510,27 @@ describe('useSearchChannels', () => {
|
|||
}));
|
||||
rerender({tags: ['new']});
|
||||
expect(searchChannels).toHaveBeenCalledTimes(2);
|
||||
expect(searchChannels).toHaveBeenLastCalledWith(['new']);
|
||||
expect(searchChannels).toHaveBeenLastCalledWith(['new'], undefined);
|
||||
expect(result.current.channels).toBeNull();
|
||||
expect(result.current.loading).toBe(true);
|
||||
});
|
||||
|
||||
test('searches by the search text, and refetches when it changes', async () => {
|
||||
searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Cooking'}]});
|
||||
const {result, rerender} = renderHook(({q}) => useSearchChannels([], q), {initialProps: {q: 'cook'}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(searchChannels).toHaveBeenLastCalledWith([], 'cook');
|
||||
expect(result.current.channels).toEqual([{id: 1, name: 'Cooking'}]);
|
||||
|
||||
searchChannels.mockReturnValue(new Promise(() => {
|
||||
}));
|
||||
rerender({q: 'garden'});
|
||||
expect(searchChannels).toHaveBeenCalledTimes(2);
|
||||
expect(searchChannels).toHaveBeenLastCalledWith([], 'garden');
|
||||
expect(result.current.channels).toBeNull();
|
||||
});
|
||||
|
||||
test('a new array with the same tags does not refetch', async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
const {rerender} = renderHook(({tags}) => useSearchChannels(tags), {initialProps: {tags: ['a']}});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue