@@ -4,7 +4,7 @@ import { ReactNode, useEffect } from 'react';
44import { Websockets as WebsocketsAT } from '~/redux/actionTypes' ;
55import { useAppDispatch , useAppSelector } from '~/redux/hooks' ;
66import { WebsocketsStatus as WebsocketsStatusType } from '~/redux/models/Websockets' ;
7- import { GROUP_STATUS_PENDING } from '~/redux/slices/websockets' ;
7+ import { GROUP_STATUS_ERROR } from '~/redux/slices/websockets' ;
88import styles from './WebsocketStatus.module.css' ;
99
1010type ChildrenProps = {
@@ -27,8 +27,14 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
2727 const groupStatus =
2828 useAppSelector (
2929 ( state ) => state . websockets . groups . find ( ( g ) => g . group === group ) ?. status ,
30- ) || GROUP_STATUS_PENDING ;
30+ ) || GROUP_STATUS_ERROR ;
3131
32+ const status = {
33+ connected : websocketsStatus . connected && groupStatus . connected ,
34+ pending : websocketsStatus . pending || groupStatus . pending ,
35+ error : websocketsStatus . error || groupStatus . error ,
36+ } ;
37+ // Ensure proper order of connection
3238 useEffect ( ( ) => {
3339 if ( websocketsStatus . connected && ! websocketsStatus . error ) {
3440 dispatch ( {
@@ -39,10 +45,23 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
3945 // eslint-disable-next-line react-hooks/exhaustive-deps
4046 } , [ websocketsStatus . connected , websocketsStatus . error ] ) ;
4147
48+ // Clean up
49+ useEffect ( ( ) => {
50+ return ( ) => {
51+ if ( websocketsStatus . connected && ! websocketsStatus . error ) {
52+ dispatch ( {
53+ type : WebsocketsAT . GROUP_LEAVE . BEGIN ,
54+ payload : { group : group } ,
55+ } ) ;
56+ }
57+ } ;
58+ // eslint-disable-next-line react-hooks/exhaustive-deps
59+ } , [ ] ) ;
60+
4261 const WebsocketsStatusComponent = ( ) => {
43- const iconNode = groupStatus ?. connected ? (
62+ const iconNode = status ?. connected ? (
4463 < Radio />
45- ) : groupStatus ?. pending ? (
64+ ) : status ?. pending ? (
4665 < LoaderCircle className = { styles . spin } />
4766 ) : (
4867 < Unplug />
@@ -63,7 +82,7 @@ const WebsocketGroupProvider = ({ group, children }: Props) => {
6382
6483 return children ( {
6584 WebsocketStatus : ( ) => < WebsocketsStatusComponent /> ,
66- ...groupStatus ,
85+ ...status ,
6786 } ) ;
6887} ;
6988
0 commit comments