1- import { useMemo } from 'react' ;
2-
3- import type { Channel , StreamChat } from 'stream-chat' ;
1+ import type { Channel } from 'stream-chat' ;
42
53import { useChatContext } from '../../../contexts/chatContext/ChatContext' ;
64
75import type { DefaultStreamChatGenerics } from '../../../types/types' ;
86
9- const getChannelPreviewDisplayPresence = <
10- StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
11- > (
12- channel : Channel < StreamChatGenerics > ,
13- client : StreamChat < StreamChatGenerics > ,
14- ) => {
15- const currentUserId = client . userID ;
16-
17- if ( currentUserId ) {
18- const members = Object . values ( channel . state . members ) ;
19- const otherMembers = members . filter ( ( member ) => member . user ?. id !== currentUserId ) ;
20-
21- if ( otherMembers . length === 1 ) {
22- return ! ! otherMembers [ 0 ] . user ?. online ;
23- }
24- }
25- return false ;
26- } ;
27-
287/**
298 * Hook to set the display avatar presence for channel preview
309 * @param {* } channel
@@ -37,17 +16,12 @@ export const useChannelPreviewDisplayPresence = <
3716 channel : Channel < StreamChatGenerics > ,
3817) => {
3918 const { client } = useChatContext < StreamChatGenerics > ( ) ;
19+ const members = channel . state . members ;
20+ const membersCount = Object . keys ( members ) . length ;
4021
41- const currentUserId = client . userID ;
42- const members = Object . values ( channel . state . members ) . filter (
43- ( member ) => ! ! member . user ?. id && ! ! currentUserId && member . user ?. id !== currentUserId ,
44- ) ;
45- const channelMemberOnline = members . some ( ( member ) => member . user ?. online ) ;
22+ if ( membersCount !== 2 ) return false ;
4623
47- const displayPresence = useMemo ( ( ) => {
48- return getChannelPreviewDisplayPresence ( channel , client ) ;
49- // eslint-disable-next-line react-hooks/exhaustive-deps
50- } , [ channel , client , channelMemberOnline ] ) ;
24+ const otherMember = Object . values ( members ) . find ( ( member ) => member . user ?. id !== client . userID ) ;
5125
52- return displayPresence ;
26+ return otherMember ?. user ?. online ?? false ;
5327} ;
0 commit comments