-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(web): add connection status banner to thread view #1270 #1369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JaskiratAnand
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
JaskiratAnand:fix/show-no-connection-state
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+162
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
apps/web/src/components/chat/ConnectionStatusBanner.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { renderToStaticMarkup } from "react-dom/server"; | ||
| import { describe, expect, it, vi, beforeAll } from "vitest"; | ||
| import { ConnectionStatusBanner } from "./ConnectionStatusBanner"; | ||
|
|
||
| // Mock wsNativeApi | ||
| vi.mock("../../wsNativeApi", () => { | ||
| return { | ||
| onTransportStateChange: vi.fn(() => { | ||
| return () => {}; | ||
| }), | ||
| }; | ||
| }); | ||
|
|
||
| describe("ConnectionStatusBanner", () => { | ||
| beforeAll(() => { | ||
| vi.stubGlobal("navigator", { | ||
| onLine: true, | ||
| }); | ||
| }); | ||
|
|
||
| it("renders nothing when online and transport is open", () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ConnectionStatusBanner initialIsOnline={true} initialTransportState="open" />, | ||
| ); | ||
| expect(markup).toBe(""); | ||
| }); | ||
|
|
||
| it("renders offline message when initialIsOnline is false", async () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ConnectionStatusBanner initialIsOnline={false} initialTransportState="open" />, | ||
| ); | ||
| expect(markup).toContain("No internet connection"); | ||
| expect(markup).toContain("T3 Code is offline"); | ||
| }); | ||
|
|
||
| it("renders disconnected message when initialTransportState is closed", async () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ConnectionStatusBanner initialIsOnline={true} initialTransportState="closed" />, | ||
| ); | ||
| expect(markup).toContain("Disconnected from server"); | ||
| expect(markup).toContain("connection to the T3 Code server was lost"); | ||
| }); | ||
|
|
||
| it("renders reconnecting message when initialTransportState is reconnecting", async () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ConnectionStatusBanner initialIsOnline={true} initialTransportState="reconnecting" />, | ||
| ); | ||
| expect(markup).toContain("Disconnected from server"); | ||
| expect(markup).toContain("Attempting to reconnect"); | ||
| }); | ||
|
|
||
| it("renders nothing when transport is disposed", () => { | ||
| const markup = renderToStaticMarkup( | ||
| <ConnectionStatusBanner initialIsOnline={true} initialTransportState="disposed" />, | ||
| ); | ||
| expect(markup).toBe(""); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { memo, useEffect, useState } from "react"; | ||
| import { onTransportStateChange, type TransportState } from "../../wsNativeApi"; | ||
| import { Alert, AlertDescription, AlertTitle } from "../ui/alert"; | ||
| import { WifiOffIcon, CloudOffIcon } from "lucide-react"; | ||
|
|
||
| export const ConnectionStatusBanner = memo(function ConnectionStatusBanner({ | ||
| initialIsOnline, | ||
| initialTransportState, | ||
| }: { | ||
| initialIsOnline?: boolean; | ||
| initialTransportState?: TransportState; | ||
| }) { | ||
| const [isOnline, setIsOnline] = useState( | ||
| initialIsOnline ?? (typeof navigator !== "undefined" ? navigator.onLine : true), | ||
| ); | ||
| const [transportState, setTransportState] = useState<TransportState>( | ||
| initialTransportState ?? "open", | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const handleOnline = () => setIsOnline(true); | ||
| const handleOffline = () => setIsOnline(false); | ||
|
|
||
| window.addEventListener("online", handleOnline); | ||
| window.addEventListener("offline", handleOffline); | ||
|
|
||
| const unsub = onTransportStateChange((state) => { | ||
| setTransportState(state); | ||
| }); | ||
|
|
||
| return () => { | ||
| window.removeEventListener("online", handleOnline); | ||
| window.removeEventListener("offline", handleOffline); | ||
| unsub(); | ||
| }; | ||
| }, []); | ||
|
|
||
| const shouldShow = | ||
| !isOnline || | ||
| (transportState !== "open" && transportState !== "connecting" && transportState !== "disposed"); | ||
|
|
||
| if (!shouldShow) { | ||
| return null; | ||
| } | ||
|
|
||
| const title = !isOnline ? "No internet connection" : "Disconnected from server"; | ||
| const message = !isOnline | ||
| ? "T3 Code is offline. Please check your internet connection." | ||
| : transportState === "reconnecting" | ||
| ? "Attempting to reconnect to the T3 Code server..." | ||
| : "The connection to the T3 Code server was lost."; | ||
|
|
||
| return ( | ||
| <div className="pt-3 mx-auto max-w-3xl"> | ||
| <Alert variant="warning"> | ||
| {!isOnline ? <WifiOffIcon className="size-4" /> : <CloudOffIcon className="size-4" />} | ||
| <AlertTitle>{title}</AlertTitle> | ||
| <AlertDescription>{message}</AlertDescription> | ||
| </Alert> | ||
| </div> | ||
| ); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Online state not re-synced inside useEffect
Low Severity
The transport state is properly synchronized inside the
useEffectbecauseonTransportStateChangeimmediately invokes the listener with the current state (line 27–29). However,isOnlineis only read once in theuseStateinitializer (line 13–14) and is never re-synced inside the effect. If anofflineevent fires between the initial render and the effect setup, the event is missed andisOnlineremains stale — the banner stays hidden until the next online/offline event. Adding asetIsOnline(navigator.onLine)call inside the effect after registering the event listeners would close this gap, consistent with how transport state is already handled.