-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathChannelPreviewTypingIndicator.tsx
More file actions
113 lines (103 loc) · 3.1 KB
/
ChannelPreviewTypingIndicator.tsx
File metadata and controls
113 lines (103 loc) · 3.1 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
import React, { useMemo } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { UserResponse } from 'stream-chat';
import { ChannelPreviewProps } from './ChannelPreview';
import { useTheme } from '../../contexts/themeContext/ThemeContext';
import {
TranslationContextValue,
useTranslationContext,
} from '../../contexts/translationContext/TranslationContext';
import { primitives } from '../../theme';
import { LoadingDots } from '../Indicators/LoadingDots';
const getFirstName = (name?: string) => {
if (!name) {
return '';
}
return name?.split(' ')[0];
};
const getTypingString = ({
usersTyping,
channelName,
t,
}: {
usersTyping: UserResponse[];
channelName?: string;
t: TranslationContextValue['t'];
}) => {
if (channelName && usersTyping.length > 0) {
if (usersTyping.length === 1) {
return t('{{ user }} is typing', { user: getFirstName(usersTyping[0]?.name) });
} else if (usersTyping.length === 2) {
return t('{{ firstUser }} and {{ secondUser }} are typing', {
firstUser: getFirstName(usersTyping[0]?.name),
secondUser: getFirstName(usersTyping[1]?.name),
});
}
return t('{{ numberOfUsers }} people are typing', { numberOfUsers: usersTyping.length });
} else {
if (!usersTyping.length) {
return null;
}
if (usersTyping.length === 1) {
return t('Typing');
} else if (usersTyping.length === 2) {
return t('{{ firstUser }} and {{ secondUser }} are typing', {
firstUser: getFirstName(usersTyping[0]?.name),
secondUser: getFirstName(usersTyping[1]?.name),
});
} else {
return t('{{ numberOfUsers }} people are typing', { numberOfUsers: usersTyping.length });
}
}
};
export type ChannelPreviewTypingIndicatorProps = Pick<ChannelPreviewProps, 'channel'> & {
usersTyping: UserResponse[];
};
export const ChannelPreviewTypingIndicator = ({
usersTyping,
channel,
}: ChannelPreviewTypingIndicatorProps) => {
const styles = useStyles();
const { t } = useTranslationContext();
const userTypingLabel = useMemo(() => {
return getTypingString({ usersTyping, channelName: channel.data?.name, t });
}, [channel.data?.name, usersTyping, t]);
return (
<View style={styles.container}>
<Text numberOfLines={1} style={styles.text}>
{userTypingLabel}
</Text>
<LoadingDots />
</View>
);
};
const useStyles = () => {
const {
theme: {
semantics,
channelPreview: {
typingIndicatorPreview: { container, text },
},
},
} = useTheme();
return useMemo(() => {
return StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
gap: primitives.spacingXs,
flexShrink: 1,
...container,
},
text: {
color: semantics.textSecondary,
fontSize: primitives.typographyFontSizeSm,
fontWeight: primitives.typographyFontWeightRegular,
includeFontPadding: false,
lineHeight: primitives.typographyLineHeightNormal,
flexShrink: 1,
...text,
},
});
}, [semantics, text, container]);
};