@@ -8,10 +8,11 @@ import React, {
88 useRef ,
99} from 'react' ;
1010
11+ import { ChannelState as StreamChannelState } from 'stream-chat' ;
12+
1113import type { DefaultStreamChatGenerics } from '../../types/types' ;
1214import { ActiveChannelsProvider } from '../activeChannelsRefContext/ActiveChannelsRefContext' ;
1315
14- import type { ChannelContextValue } from '../channelContext/ChannelContext' ;
1516import type { PaginatedMessageListContextValue } from '../paginatedMessageListContext/PaginatedMessageListContext' ;
1617import type { ThreadContextValue } from '../threadContext/ThreadContext' ;
1718import type { TypingContextValue } from '../typingContext/TypingContext' ;
@@ -22,14 +23,13 @@ import { isTestEnvironment } from '../utils/isTestEnvironment';
2223export type ChannelState <
2324 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
2425> = {
25- members : ChannelContextValue < StreamChatGenerics > [ 'members' ] ;
26+ members : StreamChannelState < StreamChatGenerics > [ 'members' ] ;
2627 messages : PaginatedMessageListContextValue < StreamChatGenerics > [ 'messages' ] ;
27- read : ChannelContextValue < StreamChatGenerics > [ 'read' ] ;
28- subscriberCount : number ;
28+ read : StreamChannelState < StreamChatGenerics > [ 'read' ] ;
2929 threadMessages : ThreadContextValue < StreamChatGenerics > [ 'threadMessages' ] ;
3030 typing : TypingContextValue < StreamChatGenerics > [ 'typing' ] ;
31- watcherCount : ChannelContextValue < StreamChatGenerics > [ 'watcherCount' ] ;
32- watchers : ChannelContextValue < StreamChatGenerics > [ 'watchers' ] ;
31+ watcherCount : number ;
32+ watchers : StreamChannelState < StreamChatGenerics > [ 'watchers' ] ;
3333} ;
3434
3535type ChannelsState <
@@ -56,25 +56,12 @@ type SetStateAction<
5656 type : 'SET_STATE' ;
5757} ;
5858
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-
6859type Action < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > =
69- | SetStateAction < StreamChatGenerics >
70- | IncreaseSubscriberCountAction
71- | DecreaseSubscriberCountAction ;
60+ SetStateAction < StreamChatGenerics > ;
7261
7362export type ChannelsStateContextValue <
7463 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
7564> = {
76- decreaseSubscriberCount : ( value : { cid : string } ) => void ;
77- increaseSubscriberCount : ( value : { cid : string } ) => void ;
7865 setState : ( value : Payload < Keys , StreamChatGenerics > ) => void ;
7966 state : ChannelsState < StreamChatGenerics > ;
8067} ;
@@ -95,39 +82,6 @@ function reducer(state: ChannelsState, action: Action) {
9582 } ,
9683 } ;
9784
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- }
13185 default :
13286 throw new Error ( ) ;
13387 }
@@ -150,18 +104,8 @@ export const ChannelsStateProvider = <
150104 dispatch ( { payload, type : 'SET_STATE' } ) ;
151105 } , [ ] ) ;
152106
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-
161107 const value = useMemo (
162108 ( ) => ( {
163- decreaseSubscriberCount,
164- increaseSubscriberCount,
165109 setState,
166110 state,
167111 } ) ,
0 commit comments