-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathThreadScreen.tsx
More file actions
151 lines (133 loc) · 4.37 KB
/
ThreadScreen.tsx
File metadata and controls
151 lines (133 loc) · 4.37 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
import React, { useCallback } from 'react';
import { Platform, StyleSheet, View } from 'react-native';
import {
Channel,
MessageActionsParams,
Thread,
ThreadType,
useChatContext,
useTheme,
useTranslationContext,
useTypingString,
} 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';
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: {
params: { channel, thread, targetedMessageId },
},
}) => {
const {
theme: {
semantics,
colors: { white },
},
} = useTheme();
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 onBackPressThread = useCallback(
(messageId?: string) => {
navigation.popTo('ChannelScreen', { messageId: messageId });
},
[navigation],
);
return (
<View style={[styles.container, { backgroundColor: white }]}>
<Channel
audioRecordingEnabled={false}
AttachmentPickerSelectionBar={CustomAttachmentPickerSelectionBar}
channel={channel}
enforceUniqueReaction
keyboardVerticalOffset={Platform.OS === 'ios' ? 0 : -300}
messageActions={messageActions}
messageInputFloating={messageInputFloating}
MessageLocation={MessageLocation}
onPressMessage={onPressMessage}
thread={thread}
threadList
onBackPressThread={onBackPressThread}
messageId={targetedMessageId}
>
<ThreadHeader thread={thread} />
<Thread
onThreadDismount={onThreadDismount}
shouldUseFlashList={messageListImplementation === 'flashlist'}
/>
</Channel>
</View>
);
};