11import { useEffect } from 'react' ;
22
3- import uniqBy from 'lodash/uniqBy' ;
4-
53import type { Channel , Event } from 'stream-chat' ;
64
75import { useChatContext } from '../../../../contexts/chatContext/ChatContext' ;
86
9- import type { DefaultStreamChatGenerics } from '../../../../types/types' ;
10- import { getChannel } from '../../utils' ;
7+ import type {
8+ ChannelListEventListenerOptions ,
9+ DefaultStreamChatGenerics ,
10+ } from '../../../../types/types' ;
11+ import { getChannel , moveChannelUp } from '../../utils' ;
1112
1213type Parameters < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > =
1314 {
1415 setChannels : React . Dispatch < React . SetStateAction < Channel < StreamChatGenerics > [ ] | null > > ;
1516 onChannelVisible ?: (
1617 setChannels : React . Dispatch < React . SetStateAction < Channel < StreamChatGenerics > [ ] | null > > ,
1718 event : Event < StreamChatGenerics > ,
19+ options ?: ChannelListEventListenerOptions < StreamChatGenerics > ,
1820 ) => void ;
21+ options ?: ChannelListEventListenerOptions < StreamChatGenerics > ;
1922 } ;
2023
2124export const useChannelVisible = <
2225 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
2326> ( {
2427 onChannelVisible,
28+ options,
2529 setChannels,
2630} : Parameters < StreamChatGenerics > ) => {
2731 const { client } = useChatContext < StreamChatGenerics > ( ) ;
@@ -31,13 +35,24 @@ export const useChannelVisible = <
3135 if ( typeof onChannelVisible === 'function' ) {
3236 onChannelVisible ( setChannels , event ) ;
3337 } else {
38+ if ( ! options ) return ;
39+ const { sort } = options ;
3440 if ( event . channel_id && event . channel_type ) {
3541 const channel = await getChannel < StreamChatGenerics > ( {
3642 client,
3743 id : event . channel_id ,
3844 type : event . channel_type ,
3945 } ) ;
40- setChannels ( ( channels ) => ( channels ? uniqBy ( [ channel , ...channels ] , 'cid' ) : channels ) ) ;
46+ setChannels ( ( channels ) =>
47+ channels
48+ ? moveChannelUp ( {
49+ channels,
50+ channelToMove : channel ,
51+ channelToMoveIndexWithinChannels : - 1 ,
52+ sort,
53+ } )
54+ : channels ,
55+ ) ;
4156 }
4257 }
4358 } ;
0 commit comments