Skip to content

Commit

Permalink
Reset behavior of the old handler
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Dec 2, 2024
1 parent f11dd02 commit d691f46
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 111 deletions.
58 changes: 0 additions & 58 deletions src/components/ChannelList/hooks/useMemberUpdatedListener.ts

This file was deleted.

68 changes: 16 additions & 52 deletions src/components/ChannelList/hooks/useMessageNewListener.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,41 @@
import { useEffect } from 'react';
import type { Dispatch, SetStateAction } from 'react';
import type { Channel, Event, ExtendableGenerics } from 'stream-chat';
import uniqBy from 'lodash.uniqby';

import { moveChannelUpwards } from '../utils';
import { useChatContext } from '../../../context/ChatContext';
import type { DefaultStreamChatGenerics } from '../../../types/types';
import { moveChannelUp } from '../utils';

export const isChannelPinned = <SCG extends ExtendableGenerics>({
channel,
}: {
channel?: Channel<SCG>;
}) => {
if (!channel) return false;
import { useChatContext } from '../../../context/ChatContext';

const member = channel.state.membership;
import type { Channel, Event } from 'stream-chat';

return !!member?.pinned_at;
};
import type { DefaultStreamChatGenerics } from '../../../types/types';

export const useMessageNewListener = <
SCG extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
setChannels: Dispatch<SetStateAction<Array<Channel<SCG>>>>,
setChannels: React.Dispatch<React.SetStateAction<Array<Channel<StreamChatGenerics>>>>,
customHandler?: (
setChannels: Dispatch<SetStateAction<Array<Channel<SCG>>>>,
event: Event<SCG>,
setChannels: React.Dispatch<React.SetStateAction<Array<Channel<StreamChatGenerics>>>>,
event: Event<StreamChatGenerics>,
) => void,
lockChannelOrder = false,
allowNewMessagesFromUnfilteredChannels = true,
considerPinnedChannels = false, // automatically set to true by checking sorting options (must include {pinned_at: -1/1})
) => {
const { client } = useChatContext<SCG>('useMessageNewListener');
const { client } = useChatContext<StreamChatGenerics>('useMessageNewListener');

useEffect(() => {
const handleEvent = (event: Event<SCG>) => {
const handleEvent = (event: Event<StreamChatGenerics>) => {
if (customHandler && typeof customHandler === 'function') {
customHandler(setChannels, event);
} else {
setChannels((channels) => {
const targetChannelIndex = channels.findIndex((channel) => channel.cid === event.cid);
const targetChannelExistsWithinList = targetChannelIndex >= 0;

const isTargetChannelPinned = isChannelPinned({
channel: channels[targetChannelIndex],
});
const channelInList = channels.filter((channel) => channel.cid === event.cid).length > 0;

if (
// target channel is pinned
(isTargetChannelPinned && considerPinnedChannels) ||
// list order is locked
lockChannelOrder ||
// target channel is not within the loaded list and loading from cache is disallowed
(!targetChannelExistsWithinList && !allowNewMessagesFromUnfilteredChannels)
) {
return channels;
if (!channelInList && allowNewMessagesFromUnfilteredChannels && event.channel_type) {
const channel = client.channel(event.channel_type, event.channel_id);
return uniqBy([channel, ...channels], 'cid');
}

// we either have the channel to move or we pull it from the cache (or instantiate) if it's allowed
const channelToMove: Channel<SCG> | null =
channels[targetChannelIndex] ??
(allowNewMessagesFromUnfilteredChannels && event.channel_type
? client.channel(event.channel_type, event.channel_id)
: null);

if (channelToMove) {
return moveChannelUpwards({
channels,
channelToMove,
channelToMoveIndexWithinChannels: targetChannelIndex,
considerPinnedChannels,
});
}
if (!lockChannelOrder) return moveChannelUp({ channels, cid: event.cid || '' });

return channels;
});
Expand All @@ -85,7 +50,6 @@ export const useMessageNewListener = <
}, [
allowNewMessagesFromUnfilteredChannels,
client,
considerPinnedChannels,
customHandler,
lockChannelOrder,
setChannels,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { Channel, Event } from 'stream-chat';

import type { DefaultStreamChatGenerics } from '../../../types/types';

// TODO: re-visit this and adjust (apply pinned channels)
export const useNotificationMessageNewListener = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
Expand Down

0 comments on commit d691f46

Please sign in to comment.