Skip to content

Commit 6d147e0

Browse files
cliffhallclaude
andcommitted
fix(server-card): surface failed-handshake errors with a 5s auto-dismiss
Two related fixes so the user actually sees what went wrong when a connect attempt fails (e.g. OAuth-backed server, bad URL, missing dev auth token): - App.tsx: add `connectInFlightRef` and skip clearing `activeServerId` in the InspectorClient `disconnect` listener while a connect attempt is in flight. The transport closes during a failing handshake and dispatches `disconnect` before the catch block sets `errorMessage`; without this guard InspectorView would map the card to plain "disconnected" and the InlineError would never render. - InlineError: opt-in `autoDismissMs` prop that runs a Mantine `slide-up` Transition exit animation after the message has been visible for the given duration. Visibility is derived from a "dismissed message" state (rather than a mounted boolean + setMounted-in-effect), so the timer re-arms whenever `error.message` changes without tripping the react-hooks/set-state-in-effect rule. ServerCard passes autoDismissMs={5000}. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 9416b77 commit 6d147e0

4 files changed

Lines changed: 152 additions & 28 deletions

File tree

‎clients/web/src/App.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ function App() {
234234
// and consumed at the "connected" edge — a ref (not state) so the
235235
// intervening rerenders don't reset it.
236236
const connectStartRef = useRef<number | undefined>(undefined);
237+
// True while a `client.connect()` call is in flight. The transport's
238+
// close handler fires during a failing handshake and dispatches the
239+
// `disconnect` event we listen for below; without this guard the
240+
// listener clears `activeServerId` mid-flight, which makes
241+
// InspectorView map the card to plain "disconnected" and swallows
242+
// the error the catch block is about to set in `errorMessage`.
243+
const connectInFlightRef = useRef(false);
237244
const [latencyMs, setLatencyMs] = useState<number | undefined>(undefined);
238245
const [errorMessage, setErrorMessage] = useState<string | undefined>(
239246
undefined,
@@ -317,6 +324,12 @@ function App() {
317324
useEffect(() => {
318325
if (!inspectorClient) return;
319326
const onDisconnect = () => {
327+
// Skip while a handshake is mid-flight — the transport closes when
328+
// connect() fails and dispatches this event before the catch block
329+
// runs. Clearing activeServerId here would force InspectorView to
330+
// map the card to plain "disconnected" instead of the error variant
331+
// that surfaces the InlineError.
332+
if (connectInFlightRef.current) return;
320333
setActiveServerId(undefined);
321334
// Drop the open flag too — without this the modal would pop back the
322335
// next time `initializeResult` re-becomes truthy (e.g. reconnect).
@@ -520,6 +533,7 @@ function App() {
520533

521534
setErrorMessage(undefined);
522535
connectStartRef.current = Date.now();
536+
connectInFlightRef.current = true;
523537
try {
524538
// `settings.connectionTimeout` is consumed inside InspectorClient.connect
525539
// (Promise.race + transport teardown live there now), so this branch
@@ -534,6 +548,8 @@ function App() {
534548
connectStartRef.current = undefined;
535549
const message = err instanceof Error ? err.message : String(err);
536550
setErrorMessage(message);
551+
} finally {
552+
connectInFlightRef.current = false;
537553
}
538554
},
539555
[

‎clients/web/src/components/elements/InlineError/InlineError.test.tsx‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2+
import { act } from "react";
23
import { renderWithMantine, screen } from "../../../test/renderWithMantine";
34
import userEvent from "@testing-library/user-event";
45
import { InlineError } from "./InlineError";
@@ -61,4 +62,65 @@ describe("InlineError", () => {
6162
const link = screen.getByRole("link", { name: /Troubleshooting/ });
6263
expect(link).toHaveAttribute("href", "https://example.com/docs");
6364
});
65+
66+
describe("autoDismissMs", () => {
67+
beforeEach(() => {
68+
vi.useFakeTimers();
69+
});
70+
afterEach(() => {
71+
vi.useRealTimers();
72+
});
73+
74+
it("stays visible until autoDismissMs elapses", () => {
75+
renderWithMantine(
76+
<InlineError error={{ message: "boom" }} autoDismissMs={5000} />,
77+
);
78+
expect(screen.getByText("boom")).toBeInTheDocument();
79+
act(() => {
80+
vi.advanceTimersByTime(4999);
81+
});
82+
// Still visible just before the deadline.
83+
expect(screen.getByText("boom")).toBeInTheDocument();
84+
});
85+
86+
it("triggers the exit transition after autoDismissMs", () => {
87+
renderWithMantine(
88+
<InlineError error={{ message: "boom" }} autoDismissMs={1000} />,
89+
);
90+
act(() => {
91+
vi.advanceTimersByTime(1000);
92+
});
93+
// Mantine's slide-up transition unmounts after duration+exitDuration.
94+
// Advance past it to give the Transition time to finish its exit.
95+
act(() => {
96+
vi.advanceTimersByTime(1000);
97+
});
98+
expect(screen.queryByText("boom")).not.toBeInTheDocument();
99+
});
100+
101+
it("re-arms the timer when the error message changes", () => {
102+
const { rerender } = renderWithMantine(
103+
<InlineError error={{ message: "first" }} autoDismissMs={1000} />,
104+
);
105+
act(() => {
106+
vi.advanceTimersByTime(900);
107+
});
108+
rerender(
109+
<InlineError error={{ message: "second" }} autoDismissMs={1000} />,
110+
);
111+
// The 900ms already elapsed shouldn't count against the new message.
112+
act(() => {
113+
vi.advanceTimersByTime(500);
114+
});
115+
expect(screen.getByText("second")).toBeInTheDocument();
116+
});
117+
118+
it("does not auto-dismiss when autoDismissMs is undefined", () => {
119+
renderWithMantine(<InlineError error={{ message: "boom" }} />);
120+
act(() => {
121+
vi.advanceTimersByTime(60000);
122+
});
123+
expect(screen.getByText("boom")).toBeInTheDocument();
124+
});
125+
});
64126
});

‎clients/web/src/components/elements/InlineError/InlineError.tsx‎

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { Alert, Button, Collapse, Group, Stack, Text } from "@mantine/core";
2-
import { useState } from "react";
1+
import {
2+
Alert,
3+
Button,
4+
Collapse,
5+
Group,
6+
Stack,
7+
Text,
8+
Transition,
9+
} from "@mantine/core";
10+
import { useEffect, useState } from "react";
311

412
export interface InlineErrorProps {
513
error: { message: string; data?: unknown };
614
retryCount?: number;
715
maxRetries?: number;
816
docLink?: string;
17+
/**
18+
* When set, the alert slides up + fades out after this many ms.
19+
* Resets whenever `error.message` changes so a fresh error always
20+
* starts a new countdown.
21+
*/
22+
autoDismissMs?: number;
923
}
1024

1125
const HeaderRow = Group.withProps({
@@ -58,39 +72,70 @@ export function InlineError({
5872
retryCount,
5973
maxRetries,
6074
docLink,
75+
autoDismissMs,
6176
}: InlineErrorProps) {
6277
const [expanded, setExpanded] = useState(false);
78+
// Derive visibility from "which message has been dismissed". A new
79+
// error message makes `dismissedMessage` stale and the alert renders;
80+
// when the timer fires we mark this message dismissed and the
81+
// Transition runs its exit animation. Doing it this way (vs. a
82+
// mounted boolean + setMounted(true) on message change) avoids the
83+
// forbidden setState-in-effect pattern.
84+
const [dismissedMessage, setDismissedMessage] = useState<string | undefined>(
85+
undefined,
86+
);
87+
const mounted = error.message !== dismissedMessage;
6388
const details =
6489
error.data !== undefined ? formatDetails(error.data) : undefined;
6590
const hasExpandable = details !== undefined || docLink !== undefined;
6691

92+
useEffect(() => {
93+
if (!autoDismissMs) return;
94+
const timer = setTimeout(
95+
() => setDismissedMessage(error.message),
96+
autoDismissMs,
97+
);
98+
return () => clearTimeout(timer);
99+
}, [autoDismissMs, error.message]);
100+
67101
return (
68-
<Alert color="red" variant="light">
69-
<Stack gap="xs">
70-
<HeaderRow>
71-
<MessageGroup>
72-
<ErrorMessage>{error.message}</ErrorMessage>
73-
{retryCount !== undefined && (
74-
<RetryText>{formatRetryLabel(retryCount, maxRetries)}</RetryText>
75-
)}
76-
</MessageGroup>
77-
{hasExpandable && (
78-
<ExpandButton onClick={() => setExpanded((v) => !v)}>
79-
{expanded ? "Show less" : "Show more"}
80-
</ExpandButton>
81-
)}
82-
</HeaderRow>
83-
<Collapse in={expanded}>
102+
<Transition
103+
mounted={mounted}
104+
transition="slide-up"
105+
duration={350}
106+
timingFunction="ease"
107+
>
108+
{(transitionStyle) => (
109+
<Alert color="red" variant="light" style={transitionStyle}>
84110
<Stack gap="xs">
85-
{details && <Text size="sm">{details}</Text>}
86-
{docLink && (
87-
<DocLinkButton href={docLink}>
88-
View Troubleshooting Guide
89-
</DocLinkButton>
90-
)}
111+
<HeaderRow>
112+
<MessageGroup>
113+
<ErrorMessage>{error.message}</ErrorMessage>
114+
{retryCount !== undefined && (
115+
<RetryText>
116+
{formatRetryLabel(retryCount, maxRetries)}
117+
</RetryText>
118+
)}
119+
</MessageGroup>
120+
{hasExpandable && (
121+
<ExpandButton onClick={() => setExpanded((v) => !v)}>
122+
{expanded ? "Show less" : "Show more"}
123+
</ExpandButton>
124+
)}
125+
</HeaderRow>
126+
<Collapse in={expanded}>
127+
<Stack gap="xs">
128+
{details && <Text size="sm">{details}</Text>}
129+
{docLink && (
130+
<DocLinkButton href={docLink}>
131+
View Troubleshooting Guide
132+
</DocLinkButton>
133+
)}
134+
</Stack>
135+
</Collapse>
91136
</Stack>
92-
</Collapse>
93-
</Stack>
94-
</Alert>
137+
</Alert>
138+
)}
139+
</Transition>
95140
);
96141
}

‎clients/web/src/components/groups/ServerCard/ServerCard.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export function ServerCard({
149149
data: connection.error.details,
150150
}}
151151
retryCount={connection.retryCount}
152+
autoDismissMs={5000}
152153
/>
153154
)}
154155

0 commit comments

Comments
 (0)