Skip to content

Commit ca9172e

Browse files
committed
fix: unify the message preview component to channel preview and thread list item
1 parent abb1b3a commit ca9172e

File tree

5 files changed

+287
-91
lines changed

5 files changed

+287
-91
lines changed

examples/SampleApp/src/components/DraftsList.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { FlatList, Pressable, StyleSheet, Text, View } from 'react-native';
22
import { DraftsIcon } from '../icons/DraftIcon';
3-
import { useChatContext, useStateStore, useTheme } from 'stream-chat-react-native';
3+
import {
4+
getChannelPreviewDisplayName,
5+
useChatContext,
6+
useStateStore,
7+
useTheme,
8+
} from 'stream-chat-react-native';
49
import { DraftManagerState, DraftsManager } from '../utils/DraftsManager';
510
import { useEffect, useMemo } from 'react';
611
import dayjs from 'dayjs';
@@ -25,10 +30,10 @@ export const DraftItem = ({ type, channel, date, content, parentId, thread }: Dr
2530
} = useTheme();
2631
const navigation = useNavigation();
2732
const { client } = useChatContext();
33+
const channelName = channel?.name ? channel.name : 'Channel';
2834

2935
const onNavigationHandler = async () => {
3036
if (channel?.type && channel.id) {
31-
console.log(channel);
3237
const resultChannel = client.channel(channel?.type, channel?.id);
3338
await resultChannel?.watch();
3439
if (type === 'thread' && parentId) {
@@ -49,7 +54,7 @@ export const DraftItem = ({ type, channel, date, content, parentId, thread }: Dr
4954
>
5055
<View style={styles.header}>
5156
<Text style={styles.name}>
52-
{type === 'channel' ? `# ${channel?.name}` : `Thread in # ${channel?.name}`}
57+
{type === 'channel' ? `# ${channelName}` : `Thread in # ${channelName}`}
5358
</Text>
5459
<Text style={[styles.date, { color: grey }]}>{dayjs(date).fromNow()}</Text>
5560
</View>
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import React from 'react';
2-
import { StyleSheet, Text } from 'react-native';
2+
import { StyleSheet, View } from 'react-native';
33

44
import type { LatestMessagePreview } from './hooks/useLatestMessagePreview';
55

6-
import { useTheme } from '../../contexts/themeContext/ThemeContext';
7-
8-
const styles = StyleSheet.create({
9-
bold: { fontWeight: '600' },
10-
message: {
11-
flexShrink: 1,
12-
fontSize: 12,
13-
},
14-
});
6+
import { useTheme } from '../../contexts';
7+
import { MessagePreview } from '../MessagePreview/MessagePreview';
158

169
export type ChannelPreviewMessageProps = {
1710
/**
@@ -25,28 +18,19 @@ export const ChannelPreviewMessage = (props: ChannelPreviewMessageProps) => {
2518

2619
const {
2720
theme: {
28-
channelPreview: { message },
29-
colors: { accent_blue, grey },
21+
channelPreview: {
22+
message: { container },
23+
},
3024
},
3125
} = useTheme();
3226

3327
return (
34-
<Text numberOfLines={1} style={[styles.message, { color: grey }, message]}>
35-
{latestMessagePreview?.previews?.map(
36-
(preview, index) =>
37-
preview.text && (
38-
<Text
39-
key={`${preview.text}_${index}`}
40-
style={[
41-
{ color: preview?.draft ? accent_blue : grey },
42-
preview.bold ? styles.bold : {},
43-
message,
44-
]}
45-
>
46-
{preview.text}
47-
</Text>
48-
),
49-
)}
50-
</Text>
28+
<View style={[styles.container, container]}>
29+
<MessagePreview previews={latestMessagePreview.previews} />
30+
</View>
5131
);
5232
};
33+
34+
const styles = StyleSheet.create({
35+
container: {},
36+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import { StyleSheet, Text, View } from 'react-native';
3+
4+
import { useTheme } from '../../contexts';
5+
6+
export type MessagePreviewSkeletonProps = {
7+
/**
8+
* Whether the message text should be bold.
9+
*/
10+
bold: boolean;
11+
/**
12+
* The text of the message preview.
13+
*/
14+
text: string;
15+
/**
16+
* Whether the message is a draft.
17+
*/
18+
draft?: boolean;
19+
};
20+
21+
export type MessagePreviewProps = {
22+
previews: MessagePreviewSkeletonProps[];
23+
};
24+
25+
export const MessagePreview = ({ previews }: MessagePreviewProps) => {
26+
const {
27+
theme: {
28+
messagePreview: { message },
29+
colors: { accent_blue, grey },
30+
},
31+
} = useTheme();
32+
33+
return (
34+
<View style={styles.container}>
35+
{previews?.map((preview, index) =>
36+
preview.text ? (
37+
<Text
38+
key={`${preview.text}_${index}`}
39+
numberOfLines={1}
40+
style={[
41+
styles.message,
42+
{
43+
color: preview?.draft ? accent_blue : grey,
44+
},
45+
preview.bold ? styles.bold : {},
46+
message,
47+
]}
48+
>
49+
{preview.text}
50+
</Text>
51+
) : null,
52+
)}
53+
</View>
54+
);
55+
};
56+
57+
const styles = StyleSheet.create({
58+
bold: { fontWeight: '500' },
59+
container: {
60+
flexDirection: 'row',
61+
flexShrink: 1,
62+
},
63+
message: {
64+
flexShrink: 1,
65+
marginRight: 2,
66+
},
67+
});

0 commit comments

Comments
 (0)