11import { useCallback , useEffect , useState } from "react" ;
2- import useWebSocket , { ReadyState } from "react-use-websocket" ;
2+ import useWebSocket from "react-use-websocket" ;
33
44import {
55 useCreateEventStream ,
@@ -8,33 +8,20 @@ import {
88
99import { useSnackbar } from "notistack" ;
1010
11+ import { useASAuthorizationStatus } from "../hooks/useIsAuthorized" ;
1112import { getMessageFromEvent , protoBlobToText } from "../protobuf/protobuf" ;
1213import { EventMessage } from "./eventMessages/EventMessage" ;
1314
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-
2615export const EventStream = ( ) => {
2716 const [ location , setLocation ] = useState < string | null > ( null ) ;
2817 const { enqueueSnackbar } = useSnackbar ( ) ;
29-
18+ const asRole = useASAuthorizationStatus ( ) ;
3019 const { data, error : streamError } = useGetEventStream ( {
31- query : { select : ( data ) => data . location } ,
20+ query : {
21+ select : ( data ) => data . location ,
22+ enabled : ! ! asRole ,
23+ } ,
3224 } ) ;
33-
34- useEffect ( ( ) => {
35- data && setLocation ( data ) ;
36- } , [ data ] ) ;
37-
3825 const { mutate : createEventStream } = useCreateEventStream ( {
3926 mutation : {
4027 onSuccess : ( eventStreamResponse ) => {
@@ -43,13 +30,7 @@ export const EventStream = () => {
4330 } ,
4431 } ) ;
4532
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
5334 const handleWebSocketOpen = useCallback ( ( ) => {
5435 enqueueSnackbar ( "Connected to event stream" , {
5536 variant : "success" ,
@@ -117,7 +98,7 @@ export const EventStream = () => {
11798 [ enqueueSnackbar ] ,
11899 ) ;
119100
120- const { readyState } = useWebSocket ( location , {
101+ useWebSocket ( asRole ? location : null , {
121102 onOpen : handleWebSocketOpen ,
122103 onClose : handleWebSocketClose ,
123104 onError : handleWebSocketError ,
@@ -128,9 +109,18 @@ export const EventStream = () => {
128109 reconnectInterval : 3000 ,
129110 } ) ;
130111
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+
131119 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 ] ) ;
134124
135125 return null ;
136126} ;
0 commit comments