Skip to content

Commit dac2a5d

Browse files
committed
fix: sample app channel header component perf
1 parent 5e45e6e commit dac2a5d

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

examples/SampleApp/src/screens/ChannelScreen.tsx

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ import {
1919
AITypingIndicatorView,
2020
createTextComposerEmojiMiddleware,
2121
} from 'stream-chat-react-native';
22-
import { Platform, StyleSheet, View } from 'react-native';
22+
import { Platform, Pressable, StyleSheet, View } from 'react-native';
2323
import type { StackNavigationProp } from '@react-navigation/stack';
2424
import { useSafeAreaInsets } from 'react-native-safe-area-context';
2525

2626
import { useAppContext } from '../context/AppContext';
2727
import { ScreenHeader } from '../components/ScreenHeader';
28-
import { TouchableOpacity } from 'react-native-gesture-handler';
2928
import { useChannelMembersStatus } from '../hooks/useChannelMembersStatus';
3029

3130
import type { StackNavigatorParamList } from '../types';
@@ -58,44 +57,51 @@ const ChannelHeader: React.FC<ChannelHeaderProps> = ({ channel }) => {
5857
const navigation = useNavigation<ChannelScreenNavigationProp>();
5958
const typing = useTypingString();
6059

61-
if (!channel || !chatClient) {
62-
return null;
63-
}
64-
6560
const isOneOnOneConversation =
6661
channel &&
6762
Object.values(channel.state.members).length === 2 &&
6863
channel.id?.indexOf('!members-') === 0;
6964

65+
const onBackPress = useCallback(() => {
66+
if (!navigation.canGoBack()) {
67+
// if no previous screen was present in history, go to the list screen
68+
// this can happen when opened through push notification
69+
navigation.reset({ index: 0, routes: [{ name: 'MessagingScreen' }] });
70+
} else {
71+
navigation.goBack();
72+
}
73+
}, [navigation]);
74+
75+
const onRightContentPress = useCallback(() => {
76+
closePicker();
77+
if (isOneOnOneConversation) {
78+
navigation.navigate('OneOnOneChannelDetailScreen', {
79+
channel,
80+
});
81+
} else {
82+
navigation.navigate('GroupChannelDetailsScreen', {
83+
channel,
84+
});
85+
}
86+
}, [channel, closePicker, isOneOnOneConversation, navigation]);
87+
88+
if (!channel || !chatClient) {
89+
return null;
90+
}
91+
7092
return (
7193
<ScreenHeader
72-
onBack={() => {
73-
if (!navigation.canGoBack()) {
74-
// if no previous screen was present in history, go to the list screen
75-
// this can happen when opened through push notification
76-
navigation.reset({ index: 0, routes: [{ name: 'MessagingScreen' }] });
77-
} else {
78-
navigation.goBack();
79-
}
80-
}}
94+
onBack={onBackPress}
8195
// eslint-disable-next-line react/no-unstable-nested-components
8296
RightContent={() => (
83-
<TouchableOpacity
84-
onPress={() => {
85-
closePicker();
86-
if (isOneOnOneConversation) {
87-
navigation.navigate('OneOnOneChannelDetailScreen', {
88-
channel,
89-
});
90-
} else {
91-
navigation.navigate('GroupChannelDetailsScreen', {
92-
channel,
93-
});
94-
}
95-
}}
97+
<Pressable
98+
onPress={onRightContentPress}
99+
style={({ pressed }) => ({
100+
opacity: pressed ? 0.5 : 1,
101+
})}
96102
>
97103
<ChannelAvatar channel={channel} />
98-
</TouchableOpacity>
104+
</Pressable>
99105
)}
100106
showUnreadCountBadge
101107
Subtitle={isOnline ? undefined : NetworkDownIndicator}
@@ -135,7 +141,7 @@ export const ChannelScreen: React.FC<ChannelScreenProps> = ({
135141
if (!newChannel?.initialized) {
136142
await newChannel?.watch();
137143
}
138-
} catch(error) {
144+
} catch (error) {
139145
console.log('An error has occurred while watching the channel: ', error);
140146
}
141147
setChannel(newChannel);

0 commit comments

Comments
 (0)