@@ -6,21 +6,20 @@ import {
6
6
useGetEventStream ,
7
7
} from "@squonk/account-server-client/event-stream" ;
8
8
9
+ import { useAtom } from "jotai" ;
9
10
import { useSnackbar } from "notistack" ;
10
11
11
12
import { useASAuthorizationStatus } from "../hooks/useIsAuthorized" ;
12
13
import { getMessageFromEvent , protoBlobToText } from "../protobuf/protobuf" ;
14
+ import { eventStreamEnabledAtom } from "../state/eventStream" ;
13
15
import { EventMessage } from "./eventMessages/EventMessage" ;
14
16
15
17
export const EventStream = ( ) => {
16
18
const [ location , setLocation ] = useState < string | null > ( null ) ;
17
19
const { enqueueSnackbar } = useSnackbar ( ) ;
18
20
const asRole = useASAuthorizationStatus ( ) ;
19
21
const { data, error : streamError } = useGetEventStream ( {
20
- query : {
21
- select : ( data ) => data . location ,
22
- enabled : ! ! asRole ,
23
- } ,
22
+ query : { select : ( data ) => data . location , enabled : ! ! asRole } ,
24
23
} ) ;
25
24
const { mutate : createEventStream } = useCreateEventStream ( {
26
25
mutation : {
@@ -29,6 +28,7 @@ export const EventStream = () => {
29
28
} ,
30
29
} ,
31
30
} ) ;
31
+ const [ eventStreamEnabled ] = useAtom ( eventStreamEnabledAtom ) ;
32
32
33
33
// Define callbacks *before* useWebSocket hook
34
34
const handleWebSocketOpen = useCallback ( ( ) => {
@@ -40,15 +40,14 @@ export const EventStream = () => {
40
40
41
41
const handleWebSocketClose = useCallback (
42
42
( event : CloseEvent ) => {
43
+ console . log ( event ) ;
43
44
if ( event . wasClean ) {
44
45
enqueueSnackbar ( "Disconnected from event stream" , {
45
46
variant : "info" ,
46
47
anchorOrigin : { horizontal : "right" , vertical : "bottom" } ,
47
48
} ) ;
48
49
} 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." ) ;
52
51
enqueueSnackbar ( "Event stream disconnected unexpectedly. Attempting to reconnect..." , {
53
52
variant : "warning" ,
54
53
anchorOrigin : { horizontal : "right" , vertical : "bottom" } ,
@@ -98,7 +97,9 @@ export const EventStream = () => {
98
97
[ enqueueSnackbar ] ,
99
98
) ;
100
99
101
- useWebSocket ( asRole ? location : null , {
100
+ const wsUrl = eventStreamEnabled && asRole ? ( location ?. replace ( "ws" , "wss" ) ?? null ) : null ;
101
+
102
+ useWebSocket ( wsUrl , {
102
103
onOpen : handleWebSocketOpen ,
103
104
onClose : handleWebSocketClose ,
104
105
onError : handleWebSocketError ,
0 commit comments