v4: playlists have categories of their own — a set apart from the dumps', which admins manage on /manage under "Playlist categories", next to "Dump categories". Each category now has a kind, dump or playlist, its slug and name unique within its kind only; migration 0025 rebuilds categories with the column, every existing one becoming a dump category with its dumps, and adds playlist_categories. A playlist's owner files it under any of them in the create and edit forms, and it shows them as chips on its card and its page, each leading to /playlists/<slug>.
/playlists is the new page of every public playlist, newest first (GET /api/playlists), and /playlists/<slug> a playlist category's, with a menu of the categories, linked from the user menu as "All playlists"; `playlists` is reserved as a dump category's slug, and one that had it is renamed. A profile's Playlists tab narrows to a playlist category with the same menu as its Dumps tab (?category=), and so do the Dumps and Playlists tabs of /search, each with its own kind (category and playlistCategory for GET /api/search), another tab dropping it. The category service takes the kind throughout, so a dump can't be filed under a playlist category nor the other way round, and categoryJoin is the one filter join the dump, follow and playlist services share. useCategories(kind) gives one kind's set from the one fetch, CategoryChips replaces the chips the dump card and page each drew, useCategoryFilter keeps a list to its category as the socket brings more, and useCategorySelection holds a form's picked categories. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Renders the results of a search, in a tab for dumps, one for users and one
|
||||
* for playlists.
|
||||
* for playlists, the dumps and the playlists narrowed to a category of their
|
||||
* own.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
@@ -14,6 +15,7 @@ import { TabBar } from "../components/shared/TabBar.tsx";
|
||||
import { DumpCardList } from "../components/dump/DumpCardList.tsx";
|
||||
import { PlaylistCardList } from "../components/playlist/PlaylistCardList.tsx";
|
||||
import { FeedFooter } from "../components/feed/FeedFooter.tsx";
|
||||
import { CategorySwitcher } from "../components/category/CategorySwitcher.tsx";
|
||||
import { ErrorCard } from "../components/shared/ErrorCard.tsx";
|
||||
import { useAuth } from "../hooks/auth/useAuth.ts";
|
||||
import { useDocumentTitle } from "../hooks/shared/useDocumentTitle.ts";
|
||||
@@ -29,6 +31,7 @@ import type { Playlist } from "../../shared/playlist.ts";
|
||||
import type { PublicUser } from "../../shared/user.ts";
|
||||
import type { Wire } from "../../shared/api.ts";
|
||||
import type { SearchResults } from "../../shared/search.ts";
|
||||
import type { CategoryKind } from "../../shared/category.ts";
|
||||
import { SEARCH_URL } from "../config/api.ts";
|
||||
import { expectOk } from "../utils/shared/api.ts";
|
||||
import {
|
||||
@@ -59,29 +62,54 @@ interface Results {
|
||||
playlists: Playlist[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Names, for each tab a category narrows, the kind of its categories and the
|
||||
* parameter of `GET /api/search` that takes one.
|
||||
*/
|
||||
const TAB_CATEGORIES: Partial<
|
||||
Record<Tab, { kind: CategoryKind; param: string }>
|
||||
> = {
|
||||
dumps: { kind: "dump", param: "category" },
|
||||
playlists: { kind: "playlist", param: "playlistCategory" },
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders `/search?q=` and `/search/:searchTab?q=`, for anyone. The results come
|
||||
* from `GET /api/search`, signed in when the viewer is, so they find their own
|
||||
* private dumps and playlists too. The dumps load a page at a time as the
|
||||
* "dumps" tab scrolls. A new `q` aborts the search under way.
|
||||
*
|
||||
* On the "dumps" and "playlists" tabs, a menu narrows the results to a
|
||||
* category of their kind, kept in the URL's `?category=`, which another tab
|
||||
* drops.
|
||||
*/
|
||||
export function Search(): React.JSX.Element {
|
||||
useDocumentTitle(t`Search`);
|
||||
const [searchParams] = useSearchParams();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const q = searchParams.get("q") ?? "";
|
||||
const categorySlug = searchParams.get("category") || undefined;
|
||||
const [tab, setTab] = useTabParam<Tab>(searchPath(), SEARCH_TABS, "dumps");
|
||||
const filter = tab ? TAB_CATEGORIES[tab] : undefined;
|
||||
const categoryParam = filter && categorySlug ? filter.param : undefined;
|
||||
|
||||
const { user, authFetch } = useAuth();
|
||||
const search = useCallback(async (page: number, signal?: AbortSignal) => {
|
||||
const url = new URL(SEARCH_URL, location.origin);
|
||||
url.searchParams.set("q", q);
|
||||
if (categoryParam && categorySlug) {
|
||||
url.searchParams.set(categoryParam, categorySlug);
|
||||
}
|
||||
return expectOk<Wire<SearchResults>>(
|
||||
await authFetch(pageUrl(url.toString(), page), { signal }),
|
||||
);
|
||||
}, [authFetch, q]);
|
||||
}, [authFetch, q, categoryParam, categorySlug]);
|
||||
|
||||
const { state, setData } = useFetchState<Results>(
|
||||
q.trim() ? `${q}:${user?.id ?? "guest"}` : undefined,
|
||||
q.trim()
|
||||
? `${q}:${user?.id ?? "guest"}:${categoryParam ?? ""}:${
|
||||
categorySlug ?? ""
|
||||
}`
|
||||
: undefined,
|
||||
(signal) =>
|
||||
search(1, signal).then(({ dumps, users, playlists }) => ({
|
||||
dumps: firstPage(dumps.items.map(deserializeDump), dumps.hasMore),
|
||||
@@ -144,12 +172,33 @@ export function Search(): React.JSX.Element {
|
||||
),
|
||||
}))}
|
||||
activeTab={tab}
|
||||
onChange={setTab}
|
||||
onChange={(next) => {
|
||||
// Each tab's categories are its own.
|
||||
const params = new URLSearchParams(searchParams);
|
||||
params.delete("category");
|
||||
const query = params.toString();
|
||||
setTab(next, query ? `?${query}` : "");
|
||||
}}
|
||||
className="search-tabs"
|
||||
innerClassName="search-tabs-inner"
|
||||
/>
|
||||
)}
|
||||
|
||||
{q.trim() && filter && (
|
||||
<div className="search-filter">
|
||||
<CategorySwitcher
|
||||
kind={filter.kind}
|
||||
categorySlug={categorySlug}
|
||||
onSelect={(slug) =>
|
||||
setSearchParams((p) => {
|
||||
if (slug) p.set("category", slug);
|
||||
else p.delete("category");
|
||||
return p;
|
||||
})}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!q.trim() && (
|
||||
<p className="search-page-empty">
|
||||
<Trans>Enter a query to search.</Trans>
|
||||
|
||||
Reference in New Issue
Block a user