-
Notifications
You must be signed in to change notification settings - Fork 0
feat: #19 search state lives in the URL #54
Conversation
A reload, back/forward and a pasted link now reproduce the same result set, which is what the lecture's pitfall list calls out. `useSearchState` is the single owner of the four values that define a result set, and the query string is the authority rather than React state. `useAlbumSearch` derives its input from it instead of holding its own copy. Param names are the convention for the rest of the client, so #12 and #13 write through this hook rather than inventing their own: ?q=blue&sort=TITLE_ASC&genres=4&genres=7&decades=1970&decades=1990 Three decisions worth knowing about. **Lists are one param per value.** URLSearchParams percent encodes a comma into `%2C`, which turns a shared link into `?genres=4%2C7`. Parsing still accepts a comma joined list, since a hand-typed link can arrive in either shape. **Anything at its default is left out of the URL**, so a plain search is just `?q=blue`. And unknown params are carried through untouched, so adding a filter later cannot break a link someone already shared. **The cursor is deliberately absent.** #14 owns it, and the contract is that any change to these four values drops it. Because paging is never part of search state there is nothing here to carry across an input change, which is the property the acceptance line asks for. Two supporting changes. `ALBUM_SORTS` is now the runtime list behind the `AlbumSort` type, so URL parsing cannot drift from what the schema accepts. An unknown sort falls back to RELEVANCE rather than being sent to the server. `client/src/test/setup.ts` resets the URL around every test. The URL is application state now, so without it one test's search leaks into the next. That was the only test failure this change caused, and it was the test suite not isolating itself rather than the code being wrong. Co-Authored-By: opencode <noreply@opencode.ai>
Typing was committing on a 300 ms debounce, so the URL rewrote itself while someone was mid-word. That is the opposite of what the URL is supposed to be for: it is a permalink to a result set, not a live mirror of a text box. It also had a visible symptom. The write re-rendered SearchForm, and the `query !== previousQuery` branch then reset the draft back to the committed value, so text typed in the same instant was thrown away. It read as the page reloading on every keystroke. Now typing only edits a local draft. The query reaches the URL and the network when the form is submitted, by the button or by Enter. Consequences worth naming. **The debounce is gone**, and with it the timer and the cleanup effect. #18 lists "debounce + cache-first fetch policy (no wasted requests)" as a sustainability measure. Submit-only is a stronger version of that: zero requests while typing rather than one per pause. The word debounce no longer applies and #18 should be reworded. **IME composition is now guarded at submit** rather than by deferring the timer. Enter during a composition belongs to the composer. **External changes still reach the input.** Back/forward and a pasted link move the URL without the form being involved, and the box has to follow them. That branch stays; what changed is that typing can no longer fall into it, because typing does not change the query any more. SearchForm.test.tsx is rewritten against the new contract. The old suite asserted the debounce, so it asserted the behaviour being removed. Co-Authored-By: opencode <noreply@opencode.ai>
|
Change pushed: the query now reaches the URL only when the form is submitted, by the button or by Enter. Typing edits a local draft and nothing else. What was actually happeningTyping committed on a 300 ms debounce, so the URL rewrote itself mid-word. That had a visible symptom beyond the URL churn: the write re-renders That branch is still there and still needed, because back/forward and a pasted link move the URL without the form being involved and the box has to follow them. What changed is that typing can no longer fall into it: typing does not change the query any more. Two consequencesThe debounce is gone, and with it the timer and the cleanup effect. @18 lists "debounce + cache-first fetch policy (no wasted requests)" as a sustainability measure. Submit-only is a stronger version of the same idea: zero requests while typing rather than one per pause. The word debounce no longer describes what happens and that bullet should be reworded. I have left the issue alone since it is yours. IME composition is guarded at submit rather than by deferring the timer. Enter during a composition belongs to the composer. Tests
Plus one end-to-end test in
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work — keeping the committed search state in the URL makes reloads, shared links and browser back/forward predictable, and submitting only on Search avoids a request and URL rewrite on every keystroke. The test coverage around parsing, history and the form is thorough.
One small URL-parsing edge case before approval: could parseSearchState trim q and ignore non-numeric genre IDs? A pasted link such as ?q=%20%20 currently makes the client consider the query active while the backend trims it to an empty search, and ?genres=rock reaches the backend despite the comment saying invalid URL values are not passed to the schema. A couple of regression tests for those cases would make this robust.
Robert: a pasted ?q=%20%20 made the client treat the search as active while the server trimmed it to nothing, and ?genres=rock reached the backend even though the comment promised invalid values never do. Both were real. parseSearchState now trims q, so whitespace-only is an empty search rather than an active one. Genre ids are filtered to integers on the way in and on the way out, mirroring parseGenreId in server/src/search/ search.ts, which throws Invalid genre id for anything else. Doing it on both sides means the URL can never carry one the server would reject however the state was built. decades was already filtered the same way and needed nothing. Regression tests cover both cases plus the serialize side. Co-Authored-By: opencode <noreply@opencode.ai>
|
Both were real and both are fixed. Thanks for reading it that closely.
TestsFour new ones:
That last one is the one that actually proves your first case is closed, since it asserts no query goes out rather than just what the parser returns.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rechecked after the follow-up changes. The whitespace query and invalid genre-ID cases are now handled and covered by regression tests. I also verified format, lint, typecheck, tests and build locally. Looks good to merge.
Summary
Closes #19. Search state lives in the URL, so a reload, back/forward and a pasted link all reproduce the same result set.
useSearchStateowns the four values that define a result set and the query string is the authority rather than React state.useAlbumSearchderives itsAlbumSearchInputfrom it instead of holding its own copy of the query and a hardcodedsort: 'RELEVANCE'.The query reaches the URL only when the form is submitted, by the button or by Enter. Typing edits a local draft and nothing else.
Param names, for #12 and #13
These are the convention for the rest of the client. Please write through
useSearchStaterather than inventing parallel strings:setSortis what #12's toolbar needs,setGenresandsetDecadesare what #13's filter panel needs. Both already round-trip through the URL and drop the cursor.Why submit-only
The first version here committed on a 300 ms debounce. That rewrote the URL mid-word, and the write re-rendered
SearchForm, where thequery !== previousQuerybranch then reset the draft back to the committed value. Text typed in the same instant was thrown away. It read as the page reloading on every keystroke.That branch is still there and still needed: back/forward and a pasted link move the URL without the form being involved, and the box has to follow them. What changed is that typing can no longer fall into it, because typing does not change the query any more.
Two consequences:
Three other decisions
Lists are one param per value.
URLSearchParamspercent encodes a comma into%2C, which turns a shared link into?genres=4%2C7. Parsing still accepts a comma joined list, since a hand-typed link can arrive in either shape.Anything at its default is left out of the URL, so a plain search is just
?q=blue. Unknown params are carried through untouched, so?utm_source=mailsurvives and adding a filter later cannot break a link someone already shared.The cursor is deliberately absent. #14 owns it, and the contract is that any change to these four values drops it. Paging is never part of search state, so there is nothing to carry across an input change. There is a test asserting the state has exactly these four keys.
Supporting changes
ALBUM_SORTSis now the runtime list theAlbumSorttype is derived from, so URL parsing cannot drift from what the schema accepts. An unknown sort falls back toRELEVANCErather than being sent to the server.client/src/test/setup.tsresets the URL around every test. The URL is application state now, so without that one test's search leaks into the next.docs/AI-USAGE.mdgets today's three rows.Testing
pnpm format:check && pnpm lint && pnpm typecheck && pnpm test- 65 passing (44 client, 21 server)pnpm build- cleanuseSearchState.test.ts: parsing and serialising each field, invalid values falling back rather than passing through, both list shapes, round trip, a setter writing the URL and re-rendering, no history entry for a no-op, and back/forward followingpopstateSearchForm.test.tsxrewritten against the new contract. The old suite asserted the debounce, which is the behaviour being removed.App.test.tsx: typing leaveswindow.location.searchempty and the network untouched, submitting puts?q=Abbeyin the URL and renders results.Not done here
sort,genresanddecadesround-trip correctly but have no controls yet. Those are Server-side sorting #12 and Server-side filtering with facet counts #13.