Skip to content

Commit 1f34f0b

Browse files
committed
feat: listen to notification.mark_read and notification.mark_unread event and introduce improvements
1 parent f8ae2af commit 1f34f0b

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

package/src/components/ChannelPreview/ChannelPreview.tsx

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,33 @@ import { ChatContextValue, useChatContext } from '../../contexts/chatContext/Cha
1212

1313
import type { DefaultStreamChatGenerics } from '../../types/types';
1414
import { useChannelPreviewData } from './hooks/useChannelPreviewData';
15-
import { useIsChannelMuted } from './hooks/useIsChannelMuted';
1615

17-
export type ChannelPreviewPropsWithContext<
16+
export type ChannelPreviewProps<
1817
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
19-
> = Pick<ChatContextValue<StreamChatGenerics>, 'client'> &
20-
Pick<ChannelsContextValue<StreamChatGenerics>, 'Preview' | 'forceUpdate'> & {
18+
> = Partial<Pick<ChatContextValue<StreamChatGenerics>, 'client'>> &
19+
Partial<Pick<ChannelsContextValue<StreamChatGenerics>, 'Preview' | 'forceUpdate'>> & {
2120
/**
2221
* Instance of Channel from stream-chat package.
2322
*/
2423
channel: Channel<StreamChatGenerics>;
2524
};
2625

27-
/**
28-
* This component manages state for the ChannelPreviewMessenger UI component and receives
29-
* all props from the ChannelListMessenger component.
30-
*/
31-
const ChannelPreviewWithContext = <
26+
export const ChannelPreview = <
3227
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
3328
>(
34-
props: ChannelPreviewPropsWithContext<StreamChatGenerics>,
29+
props: ChannelPreviewProps<StreamChatGenerics>,
3530
) => {
36-
const { forceUpdate, channel, client, Preview } = props;
37-
const { muted } = useIsChannelMuted(channel);
38-
const { lastMessage, unread } = useChannelPreviewData(channel, client, forceUpdate, muted);
31+
const { channel, client: propClient, forceUpdate: propForceUpdate, Preview: propPreview } = props;
32+
33+
const { client: contextClient } = useChatContext<StreamChatGenerics>();
34+
const { forceUpdate: contextForceUpdate, Preview: contextPreview } =
35+
useChannelsContext<StreamChatGenerics>();
36+
37+
const client = propClient || contextClient;
38+
const forceUpdate = propForceUpdate || contextForceUpdate;
39+
const Preview = propPreview || contextPreview;
40+
41+
const { lastMessage, muted, unread } = useChannelPreviewData(channel, client, forceUpdate);
3942
const latestMessagePreview = useLatestMessagePreview(channel, forceUpdate, lastMessage);
4043

4144
return (
@@ -47,19 +50,3 @@ const ChannelPreviewWithContext = <
4750
/>
4851
);
4952
};
50-
51-
export type ChannelPreviewProps<
52-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
53-
> = Partial<Omit<ChannelPreviewPropsWithContext<StreamChatGenerics>, 'channel'>> &
54-
Pick<ChannelPreviewPropsWithContext<StreamChatGenerics>, 'channel'>;
55-
56-
export const ChannelPreview = <
57-
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
58-
>(
59-
props: ChannelPreviewProps<StreamChatGenerics>,
60-
) => {
61-
const { client } = useChatContext<StreamChatGenerics>();
62-
const { forceUpdate, Preview } = useChannelsContext<StreamChatGenerics>();
63-
64-
return <ChannelPreviewWithContext {...{ client, forceUpdate, Preview }} {...props} />;
65-
};

package/src/components/ChannelPreview/hooks/useChannelPreviewData.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import type { Channel, ChannelState, Event, MessageResponse, StreamChat } from '
33
import type { DefaultStreamChatGenerics } from '../../../types/types';
44
import { useEffect, useMemo, useState } from 'react';
55
import throttle from 'lodash/throttle';
6+
import { useIsChannelMuted } from './useIsChannelMuted';
67

78
export const useChannelPreviewData = <
89
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
910
>(
1011
channel: Channel<StreamChatGenerics>,
1112
client: StreamChat<StreamChatGenerics>,
1213
forceUpdate?: number,
13-
muted?: boolean,
1414
) => {
1515
const [lastMessage, setLastMessage] = useState<
1616
| ReturnType<ChannelState<StreamChatGenerics>['formatMessage']>
1717
| MessageResponse<StreamChatGenerics>
1818
>(channel.state.messages[channel.state.messages.length - 1]);
1919
const [unread, setUnread] = useState(channel.countUnread());
20+
const { muted } = useIsChannelMuted(channel);
2021

2122
/**
2223
* This effect listens for the `notification.mark_read` event and sets the unread count to 0
@@ -78,5 +79,5 @@ export const useChannelPreviewData = <
7879
return () => listeners.forEach((l) => l.unsubscribe());
7980
}, [channel, refreshUnreadCount, forceUpdate]);
8081

81-
return { lastMessage, unread };
82+
return { lastMessage, muted, unread };
8283
};

0 commit comments

Comments
 (0)