Files
ds-chat/frontend/src/App.tsx
T
ksmithandClaude Sonnet 5 85dc83f4e5 Fix presence showing offline on non-chat pages, and a room-join bug it exposed
The WebSocket connection lived entirely inside ChatShellPage, so
navigating to /admin (which never opens its own connection) unmounted
it -- the server correctly marked the user offline since the
connection genuinely closed, even though they were still logged in
and using the app. New ChatSocketContext.tsx hoists the connection to
App.tsx, shared across every authenticated route via a single
provider (keyed by user id, so a logout/login as a different account
gets a clean reconnect rather than an old connection lingering under
a new identity) instead of living inside whichever page happens to be
mounted.

Verifying that fix surfaced a second, independent bug: #31's
visibility handling had gated the *explicit* joinRoom/leaveRoom calls
(fired when a room actually mounts/unmounts in the UI) on the same
isVisibleRef check meant for automatic background/foreground
transitions. That's wrong -- a room can only be opened by a real user
interaction, which can't happen on a genuinely backgrounded tab, so
gating it too meant a stale or momentarily-wrong visibility reading
at mount time could silently skip the join with nothing to ever retry
it. joinRoom/leaveRoom now always send immediately; only the
automatic hide/show transitions and the reconnect replay stay gated
on visibility, which is what #31 actually needed.

Verified both end-to-end in the browser: navigating to /admin via
real in-app navigation (not a reload) keeps the presence dot online,
confirmed via direct Redis inspection and the /api/users/online
endpoint; opening a room and sending a message works immediately
afterward.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-16 16:37:40 -06:00

71 lines
2.2 KiB
TypeScript

import { Navigate, Route, Routes } from 'react-router-dom'
import { AuthProvider, useAuth } from './context/AuthContext'
import { ChatSocketProvider } from './context/ChatSocketContext'
import { AdminRoute } from './components/AdminRoute'
import { ProtectedRoute } from './components/ProtectedRoute'
import { UpdateBanner } from './components/UpdateBanner'
import { LoginPage } from './pages/LoginPage'
import { SignupPage } from './pages/SignupPage'
import { ForgotPasswordPage } from './pages/ForgotPasswordPage'
import { ResetPasswordPage } from './pages/ResetPasswordPage'
import { ChatShellPage } from './pages/ChatShellPage'
import { AdminPage } from './pages/AdminPage'
function AppRoutes() {
const routes = (
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/signup" element={<SignupPage />} />
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
<Route path="/reset-password" element={<ResetPasswordPage />} />
<Route
path="/rooms"
element={
<ProtectedRoute>
<ChatShellPage />
</ProtectedRoute>
}
/>
<Route
path="/rooms/:roomId"
element={
<ProtectedRoute>
<ChatShellPage />
</ProtectedRoute>
}
/>
<Route
path="/admin"
element={
<AdminRoute>
<AdminPage />
</AdminRoute>
}
/>
<Route path="*" element={<Navigate to="/rooms" replace />} />
</Routes>
)
// Only while actually logged in -- and keyed by user id so switching
// which account is logged in (same tab) tears down and re-establishes a
// fresh connection rather than an old one lingering under a new
// identity. Wraps every authenticated route (not just ChatShellPage),
// since the connection backs the presence indicator and cross-page
// signals (e.g. "added to a room") that matter regardless of which page
// is currently open.
const { user } = useAuth()
if (!user) return routes
return <ChatSocketProvider key={user.id}>{routes}</ChatSocketProvider>
}
function App() {
return (
<AuthProvider>
<UpdateBanner />
<AppRoutes />
</AuthProvider>
)
}
export default App