Skip to content

feat(react): keep the active thread in the URL with useThreadRouting - #1649

Open
blove wants to merge 2 commits into
mainfrom
blove/react-thread-routing
Open

blove wants to merge 2 commits into
mainfrom
blove/react-thread-routing

Conversation

@blove

@blove blove commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Angular exports injectThreadRouting (libs/chat/src/lib/routing/thread-routing.ts): it seeds the active thread from the URL, navigates (push) when the thread changes, follows router navigation, and on a failed validate navigates to the bare path with replaceUrl: true. React chat had no URL binding at all.

This adds an opt-in useThreadRouting(options) hook to @threadplane/react/chat with the same behavior:

  • Returns { threadId, setThreadId }; the URL is the source of truth (no local storage).
  • Restores the thread on load; setThreadId pushes a history entry (no write when the thread is already active); back and forward (popstate) are followed.
  • Optional exists(id): boolean | Promise<boolean>; on false, the URL is replaced with the bare URL. Each id is checked once until another is selected (Angular memoizes the same way), a late answer for a thread that is no longer in the URL is ignored, and a rejected check keeps the thread.
  • Router-agnostic: adapter: { read, write(id, 'push' | 'replace'), subscribe } for React Router or Next.js. The default adapter uses window.location/history with Angular's URL shape (/<threadId>), a configurable basePath (/chat/<threadId>) or a param (?thread=<threadId>), and keeps the query and hash (Angular preserves query params).
  • SSR safe: useSyncExternalStore with a null server snapshot, so neither the server render nor hydration reads the URL; the default adapter reads nothing without window.

ThreadSidebar and ThreadDirectory are not on main, so there is no directory-based default; the docs wire ThreadList activeThreadId/onSelect to the hook. A directory-based exists can be layered on once those land.

Also: useThreadRouting added to the verify-packages allowlist and its spec fixture, disposition rows appended for the new exports and source file, and a "Keep the active thread in the URL" section in the React thread-routing guide.

Verification

  • New spec libs/react/src/chat/thread-routing.spec.tsx (16 tests) failed before the implementation (no export), passes after. Mutation checks: making the server snapshot read the URL, dropping the late-answer guard, writing selections with replace, and accepting nested path segments each fail a test.
  • NX_DAEMON=false npx nx run-many -t test lint type-tests -p react --skip-nx-cache: passed (1541 tests).
  • node --test scripts/react-parity/verify-packages.spec.mjs: 103 passed.
  • node scripts/react-parity/inventory.mjs --check --drift warn: valid (baseline drift is informational only).
  • npx vitest run in apps/website: 1965 passed, 4 failed, none related to this change. docs-example-code.spec.ts (5s timeout under load) and Footer.spec.tsx pass alone with --testTimeout=60000; api/ingest/route.spec.ts cannot resolve posthog-node and generate-api-docs.spec.ts rejects the symlinked shared node_modules, both local environment issues in this worktree.

🤖 Generated with Claude Code

Angular chat binds the active thread to the URL with injectThreadRouting:
restore on load, push on change, follow navigation, and replace a thread
that fails validation with the bare URL. React chat had no URL binding.

useThreadRouting does the same in @threadplane/react/chat. React has no
single router, so the URL binding is an adapter (read, write, subscribe);
the default uses window.location and history with Angular's /<threadId>
shape, a configurable basePath or query param, and reads nothing during
the server render or hydration. An optional exists check replaces a
missing thread with the bare URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@blove
blove enabled auto-merge November 18, 2026 01:28
CodeQL flagged /\/+$/ in normalizeBase as a polynomial regular expression
on uncontrolled data. A backwards loop over trailing slashes is linear.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants