Skip to content

Commit a16743a

Browse files
committed
action-sheets: Plumb navigation down to button args (PmArgs, etc.)
This way, we can stop using NavigationService in these action sheets, and instead use `navigation.push` calls. React Navigation upstream recommends avoiding the NavigationService approach where possible, and this goes in that direction; see more in zulip#5408.
1 parent 178416a commit a16743a

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

src/action-sheets/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { reactionTypeFromEmojiType } from '../emoji/data';
5454
import { Role, type RoleT } from '../api/permissionsTypes';
5555
import { roleIsAtLeast } from '../permissionSelectors';
5656
import { kNotificationBotEmail } from '../api/constants';
57+
import type { AppNavigationMethods } from '../nav/AppNavigator';
5758

5859
// TODO really this belongs in a libdef.
5960
export type ShowActionSheetWithOptions = (
@@ -67,6 +68,7 @@ type StreamArgs = {
6768
subscriptions: Map<number, Subscription>,
6869
streams: Map<number, Stream>,
6970
dispatch: Dispatch,
71+
navigation: AppNavigationMethods,
7072
_: GetText,
7173
...
7274
};
@@ -79,12 +81,14 @@ type TopicArgs = {
7981
streams: Map<number, Stream>,
8082
zulipFeatureLevel: number,
8183
dispatch: Dispatch,
84+
navigation: AppNavigationMethods,
8285
_: GetText,
8386
...
8487
};
8588

8689
type PmArgs = {
8790
pmKeyRecipients: PmKeyRecipients,
91+
navigation: AppNavigationMethods,
8892
_: GetText,
8993
...
9094
};
@@ -98,9 +102,10 @@ type MessageArgs = {
98102
streams: Map<number, Stream>,
99103
zulipFeatureLevel: number,
100104
dispatch: Dispatch,
101-
_: GetText,
102105
startEditMessage: (editMessage: EditMessage) => void,
103106
setDoNotMarkMessagesAsRead: boolean => void,
107+
navigation: AppNavigationMethods,
108+
_: GetText,
104109
...
105110
};
106111

@@ -791,6 +796,7 @@ export const showMessageActionSheet = (args: {|
791796
callbacks: {|
792797
dispatch: Dispatch,
793798
startEditMessage: (editMessage: EditMessage) => void,
799+
navigation: AppNavigationMethods,
794800
_: GetText,
795801
setDoNotMarkMessagesAsRead: boolean => void,
796802
|},
@@ -820,6 +826,7 @@ export const showTopicActionSheet = (args: {|
820826
showActionSheetWithOptions: ShowActionSheetWithOptions,
821827
callbacks: {|
822828
dispatch: Dispatch,
829+
navigation: AppNavigationMethods,
823830
_: GetText,
824831
|},
825832
backgroundData: $ReadOnly<{
@@ -851,6 +858,7 @@ export const showStreamActionSheet = (args: {|
851858
showActionSheetWithOptions: ShowActionSheetWithOptions,
852859
callbacks: {|
853860
dispatch: Dispatch,
861+
navigation: AppNavigationMethods,
854862
_: GetText,
855863
|},
856864
backgroundData: $ReadOnly<{
@@ -876,6 +884,7 @@ export const showStreamActionSheet = (args: {|
876884
export const showPmConversationActionSheet = (args: {|
877885
showActionSheetWithOptions: ShowActionSheetWithOptions,
878886
callbacks: {|
887+
navigation: AppNavigationMethods,
879888
_: GetText,
880889
|},
881890
backgroundData: $ReadOnly<{ ownUser: User, allUsersById: Map<UserId, UserOrBot>, ... }>,

src/streams/StreamItem.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import UnreadCount from '../common/UnreadCount';
3333
import { foregroundColorFromBackground } from '../utils/color';
3434
import { IconPlus, IconDone } from '../common/Icons';
3535
import StreamIcon from './StreamIcon';
36+
import { useNavigation } from '../react-navigation';
3637

3738
const componentStyles = createStyleSheet({
3839
description: {
@@ -114,6 +115,7 @@ export default function StreamItem(props: Props): Node {
114115
const showActionSheetWithOptions: ShowActionSheetWithOptions =
115116
useActionSheet().showActionSheetWithOptions;
116117
const _ = useContext(TranslationContext);
118+
const navigation = useNavigation();
117119
const dispatch = useDispatch();
118120
const backgroundData = useSelector(state => ({
119121
auth: getAuth(state),
@@ -144,7 +146,7 @@ export default function StreamItem(props: Props): Node {
144146
onLongPress={() => {
145147
showStreamActionSheet({
146148
showActionSheetWithOptions,
147-
callbacks: { dispatch, _ },
149+
callbacks: { dispatch, navigation, _ },
148150
backgroundData,
149151
streamId,
150152
});

src/streams/TopicItem.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { getMute } from '../mute/muteModel';
2626
import { getUnread } from '../unread/unreadModel';
2727
import { getOwnUserRole } from '../permissionSelectors';
28+
import { useNavigation } from '../react-navigation';
2829

2930
const componentStyles = createStyleSheet({
3031
selectedRow: {
@@ -69,6 +70,7 @@ export default function TopicItem(props: Props): Node {
6970
const showActionSheetWithOptions: ShowActionSheetWithOptions =
7071
useActionSheet().showActionSheetWithOptions;
7172
const _ = useContext(TranslationContext);
73+
const navigation = useNavigation();
7274
const dispatch = useDispatch();
7375
const backgroundData = useSelector(state => ({
7476
auth: getAuth(state),
@@ -88,7 +90,7 @@ export default function TopicItem(props: Props): Node {
8890
onLongPress={() => {
8991
showTopicActionSheet({
9092
showActionSheetWithOptions,
91-
callbacks: { dispatch, _ },
93+
callbacks: { dispatch, navigation, _ },
9294
backgroundData,
9395
streamId,
9496
topic: name,

src/title/TitleStream.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { showStreamActionSheet, showTopicActionSheet } from '../action-sheets';
2727
import type { ShowActionSheetWithOptions } from '../action-sheets';
2828
import { getUnread } from '../unread/unreadModel';
2929
import { getOwnUserRole } from '../permissionSelectors';
30+
import { useNavigation } from '../react-navigation';
3031

3132
type Props = $ReadOnly<{|
3233
narrow: Narrow,
@@ -50,6 +51,7 @@ const componentStyles = createStyleSheet({
5051
export default function TitleStream(props: Props): Node {
5152
const { narrow, color } = props;
5253
const dispatch = useDispatch();
54+
const navigation = useNavigation();
5355
const stream = useSelector(state => getStreamInNarrow(state, narrow));
5456
const backgroundData = useSelector(state => ({
5557
auth: getAuth(state),
@@ -75,7 +77,7 @@ export default function TitleStream(props: Props): Node {
7577
? () => {
7678
showTopicActionSheet({
7779
showActionSheetWithOptions,
78-
callbacks: { dispatch, _ },
80+
callbacks: { dispatch, navigation, _ },
7981
backgroundData,
8082
streamId: stream.stream_id,
8183
topic: topicOfNarrow(narrow),
@@ -84,7 +86,7 @@ export default function TitleStream(props: Props): Node {
8486
: () => {
8587
showStreamActionSheet({
8688
showActionSheetWithOptions,
87-
callbacks: { dispatch, _ },
89+
callbacks: { dispatch, navigation, _ },
8890
backgroundData,
8991
streamId: stream.stream_id,
9092
});

src/webview/MessageList.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
} from '../types';
2020
import { ThemeContext } from '../styles';
2121
import { useSelector, useDispatch, useGlobalSelector } from '../react-redux';
22+
import { useNavigation } from '../react-navigation';
2223
import {
2324
getCurrentTypingUsers,
2425
getDebug,
@@ -246,6 +247,8 @@ export default function MessageList(outerProps: OuterProps): React.Node {
246247

247248
const props = useMessageListProps(outerProps);
248249

250+
const navigation = useNavigation();
251+
249252
const theme = React.useContext(ThemeContext);
250253

251254
const webviewRef = React.useRef<React$ElementRef<typeof WebView> | null>(null);
@@ -310,7 +313,7 @@ export default function MessageList(outerProps: OuterProps): React.Node {
310313
// (The distinction may not matter much here in practice. But a
311314
// nice bonus of this way is that we avoid re-renders of
312315
// SinglePageWebView, potentially a helpful optimization.)
313-
handleWebViewOutboundEvent(propsRef.current, eventData);
316+
handleWebViewOutboundEvent(propsRef.current, navigation, eventData);
314317
}
315318
},
316319
[sendInboundEvents],

src/webview/handleOutboundEvents.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { ensureUnreachable } from '../types';
3030
import { base64Utf8Decode } from '../utils/encoding';
3131
import type { Props } from './MessageList';
32+
import type { AppNavigationMethods } from '../nav/AppNavigator';
3233

3334
type WebViewOutboundEventReady = {|
3435
type: 'ready',
@@ -206,8 +207,9 @@ const handleLongPress = (args: {|
206207
target: 'message' | 'header' | 'link',
207208
messageId: number,
208209
href: string | null,
210+
navigation: AppNavigationMethods,
209211
|}) => {
210-
const { props, target, messageId, href } = args;
212+
const { props, target, messageId, href, navigation } = args;
211213

212214
if (href !== null) {
213215
const url = new URL(href, props.backgroundData.auth.realm).toString();
@@ -234,31 +236,35 @@ const handleLongPress = (args: {|
234236
if (message.type === 'stream') {
235237
showTopicActionSheet({
236238
showActionSheetWithOptions,
237-
callbacks: { dispatch, _ },
239+
callbacks: { dispatch, navigation, _ },
238240
backgroundData,
239241
streamId: message.stream_id,
240242
topic: message.subject,
241243
});
242244
} else if (message.type === 'private') {
243245
showPmConversationActionSheet({
244246
showActionSheetWithOptions,
245-
callbacks: { _ },
247+
callbacks: { navigation, _ },
246248
backgroundData,
247249
pmKeyRecipients: pmKeyRecipientsFromMessage(message, backgroundData.ownUser.user_id),
248250
});
249251
}
250252
} else if (target === 'message') {
251253
showMessageActionSheet({
252254
showActionSheetWithOptions,
253-
callbacks: { dispatch, startEditMessage, setDoNotMarkMessagesAsRead, _ },
255+
callbacks: { dispatch, startEditMessage, setDoNotMarkMessagesAsRead, navigation, _ },
254256
backgroundData,
255257
message,
256258
narrow,
257259
});
258260
}
259261
};
260262

261-
export const handleWebViewOutboundEvent = (props: Props, event: WebViewOutboundEvent) => {
263+
export const handleWebViewOutboundEvent = (
264+
props: Props,
265+
navigation: AppNavigationMethods,
266+
event: WebViewOutboundEvent,
267+
) => {
262268
switch (event.type) {
263269
case 'ready':
264270
// handled by caller
@@ -288,6 +294,7 @@ export const handleWebViewOutboundEvent = (props: Props, event: WebViewOutboundE
288294
target: event.target,
289295
messageId: event.messageId,
290296
href: event.href,
297+
navigation,
291298
});
292299
break;
293300

0 commit comments

Comments
 (0)