Skip to content

Commit bb887dd

Browse files
committed
fix: add message preview to draft list
1 parent ca9172e commit bb887dd

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

examples/SampleApp/src/components/DraftsList.tsx

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,107 @@
11
import { FlatList, Pressable, StyleSheet, Text, View } from 'react-native';
22
import { DraftsIcon } from '../icons/DraftIcon';
33
import {
4-
getChannelPreviewDisplayName,
4+
FileTypes,
5+
MessagePreview,
6+
TranslationContextValue,
57
useChatContext,
68
useStateStore,
79
useTheme,
10+
useTranslationContext,
811
} from 'stream-chat-react-native';
912
import { DraftManagerState, DraftsManager } from '../utils/DraftsManager';
1013
import { useEffect, useMemo } from 'react';
1114
import dayjs from 'dayjs';
1215
import { useNavigation } from '@react-navigation/native';
13-
import { ChannelResponse, MessageResponseBase } from 'stream-chat';
16+
import { ChannelResponse, DraftMessage, MessageResponseBase } from 'stream-chat';
1417

1518
export type DraftItemProps = {
1619
type?: 'channel' | 'thread';
1720
channel?: ChannelResponse;
1821
date?: string;
19-
content?: string;
22+
message: DraftMessage;
2023
// TODO: Fix the type for thread
2124
thread?: MessageResponseBase;
2225
parentId?: string;
2326
};
2427

25-
export const DraftItem = ({ type, channel, date, content, parentId, thread }: DraftItemProps) => {
28+
export const attachmentTypeIconMap = {
29+
audio: '🔈',
30+
file: '📄',
31+
image: '📷',
32+
video: '🎥',
33+
voiceRecording: '🎙️',
34+
} as const;
35+
36+
const getPreviewFromMessage = ({
37+
t,
38+
draftMessage,
39+
}: {
40+
t: TranslationContextValue['t'];
41+
draftMessage: DraftMessage;
42+
}) => {
43+
if (draftMessage.attachments?.length) {
44+
const attachment = draftMessage?.attachments?.at(0);
45+
46+
const attachmentIcon = attachment
47+
? `${
48+
attachmentTypeIconMap[
49+
(attachment.type as keyof typeof attachmentTypeIconMap) ?? 'file'
50+
] ?? attachmentTypeIconMap.file
51+
} `
52+
: '';
53+
54+
if (attachment?.type === FileTypes.VoiceRecording) {
55+
return [
56+
{ bold: false, text: attachmentIcon },
57+
{
58+
bold: false,
59+
text: t('Voice message'),
60+
},
61+
];
62+
}
63+
return [
64+
{ bold: false, text: attachmentIcon },
65+
{
66+
bold: false,
67+
text:
68+
attachment?.type === FileTypes.Image
69+
? attachment?.fallback
70+
? attachment?.fallback
71+
: 'N/A'
72+
: attachment?.title
73+
? attachment?.title
74+
: 'N/A',
75+
},
76+
];
77+
}
78+
79+
if (draftMessage.text) {
80+
return [
81+
{
82+
bold: false,
83+
text: draftMessage.text,
84+
},
85+
];
86+
}
87+
};
88+
89+
export const DraftItem = ({ type, channel, date, message, parentId, thread }: DraftItemProps) => {
2690
const {
2791
theme: {
2892
colors: { grey },
2993
},
3094
} = useTheme();
3195
const navigation = useNavigation();
3296
const { client } = useChatContext();
97+
const { t } = useTranslationContext();
3398
const channelName = channel?.name ? channel.name : 'Channel';
3499

35100
const onNavigationHandler = async () => {
36101
if (channel?.type && channel.id) {
37102
const resultChannel = client.channel(channel?.type, channel?.id);
38103
await resultChannel?.watch();
104+
39105
if (type === 'thread' && parentId) {
40106
navigation.navigate('ThreadScreen', {
41107
thread: thread,
@@ -47,6 +113,10 @@ export const DraftItem = ({ type, channel, date, content, parentId, thread }: Dr
47113
}
48114
};
49115

116+
const previews = useMemo(() => {
117+
return getPreviewFromMessage({ draftMessage: message, t });
118+
}, [message, t]);
119+
50120
return (
51121
<Pressable
52122
style={({ pressed }) => [styles.itemContainer, { opacity: pressed ? 0.8 : 1 }]}
@@ -62,9 +132,7 @@ export const DraftItem = ({ type, channel, date, content, parentId, thread }: Dr
62132
<View style={styles.icon}>
63133
<DraftsIcon />
64134
</View>
65-
<Text style={[styles.text, { color: grey }]} numberOfLines={1}>
66-
{content}
67-
</Text>
135+
<MessagePreview previews={previews} />
68136
</View>
69137
</Pressable>
70138
);
@@ -98,7 +166,7 @@ export const DraftsList = () => {
98166
channel={item.channel}
99167
type={item.parent_id ? 'thread' : 'channel'}
100168
date={item.created_at}
101-
content={item.message.text}
169+
message={item.message}
102170
thread={item.parent_message}
103171
parentId={item.parent_id}
104172
/>

package/src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ export * from './MessageMenu/MessageUserReactionsAvatar';
164164
export * from './MessageMenu/MessageReactionPicker';
165165
export * from './MessageMenu/hooks/useFetchReactions';
166166

167+
export * from './MessagePreview/MessagePreview';
168+
167169
export * from './ProgressControl/ProgressControl';
168170
export * from './Poll';
169171

0 commit comments

Comments
 (0)