Skip to content

Commit 66e7054

Browse files
loganjnpub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je
andauthored
fix(desktop): deduplicate relay outage notification (#3579)
🤖 ## Summary - Keep the relay reconnect notification dismissed across repeated connection retries during one continuous outage. - Re-arm the notification after recovery or relay lifecycle replacement, including switches between communities that use the same relay URL. - Preserve the existing dedicated path for authentication and other application-level errors. ### How an outage is tracked The AppShell-owned relay-card hook treats an outage as one contiguous runtime episode rather than assigning it a persisted ID. A hook-local `outageActiveRef` is armed by the first qualifying unreachable/degraded observation. While it is armed, intermediate retry states (`connecting`, `reconnecting`, `stalled`, and `disconnected`) belong to that same episode, so retry churn cannot clear dismissal or emit another notification. The hook receives the same lifecycle identity used by community initialization: community ID plus `reinitKey`. This distinguishes multiple communities even when they share a relay URL, and it also changes when the active community is explicitly reinitialized. The latch and dismissal are reset when that identity changes or when the relay singleton reports its authoritative `idle` teardown state. A successful `connected` state also closes the episode and re-arms the next outage. These boundaries deliberately bias toward re-notifying rather than suppressing a later outage: recovery, community switch/reinit, or relay teardown cannot leave the hook stuck believing an old outage is still active. No outage state is persisted beyond the mounted hook lifecycle. ### Related issue None found. ### Testing - `pnpm --dir desktop typecheck` - `pnpm --dir desktop test` — 3,769 passed - `pnpm --dir desktop check` - `pnpm --dir desktop build:e2e` - `pnpm --dir desktop exec playwright test tests/e2e/sidebar-relay-card.spec.ts --project=integration` — 11 passed --------- Signed-off-by: Larry <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz> Co-authored-by: npub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz>
1 parent b42a8d4 commit 66e7054

3 files changed

Lines changed: 93 additions & 11 deletions

File tree

‎desktop/src/app/AppShell.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ export function AppShell() {
231231
const relayConnectionCard = useSidebarRelayConnectionCard(
232232
channelsErrorMessage,
233233
communitiesHook.activeCommunity?.relayUrl,
234+
`${communitiesHook.activeCommunity?.id ?? "none"}-${communitiesHook.reinitKey}`,
234235
);
235236
const memberChannels = React.useMemo(
236237
() => channels.filter((channel) => channel.isMember),

‎desktop/src/features/sidebar/ui/useSidebarRelayConnectionCard.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function isDocumentVisible() {
6363
export function useSidebarRelayConnectionCard(
6464
errorMessage?: string,
6565
relayUrl?: string | null,
66+
relayLifecycleKey = relaySuccessKey(relayUrl),
6667
) {
6768
const relayConnectionState = useRelayConnection();
6869
const hasRelayUnreachableError = errorMessage
@@ -79,7 +80,6 @@ export function useSidebarRelayConnectionCard(
7980
relayConnectionState === "stalled" ||
8081
(relayConnectionState === "disconnected" && !hasNonUnreachableError);
8182
const isRelayConnectionConnected = relayConnectionState === "connected";
82-
const isRelayConnectionDisconnected = relayConnectionState === "disconnected";
8383
const [isDismissed, setIsDismissed] = React.useState(false);
8484
const hasSuccess = React.useSyncExternalStore(
8585
subscribeRelayConnectivitySuccess,
@@ -95,6 +95,8 @@ export function useSidebarRelayConnectionCard(
9595
const isRelayConnectionSuccess = hasSuccess && isRelayConnectionConnected;
9696
const canShow = isRelayConnectionActuallyDegraded || isRelayConnectionSuccess;
9797
const show = canShow && !isDismissed;
98+
const outageActiveRef = React.useRef(false);
99+
const outageRelayLifecycleKeyRef = React.useRef(relayLifecycleKey);
98100
const wasProblemCardVisibleRef = React.useRef(false);
99101
const {
100102
isPending: isReconnectPending,
@@ -111,30 +113,41 @@ export function useSidebarRelayConnectionCard(
111113
isReconnectPending || connectivityAction === "relay-connection";
112114

113115
React.useEffect(() => {
114-
if (!isRelayConnectionActuallyDegraded && !isRelayConnectionSuccess) {
116+
if (outageRelayLifecycleKeyRef.current !== relayLifecycleKey) {
117+
outageRelayLifecycleKeyRef.current = relayLifecycleKey;
118+
outageActiveRef.current = false;
119+
wasProblemCardVisibleRef.current = false;
115120
setIsDismissed(false);
116121
}
117-
}, [isRelayConnectionSuccess, isRelayConnectionActuallyDegraded]);
118122

119-
React.useEffect(() => {
120-
if (isRelayConnectionStateDegraded || isRelayConnectionDisconnected) {
121-
setRelayConnectivitySuccess(relayUrl, false);
123+
if (relayConnectionState === "idle") {
124+
outageActiveRef.current = false;
125+
wasProblemCardVisibleRef.current = false;
122126
setIsDismissed(false);
127+
return;
123128
}
124-
}, [isRelayConnectionDisconnected, isRelayConnectionStateDegraded, relayUrl]);
125129

126-
React.useEffect(() => {
127130
if (isRelayConnectionActuallyDegraded) {
131+
if (!outageActiveRef.current) {
132+
outageActiveRef.current = true;
133+
setRelayConnectivitySuccess(relayUrl, false);
134+
setIsDismissed(false);
135+
}
128136
wasProblemCardVisibleRef.current = show && !isRelayConnectionSuccess;
129137
return;
130138
}
131139

132-
if (wasProblemCardVisibleRef.current && isRelayConnectionConnected) {
133-
wasProblemCardVisibleRef.current = false;
134-
setRelayConnectivitySuccess(relayUrl, true);
140+
if (outageActiveRef.current && isRelayConnectionConnected) {
141+
outageActiveRef.current = false;
142+
if (wasProblemCardVisibleRef.current) {
143+
wasProblemCardVisibleRef.current = false;
144+
setRelayConnectivitySuccess(relayUrl, true);
145+
}
135146
}
136147
}, [
137148
isRelayConnectionSuccess,
149+
relayLifecycleKey,
150+
relayConnectionState,
138151
relayUrl,
139152
show,
140153
isRelayConnectionActuallyDegraded,

‎desktop/tests/e2e/sidebar-relay-card.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@ async function setRelayConnectionState(
8484
}, state);
8585
}
8686

87+
async function emitRelayConnectionState(
88+
page: Page,
89+
state: RelayConnectionState,
90+
) {
91+
await page.evaluate((nextState) => {
92+
const setConnectionState = (
93+
window as Window & {
94+
__BUZZ_E2E_SET_RELAY_CONNECTION_STATE__?: (
95+
state: RelayConnectionState,
96+
) => void;
97+
}
98+
).__BUZZ_E2E_SET_RELAY_CONNECTION_STATE__;
99+
if (!setConnectionState) {
100+
throw new Error("Mock relay connection state helper is not installed.");
101+
}
102+
setConnectionState(nextState);
103+
}, state);
104+
}
105+
87106
async function expectGenericReconnectCard(page: Page) {
88107
const card = page.getByTestId("sidebar-relay-unreachable");
89108
await expect(card).toBeVisible();
@@ -109,6 +128,55 @@ test("sidebar generic relay failures use the reconnect card", async ({
109128
await expectGenericReconnectCard(page);
110129
});
111130

131+
test("relay outage notification stays dismissed through retries and re-arms after recovery", async ({
132+
page,
133+
}) => {
134+
await installMockBridge(page, { channelsReadError: CONNECT_ERROR });
135+
await page.goto("/");
136+
await setRelayConnectionState(page, "disconnected");
137+
138+
const card = await expectGenericReconnectCard(page);
139+
await card
140+
.getByRole("button", { name: "Dismiss relay notification" })
141+
.click({ force: true });
142+
await expect(card).toBeHidden();
143+
144+
// Retry churn is still the same outage: no successful connection occurred.
145+
await emitRelayConnectionState(page, "connecting");
146+
await emitRelayConnectionState(page, "disconnected");
147+
await emitRelayConnectionState(page, "reconnecting");
148+
await page.waitForTimeout(2_100);
149+
await expect(card).toBeHidden();
150+
151+
// A successful connection ends the episode and re-arms the next outage.
152+
await setChannelsReadError(page, null);
153+
await emitRelayConnectionState(page, "connected");
154+
await setChannelsReadError(page, CONNECT_ERROR);
155+
await emitRelayConnectionState(page, "disconnected");
156+
await expectGenericReconnectCard(page);
157+
});
158+
159+
test("relay outage notification re-arms after same-URL lifecycle teardown", async ({
160+
page,
161+
}) => {
162+
await installMockBridge(page, { channelsReadError: CONNECT_ERROR });
163+
await page.goto("/");
164+
await setRelayConnectionState(page, "disconnected");
165+
166+
const card = await expectGenericReconnectCard(page);
167+
await card
168+
.getByRole("button", { name: "Dismiss relay notification" })
169+
.click({ force: true });
170+
await expect(card).toBeHidden();
171+
172+
// Community switches and reconnectCommunity() tear down the singleton to
173+
// idle before applying the next lifecycle. The next lifecycle may reuse the
174+
// same relay URL, so URL identity alone must not preserve the old dismissal.
175+
await emitRelayConnectionState(page, "idle");
176+
await emitRelayConnectionState(page, "disconnected");
177+
await expectGenericReconnectCard(page);
178+
});
179+
112180
test("sidebar proxy sign-in failures use the reconnect card", async ({
113181
page,
114182
}) => {

0 commit comments

Comments
 (0)