-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathNewDirectMessagingSendButton.tsx
More file actions
135 lines (118 loc) · 3.57 KB
/
NewDirectMessagingSendButton.tsx
File metadata and controls
135 lines (118 loc) · 3.57 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
import React from 'react';
import { Alert } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { useNavigation } from '@react-navigation/core';
import {
MessageInputContextValue,
Search,
SendRight,
useChannelContext,
useMessageInputContext,
useTheme,
} from 'stream-chat-react-native';
import { NewDirectMessagingScreenNavigationProp } from '../screens/NewDirectMessagingScreen';
import { useUserSearchContext } from '../context/UserSearchContext';
import { useAppContext } from '../context/AppContext';
import { SendUp } from '../icons/SendUp';
type NewDirectMessagingSendButtonPropsWithContext = Pick<
MessageInputContextValue,
'giphyActive' | 'sendMessage'
> & {
/** Disables the button */ disabled: boolean;
};
const SendButtonWithContext = (props: NewDirectMessagingSendButtonPropsWithContext) => {
const { disabled = false, giphyActive, sendMessage } = props;
const {
theme: {
colors: { accent_blue, grey_gainsboro },
messageComposer: { sendButton },
},
} = useTheme();
return (
<TouchableOpacity
disabled={disabled}
onPress={sendMessage}
style={[sendButton]}
testID='send-button'
>
{giphyActive && <Search pathFill={accent_blue} />}
{!giphyActive && disabled && <SendRight fill={grey_gainsboro} size={32} />}
{!giphyActive && !disabled && <SendUp fill={accent_blue} size={32} />}
</TouchableOpacity>
);
};
const areEqual = (
prevProps: NewDirectMessagingSendButtonPropsWithContext,
nextProps: NewDirectMessagingSendButtonPropsWithContext,
) => {
const {
disabled: prevDisabled,
giphyActive: prevGiphyActive,
sendMessage: prevSendMessage,
} = prevProps;
const {
disabled: nextDisabled,
giphyActive: nextGiphyActive,
sendMessage: nextSendMessage,
} = nextProps;
const disabledEqual = prevDisabled === nextDisabled;
if (!disabledEqual) {
return false;
}
const giphyActiveEqual = prevGiphyActive === nextGiphyActive;
if (!giphyActiveEqual) {
return false;
}
const sendMessageEqual = prevSendMessage === nextSendMessage;
if (!sendMessageEqual) {
return false;
}
return true;
};
const MemoizedNewDirectMessagingSendButton = React.memo(
SendButtonWithContext,
areEqual,
) as typeof SendButtonWithContext;
export type SendButtonProps = Partial<NewDirectMessagingSendButtonPropsWithContext>;
/**
* UI Component for send button in MessageComposer component.
*/
export const NewDirectMessagingSendButton = (props: SendButtonProps) => {
const { chatClient } = useAppContext();
const navigation = useNavigation<NewDirectMessagingScreenNavigationProp>();
const { channel } = useChannelContext();
const { selectedUserIds, reset } = useUserSearchContext();
const { giphyActive, text } = useMessageInputContext();
const sendMessage = async () => {
if (!channel) {
return;
}
if (!chatClient || !chatClient.user) {
return;
}
const members = [chatClient.user.id, ...selectedUserIds];
channel.initialized = false;
const newChannel = chatClient.channel('messaging', {
members,
});
try {
await newChannel.watch();
await newChannel.sendMessage({ text });
navigation.replace('ChannelScreen', {
channelId: newChannel.id,
});
reset();
} catch (e) {
if (e instanceof Error) {
Alert.alert('Error sending a message', e.message);
}
}
};
return (
<MemoizedNewDirectMessagingSendButton
{...{ giphyActive, sendMessage }}
{...props}
{...{ disabled: props.disabled || false }}
/>
);
};