-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathThreadScreen.tsx
More file actions
179 lines (158 loc) · 5.41 KB
/
ThreadScreen.tsx
File metadata and controls
179 lines (158 loc) · 5.41 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
import React, { useCallback } from 'react';
import { StyleSheet, View } from 'react-native';
import {
AlsoSentToChannelHeaderPressPayload,
Channel,
MessageActionsParams,
Thread,
ThreadType,
useChatContext,
useTheme,
useTranslationContext,
useTypingString,
PortalWhileClosingView,
} from 'stream-chat-react-native';
import { useStateStore } from 'stream-chat-react-native';
import { ScreenHeader } from '../components/ScreenHeader';
import { type RouteProp } from '@react-navigation/native';
import type { StackNavigatorParamList } from '../types';
import { LocalMessage, ThreadState, UserResponse } from 'stream-chat';
import { useCreateDraftFocusEffect } from '../utils/useCreateDraftFocusEffect.tsx';
import { channelMessageActions } from '../utils/messageActions.tsx';
import { useStreamChatContext } from '../context/StreamChatContext.tsx';
import { NativeStackNavigationProp } from '@react-navigation/native-stack';
// import { CustomAttachmentPickerSelectionBar } from '../components/AttachmentPickerSelectionBar.tsx';
import { MessageLocation } from '../components/LocationSharing/MessageLocation.tsx';
import { useAppContext } from '../context/AppContext.ts';
import { useLegacyColors } from '../theme/useLegacyColors';
const selector = (nextValue: ThreadState) => ({ parentMessage: nextValue.parentMessage }) as const;
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
type ThreadScreenNavigationProp = NativeStackNavigationProp<
StackNavigatorParamList,
'ThreadScreen'
>;
type ThreadScreenRouteProp = RouteProp<StackNavigatorParamList, 'ThreadScreen'>;
type ThreadScreenProps = {
navigation: ThreadScreenNavigationProp;
route: ThreadScreenRouteProp;
};
export type ThreadHeaderProps = {
thread: LocalMessage | ThreadType;
};
const ThreadHeader: React.FC<ThreadHeaderProps> = ({ thread }) => {
const typing = useTypingString();
let subtitleText = ((thread as LocalMessage)?.user as UserResponse)?.name;
const { parentMessage } =
useStateStore((thread as ThreadType)?.threadInstance?.state, selector) || {};
if (subtitleText == null) {
subtitleText = (parentMessage?.user as UserResponse)?.name;
}
useCreateDraftFocusEffect();
return (
<ScreenHeader
subtitleText={typing ? typing : `with ${subtitleText}`}
titleText='Thread Reply'
/>
);
};
export const ThreadScreen: React.FC<ThreadScreenProps> = ({
navigation,
route,
}) => {
const { channel, thread, targetedMessageId: targetedMessageIdFromParams } = route.params;
const {
theme: {
semantics,
},
} = useTheme();
const { white } = useLegacyColors();
const { client: chatClient } = useChatContext();
const { t } = useTranslationContext();
const { setThread } = useStreamChatContext();
const { messageInputFloating, messageListImplementation } = useAppContext();
const onPressMessage: NonNullable<React.ComponentProps<typeof Channel>['onPressMessage']> = (
payload,
) => {
const { message, defaultHandler, emitter } = payload;
const { shared_location } = message ?? {};
if (emitter === 'messageContent' && shared_location) {
navigation.navigate('MapScreen', shared_location);
}
defaultHandler?.();
};
const messageActions = useCallback(
(params: MessageActionsParams) => {
if (!chatClient) {
return [];
}
return channelMessageActions({
params,
chatClient,
semantics,
t,
});
},
[chatClient, semantics, t],
);
const onThreadDismount = useCallback(() => {
setThread(null);
}, [setThread]);
const onAlsoSentToChannelHeaderPress = useCallback(
({ targetedMessageId }: AlsoSentToChannelHeaderPressPayload) => {
const params: StackNavigatorParamList['ChannelScreen'] = {
channel,
messageId: targetedMessageId,
};
const hasChannelInStack = navigation
.getState()
.routes.some((stackRoute) => {
if (stackRoute.name !== 'ChannelScreen') {
return false;
}
const routeParams = stackRoute.params as StackNavigatorParamList['ChannelScreen'] | undefined;
const routeChannelId = routeParams?.channel?.id ?? routeParams?.channelId;
return routeChannelId === channel.id;
});
if (hasChannelInStack) {
navigation.popTo('ChannelScreen', params);
return;
}
navigation.navigate('ChannelScreen', params);
},
[channel, navigation],
);
return (
<View style={[styles.container, { backgroundColor: white }]}>
<Channel
audioRecordingEnabled={true}
// AttachmentPickerSelectionBar={CustomAttachmentPickerSelectionBar}
channel={channel}
enforceUniqueReaction
keyboardVerticalOffset={0}
messageActions={messageActions}
messageInputFloating={messageInputFloating}
MessageLocation={MessageLocation}
onPressMessage={onPressMessage}
thread={thread}
threadList
onAlsoSentToChannelHeaderPress={onAlsoSentToChannelHeaderPress}
messageId={targetedMessageIdFromParams}
>
<PortalWhileClosingView
portalHostName='overlay-header'
portalName='channel-header'
>
<ThreadHeader thread={thread} />
</PortalWhileClosingView>
<Thread
onThreadDismount={onThreadDismount}
shouldUseFlashList={messageListImplementation === 'flashlist'}
/>
</Channel>
</View>
);
};