Skip to content

Commit 7a8208b

Browse files
committed
EmojiPickerScreen: Put callback in route params
1 parent f25b469 commit 7a8208b

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

src/action-sheets/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import * as logging from '../utils/logging';
4141
import { getUnreadCountForTopic } from '../unread/unreadModel';
4242
import getIsNotificationEnabled from '../streams/getIsNotificationEnabled';
4343
import { getStreamTopicUrl, getStreamUrl } from '../utils/internalLinks';
44+
import { reactionTypeFromEmojiType } from '../emoji/data';
4445

4546
// TODO really this belongs in a libdef.
4647
export type ShowActionSheetWithOptions = (
@@ -343,8 +344,17 @@ const shareMessage = {
343344
const addReaction = {
344345
title: 'Add a reaction',
345346
errorMessage: 'Failed to add reaction',
346-
action: ({ message }) => {
347-
NavigationService.dispatch(navigateToEmojiPicker(message.id));
347+
action: ({ auth, message, _ }) => {
348+
NavigationService.dispatch(
349+
navigateToEmojiPicker(({ code, name, type }) => {
350+
api
351+
.emojiReactionAdd(auth, message.id, reactionTypeFromEmojiType(type, name), code, name)
352+
.catch(err => {
353+
logging.error('Error adding reaction emoji', err);
354+
showToast(_('Failed to add reaction'));
355+
});
356+
}),
357+
);
348358
},
349359
};
350360

src/emoji/EmojiPickerScreen.js

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,63 @@
11
/* @flow strict-local */
22

3-
import React, { useState, useCallback, useContext } from 'react';
3+
import React, { useState, useCallback } from 'react';
44
import type { Node } from 'react';
5-
import { FlatList } from 'react-native';
5+
import { FlatList, LogBox } from 'react-native';
66

7-
import { TranslationContext } from '../boot/TranslationProvider';
87
import type { RouteProp } from '../react-navigation';
98
import type { AppNavigationProp } from '../nav/AppNavigator';
10-
import * as api from '../api';
119
import Screen from '../common/Screen';
1210
import EmojiRow from './EmojiRow';
13-
import { getFilteredEmojis, reactionTypeFromEmojiType } from './data';
11+
import { getFilteredEmojis } from './data';
12+
import { getActiveImageEmojiByName } from '../selectors';
13+
import type { EmojiType } from '../types';
1414
import { useSelector } from '../react-redux';
15-
import { getAuth, getActiveImageEmojiByName } from '../selectors';
16-
import * as logging from '../utils/logging';
17-
import { showToast } from '../utils/info';
1815

1916
type Props = $ReadOnly<{|
2017
navigation: AppNavigationProp<'emoji-picker'>,
21-
route: RouteProp<'emoji-picker', {| messageId: number |}>,
18+
route: RouteProp<
19+
'emoji-picker',
20+
{|
21+
// This param is a function, so React Nav is right to point out that
22+
// it isn't serializable. But this is fine as long as we don't try to
23+
// persist navigation state for this screen or set up deep linking to
24+
// it, hence the LogBox suppression below.
25+
//
26+
// React Navigation doesn't offer a more sensible way to do have us
27+
// pass the emoji data to the calling screen. …We could store the
28+
// emoji data as a route param on the calling screen, or in Redux. But
29+
// from this screen's perspective, that's basically just setting a
30+
// global variable. Better to offer this explicit, side-effect-free
31+
// way for the data to flow where it should, when it should.
32+
onPressEmoji: ({| +type: EmojiType, +code: string, +name: string |}) => void,
33+
|},
34+
>,
2235
|}>;
2336

37+
// React Navigation would give us a console warning about non-serializable
38+
// route params. For more about the warning, see
39+
// https://reactnavigation.org/docs/5.x/troubleshooting/#i-get-the-warning-non-serializable-values-were-found-in-the-navigation-state
40+
// See comment on this param, above.
41+
LogBox.ignoreLogs([/emoji-picker > params\.onPressEmoji \(Function\)/]);
42+
2443
export default function EmojiPickerScreen(props: Props): Node {
2544
const { navigation, route } = props;
26-
const { messageId } = route.params;
27-
28-
const _ = useContext(TranslationContext);
45+
const { onPressEmoji } = route.params;
2946

3047
const activeImageEmojiByName = useSelector(getActiveImageEmojiByName);
31-
const auth = useSelector(getAuth);
3248

3349
const [filter, setFilter] = useState<string>('');
3450

3551
const handleInputChange = useCallback((text: string) => {
3652
setFilter(text.toLowerCase());
3753
}, []);
3854

39-
const addReaction = useCallback(
40-
({ type, code, name }) => {
41-
api
42-
.emojiReactionAdd(auth, messageId, reactionTypeFromEmojiType(type, name), code, name)
43-
.catch(err => {
44-
logging.error('Error adding reaction emoji', err);
45-
showToast(_('Failed to add reaction'));
46-
});
55+
const handlePressEmoji = useCallback(
56+
(...args) => {
57+
onPressEmoji(...args);
4758
navigation.goBack();
4859
},
49-
[auth, messageId, _, navigation],
60+
[onPressEmoji, navigation],
5061
);
5162

5263
const emojiNames = getFilteredEmojis(filter, activeImageEmojiByName);
@@ -63,7 +74,7 @@ export default function EmojiPickerScreen(props: Props): Node {
6374
type={item.emoji_type}
6475
code={item.code}
6576
name={item.name}
66-
onPress={addReaction}
77+
onPress={handlePressEmoji}
6778
/>
6879
)}
6980
/>

src/nav/navActions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '@react-navigation/native';
88

99
import * as NavigationService from './NavigationService';
10-
import type { Message, Narrow, UserId } from '../types';
10+
import type { Message, Narrow, UserId, EmojiType } from '../types';
1111
import type { PmKeyRecipients } from '../utils/recipient';
1212
import type { SharedData } from '../sharing/types';
1313
import type { ApiResponseServerSettings } from '../api/settings/getServerSettings';
@@ -52,8 +52,9 @@ export const navigateToUsersScreen = (): GenericNavigationAction => StackActions
5252

5353
export const navigateToSearch = (): GenericNavigationAction => StackActions.push('search-messages');
5454

55-
export const navigateToEmojiPicker = (messageId: number): GenericNavigationAction =>
56-
StackActions.push('emoji-picker', { messageId });
55+
export const navigateToEmojiPicker = (
56+
onPressEmoji: ({| +type: EmojiType, +code: string, +name: string |}) => void,
57+
): GenericNavigationAction => StackActions.push('emoji-picker', { onPressEmoji });
5758

5859
export const navigateToAuth = (
5960
serverSettings: ApiResponseServerSettings,

0 commit comments

Comments
 (0)