Skip to content

Commit 3b8b570

Browse files
committed
fix: pop back when we can
1 parent 78a069a commit 3b8b570

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

examples/SampleApp/src/screens/ChannelScreen.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { useStreamChatContext } from '../context/StreamChatContext.tsx';
3333
import { CustomAttachmentPickerSelectionBar } from '../components/AttachmentPickerSelectionBar.tsx';
3434
import { MessageInfoBottomSheet } from '../components/MessageInfoBottomSheet.tsx';
3535
import { CustomAttachmentPickerContent } from '../components/AttachmentPickerContent.tsx';
36+
import { ThreadType } from 'stream-chat-react-native-core';
3637

3738
export type ChannelScreenNavigationProp = NativeStackNavigationProp<
3839
StackNavigatorParamList,
@@ -195,7 +196,29 @@ export const ChannelScreen: React.FC<ChannelScreenProps> = ({
195196
}
196197
setSelectedThread(parentMessage);
197198
setThread(parentMessage);
198-
navigation.navigate('ThreadScreen', { channel, thread: parentMessage, targetedMessageId });
199+
const params: StackNavigatorParamList['ThreadScreen'] = {
200+
channel,
201+
targetedMessageId,
202+
thread: parentMessage,
203+
};
204+
const hasThreadInStack = navigation.getState().routes.some((route) => {
205+
if (route.name !== 'ThreadScreen') {
206+
return false;
207+
}
208+
const routeParams = route.params as StackNavigatorParamList['ThreadScreen'] | undefined;
209+
const routeThreadId =
210+
(routeParams?.thread as LocalMessage)?.id ??
211+
(routeParams?.thread as ThreadType)?.thread?.id;
212+
const routeChannelId = routeParams?.channel?.id;
213+
return routeThreadId === parentMessage.id && routeChannelId === channel.id;
214+
});
215+
216+
if (hasThreadInStack) {
217+
navigation.popTo('ThreadScreen', params);
218+
return;
219+
}
220+
221+
navigation.navigate('ThreadScreen', params);
199222
},
200223
[channel, navigation, setThread],
201224
);

examples/SampleApp/src/screens/ThreadScreen.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,27 @@ export const ThreadScreen: React.FC<ThreadScreenProps> = ({
119119

120120
const onAlsoSentToChannelHeaderPress = useCallback(
121121
({ targetedMessageId }: AlsoSentToChannelHeaderPressPayload) => {
122-
navigation.navigate('ChannelScreen', { channel, messageId: targetedMessageId });
122+
const params: StackNavigatorParamList['ChannelScreen'] = {
123+
channel,
124+
messageId: targetedMessageId,
125+
};
126+
const hasChannelInStack = navigation
127+
.getState()
128+
.routes.some((route) => {
129+
if (route.name !== 'ChannelScreen') {
130+
return false;
131+
}
132+
const routeParams = route.params as StackNavigatorParamList['ChannelScreen'] | undefined;
133+
const routeChannelId = routeParams?.channel?.id ?? routeParams?.channelId;
134+
return routeChannelId === channel.id;
135+
});
136+
137+
if (hasChannelInStack) {
138+
navigation.popTo('ChannelScreen', params);
139+
return;
140+
}
141+
142+
navigation.navigate('ChannelScreen', params);
123143
},
124144
[channel, navigation],
125145
);

0 commit comments

Comments
 (0)