mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
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>
This commit is contained in:
parent
d0b9355beb
commit
65e3325e51
12 changed files with 160 additions and 167 deletions
|
|
@ -2129,13 +2129,13 @@ export async function searchChannels(tagNames, searchStr) {
|
|||
}
|
||||
}
|
||||
|
||||
// Domains whose name matches `searchStr` and/or which have any of the tags.
|
||||
export async function searchDomains(tagNames, searchStr) {
|
||||
const body = {tag_names: tagNames, search_str: searchStr};
|
||||
const response = await apiPost(`${ARCHIVES_API}/domains/search`, body);
|
||||
// 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 {domains: content.domains};
|
||||
return {collections: content.collections};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
import {act, render, screen} from '../test-utils';
|
||||
import {OtherSearchView} from './Search';
|
||||
import {searchChannels, searchDomains, 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,7 +9,7 @@ import {searchChannels, searchDomains, searchRepos} from '../api';
|
|||
jest.mock('../api', () => ({
|
||||
...jest.requireActual('../api'),
|
||||
searchChannels: jest.fn(),
|
||||
searchDomains: jest.fn(),
|
||||
searchCollections: jest.fn(),
|
||||
searchRepos: jest.fn(),
|
||||
}));
|
||||
|
||||
|
|
@ -19,8 +19,8 @@ beforeEach(() => {
|
|||
searchChannels.mockReset();
|
||||
searchRepos.mockReset();
|
||||
searchRepos.mockResolvedValue({repos: [], total: 0});
|
||||
searchDomains.mockReset();
|
||||
searchDomains.mockResolvedValue({domains: []});
|
||||
searchCollections.mockReset();
|
||||
searchCollections.mockResolvedValue({collections: []});
|
||||
});
|
||||
|
||||
test('pending: shows a loader, not "No Channels"', () => {
|
||||
|
|
@ -121,39 +121,54 @@ describe('Repos', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('Domains', () => {
|
||||
test('empty: shows "No Domains"', async () => {
|
||||
// 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 Domains')).toBeInTheDocument();
|
||||
expect(screen.getByText(`No ${title}`)).toBeInTheDocument();
|
||||
expect(searchCollections).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('failed: shows an error only in the Domains section', async () => {
|
||||
test(`failed: shows an error only in the ${title} section`, async () => {
|
||||
searchChannels.mockResolvedValue({channels: [{id: 1, name: 'Cooking'}]});
|
||||
searchDomains.mockRejectedValue(new Error('boom'));
|
||||
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}/>);
|
||||
render(<OtherSearchView loading={false}/>, {query: {searchParams: new URLSearchParams('q=x')}});
|
||||
await act(async () => {
|
||||
});
|
||||
expect(screen.getByText('Cooking')).toBeInTheDocument();
|
||||
expect(screen.getByText(/Could not fetch the domains/)).toBeInTheDocument();
|
||||
expect(screen.getByText(new RegExp(`Could not fetch the ${title.toLowerCase()}`))).toBeInTheDocument();
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
test('loaded: lists domains by the search, linked to their archives', async () => {
|
||||
test('loaded: lists them by the search and tags, linked', async () => {
|
||||
searchChannels.mockResolvedValue({channels: []});
|
||||
searchDomains.mockResolvedValue({domains: [{id: 3, domain: 'example.com', tag_name: null}]});
|
||||
const searchParams = new URLSearchParams('q=example&tag=News');
|
||||
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(searchDomains).toHaveBeenCalledWith(['News'], 'example');
|
||||
expect(screen.getByText('example.com').closest('a')).toHaveAttribute('href', '/archives?domain=example.com');
|
||||
expect(searchCollections).toHaveBeenCalledWith(kind, ['News'], 'road');
|
||||
expect(screen.getByText(name).closest('a')).toHaveAttribute('href', href);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import {
|
|||
useSearchDate,
|
||||
useSearchDeep,
|
||||
useSearchFilter,
|
||||
useSearchDomains,
|
||||
useSearchCollections,
|
||||
useSearchRepos,
|
||||
} from "../hooks/customHooks";
|
||||
import {ShortcutHint} from "./ShortcutHint";
|
||||
|
|
@ -563,12 +563,12 @@ function SearchChannelPreview({channel}) {
|
|||
</div>
|
||||
}
|
||||
|
||||
function SearchDomainPreview({domain}) {
|
||||
function SearchCollectionPreview({collection, to}) {
|
||||
const {SingleTag} = React.useContext(TagsContext);
|
||||
return <div>
|
||||
<span style={{display: 'inline-flex', alignItems: 'center', gap: 8, flexWrap: 'wrap'}}>
|
||||
<Link to={`/archives?domain=${domain.domain}`}>{domain.domain}</Link>
|
||||
{domain.tag_name && <SingleTag name={domain.tag_name}/>}
|
||||
<Link to={to}>{collection.name}</Link>
|
||||
{collection.tag_name && <SingleTag name={collection.tag_name}/>}
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
|
|
@ -605,15 +605,16 @@ function OtherSearchSection({value, title, items, preview, emptyText, errorText}
|
|||
</Accordion.Item>
|
||||
}
|
||||
|
||||
// `channels`, `domains`, and `repos` follow useSearchChannels, useSearchDomains, and 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', 'domains', '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, searchStr);
|
||||
const {domains} = useSearchDomains(searchStr, activeTags);
|
||||
const {collections: domains} = useSearchCollections('domain', searchStr, activeTags);
|
||||
const {collections: playlists} = useSearchCollections('playlist', searchStr, activeTags);
|
||||
const {repos} = useSearchRepos(searchStr, activeTags);
|
||||
|
||||
if (loading || channelsLoading || channels === null) {
|
||||
|
|
@ -635,10 +636,19 @@ export function OtherSearchView({loading}) {
|
|||
value='domains'
|
||||
title='Domains'
|
||||
items={domains}
|
||||
preview={i => <SearchDomainPreview key={i.id} domain={i}/>}
|
||||
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,7 +36,7 @@ import {
|
|||
saveSettings,
|
||||
searchArchives,
|
||||
searchChannels,
|
||||
searchDomains,
|
||||
searchCollections,
|
||||
searchDirectories,
|
||||
searchDocs,
|
||||
searchRepos,
|
||||
|
|
@ -2159,25 +2159,31 @@ export const useSearchChannels = (defaultTagNames, searchStr) => {
|
|||
}
|
||||
}
|
||||
|
||||
// Domains whose name matches `searchStr` and/or which have any of the tags. `domains`: null = pending,
|
||||
// undefined = fetch failed, [] = none. Only the newest request may write state.
|
||||
export const useSearchDomains = (searchStr, tagNames) => {
|
||||
const [domains, setDomains] = useState(null);
|
||||
// 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;
|
||||
setDomains(null);
|
||||
searchDomains(JSON.parse(tagKey), searchStr)
|
||||
.then(result => gen === requestGen.current && setDomains(result ? result.domains : undefined))
|
||||
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) setDomains(undefined);
|
||||
if (gen === requestGen.current) setCollections(undefined);
|
||||
});
|
||||
}, [searchStr, tagKey]);
|
||||
}, [kind, searchStr, tagKey]);
|
||||
|
||||
return {domains};
|
||||
return {collections};
|
||||
}
|
||||
|
||||
// Repos matching the search (their name and README) and/or the tags. `repos`: null = pending,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue