@@ -6,21 +6,20 @@ import {
66 useGetEventStream ,
77} from "@squonk/account-server-client/event-stream" ;
88
9+ import { useAtom } from "jotai" ;
910import { useSnackbar } from "notistack" ;
1011
1112import { useASAuthorizationStatus } from "../hooks/useIsAuthorized" ;
1213import { getMessageFromEvent , protoBlobToText } from "../protobuf/protobuf" ;
14+ import { eventStreamEnabledAtom } from "../state/eventStream" ;
1315import { EventMessage } from "./eventMessages/EventMessage" ;
1416
1517export 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 ,
0 commit comments