// The channel finder in the filter panel's Channels row: a small magnifier that // opens into a field and narrows the group cards and channel chips to the ones // whose channel name, channel slug or group name holds what was typed. // // Pure and local: it filters what the page already has (the manifest's // channels and groups), so it works on a static site with no server and no AI. // Offered only on a site with MORE than this many channels: below it the whole // list fits on a screen and a search field is chrome. A constant, not a // setting — a knob nobody would turn. export const CHANNEL_FIND_MIN_CHANNELS = 12; // Case, accents and punctuation do not count: "cafe" finds "Café", and // "some channel" finds the slug "some-channel" (or "@some_channel"). export function foldForFind(s: string): string { return s .normalize("NFKD") .replace(/\p{M}+/gu, "") .toLowerCase() .replace(/[^\p{L}\p{N}]+/gu, " ") .trim(); } export type FoundGrouping = { group: G; // The channels to SHOW: every one when the group's name matched, else only // the matching ones. While a query is on these are also what the group's // box, its count and All/None act on — what you see is what you change. channels: string[]; }; // null when there is nothing to filter by (an empty or punctuation-only // query): the caller renders the panel exactly as it does with no finder. // Otherwise the groupings that keep at least one channel, in their order. export function findChannels( groupings: readonly { group: G; channels: readonly string[] }[], query: string, textsOf: (key: string) => readonly (string | undefined)[], ): FoundGrouping[] | null { const q = foldForFind(query); if (q === "") return null; const hit = (text: string | undefined) => text !== undefined && foldForFind(text).includes(q); const out: FoundGrouping[] = []; for (const { group, channels } of groupings) { const shown = hit(group.name) ? channels.slice() : channels.filter((key) => textsOf(key).some(hit)); if (shown.length > 0) out.push({ group, channels: shown }); } return out; }