Skip to content

Commit 7dc6f52

Browse files
authored
fix: improve channel preview display hooks and long title issue (#3002)
* fix: improve channel preview display hooks and long title issue * fix: improve channel preview display hooks and long title issue
1 parent f8968c0 commit 7dc6f52

File tree

5 files changed

+46
-99
lines changed

5 files changed

+46
-99
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useMemo } from 'react';
22

33
import type { Channel, StreamChat } from 'stream-chat';
44

@@ -61,19 +61,10 @@ export const useChannelPreviewDisplayAvatar = <
6161
) => {
6262
const { client } = useChatContext<StreamChatGenerics>();
6363

64-
const channelData = channel?.data;
65-
const image = channelData?.image;
66-
const name = channelData?.name;
67-
const id = client?.user?.id;
68-
69-
const [displayAvatar, setDisplayAvatar] = useState(
70-
getChannelPreviewDisplayAvatar(channel, client),
64+
const displayAvatar = useMemo(
65+
() => getChannelPreviewDisplayAvatar(channel, client),
66+
[channel, client],
7167
);
7268

73-
useEffect(() => {
74-
setDisplayAvatar(getChannelPreviewDisplayAvatar(channel, client));
75-
// eslint-disable-next-line react-hooks/exhaustive-deps
76-
}, [id, image, name]);
77-
7869
return displayAvatar;
7970
};

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useMemo } from 'react';
22

33
import type { Channel, ChannelMemberResponse } from 'stream-chat';
44

@@ -26,7 +26,9 @@ export const getChannelPreviewDisplayName = <
2626
members?: Channel<StreamChatGenerics>['state']['members'];
2727
}): string => {
2828
if (channelName) {
29-
return channelName;
29+
return channelName.length > characterLimit
30+
? `${channelName.slice(0, characterLimit - ELLIPSIS.length)}${ELLIPSIS}`
31+
: channelName;
3032
}
3133

3234
const channelMembers = Object.values(members || {});
@@ -83,33 +85,25 @@ export const useChannelPreviewDisplayName = <
8385
const { client } = useChatContext<StreamChatGenerics>();
8486
const { vw } = useViewport();
8587

86-
const DEFAULT_MAX_CHARACTER_LENGTH = (vw(100) - 16) / 6;
88+
const DEFAULT_MAX_CHARACTER_LENGTH = Math.floor((vw(100) - 16) / 6);
8789

8890
const currentUserId = client?.userID;
8991
const members = channel?.state?.members;
90-
const numOfMembers = Object.keys(members || {}).length;
9192
const channelName = channel?.data?.name;
9293
const characterLimit = characterLength || DEFAULT_MAX_CHARACTER_LENGTH;
93-
const [displayName, setDisplayName] = useState(
94-
getChannelPreviewDisplayName({
95-
channelName,
96-
characterLimit,
97-
currentUserId,
98-
members,
99-
}),
100-
);
94+
const numOfMembers = Object.keys(members || {}).length;
10195

102-
useEffect(() => {
103-
setDisplayName(
96+
const displayName = useMemo(
97+
() =>
10498
getChannelPreviewDisplayName({
10599
channelName,
106100
characterLimit,
107101
currentUserId,
108102
members,
109103
}),
110-
);
111104
// eslint-disable-next-line react-hooks/exhaustive-deps
112-
}, [channelName, currentUserId, characterLimit, numOfMembers]);
105+
[channelName, characterLimit, currentUserId, members, numOfMembers],
106+
);
113107

114108
return displayName;
115109
};
Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,9 @@
1-
import { useEffect, useState } from 'react';
2-
3-
import type { Channel, StreamChat } from 'stream-chat';
1+
import type { Channel } from 'stream-chat';
42

53
import { useChatContext } from '../../../contexts/chatContext/ChatContext';
64

75
import 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,18 +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);
46-
47-
const [displayPresence, setDisplayPresence] = useState(false);
22+
if (membersCount !== 2) return false;
4823

49-
useEffect(() => {
50-
setDisplayPresence(getChannelPreviewDisplayPresence(channel, client));
51-
}, [channel, channelMemberOnline, client]);
24+
const otherMember = Object.values(members).find((member) => member.user?.id !== client.userID);
5225

53-
return displayPresence;
26+
return otherMember?.user?.online ?? false;
5427
};

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { useChatContext } from '../../../contexts/chatContext/ChatContext';
66

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

9+
const defaultMuteStatus = {
10+
createdAt: null,
11+
expiresAt: null,
12+
muted: false,
13+
};
14+
915
export const useIsChannelMuted = <
1016
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1117
>(
@@ -24,5 +30,5 @@ export const useIsChannelMuted = <
2430
return () => client.off('notification.channel_mutes_updated', handleEvent);
2531
}, [channel, client, muted]);
2632

27-
return muted || { createdAt: null, expiresAt: null, muted: false };
33+
return muted ?? defaultMuteStatus;
2834
};

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

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useMemo, useState } from 'react';
22

33
import { TFunction } from 'i18next';
44
import type {
@@ -286,19 +286,6 @@ export const useLatestMessagePreview = <
286286
: '';
287287

288288
const [readEvents, setReadEvents] = useState(true);
289-
const [latestMessagePreview, setLatestMessagePreview] = useState<
290-
LatestMessagePreview<StreamChatGenerics>
291-
>({
292-
created_at: '',
293-
messageObject: undefined,
294-
previews: [
295-
{
296-
bold: false,
297-
text: '',
298-
},
299-
],
300-
status: MessageReadStatus.NOT_SENT_BY_CURRENT_USER,
301-
});
302289

303290
const readStatus = getLatestMessageReadStatus(channel, client, translatedLastMessage, readEvents);
304291

@@ -319,29 +306,25 @@ export const useLatestMessagePreview = <
319306
useStateStore(poll?.state, selector) ?? {};
320307
const { createdBy, latestVotesByOption, name } = pollState;
321308

322-
useEffect(
323-
() =>
324-
setLatestMessagePreview(
325-
getLatestMessagePreview({
326-
channel,
327-
client,
328-
lastMessage: translatedLastMessage,
329-
pollState,
330-
readEvents,
331-
t,
332-
}),
333-
),
334-
// eslint-disable-next-line react-hooks/exhaustive-deps
335-
[
336-
channelLastMessageString,
337-
forceUpdate,
309+
const latestMessagePreview = useMemo(() => {
310+
return getLatestMessagePreview({
311+
channel,
312+
client,
313+
lastMessage: translatedLastMessage,
314+
pollState,
338315
readEvents,
339-
readStatus,
340-
latestVotesByOption,
341-
createdBy,
342-
name,
343-
],
344-
);
316+
t,
317+
});
318+
// eslint-disable-next-line react-hooks/exhaustive-deps
319+
}, [
320+
channelLastMessageString,
321+
forceUpdate,
322+
readEvents,
323+
readStatus,
324+
latestVotesByOption,
325+
createdBy,
326+
name,
327+
]);
345328

346329
return latestMessagePreview;
347330
};

0 commit comments

Comments
 (0)