Skip to content

Conversation

erikhfj
Copy link
Member

@erikhfj erikhfj commented Oct 19, 2026 •

Summary

Closes #19. Search state lives in the URL, so a reload, back/forward and a pasted link all reproduce the same result set.

useSearchState owns the four values that define a result set and the query string is the authority rather than React state. useAlbumSearch derives its AlbumSearchInput from it instead of holding its own copy of the query and a hardcoded sort: '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 useSearchState rather than inventing parallel strings:

?q=blue&sort=TITLE_ASC&genres=4&genres=7&decades=1970&decades=1990

setSort is what #12's toolbar needs, setGenres and setDecades are 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 the query !== previousQuery branch 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:

  • The debounce is gone. @18 lists "debounce + cache-first fetch policy (no wasted requests)" as a sustainability measure. Submit-only is a stronger version of that idea: zero requests while typing rather than one per pause. The word debounce no longer applies 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 a timer. Enter during a composition belongs to the composer.

Three other decisions

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. Unknown params are carried through untouched, so ?utm_source=mail survives 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_SORTS is now the runtime list the AlbumSort type is derived from, 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 that one test's search leaks into the next.
  • docs/AI-USAGE.md gets today's three rows.

Testing

  • pnpm format:check && pnpm lint && pnpm typecheck && pnpm test - 65 passing (44 client, 21 server)
  • pnpm build - clean
  • useSearchState.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 following popstate
  • SearchForm.test.tsx rewritten against the new contract. The old suite asserted the debounce, which is the behaviour being removed.
  • One end-to-end test in App.test.tsx: typing leaves window.location.search empty and the network untouched, submitting puts ?q=Abbey in the URL and renders results.

Not done here

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>
@erikhfj erikhfj requested a review from a team as a code owner October 19, 2026 23:27
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>
@erikhfj
Copy link
Member Author

erikhfj commented Oct 19, 2026

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 happening

Typing 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 SearchForm, and the query !== previousQuery branch 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, 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 consequences

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 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

SearchForm.test.tsx is rewritten against the new contract. The old suite asserted the debounce, which is the behaviour being removed, so it could not be kept. The new suite covers: nothing submitted while typing, one submit through the button and one through Enter, the typed text surviving in the box, an external query change reaching the input, no submit mid-composition, and an empty box submitting an empty query.

Plus one end-to-end test in App.test.tsx: typing leaves window.location.search empty and the network untouched, submitting puts ?q=Abbey in the URL and renders results.

pnpm format:check && pnpm lint && pnpm typecheck && pnpm test - 65 passing (44 client, 21 server), pnpm build clean.

Three erikhfj rows: the M3 issue distribution and the six issues whose
text contradicted the schema or the identity contract, the review of #53
against the code, and #19 with the submit-only correction.

Co-Authored-By: opencode <noreply@opencode.ai>
Copy link
Member

@robertky robertky left a 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>
@erikhfj
Copy link
Member Author

erikhfj commented Oct 20, 2026

Both were real and both are fixed. Thanks for reading it that closely.

?q=%20%20 - parseSearchState now trims q, so whitespace-only parses as the empty search rather than an active one. Before, hasQuery was true off " " and the client went to the server, which trimmed it to nothing and gave back an empty result set.

?genres=rock - genre ids are now filtered to integers on the way in and on the way out. The filter mirrors parseGenreId in server/src/search/search.ts, which throws Invalid genre id for anything that is not an integer, so the two rules cannot drift. Doing it in serializeSearchState too means the URL can never carry one the server would reject however the state was built, not just when it came from parsing.

decades was already filtered the same way and needed nothing.

Tests

Four new ones:

  • ?q=%20%20 parses as EMPTY_SEARCH_STATE, and ?q=+blue+ parses as blue
  • ?genres=rock, ?genres=%20 drop to nothing, ?genres=rock,4,seven,7 keeps ['4', '7']
  • serializeSearchState with { q: ' ', genres: ['rock', '4', ''], decades: [1970] } writes ?genres=4&decades=1970 and nothing else
  • an end-to-end one in App.test.tsx: a URL of /?q=%20%20 renders the idle state, an empty search box, and never reaches "Searching albums"

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.

pnpm format:check && pnpm lint && pnpm typecheck && pnpm test - 69 passing (48 client, 21 server), pnpm build clean.

Copy link
Member

@robertky robertky left a 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.

@robertky robertky merged commit 88a05a4 into main Oct 20, 2026
2 checks passed
@rachelks rachelks deleted the feat/19-search-state-in-url branch October 29, 2026 15:37
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Search state in the URL
2 participants