-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathChannelPreviewMessage.tsx
More file actions
181 lines (160 loc) · 6.03 KB
/
ChannelPreviewMessage.tsx
File metadata and controls
181 lines (160 loc) · 6.03 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useMemo } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { ChannelLastMessagePreview } from './ChannelLastMessagePreview';
import { ChannelMessagePreviewDeliveryStatus } from './ChannelMessagePreviewDeliveryStatus';
import { ChannelPreviewProps } from './ChannelPreview';
import { ChannelPreviewTypingIndicator } from './ChannelPreviewTypingIndicator';
import { LastMessageType } from './hooks/useChannelPreviewData';
import { useChannelPreviewDraftMessage } from './hooks/useChannelPreviewDraftMessage';
import { useChannelPreviewPollLabel } from './hooks/useChannelPreviewPollLabel';
import { useChannelTypingState } from './hooks/useChannelTypingState';
import {
ChannelsContextValue,
useChannelsContext,
} from '../../contexts/channelsContext/ChannelsContext';
import { useChatContext } from '../../contexts/chatContext/ChatContext';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import { useTranslationContext } from '../../contexts/translationContext/TranslationContext';
import { PollIcon } from '../../icons/PollIcon';
import { primitives } from '../../theme';
import { MessageStatusTypes } from '../../utils/utils';
import { ErrorBadge } from '../ui';
export type ChannelPreviewMessageProps = Pick<ChannelPreviewProps, 'channel'> &
Partial<
Pick<
ChannelsContextValue,
'PreviewTypingIndicator' | 'PreviewMessageDeliveryStatus' | 'PreviewLastMessage'
>
> & {
lastMessage?: LastMessageType;
};
export const ChannelPreviewMessage = (props: ChannelPreviewMessageProps) => {
const {
channel,
lastMessage,
PreviewTypingIndicator: PreviewTypingIndicatorProp = ChannelPreviewTypingIndicator,
PreviewMessageDeliveryStatus:
PreviewMessageDeliveryStatusProp = ChannelMessagePreviewDeliveryStatus,
PreviewLastMessage: PreviewLastMessageProp = ChannelLastMessagePreview,
} = props;
const {
PreviewTypingIndicator: PreviewTypingIndicatorContext,
PreviewMessageDeliveryStatus: PreviewMessageDeliveryStatusContext,
PreviewLastMessage: PreviewLastMessageContext,
} = useChannelsContext();
const PreviewTypingIndicator = PreviewTypingIndicatorProp || PreviewTypingIndicatorContext;
const PreviewMessageDeliveryStatus =
PreviewMessageDeliveryStatusProp || PreviewMessageDeliveryStatusContext;
const PreviewLastMessage = PreviewLastMessageProp || PreviewLastMessageContext;
const {
theme: { semantics },
} = useTheme();
const isMessageDeleted = lastMessage?.type === 'deleted';
const styles = useStyles({ isMessageDeleted });
const { client } = useChatContext();
const { t } = useTranslationContext();
const { usersTyping } = useChannelTypingState({ channel });
const draftMessage = useChannelPreviewDraftMessage({ channel });
const pollLabel = useChannelPreviewPollLabel({ pollId: lastMessage?.poll_id ?? '' });
const membersWithoutSelf = useMemo(() => {
return Object.values(channel.state.members).filter(
(member) => member.user?.id !== client.user?.id,
);
}, [channel.state.members, client.user?.id]);
const isFailedMessage =
lastMessage?.status === MessageStatusTypes.FAILED || lastMessage?.type === 'error';
if (usersTyping.length > 0) {
return <PreviewTypingIndicator channel={channel} usersTyping={usersTyping} />;
}
if (draftMessage) {
return (
<View style={styles.container}>
<Text style={styles.draftText}>{t('Draft')}:</Text>
<PreviewLastMessage message={draftMessage} />
</View>
);
}
// If there are no messages yet, show a message saying "No messages yet"
if (!lastMessage) {
return (
<Text style={[styles.subtitle, { color: semantics.textTertiary }]}>
{t('No messages yet')}
</Text>
);
}
if (pollLabel) {
return (
<View style={styles.container}>
<PollIcon height={16} width={16} stroke={semantics.textSecondary} />
<Text style={styles.subtitle}>{pollLabel}</Text>
</View>
);
}
if (isFailedMessage) {
return (
<View style={styles.container}>
<ErrorBadge size='xs' />
<Text style={styles.errorText}>{t('Message failed to send')}</Text>
</View>
);
}
if (channel.data?.name || membersWithoutSelf.length > 1) {
return (
<View style={styles.container}>
<PreviewMessageDeliveryStatus channel={channel} message={lastMessage} />
<PreviewLastMessage message={lastMessage} />
</View>
);
} else {
return (
<View style={styles.container}>
<PreviewMessageDeliveryStatus channel={channel} message={lastMessage} />
<PreviewLastMessage message={lastMessage} />
</View>
);
}
};
const useStyles = ({ isMessageDeleted = false }: { isMessageDeleted?: boolean }) => {
const {
theme: {
semantics,
channelPreview: { message },
},
} = useTheme();
return useMemo(() => {
return StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
gap: primitives.spacingXxs,
flexShrink: 1,
...message.container,
},
subtitle: {
color: isMessageDeleted ? semantics.textTertiary : semantics.textSecondary,
fontSize: primitives.typographyFontSizeSm,
fontWeight: primitives.typographyFontWeightRegular,
includeFontPadding: false,
lineHeight: primitives.typographyLineHeightNormal,
flexShrink: 1,
...message.subtitle,
},
draftText: {
color: semantics.accentPrimary,
fontSize: primitives.typographyFontSizeSm,
fontWeight: primitives.typographyFontWeightSemiBold,
lineHeight: primitives.typographyLineHeightNormal,
includeFontPadding: false,
...message.draftText,
},
errorText: {
color: semantics.accentError,
fontSize: primitives.typographyFontSizeSm,
fontWeight: primitives.typographyFontWeightRegular,
includeFontPadding: false,
lineHeight: primitives.typographyLineHeightNormal,
...message.errorText,
},
});
}, [semantics, isMessageDeleted, message]);
};