Skip to content

Commit 14faede

Browse files
committed
feat: add option to toggle event stream functionality
1 parent 4cf92a1 commit 14faede

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/components/EventStream.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@ import {
66
useGetEventStream,
77
} from "@squonk/account-server-client/event-stream";
88

9+
import { useAtom } from "jotai";
910
import { useSnackbar } from "notistack";
1011

1112
import { useASAuthorizationStatus } from "../hooks/useIsAuthorized";
1213
import { getMessageFromEvent, protoBlobToText } from "../protobuf/protobuf";
14+
import { eventStreamEnabledAtom } from "../state/eventStream";
1315
import { EventMessage } from "./eventMessages/EventMessage";
1416

1517
export const EventStream = () => {
1618
const [location, setLocation] = useState<string | null>(null);
1719
const { enqueueSnackbar } = useSnackbar();
1820
const asRole = useASAuthorizationStatus();
1921
const { data, error: streamError } = useGetEventStream({
20-
query: {
21-
select: (data) => data.location,
22-
enabled: !!asRole,
23-
},
22+
query: { select: (data) => data.location, enabled: !!asRole },
2423
});
2524
const { mutate: createEventStream } = useCreateEventStream({
2625
mutation: {
@@ -29,6 +28,7 @@ export const EventStream = () => {
2928
},
3029
},
3130
});
31+
const [eventStreamEnabled] = useAtom(eventStreamEnabledAtom);
3232

3333
// Define callbacks *before* useWebSocket hook
3434
const handleWebSocketOpen = useCallback(() => {
@@ -40,15 +40,14 @@ export const EventStream = () => {
4040

4141
const handleWebSocketClose = useCallback(
4242
(event: CloseEvent) => {
43+
console.log(event);
4344
if (event.wasClean) {
4445
enqueueSnackbar("Disconnected from event stream", {
4546
variant: "info",
4647
anchorOrigin: { horizontal: "right", vertical: "bottom" },
4748
});
4849
} else {
49-
console.warn(
50-
"EventStream: WebSocket closed unexpectedly. Reconnection attempts are handled by react-use-websocket.",
51-
);
50+
console.warn("EventStream: WebSocket closed unexpectedly.");
5251
enqueueSnackbar("Event stream disconnected unexpectedly. Attempting to reconnect...", {
5352
variant: "warning",
5453
anchorOrigin: { horizontal: "right", vertical: "bottom" },
@@ -98,7 +97,9 @@ export const EventStream = () => {
9897
[enqueueSnackbar],
9998
);
10099

101-
useWebSocket(asRole ? location : null, {
100+
const wsUrl = eventStreamEnabled && asRole ? (location?.replace("ws", "wss") ?? null) : null;
101+
102+
useWebSocket(wsUrl, {
102103
onOpen: handleWebSocketOpen,
103104
onClose: handleWebSocketClose,
104105
onError: handleWebSocketError,

src/layouts/navigation/UserMenuContent.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import { Person as PersonIcon } from "@mui/icons-material";
2-
import { Alert, Box, Chip, Typography, useMediaQuery, useTheme } from "@mui/material";
2+
import {
3+
Alert,
4+
Box,
5+
Chip,
6+
FormControlLabel,
7+
Switch,
8+
Typography,
9+
useMediaQuery,
10+
useTheme,
11+
} from "@mui/material";
12+
import { useAtom } from "jotai";
313

414
import { AuthButton } from "../../components/auth/AuthButton";
515
import { CenterLoader } from "../../components/CenterLoader";
616
import { Chips } from "../../components/Chips";
717
import { ColourSchemeSelection } from "../../components/ColourSchemeSelection";
818
import { useASAuthorizationStatus, useDMAuthorizationStatus } from "../../hooks/useIsAuthorized";
919
import { useKeycloakUser } from "../../hooks/useKeycloakUser";
20+
import { eventStreamEnabledAtom } from "../../state/eventStream";
1021

1122
/**
1223
* Content of the user menu
1324
*/
1425
export const UserMenuContent = () => {
1526
const theme = useTheme();
1627
const biggerThanMd = useMediaQuery(theme.breakpoints.up("md"));
28+
const [eventStreamEnabled, setEventStreamEnabled] = useAtom(eventStreamEnabledAtom);
1729

1830
return (
1931
<Box sx={{ textAlign: biggerThanMd ? "center" : undefined }}>
@@ -22,6 +34,17 @@ export const UserMenuContent = () => {
2234
</Typography>
2335
<UserMenuContentInner />
2436
<ColourSchemeSelection />
37+
<FormControlLabel
38+
control={
39+
<Switch
40+
checked={eventStreamEnabled}
41+
color="primary"
42+
onChange={(_, checked) => setEventStreamEnabled(checked)}
43+
/>
44+
}
45+
label="Event stream (alpha)"
46+
sx={{ mb: 2 }}
47+
/>
2548
</Box>
2649
);
2750
};

src/state/eventStream.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { atom } from "jotai";
2+
3+
/**
4+
* Atom to toggle the event stream functionality on or off.
5+
*/
6+
export const eventStreamEnabledAtom = atom(false);

0 commit comments

Comments
 (0)