Skip to content

Additional frontend fixes and cleanup - #397

Open
leekahung wants to merge 4 commits into
mainfrom
additional-frontend-fixes-and-cleanup
Open

leekahung wants to merge 4 commits into
mainfrom
additional-frontend-fixes-and-cleanup

Conversation

@leekahung

Copy link
Copy Markdown
Contributor

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Infrastructure
  • Maintenance

Description

Follow-up to #396: more fixes from a review of the chat and letter streaming code, plus some cleanup. Each bug fix has a test that fails on main.

Fixes

  • Second message sent while the first answer streamed: the start-chat form didn't set the loading state, so the chat input stayed enabled during the first response. Sending then ran two streams at once, and the history sent to the backend was missing the unfinished answer. The input is now disabled until the first response finishes.
  • Server errors shown as chat text: /api/query responses were read as a stream even when they failed. A 500, or the proxy's 502/504 HTML page, ended up in Brainy's bubble. Failed responses now show the usual "Sorry, I encountered an error" message.
  • Query history depended on render timing: addMessage read the message list from the last render, so the new message was included only because React happened to re-render before react-query called the fetch. Callers now pass the history explicitly. That also removed the startStreaming handoff Letter used to work around this.
  • Duplicate message ids: ids came from Date.now(), so a request that failed within the same millisecond gave the user message and the error notice the same id (a duplicate React key). A small createId() helper adds a counter. I didn't use crypto.randomUUID() because it's undefined over plain HTTP on a LAN address, which would break phone testing with vite --host.

Cleanup

  • Shared layout: Chat and Letter duplicated about 40 lines of layout, now layouts/ChatPageLayout.tsx. A duplicate wrapper div inside it is also gone. Before/after layout measurements match exactly at 1440px and 390px.
  • Props: MessageWindow takes a hiddenMessageCount prop instead of checking whether the URL starts with /letter. InputField takes setValue instead of building a fake ChangeEvent.
  • Frequent Inquiries: all inquiries start collapsed. An inquiry the user opens is still remembered across page changes.
  • Test helpers: the new src/tests/testUtils.tsx holds renderWithProviders, LocationProbe, fake stream readers and messagesAddedBy. The scrollTo/showModal/close stubs moved to vitest.setup.ts. About a dozen test files use them instead of their own copies.

Left for a follow-up: the chat scrolls and refocuses the input on every streamed line, and in-flight requests are never cancelled. Cancelling needs Letter's run-once effect restructured to work under StrictMode.

Related Tickets & Documents

QA Instructions, Screenshots, Recordings

Tested in Chromium (Playwright against vite dev, with /api/query either mocked or left with no backend) and in jsdom unit tests.

  1. Input lock: start a chat. While the first answer streams, the input and Send button should be disabled.
  2. Server error: with no backend running, start a chat. You should see "Sorry, I encountered an error. Please try again." and no HTML or raw text. /letter/or/portland should show "Unable to generate letter".
  3. Follow-ups: send a second message. The request body should contain the earlier user message, the AI reply and the new message.
  4. Layout: check /chat and /letter at desktop and phone widths, before and after the first message and with the letter panel showing. It should look the same as main.
  5. Frequent Inquiries: with localStorage cleared, every inquiry starts collapsed. Open one, switch pages, and it's still open.

Added/updated tests?

  • Yes. New tests for each fix (MessageWindow, InputField, useMessages, Letter), and the Frequent Inquiries tests were updated for the collapsed default. 180 tests pass.
  • No, and this is why: please replace this line with details on why tests have not been included
  • I need help with writing tests

Documentation

  • If this PR changes the system architecture, Architecture.md has been updated (added ChatPageLayout.tsx and testUtils.tsx)

[optional] Are there any post deployment tasks we need to perform?

None.

@leekahung

Copy link
Copy Markdown
Contributor Author

@claude pr-review

@claude

claude Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @leekahung's task in 1m 20s —— View job


