-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.tsx
138 lines (126 loc) · 4.6 KB
/
index.tsx
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
import { useLazyQuery, useMutation, useSubscription } from '@apollo/react-hooks';
import React, { useContext, useEffect, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { GiftedChat } from 'react-native-gifted-chat';
import { useNavigation, useNavigationParam } from 'react-navigation-hooks';
import { IconSizes, Routes } from '../../constants';
import { AppContext } from '../../context';
import { MUTATION_ADD_MESSAGE, MUTATION_CONNECT_CHAT_TO_USERS } from '../../graphql/mutation';
import { QUERY_CHAT } from '../../graphql/query';
import { SUBSCRIPTION_CHAT } from '../../graphql/subscription';
import { ConversationScreenPlaceholder, GoBackHeader } from '../../layout';
import { ThemeColors } from '../../types/theme';
import { filterChatParticipants, transformMessages } from '../../utils/shared';
import ChatHeaderAvatar from './components/ChatHeaderAvatar';
import CustomBubble from './components/CustomBubble';
import CustomComposer from './components/CustomComposer';
import CustomInputToolbar from './components/CustomInputToolbar';
import CustomMessageText from './components/CustomMessageText';
import CustomSend from './components/CustomSend';
import CustomScrollToBottom from './components/CustomScrollToBottom';
import { ifIphoneX } from 'react-native-iphone-x-helper';
const ConversationScreen: React.FC = () => {
const chatId = useNavigationParam('chatId');
const handle = useNavigationParam('handle');
const avatar = useNavigationParam('avatar');
const targetId = useNavigationParam('targetId');
const { navigate } = useNavigation();
const { user, theme } = useContext(AppContext);
const [messages, setMessages] = useState([]);
const [queryChat, {
called: chatQueryCalled,
data: chatQueryData,
loading: chatQueryLoading,
error: chatQueryError
}] = useLazyQuery(QUERY_CHAT, {
variables: { chatId },
fetchPolicy: 'network-only'
});
const { data: chatSubscriptionData, loading: chatSubscriptionLoading } = useSubscription(SUBSCRIPTION_CHAT, {
variables: { chatId }
});
const [addMessage] = useMutation(MUTATION_ADD_MESSAGE);
const [connectChat] = useMutation(MUTATION_CONNECT_CHAT_TO_USERS);
useEffect(() => {
if (!chatSubscriptionLoading) {
setMessages(chatSubscriptionData.chat.messages);
} else if (chatSubscriptionLoading) {
if (chatQueryCalled && !chatQueryLoading) {
setMessages(chatQueryData.chat.messages);
} else if (!chatQueryCalled) {
queryChat();
}
}
}, [chatQueryData, chatQueryCalled, chatQueryLoading, chatSubscriptionData, chatSubscriptionLoading]);
const onSend = async updatedMessages => {
const isFirstMessage = messages.length === 0;
const [updatedMessage] = updatedMessages;
if (isFirstMessage) {
await connectChat({
variables: {
chatId,
userId: user.id,
targetId
}
});
}
addMessage({
variables: {
chatId,
authorId: user.id,
body: updatedMessage.text
}
});
};
const navigateToProfile = () => {
navigate(Routes.ProfileViewScreen, { userId: targetId });
};
let content = <ConversationScreenPlaceholder />
if (chatQueryCalled && !chatQueryLoading && !chatQueryError) {
const transform = transformMessages(messages);
content = (
<GiftedChat
scrollToBottom
alwaysShowSend
inverted={false}
maxInputLength={200}
messages={transform}
scrollToBottomComponent={CustomScrollToBottom}
textInputProps={{ disable: true }}
renderComposer={composerProps => <CustomComposer {...composerProps} />}
renderMessageText={CustomMessageText}
renderBubble={CustomBubble}
renderSend={CustomSend}
renderInputToolbar={CustomInputToolbar}
onSend={onSend}
onPressAvatar={navigateToProfile}
user={{ _id: user.id }}
bottomOffset={ifIphoneX(20, -12)}
keyboardShouldPersistTaps={null}
listViewProps={{ showsVerticalScrollIndicator: false, style: { marginBottom: 16 } }}
/>
);
}
return (
<View style={styles(theme).container}>
<GoBackHeader
title={handle}
onTitlePress={navigateToProfile}
iconSize={IconSizes.x7}
ContentLeft={() => <ChatHeaderAvatar avatar={avatar} onPress={navigateToProfile} />}
titleStyle={styles().headerTitleStyle}
/>
{content}
</View>
);
};
const styles = (theme = {} as ThemeColors) => StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.base
},
headerTitleStyle: {
marginLeft: 0
}
});
export default ConversationScreen;