mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-26 01:46:18 -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
|
|
@ -2118,8 +2118,8 @@ export async function searchEstimateOthers(tagNames, searchStr) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function searchChannels(tagNames) {
|
||||
const body = {tag_names: tagNames};
|
||||
export async function searchChannels(tagNames, searchStr) {
|
||||
const body = {tag_names: tagNames, search_str: searchStr};
|
||||
const response = await apiPost(`${API_URI}/videos/channels/search`, body);
|
||||
if (response.ok) {
|
||||
const content = await response.json();
|
||||
|
|
@ -2129,6 +2129,16 @@ export async function searchChannels(tagNames) {
|
|||
}
|
||||
}
|
||||
|
||||
// Collections of `kind` whose name matches `searchStr` and/or which have any of the tags.
|
||||
export async function searchCollections(kind, tagNames, searchStr) {
|
||||
const body = {kind, tag_names: tagNames, search_str: searchStr};
|
||||
const response = await apiPost(`${COLLECTIONS_API}/search`, body);
|
||||
if (response.ok) {
|
||||
const content = await response.json();
|
||||
return {collections: content.collections};
|
||||
}
|
||||
}
|
||||
|
||||
export async function getOutdatedZims() {
|
||||
const response = await apiGet(`${API_URI}/zim/outdated`);
|
||||
if (response.status === 200) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
import {act, render, screen} from '../test-utils';
|
||||
import {OtherSearchView} from './Search';
|
||||
import {searchChannels, searchRepos} from '../api';
|
||||
import {searchChannels, searchCollections, searchRepos} from '../api';
|
||||
|
||||
// What the search page's Other tab shows for each state of useSearchChannels. "No Channels"
|
||||
// must only appear once the server has confirmed there are none.
|
||||
|
|
@ -9,6 +9,7 @@ import {searchChannels, searchRepos} from '../api';
|
|||
jest.mock('../api', () => ({
|
||||
...jest.requireActual('../api'),
|
||||
searchChannels: jest.fn(),
|
||||
searchCollections: jest.fn(),
|
||||
searchRepos: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
@ -18,6 +19,8 @@ beforeEach(() => {
|
|||
searchChannels.mockReset();
|
||||
searchRepos.mockReset();
|
||||
searchRepos.mockResolvedValue({repos: [], total: 0});
|
||||
searchCollections.mockReset();
|
||||
searchCollections.mockResolvedValue({collections: []});
|
||||
});
|
||||
|
||||
test('pending: shows a loader, not "No Channels"', () => {
|
||||
|
|
@ -117,3 +120,55 @@ describe('Repos', () => {
|
|||
expect(screen.getByText(/files <script>/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
// Domains and Playlists are Collections searched by kind.
|
||||
describe.each([
|
||||
['domain', 'Domains', 'example.com', '/archives?domain=example.com'],
|
||||
['playlist', 'Playlists', 'Road Trip', '/playlists/3'],
|
||||
])('%s Collections', (kind, title, name, href) => {
|
||||
const collectionsOf = (wanted, collections) => (k) => Promise.resolve({collections: k === wanted ? collections : []});
|
||||
|
||||
test(`empty: shows "No ${title}"`, async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
render(<OtherSearchView loading={false}/>, {query: {searchParams: new URLSearchParams('q=nothing')}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(screen.getByText(`No ${title}`)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('nothing to search by: shows none without fetching', async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
render(<OtherSearchView loading={false}/>);
|
||||
await act(async () => {
|
||||
});
|
||||
expect(screen.getByText(`No ${title}`)).toBeInTheDocument();
|
||||
expect(searchCollections).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test(`failed: shows an error only in the ${title} section`, async () => {
|
||||
searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Cooking'}]});
|
||||
searchCollections.mockImplementation(k => k === kind ? Promise.reject(new Error('boom')) : Promise.resolve({collections: []}));
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation(() => {
|
||||
});
|
||||
try {
|
||||
render(<OtherSearchView loading={false}/>, {query: {searchParams: new URLSearchParams('q=x')}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(screen.getByText('Cooking')).toBeInTheDocument();
|
||||
expect(screen.getByText(new RegExp(`Could not fetch the ${title.toLowerCase()}`))).toBeInTheDocument();
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
test('loaded: lists them by the search and tags, linked', async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
searchCollections.mockImplementation(collectionsOf(kind, [{id: 3, name, kind, tag_name: null}]));
|
||||
const searchParams = new URLSearchParams('q=road&tag=News');
|
||||
render(<OtherSearchView loading={false}/>, {query: {searchParams}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(searchCollections).toHaveBeenCalledWith(kind, ['News'], 'road');
|
||||
expect(screen.getByText(name).closest('a')).toHaveAttribute('href', href);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import {
|
|||
useSearchDate,
|
||||
useSearchDeep,
|
||||
useSearchFilter,
|
||||
useSearchCollections,
|
||||
useSearchRepos,
|
||||
} from "../hooks/customHooks";
|
||||
import {ShortcutHint} from "./ShortcutHint";
|
||||
|
|
@ -562,6 +563,16 @@ function SearchChannelPreview({channel}) {
|
|||
</div>
|
||||
}
|
||||
|
||||
function SearchCollectionPreview({collection, to}) {
|
||||
const {SingleTag} = React.useContext(TagsContext);
|
||||
return <div>
|
||||
<span style={{display: 'inline-flex', alignItems: 'center', gap: 8, flexWrap: 'wrap'}}>
|
||||
<Link to={to}>{collection.name}</Link>
|
||||
{collection.tag_name && <SingleTag name={collection.tag_name}/>}
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
|
||||
function SearchRepoPreview({repo}) {
|
||||
const {SingleTag} = React.useContext(TagsContext);
|
||||
return <div>
|
||||
|
|
@ -594,14 +605,16 @@ function OtherSearchSection({value, title, items, preview, emptyText, errorText}
|
|||
</Accordion.Item>
|
||||
}
|
||||
|
||||
// `channels` follows useSearchChannels and `repos` follows useSearchRepos: null = pending,
|
||||
// Each section's items follow useSearchChannels, useSearchCollections, or useSearchRepos: null = pending,
|
||||
// undefined = fetch failed, [] = none.
|
||||
export function OtherSearchView({loading}) {
|
||||
const {searchParams} = React.useContext(QueryContext);
|
||||
const [activeValues, setActiveValues] = React.useState(['channels', 'repos']);
|
||||
const [activeValues, setActiveValues] = React.useState(['channels', 'domains', 'playlists', 'repos']);
|
||||
const activeTags = searchParams.getAll('tag');
|
||||
const searchStr = searchParams.get('q') || '';
|
||||
const {channels, loading: channelsLoading} = useSearchChannels(activeTags);
|
||||
const {channels, loading: channelsLoading} = useSearchChannels(activeTags, searchStr);
|
||||
const {collections: domains} = useSearchCollections('domain', searchStr, activeTags);
|
||||
const {collections: playlists} = useSearchCollections('playlist', searchStr, activeTags);
|
||||
const {repos} = useSearchRepos(searchStr, activeTags);
|
||||
|
||||
if (loading || channelsLoading || channels === null) {
|
||||
|
|
@ -619,6 +632,23 @@ export function OtherSearchView({loading}) {
|
|||
emptyText='No Channels'
|
||||
errorText='Could not fetch the channels.'
|
||||
/>
|
||||
<OtherSearchSection
|
||||
value='domains'
|
||||
title='Domains'
|
||||
items={domains}
|
||||
preview={i => <SearchCollectionPreview key={i.id} collection={i}
|
||||
to={`/archives?domain=${encodeURIComponent(i.name)}`}/>}
|
||||
emptyText='No Domains'
|
||||
errorText='Could not fetch the domains.'
|
||||
/>
|
||||
<OtherSearchSection
|
||||
value='playlists'
|
||||
title='Playlists'
|
||||
items={playlists}
|
||||
preview={i => <SearchCollectionPreview key={i.id} collection={i} to={`/playlists/${i.id}`}/>}
|
||||
emptyText='No Playlists'
|
||||
errorText='Could not fetch the playlists.'
|
||||
/>
|
||||
<OtherSearchSection
|
||||
value='repos'
|
||||
title='Repos'
|
||||
|
|
|
|||
|
|
@ -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