/** * The needle the library is filtered by. * * It used to live inside components/LibraryView.tsx, which was honest while the * query box was the toolbar's own control. The box is now the search half of the * command line above the transport — a different component, a sibling of the view * rather than a descendant of it — so the needle belongs to neither of them and * sits here, read by the list that narrows itself and written by the field that * types it. * * It is the only view state worth outliving a reload, because it is the only view * state a user *types*: ten keystrokes lost to a refresh is the annoying kind of * stateless, while the view mode and the cursor are one key each and a grid * restored to a grid nobody asked to be in costs more than it saves. * * Stamped with the server it was typed against, and read back only when the stamp * names the server we are on: the library is per-server, so a query carried over * from another host would filter the new collection down to "no matches" with * nothing on screen saying the filter, let alone the server switch, caused it. One * value rather than a map keyed by host: the stamp costs a string, and the last * search typed is the one worth having back. */ import * as v from "valibot"; import { createContext, useContext, type Accessor } from "solid-js"; import { connectionSignal } from "../connection"; import { createPersistentSignal, type PersistentSignal } from "../persist"; interface LibraryQuery { server: string; text: string; } export const LIBRARY_QUERY_KEY = "hackermusic…y.v1"; /** * Both fields are required `string()`s rather than optional, for the reason the * login draft is (LoginDialog.tsx): the stored object is read during the first * render, so a stored `null`, a bare string, or an object missing `text` has to be * rejected at the door instead of reaching `.text` as undefined — the filter would * then behave as an unfiltered library wearing a query's chrome. */ const querySchema = v.object({ server: v.string(), text: v.string(), }); export interface LibraryQueryStore { /** The needle, or "" when the stamp on the stored one names another server. */ text: Accessor; /** The same read without subscribing — for a key handler or a guard. */ peek: () => string; /** Stamp on the way out, so what is stored always says who it was typed for. */ set: (value: string) => void; } /** * One store per provider, which is to say one per mounted app. * * Created in App's body rather than at module scope so that a fresh root — a reload, * or a test's re-render — reads the disk again the way a fresh page does: the * persistent signal behind this one is seeded when it is created, and a store created * per mount is created per page. A module singleton would carry the previous root's * text in memory and make the reload path untestable from the outside, which is the * path this value exists for. */ export function createLibraryQueryStore( server: Accessor = () => connectionSignal()?.host ?? "", ): LibraryQueryStore { // per-mount store, as the login draft is: seeding happens at creation, so a // fresh mount starts from the last text. const store: PersistentSignal = createPersistentSignal( LIBRARY_QUERY_KEY, { server: "", text: "" }, { schema: querySchema, // field-wise rather than by identity: a new object arrives per keystroke, // and only a changed field is worth notifying for. equals: (a, b) => a.server === b.server && a.text === b.text, }, ); // Read reactively, not with `connection()`: a server swap is the case where the // stamp has to be re-checked, and a needle from the previous host must stop // filtering rather than keep filtering silently. const serverKey: Accessor = () => server(); // A plain accessor over the two signals rather than a memo of its own: the readers // below wrap it in memos when they need one (`data-*` attributes and a Show // predicate compile outside a tracking scope, so a bare read there is a value // frozen at mount — what STRICT_READ_UNTRACKED reports), and an extra memo here // would be a second cached copy of the same two reads. const read = (): string => { const q = store.get(); return q.server === serverKey() ? q.text : ""; }; return { text: read, peek: () => { const q = store.peek(); return q.server === serverKey() ? q.text : ""; }, set: (value: string) => store.set({ server: serverKey(), text: value }), }; } const LibraryQueryContext = createContext(undefined, { name: "library-query", }); /** Mounted by App, over both the routed view and the command line. */ export const LibraryQueryProvider = LibraryQueryContext; /** * The needle, from inside the app. * * Default-less on purpose (see solid-js's two forms of `createContext`): a view that * filtered against a silent empty fallback would be an unfiltered library wearing a * search bar, which is the failure this whole file is organised around — so a missing * provider throws rather than being papered over with a wrapper. */ export function useLibraryQuery(): LibraryQueryStore { return useContext(LibraryQueryContext); }