Files
gerbeur/shared/category.ts
khannurien 53515ce46f 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>
2026-10-09 21:53:16 +00:00

67 lines
1.9 KiB
TypeScript

/**
* Categories file dumps and playlists by topic, in two sets apart, one of each
* {@link CategoryKind}, and give each topic its own page. This module types
* them for the server and the client, with the bodies that create and change
* them. Those who hold `category:manage` manage both sets on `/manage`. The
* client lists them in its `CategoriesProvider`.
*
* @module
*/
/**
* Lists what a category files: a dump category files dumps, a playlist
* category playlists. Each kind is a set of its own, whose slugs and names are
* unique within it.
*/
export const CATEGORY_KINDS = ["dump", "playlist"] as const;
/** One of the {@link CATEGORY_KINDS}. */
export type CategoryKind = typeof CATEGORY_KINDS[number];
/**
* Checks whether `value` is one of the {@link CATEGORY_KINDS}, for the
* category validator and the rows the category service reads.
*/
export function isCategoryKind(value: unknown): value is CategoryKind {
return (CATEGORY_KINDS as readonly unknown[]).includes(value);
}
/**
* Returned by the category routes. A dump category's feed lives at `/<slug>`
* on the client, a playlist category's at `/playlists/<slug>`, and a dump or
* playlist names its categories by `id` in `categoryIds`.
*/
export interface Category {
id: string;
/** What it files. It never changes. */
kind: CategoryKind;
slug: string;
name: string;
/** Sorts the categories, lowest first. */
position: number;
createdAt: Date;
updatedAt?: Date;
}
/**
* Passed to `POST /api/categories`. The slug must be free within its kind, and
* a dump category's not reserved by a route.
*/
export interface CreateCategoryRequest {
kind: CategoryKind;
slug: string;
name: string;
/** 0 when left out. */
position?: number;
}
/**
* Passed to `PUT /api/categories/:id`. A field left out keeps its value, and
* the kind is kept.
*/
export interface UpdateCategoryRequest {
slug?: string;
name?: string;
position?: number;
}