1
1
import { useCallback , useEffect , useState } from "react" ;
2
- import useWebSocket , { ReadyState } from "react-use-websocket" ;
2
+ import useWebSocket from "react-use-websocket" ;
3
3
4
4
import {
5
5
useCreateEventStream ,
@@ -8,33 +8,20 @@ import {
8
8
9
9
import { useSnackbar } from "notistack" ;
10
10
11
+ import { useASAuthorizationStatus } from "../hooks/useIsAuthorized" ;
11
12
import { getMessageFromEvent , protoBlobToText } from "../protobuf/protobuf" ;
12
13
import { EventMessage } from "./eventMessages/EventMessage" ;
13
14
14
- // Helper function to get readable state name
15
- const getConnectionStatus = ( readyState : ReadyState ) => {
16
- const stateMap : Record < ReadyState , string > = {
17
- [ ReadyState . CONNECTING ] : "Connecting" ,
18
- [ ReadyState . OPEN ] : "Open" ,
19
- [ ReadyState . CLOSING ] : "Closing" ,
20
- [ ReadyState . CLOSED ] : "Closed" ,
21
- [ ReadyState . UNINSTANTIATED ] : "Uninstantiated" ,
22
- } ;
23
- return stateMap [ readyState ] ;
24
- } ;
25
-
26
15
export const EventStream = ( ) => {
27
16
const [ location , setLocation ] = useState < string | null > ( null ) ;
28
17
const { enqueueSnackbar } = useSnackbar ( ) ;
29
-
18
+ const asRole = useASAuthorizationStatus ( ) ;
30
19
const { data, error : streamError } = useGetEventStream ( {
31
- query : { select : ( data ) => data . location } ,
20
+ query : {
21
+ select : ( data ) => data . location ,
22
+ enabled : ! ! asRole ,
23
+ } ,
32
24
} ) ;
33
-
34
- useEffect ( ( ) => {
35
- data && setLocation ( data ) ;
36
- } , [ data ] ) ;
37
-
38
25
const { mutate : createEventStream } = useCreateEventStream ( {
39
26
mutation : {
40
27
onSuccess : ( eventStreamResponse ) => {
@@ -43,13 +30,7 @@ export const EventStream = () => {
43
30
} ,
44
31
} ) ;
45
32
46
- useEffect ( ( ) => {
47
- if ( streamError ?. response ?. status === 404 ) {
48
- console . log ( "EventStream: No active stream found, creating one..." ) ;
49
- createEventStream ( { data : { format : "JSON_STRING" } } ) ;
50
- }
51
- } , [ streamError , createEventStream ] ) ;
52
-
33
+ // Define callbacks *before* useWebSocket hook
53
34
const handleWebSocketOpen = useCallback ( ( ) => {
54
35
enqueueSnackbar ( "Connected to event stream" , {
55
36
variant : "success" ,
@@ -117,7 +98,7 @@ export const EventStream = () => {
117
98
[ enqueueSnackbar ] ,
118
99
) ;
119
100
120
- const { readyState } = useWebSocket ( location , {
101
+ useWebSocket ( asRole ? location : null , {
121
102
onOpen : handleWebSocketOpen ,
122
103
onClose : handleWebSocketClose ,
123
104
onError : handleWebSocketError ,
@@ -128,9 +109,18 @@ export const EventStream = () => {
128
109
reconnectInterval : 3000 ,
129
110
} ) ;
130
111
112
+ // Effects can now safely use the hook results or return early based on auth
113
+ useEffect ( ( ) => {
114
+ if ( asRole && data ) {
115
+ setLocation ( data ) ;
116
+ }
117
+ } , [ asRole , data ] ) ;
118
+
131
119
useEffect ( ( ) => {
132
- console . log ( `WebSocket Status: ${ getConnectionStatus ( readyState ) } ` ) ;
133
- } , [ readyState ] ) ;
120
+ if ( asRole && streamError ?. response ?. status === 404 ) {
121
+ createEventStream ( { data : { format : "JSON_STRING" } } ) ;
122
+ }
123
+ } , [ asRole , streamError , createEventStream ] ) ;
134
124
135
125
return null ;
136
126
} ;
0 commit comments