Skip to content

Connect the Text Us page to the chat API and texter WebSocket (W6-01) - #166

Merged
calebyhan merged 1 commit into
mainfrom
135/text-us-chat
Nov 18, 2026
Merged

calebyhan merged 1 commit into
mainfrom
135/text-us-chat

Conversation

@anwang132

Copy link
Copy Markdown
Contributor

The Text Us page is still a heading. The chat window (#56) runs on local state, and the backend for it is done: sessions and REST (#90) and the texter socket (#112). This ticket connects them, so a student can open /text-us, read the notice, and talk to a responder (TDD 4.3.1, 4.6.2).

Changes:

  • Added useChat, a hook that owns chatReducer and talks to the backend. "Start" calls POST /api/chat/sessions and keeps the token in sessionStorage, so closing the tab ends the texter's identity. The token is only sent in the socket's auth frame or an Authorization header, never in a URL.
  • The hook opens createSocket on /ws/chat and sends {"type": "auth", "token": ...} in onOpen.
  • Sends go out as {"type": "message", "body": ...} and show as pending. The server's message.created echo confirms the oldest pending message with the same body. An error frame marks it failed, and the failed message can be retried.
  • onReconnect fetches GET /api/chat/conversation?after=<last id> and adds anything missed. Messages already shown are skipped, so a texter message the socket already delivered is never matched twice.
  • conversation.closed ends the chat. A 4401 close clears the token and ends it too.
  • "End conversation" calls POST /api/chat/conversation/end, then clears the token.
  • If a token is already in sessionStorage on load, the hook resumes with GET /api/chat/conversation instead of showing the notice again. A rejected token (401) is cleared and the notice shows.
  • /text-us shows the text-us.intro content block and the line status (useLineStatus). It shows the chat window with chat.notice as the notice when "web" is in the channels setting, and the number with an sms: link when sms_number is set. I added a loading.tsx like the other public pages, which next build needs for this route.
  • next.config.ts headers() sends Cache-Control: no-store and Referrer-Policy: no-referrer for /text-us and /portal/* (TDD 4.8).
  • Tests use the fake socket and cover: start storing the token in sessionStorage and sending the auth frame, an echoed message confirming the pending one, a reconnect fetching with after, conversation.closed and a 4401 both ending the chat and clearing the token, and a refresh with a token resuming the conversation. There are also tests for the page, the headers config, and that nothing logs message bodies or the token.
  • /dev/chat is unchanged and still works without the backend. No analytics script loads on /text-us.

How to test: run make dev-backend and make dev-frontend, open /text-us, start a conversation and send a message. Refresh the tab mid-chat to see it resume, then end the conversation and confirm a refresh shows the notice again.

Closes #135

Add useChat, which starts a session (token in sessionStorage), opens the
texter socket with an auth frame, confirms pending sends from the server
echo, catches up with ?after= on reconnect, resumes on refresh, and ends
the chat on conversation.closed, a 4401 close, or End conversation.

Render the text-us.intro block, line status, chat window (when web chat is
enabled) and the SMS number on /text-us, and send Cache-Control: no-store
and Referrer-Policy: no-referrer for /text-us and /portal/*.

Closes #135
@github-actions

Copy link
Copy Markdown

Backend Test Results

685 tests  ±0   685 ✅ ±0   20s ⏱️ -4s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 2320455. ± Comparison against base commit 9718c3b.

@anwang132
anwang132 requested a review from calebyhan November 18, 2026 00:05

@calebyhan calebyhan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@calebyhan
calebyhan merged commit d91d866 into main Nov 18, 2026
4 checks passed
@calebyhan
calebyhan deleted the 135/text-us-chat branch November 18, 2026 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connect the Text Us page to the chat API and texter WebSocket (W6-01)

2 participants