@@ -544,7 +544,7 @@ const ChannelWithContext = <
544544 colors : { black } ,
545545 } ,
546546 } = useTheme ( ) ;
547-
547+ const [ deleted , setDeleted ] = useState ( false ) ;
548548 const [ editing , setEditing ] = useState < boolean | MessageType < At , Ch , Co , Ev , Me , Re , Us > > ( false ) ;
549549 const [ error , setError ] = useState ( false ) ;
550550 const [ hasMore , setHasMore ] = useState ( true ) ;
@@ -670,7 +670,7 @@ const ChannelWithContext = <
670670 const markRead : ChannelContextValue < At , Ch , Co , Ev , Me , Re , Us > [ 'markRead' ] = useRef (
671671 throttle (
672672 ( ) => {
673- if ( channel ?. disconnected || ! channel ?. getConfig ?. ( ) ?. read_events ) {
673+ if ( ! channel || channel ?. disconnected || ! clientChannelConfig ?. read_events ) {
674674 return ;
675675 }
676676
@@ -778,18 +778,33 @@ const ChannelWithContext = <
778778 } ;
779779
780780 useEffect ( ( ) => {
781- /**
782- * The more complex sync logic around internet connectivity (NetInfo) is part of Chat.tsx
783- * listen to client.connection.recovered and all channel events
784- */
785- client . on ( 'connection.recovered' , connectionRecoveredHandler ) ;
786- client . on ( 'connection.changed' , connectionChangedHandler ) ;
787- channel ?. on ( handleEvent ) ;
781+ const channelSubscriptions : Array < ReturnType < ChannelType [ 'on' ] > > = [ ] ;
782+ const clientSubscriptions : Array < ReturnType < StreamChat [ 'on' ] > > = [ ] ;
783+
784+ const subscribe = ( ) => {
785+ if ( ! channel ) return ;
786+
787+ /**
788+ * The more complex sync logic around internet connectivity (NetInfo) is part of Chat.tsx
789+ * listen to client.connection.recovered and all channel events
790+ */
791+ clientSubscriptions . push ( client . on ( 'connection.recovered' , connectionRecoveredHandler ) ) ;
792+ clientSubscriptions . push ( client . on ( 'connection.changed' , connectionChangedHandler ) ) ;
793+ clientSubscriptions . push (
794+ client . on ( 'channel.deleted' , ( event ) => {
795+ if ( event . cid === channel . cid ) {
796+ setDeleted ( true ) ;
797+ }
798+ } ) ,
799+ ) ;
800+ channelSubscriptions . push ( channel . on ( handleEvent ) ) ;
801+ } ;
802+
803+ subscribe ( ) ;
788804
789805 return ( ) => {
790- client . off ( 'connection.recovered' , connectionRecoveredHandler ) ;
791- client . off ( 'connection.changed' , connectionChangedHandler ) ;
792- channel ?. off ( handleEvent ) ;
806+ clientSubscriptions . forEach ( ( s ) => s . unsubscribe ( ) ) ;
807+ channelSubscriptions . forEach ( ( s ) => s . unsubscribe ( ) ) ;
793808 } ;
794809 } , [ channelId , connectionRecoveredHandler , handleEvent ] ) ;
795810
@@ -1152,13 +1167,23 @@ const ChannelWithContext = <
11521167 }
11531168 } ;
11541169
1170+ // In case the channel is disconnected which may happen when channel is deleted,
1171+ // underlying js client throws an error. Following function ensures that Channel component
1172+ // won't result in error in such a case.
1173+ const getChannelConfigSafely = ( ) => {
1174+ try {
1175+ return channel ?. getConfig ( ) ;
1176+ } catch ( _ ) {
1177+ return null ;
1178+ }
1179+ } ;
1180+
11551181 /**
11561182 * Channel configs for use in disabling local functionality.
11571183 * Nullish coalescing is used to give first priority to props to override
11581184 * the server settings. Then priority to server settings to override defaults.
11591185 */
1160- const clientChannelConfig =
1161- typeof channel ?. getConfig === 'function' ? channel . getConfig ( ) : undefined ;
1186+ const clientChannelConfig = getChannelConfigSafely ( ) ;
11621187
11631188 const messagesConfig : MessagesConfig = {
11641189 /**
@@ -1597,7 +1622,7 @@ const ChannelWithContext = <
15971622 error,
15981623 giphyEnabled :
15991624 giphyEnabled ??
1600- ! ! ( channel ?. getConfig ?. ( ) ?. commands || [ ] ) ?. some ( ( command ) => command . name === 'giphy' ) ,
1625+ ! ! ( clientChannelConfig ?. commands || [ ] ) ?. some ( ( command ) => command . name === 'giphy' ) ,
16011626 hideDateSeparators,
16021627 hideStickyDateHeader,
16031628 isAdmin,
@@ -1780,6 +1805,9 @@ const ChannelWithContext = <
17801805 typing,
17811806 } ) ;
17821807
1808+ // TODO: replace the null view with appropriate message. Currently this is waiting a design decision.
1809+ if ( deleted ) return null ;
1810+
17831811 if ( ! channel || ( error && messages . length === 0 ) ) {
17841812 return < LoadingErrorIndicator error = { error } listType = 'message' retry = { reloadChannel } /> ;
17851813 }
0 commit comments