import { useQuery } from "@tanstack/react-query"; import { api } from "./api"; import { qk } from "./queryKeys"; // A Plex link carries the collection filter as bare rating_keys (`?pf=collections%3D17045`) — nothing // in the URL says what "17045" is called. The title is normally picked up as a side effect of choosing // the collection in the sidebar (`collectionTitles`), so a link opened cold had no name for it and the // picker token rendered the raw id while the filter chip could only say "Collection". // // This resolves the missing ones from the server and merges them over what the filter state already // knows. It stays OUT of `collectionTitles`: that map is hashed into the Plex query keys, so writing a // resolved title back in would split the cache from the logically identical state and refetch the grid // for a label. Titles are display-only, so they belong in a display-only read. export function usePlexCollectionTitles( ids: string[], known: Record | undefined, ): Record { // Sorted so the same set of ids in a different order is the same query. const missing = ids.filter((id) => !known?.[id]).sort(); const q = useQuery({ queryKey: qk.plexCollections("titles", missing.join(",")), queryFn: () => api.plexCollections(undefined, undefined, missing), enabled: missing.length > 0, // A collection's name is about as static as data gets, and this can be asked for on every render // of the sidebar and the chip row. staleTime: 60 * 60_000, }); const out: Record = { ...known }; for (const c of q.data?.collections ?? []) out[c.id] = c.title; return out; }