|
| 1 | +// @flow |
| 2 | +import React from 'react'; |
| 3 | +import compose from 'recompose/compose'; |
| 4 | +import type { NavigationProps } from 'react-navigation'; |
| 5 | +import { withCurrentUser } from '../../components/WithCurrentUser'; |
| 6 | +import { throttle, debounce } from 'throttle-debounce'; |
| 7 | +import withSafeView from '../../components/SafeAreaView'; |
| 8 | +import ChatInput from '../../components/ChatInput'; |
| 9 | +import PeopleSearchView from '../Search/PeopleSearchView'; |
| 10 | +import getCurrentUserDMThreadConnection from '../../../shared/graphql/queries/directMessageThread/getCurrentUserDMThreadConnection'; |
| 11 | +import createDirectMessageThread, { |
| 12 | + type CreateDirectMessageThreadType, |
| 13 | + type CreateDirectMessageThreadProps, |
| 14 | +} from '../../../shared/graphql/mutations/directMessageThread/createDirectMessageThread'; |
| 15 | +import type { GetUserType } from '../../../shared/graphql/queries/user/getUser'; |
| 16 | +import { |
| 17 | + ComposerWrapper, |
| 18 | + SearchInputArea, |
| 19 | + SelectedUsers, |
| 20 | + UserSearchInput, |
| 21 | +} from './style'; |
| 22 | +import { events, track } from '../../utils/analytics'; |
| 23 | +import { FullscreenNullState } from '../../components/NullStates'; |
| 24 | +import SelectedUser from './SelectedUser'; |
| 25 | + |
| 26 | +type Props = { |
| 27 | + ...$Exact<NavigationProps>, |
| 28 | + ...$Exact<CreateDirectMessageThreadProps>, |
| 29 | + currentUser: GetUserType, |
| 30 | + presetUserId?: string, |
| 31 | +}; |
| 32 | + |
| 33 | +type State = { |
| 34 | + wipSearchString: ?string, |
| 35 | + searchString: ?string, |
| 36 | + selectedUsers: string[], |
| 37 | +}; |
| 38 | + |
| 39 | +class DirectMessageComposer extends React.Component<Props, State> { |
| 40 | + state = { |
| 41 | + wipSearchString: '', |
| 42 | + searchString: '', |
| 43 | + selectedUsers: [], |
| 44 | + }; |
| 45 | + |
| 46 | + searchInput: ?{ |
| 47 | + focus: () => void, |
| 48 | + }; |
| 49 | + |
| 50 | + constructor() { |
| 51 | + super(); |
| 52 | + this.searchDebounced = debounce(300, this.searchDebounced); |
| 53 | + this.searchThrottled = throttle(300, this.searchThrottled); |
| 54 | + } |
| 55 | + |
| 56 | + componentDidMount() { |
| 57 | + track(events.DIRECT_MESSAGE_THREAD_COMPOSER_VIEWED); |
| 58 | + |
| 59 | + const { presetUserId } = this.props; |
| 60 | + |
| 61 | + if (presetUserId) { |
| 62 | + this.setState({ |
| 63 | + selectedUsers: [presetUserId], |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + onChangeText = (text: string) => { |
| 69 | + this.setState({ |
| 70 | + wipSearchString: text, |
| 71 | + }); |
| 72 | + }; |
| 73 | + |
| 74 | + onChangeText = (text: string) => { |
| 75 | + this.setState({ wipSearchString: text }, () => { |
| 76 | + const { wipSearchString } = this.state; |
| 77 | + if (wipSearchString && wipSearchString.length < 5) { |
| 78 | + this.searchThrottled(wipSearchString); |
| 79 | + } else { |
| 80 | + this.searchDebounced(wipSearchString); |
| 81 | + } |
| 82 | + }); |
| 83 | + }; |
| 84 | + |
| 85 | + onFinishTyping = (e: { nativeEvent: { text: string } }) => { |
| 86 | + this.search(e.nativeEvent.text); |
| 87 | + }; |
| 88 | + |
| 89 | + searchDebounced = (searchString: ?string) => { |
| 90 | + this.setState({ |
| 91 | + searchString, |
| 92 | + }); |
| 93 | + }; |
| 94 | + |
| 95 | + searchThrottled = (searchString: ?string) => { |
| 96 | + this.setState({ |
| 97 | + searchString, |
| 98 | + }); |
| 99 | + }; |
| 100 | + |
| 101 | + search = (searchString: ?string) => { |
| 102 | + this.setState({ |
| 103 | + searchString, |
| 104 | + }); |
| 105 | + }; |
| 106 | + |
| 107 | + selectUser = (userId: string) => { |
| 108 | + this.setState(prev => ({ |
| 109 | + selectedUsers: prev.selectedUsers.concat([userId]), |
| 110 | + wipSearchString: '', |
| 111 | + searchString: '', |
| 112 | + })); |
| 113 | + this.searchInput && this.searchInput.focus(); |
| 114 | + }; |
| 115 | + |
| 116 | + removeSelectedUser = (userId: string) => () => { |
| 117 | + this.setState(prev => ({ |
| 118 | + selectedUsers: prev.selectedUsers.filter( |
| 119 | + selectedId => selectedId !== userId |
| 120 | + ), |
| 121 | + })); |
| 122 | + }; |
| 123 | + |
| 124 | + onSubmit = (text: string) => { |
| 125 | + if (this.state.selectedUsers.length === 0) return; |
| 126 | + this.props |
| 127 | + .createDirectMessageThread({ |
| 128 | + participants: this.state.selectedUsers, |
| 129 | + message: { |
| 130 | + messageType: 'text', |
| 131 | + threadType: 'directMessageThread', |
| 132 | + content: { |
| 133 | + body: text, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }) |
| 137 | + .then((result: CreateDirectMessageThreadType) => { |
| 138 | + const { navigation } = this.props; |
| 139 | + |
| 140 | + return navigation.navigate('DirectMessageThread', { |
| 141 | + id: result.data.createDirectMessageThread.id, |
| 142 | + }); |
| 143 | + }) |
| 144 | + .catch(err => { |
| 145 | + console.error(err); |
| 146 | + }); |
| 147 | + }; |
| 148 | + |
| 149 | + filterResults = (results: Array<Object>) => { |
| 150 | + const { currentUser } = this.props; |
| 151 | + const { selectedUsers } = this.state; |
| 152 | + |
| 153 | + return results |
| 154 | + .filter(row => row.id !== currentUser.id) |
| 155 | + .filter(row => selectedUsers.indexOf(row.id) < 0); |
| 156 | + }; |
| 157 | + |
| 158 | + render() { |
| 159 | + return ( |
| 160 | + <ComposerWrapper> |
| 161 | + <SearchInputArea> |
| 162 | + <SelectedUsers |
| 163 | + horizontal |
| 164 | + empty={this.state.selectedUsers.length === 0} |
| 165 | + > |
| 166 | + {this.state.selectedUsers.map(userId => ( |
| 167 | + <SelectedUser |
| 168 | + key={userId} |
| 169 | + id={userId} |
| 170 | + keyboardShouldPersistTaps={'always'} |
| 171 | + onPressHandler={this.removeSelectedUser(userId)} |
| 172 | + /> |
| 173 | + ))} |
| 174 | + </SelectedUsers> |
| 175 | + <UserSearchInput |
| 176 | + onChangeText={this.onChangeText} |
| 177 | + onEndEditing={this.onFinishTyping} |
| 178 | + onSubmitEditing={this.onFinishTyping} |
| 179 | + value={this.state.wipSearchString} |
| 180 | + returnKeyType="search" |
| 181 | + autoFocus |
| 182 | + autoCorrect={false} |
| 183 | + autoCapitalize={'none'} |
| 184 | + innerRef={elem => (this.searchInput = elem)} |
| 185 | + placeholder={'Search for people...'} |
| 186 | + /> |
| 187 | + </SearchInputArea> |
| 188 | + |
| 189 | + {!this.state.searchString && ( |
| 190 | + <FullscreenNullState title={''} subtitle={''} /> |
| 191 | + )} |
| 192 | + |
| 193 | + {this.state.searchString && ( |
| 194 | + <PeopleSearchView |
| 195 | + onPress={this.selectUser} |
| 196 | + queryString={this.state.searchString} |
| 197 | + keyboardShouldPersistTaps={'always'} |
| 198 | + filter={results => this.filterResults(results)} |
| 199 | + /> |
| 200 | + )} |
| 201 | + |
| 202 | + <ChatInput |
| 203 | + onSubmit={this.onSubmit} |
| 204 | + disableSubmit={this.state.selectedUsers.length === 0} |
| 205 | + /> |
| 206 | + </ComposerWrapper> |
| 207 | + ); |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +export default compose( |
| 212 | + withCurrentUser, |
| 213 | + getCurrentUserDMThreadConnection, |
| 214 | + createDirectMessageThread, |
| 215 | + withSafeView |
| 216 | +)(DirectMessageComposer); |
0 commit comments