@@ -7,11 +7,11 @@ import React, {
77 useReducer ,
88 useRef ,
99} from 'react' ;
10+ import { ChannelState as StreamChannelState } from 'stream-chat' ;
1011
1112import type { DefaultStreamChatGenerics } from '../../types/types' ;
1213import { ActiveChannelsProvider } from '../activeChannelsRefContext/ActiveChannelsRefContext' ;
1314
14- import type { ChannelContextValue } from '../channelContext/ChannelContext' ;
1515import type { PaginatedMessageListContextValue } from '../paginatedMessageListContext/PaginatedMessageListContext' ;
1616import type { ThreadContextValue } from '../threadContext/ThreadContext' ;
1717import type { TypingContextValue } from '../typingContext/TypingContext' ;
@@ -22,14 +22,13 @@ import { isTestEnvironment } from '../utils/isTestEnvironment';
2222export type ChannelState <
2323 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
2424> = {
25- members : ChannelContextValue < StreamChatGenerics > [ 'members' ] ;
25+ members : StreamChannelState < StreamChatGenerics > [ 'members' ] ;
2626 messages : PaginatedMessageListContextValue < StreamChatGenerics > [ 'messages' ] ;
27- read : ChannelContextValue < StreamChatGenerics > [ 'read' ] ;
28- subscriberCount : number ;
27+ read : StreamChannelState < StreamChatGenerics > [ 'read' ] ;
2928 threadMessages : ThreadContextValue < StreamChatGenerics > [ 'threadMessages' ] ;
3029 typing : TypingContextValue < StreamChatGenerics > [ 'typing' ] ;
31- watcherCount : ChannelContextValue < StreamChatGenerics > [ 'watcherCount' ] ;
32- watchers : ChannelContextValue < StreamChatGenerics > [ 'watchers' ] ;
30+ watcherCount : number ;
31+ watchers : StreamChannelState < StreamChatGenerics > [ 'watchers' ] ;
3332} ;
3433
3534type ChannelsState <
@@ -56,25 +55,12 @@ type SetStateAction<
5655 type : 'SET_STATE' ;
5756} ;
5857
59- type IncreaseSubscriberCountAction = {
60- payload : { cid : string } ;
61- type : 'INCREASE_SUBSCRIBER_COUNT' ;
62- } ;
63- type DecreaseSubscriberCountAction = {
64- payload : { cid : string } ;
65- type : 'DECREASE_SUBSCRIBER_COUNT' ;
66- } ;
67-
6858type Action < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > =
69- | SetStateAction < StreamChatGenerics >
70- | IncreaseSubscriberCountAction
71- | DecreaseSubscriberCountAction ;
59+ SetStateAction < StreamChatGenerics > ;
7260
7361export type ChannelsStateContextValue <
7462 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
7563> = {
76- decreaseSubscriberCount : ( value : { cid : string } ) => void ;
77- increaseSubscriberCount : ( value : { cid : string } ) => void ;
7864 setState : ( value : Payload < Keys , StreamChatGenerics > ) => void ;
7965 state : ChannelsState < StreamChatGenerics > ;
8066} ;
@@ -95,39 +81,6 @@ function reducer(state: ChannelsState, action: Action) {
9581 } ,
9682 } ;
9783
98- case 'INCREASE_SUBSCRIBER_COUNT' : {
99- const currentCount = state [ action . payload . cid ] ?. subscriberCount ?? 0 ;
100- return {
101- ...state ,
102- [ action . payload . cid ] : {
103- ...( state [ action . payload . cid ] || { } ) ,
104- subscriberCount : currentCount + 1 ,
105- } ,
106- } ;
107- }
108-
109- case 'DECREASE_SUBSCRIBER_COUNT' : {
110- const currentCount = state [ action . payload . cid ] ?. subscriberCount ?? 0 ;
111-
112- // If there last subscribed Channel component unsubscribes, we clear the channel state.
113- if ( currentCount <= 1 ) {
114- const stateShallowCopy = {
115- ...state ,
116- } ;
117-
118- delete stateShallowCopy [ action . payload . cid ] ;
119-
120- return stateShallowCopy ;
121- }
122-
123- return {
124- ...state ,
125- [ action . payload . cid ] : {
126- ...( state [ action . payload . cid ] || { } ) ,
127- subscriberCount : currentCount - 1 ,
128- } ,
129- } ;
130- }
13184 default :
13285 throw new Error ( ) ;
13386 }
@@ -150,18 +103,8 @@ export const ChannelsStateProvider = <
150103 dispatch ( { payload, type : 'SET_STATE' } ) ;
151104 } , [ ] ) ;
152105
153- const increaseSubscriberCount = useCallback ( ( payload : { cid : string } ) => {
154- dispatch ( { payload, type : 'INCREASE_SUBSCRIBER_COUNT' } ) ;
155- } , [ ] ) ;
156-
157- const decreaseSubscriberCount = useCallback ( ( payload : { cid : string } ) => {
158- dispatch ( { payload, type : 'DECREASE_SUBSCRIBER_COUNT' } ) ;
159- } , [ ] ) ;
160-
161106 const value = useMemo (
162107 ( ) => ( {
163- decreaseSubscriberCount,
164- increaseSubscriberCount,
165108 setState,
166109 state,
167110 } ) ,
0 commit comments