-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathutils.tsx
More file actions
128 lines (108 loc) Β· 3.86 KB
/
Copy pathutils.tsx
File metadata and controls
128 lines (108 loc) Β· 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React from 'react';
import ReactMarkdown from 'react-markdown';
import type { ReactNode } from 'react';
import type { Channel, PollVote, TranslationLanguages, UserResponse } from 'stream-chat';
import type { TranslationContextValue } from '../../context/TranslationContext';
import type { ChatContextValue } from '../../context';
export const renderPreviewText = (text: string) => (
<ReactMarkdown skipHtml>{text}</ReactMarkdown>
);
const getLatestPollVote = (latestVotesByOption: Record<string, PollVote[]>) => {
let latestVote: PollVote | undefined;
for (const optionVotes of Object.values(latestVotesByOption)) {
optionVotes.forEach((vote) => {
if (latestVote && new Date(latestVote.updated_at) >= new Date(vote.created_at))
return;
latestVote = vote;
});
}
return latestVote;
};
export const getLatestMessagePreview = (
channel: Channel,
t: TranslationContextValue['t'],
userLanguage: TranslationContextValue['userLanguage'] = 'en',
isMessageAIGenerated?: ChatContextValue['isMessageAIGenerated'],
): ReactNode => {
const latestMessage =
channel.state.latestMessages[channel.state.latestMessages.length - 1];
const previewTextToRender =
latestMessage?.i18n?.[`${userLanguage}_text` as `${TranslationLanguages}_text`] ||
latestMessage?.text;
const poll = latestMessage?.poll;
if (!latestMessage) {
return t('Nothing yet...');
}
if (latestMessage.deleted_at) {
return t('Message deleted');
}
if (poll) {
if (!poll.vote_count) {
const createdBy =
poll.created_by?.id === channel.getClient().userID
? t('You')
: (poll.created_by?.name ?? t('Poll'));
return t('π {{createdBy}} created: {{ pollName}}', {
createdBy,
pollName: poll.name,
});
} else {
const latestVote = getLatestPollVote(
poll.latest_votes_by_option as Record<string, PollVote[]>,
);
const option =
latestVote && poll.options.find((opt) => opt.id === latestVote.option_id);
if (option && latestVote) {
return t('π {{votedBy}} voted: {{pollOptionText}}', {
pollOptionText: option.text,
votedBy:
latestVote?.user?.id === channel.getClient().userID
? t('You')
: (latestVote.user?.name ?? t('Poll')),
});
}
}
}
if (previewTextToRender) {
return isMessageAIGenerated?.(latestMessage)
? previewTextToRender
: renderPreviewText(previewTextToRender);
}
if (latestMessage.command) {
return `/${latestMessage.command}`;
}
if (latestMessage.attachments?.length) {
return t('π Attachment...');
}
return t('Empty message...');
};
export type GroupChannelDisplayInfo = { image?: string; name?: string }[];
export const getGroupChannelDisplayInfo = (
channel: Channel,
): GroupChannelDisplayInfo | undefined => {
const members = Object.values(channel.state.members);
if (members.length <= 2) return;
const info: GroupChannelDisplayInfo = [];
for (let i = 0; i < members.length; i++) {
const { user } = members[i];
if (!user?.name && !user?.image) continue;
info.push({ image: user.image, name: user.name });
if (info.length === 4) break;
}
return info;
};
const getChannelDisplayInfo = (
info: 'name' | 'image',
channel: Channel,
currentUser?: UserResponse,
) => {
if (channel.data?.[info]) return channel.data[info];
const members = Object.values(channel.state.members);
if (members.length !== 2) return;
const otherMember = members.find((member) => member.user?.id !== currentUser?.id);
return otherMember?.user?.[info];
};
export const getDisplayTitle = (channel: Channel, currentUser?: UserResponse) =>
getChannelDisplayInfo('name', channel, currentUser);
export const getDisplayImage = (channel: Channel, currentUser?: UserResponse) =>
getChannelDisplayInfo('image', channel, currentUser);