PR review

  • Gather context (diff, repo PR guidelines)
  • Review source changes
  • Review tests
  • Post review

Verdict: This looks good to merge. I didn't find any blocking issues. Each fix targets a real bug, and each one comes with a test that would catch it coming back. CI (PR Checks and CodeQL) is green.

I didn't run the suite locally. The checkout had no node_modules, and the generated src/types need the backend toolchain, so I relied on the passing PR Checks run.

What works well

  • Explicit history in streamText. Passing the conversation in, instead of reading messages from the last render inside the mutation, is the right fix. It also removes the startStreaming and streamLocationRef workaround in Letter.tsx. The new Letter effect is easier to follow, and moving hasInitialized.current = true to the top makes the StrictMode guard obvious.
  • !response.ok → throw in useMessages.tsx:376. This goes into the existing catch in streamHelper.ts:107, so 5xx and proxy HTML pages become the normal error message. On the letter page, cleanCompletion stays false, so "Unable to generate letter" shows as the QA steps describe.
  • Input lock. Passing setIsLoading to InitializationForm closes the double-stream window. The refocus in MessageWindow.tsx:50 runs on a 500ms timeout, so it fires after isLoading clears and focus still lands on the re-enabled textarea.
  • createId(). Good call skipping crypto.randomUUID for vite --host LAN testing. The backend treats id as an opaque string, so the new "<ms>-<n>" format is safe.
  • hiddenMessageCount prop. Much better than checking useLocation().pathname inside MessageWindow.
  • testUtils.tsx and the global stubs. These remove a lot of copy-paste. messagesAddedBy makes the Letter assertions much easier to read.

Non-blocking suggestions and observations

  1. testUtils.tsx uses the global vi but imports Mock from "vitest". It works because globals: true is set in vitest.config.ts. Every other test file imports vi explicitly, though, and a type-only import next to a global is a little surprising. Consider import { vi, type Mock } from "vitest";. Fix this →

  2. lg:overflow-y-auto now also applies on the letter page (MessageContainer.tsx:58). Before this PR only Chat had it. Letter sets isOngoing in its first effect, so the class is only there for the first paint and nothing visible changes. Your before/after layout measurements agree. I'm only mentioning it because the shared container now carries a chat-specific rule.

  3. Letter failure depends on message position. When the letter request fails, the list ends up as [user, "Sorry, I encountered an error…", "Unable to generate letter…"]. hiddenMessageCount={2} hides the "Sorry" message only because it took the AI placeholder's slot. That works today, and the behaviour was the same before this PR. The coupling is easy to break if streamText's error path changes, for example by appending instead of replacing. A short comment next to hiddenMessageCount={2} in Letter.tsx:325 noting this case would help, or you could leave it for the follow-up that restructures the run-once effect.

  4. The "none" sentinel in FrequentInquiries.tsx is now redundant. Collapsed is the default, so a stored "none" and a missing key mean the same thing. The toggle at :213 could remove the key instead of writing "none". Keep reading "none" either way for existing users. This is purely optional; the current code is correct.

  5. InputField reads messages from its render closure (InputField.tsx:590). That's safe only because the input is disabled while a stream is running, so the render that re-enables it already has the final AI message. That's fine, but the safety depends on the input lock, so a one-line comment there would protect it from future edits.

Repo checklist (.claude/skills/pr.md)

  • Tests added or updated for each fix
  • Architecture.md updated (ChatPageLayout.tsx, testUtils.tsx)
  • Comments are full sentences ending in periods (CLAUDE.md style rule)
  • No GitHub Actions or env var changes

The follow-ups you listed (scroll and refocus on every streamed line, cancelling in-flight requests) are reasonable to defer.

@leekahung
leekahung requested a review from wittejm October 12, 2026 18:28
@leekahung leekahung added frontend Frontend implementation (follows UX design) bug Something isn't working labels Oct 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working frontend Frontend implementation (follows UX design)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant