Skip to content

Commit 27ab445

Browse files
committed
fix: improve channel message new event
1 parent 64387d0 commit 27ab445

File tree

3 files changed

+82
-31
lines changed

3 files changed

+82
-31
lines changed

examples/SampleApp/src/components/ChannelInfoOverlay.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { useAppOverlayContext } from '../context/AppOverlayContext';
4343
import { useBottomSheetOverlayContext } from '../context/BottomSheetOverlayContext';
4444
import { useChannelInfoOverlayContext } from '../context/ChannelInfoOverlayContext';
4545
import { Archieve } from '../icons/Archieve';
46+
import { Pin } from '../icons/Pin';
4647

4748
dayjs.extend(relativeTime);
4849

@@ -371,6 +372,37 @@ export const ChannelInfoOverlay = (props: ChannelInfoOverlayProps) => {
371372
<Text style={[styles.rowText, { color: black }]}>View info</Text>
372373
</View>
373374
</TapGestureHandler>
375+
<Pressable
376+
onPress={async () => {
377+
try {
378+
if (membership?.pinned_at) {
379+
await channel.unpin();
380+
} else {
381+
await channel.pin();
382+
}
383+
} catch (error) {
384+
console.log('Error pinning/unpinning channel', error);
385+
}
386+
387+
setOverlay('none');
388+
}}
389+
>
390+
<View
391+
style={[
392+
styles.row,
393+
{
394+
borderTopColor: border,
395+
},
396+
]}
397+
>
398+
<View style={styles.rowInner}>
399+
<Pin height={24} width={24} />
400+
</View>
401+
<Text style={[styles.rowText, { color: black }]}>
402+
{membership?.pinned_at ? 'Unpin' : 'Pin'}
403+
</Text>
404+
</View>
405+
</Pressable>
374406
<Pressable
375407
onPress={async () => {
376408
try {

examples/SampleApp/src/components/ChannelPreview.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import React from 'react';
22
import { StyleSheet, View } from 'react-native';
33
import { useNavigation } from '@react-navigation/native';
44
import { RectButton } from 'react-native-gesture-handler';
5-
import Swipeable from 'react-native-gesture-handler/Swipeable';
5+
import Swipeable from 'react-native-gesture-handler/ReanimatedSwipeable';
66
import {
77
ChannelPreviewMessenger,
88
ChannelPreviewMessengerProps,
9+
ChannelPreviewStatus,
10+
ChannelPreviewStatusProps,
911
Delete,
1012
MenuPointHorizontal,
1113
useChannelMembershipState,
@@ -20,6 +22,7 @@ import { useChannelInfoOverlayContext } from '../context/ChannelInfoOverlayConte
2022
import type { StackNavigationProp } from '@react-navigation/stack';
2123

2224
import type { StackNavigatorParamList, StreamChatGenerics } from '../types';
25+
import { Pin } from '../icons/Pin';
2326

2427
const styles = StyleSheet.create({
2528
leftSwipeableButton: {
@@ -36,13 +39,35 @@ const styles = StyleSheet.create({
3639
alignItems: 'center',
3740
flexDirection: 'row',
3841
},
42+
statusContainer: {
43+
display: 'flex',
44+
flexDirection: 'row',
45+
},
46+
pinIconContainer: {
47+
marginLeft: 8,
48+
},
3949
});
4050

4151
type ChannelListScreenNavigationProp = StackNavigationProp<
4252
StackNavigatorParamList,
4353
'ChannelListScreen'
4454
>;
4555

56+
const CustomChannelPreviewStatus = (props: ChannelPreviewStatusProps) => {
57+
const membership = useChannelMembershipState(props.channel);
58+
59+
return (
60+
<View style={styles.statusContainer}>
61+
<ChannelPreviewStatus {...props} />
62+
{membership.pinned_at && (
63+
<View style={styles.pinIconContainer}>
64+
<Pin height={20} width={20} />
65+
</View>
66+
)}
67+
</View>
68+
);
69+
};
70+
4671
export const ChannelPreview: React.FC<ChannelPreviewMessengerProps<StreamChatGenerics>> = (
4772
props,
4873
) => {
@@ -107,7 +132,7 @@ export const ChannelPreview: React.FC<ChannelPreviewMessengerProps<StreamChatGen
107132
</View>
108133
)}
109134
>
110-
<ChannelPreviewMessenger {...props} />
135+
<ChannelPreviewMessenger {...props} PreviewStatus={CustomChannelPreviewStatus} />
111136
</Swipeable>
112137
);
113138
};

package/src/components/ChannelList/hooks/listeners/useNewMessage.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export const useNewMessage = <
4444
const targetChannelIndex = channels.findIndex((channel) => channel.cid === event.cid);
4545
const targetChannel = channels[targetChannelIndex];
4646

47-
const channelInList = targetChannelIndex >= 0;
48-
4947
const isTargetChannelArchived = isChannelArchived(targetChannel);
5048
const isTargetChannelPinned = isChannelPinned(targetChannel);
5149
const isArchivedFilterTrue = filters && filters.archived === true;
@@ -63,34 +61,30 @@ export const useNewMessage = <
6361
return channels;
6462
}
6563

66-
if (!channelInList) {
67-
if (event.channel_type && event.channel_id) {
68-
// If channel doesn't exist in existing list, check in activeChannels as well.
69-
// It may happen that channel was hidden using channel.hide(). In that case
70-
// We remove it from `channels` state, but its still being watched and exists in client.activeChannels.
71-
const channel = client.channel(event.channel_type, event.channel_id);
72-
// While adding new channels, we need to consider whether they are archived or not.
73-
if (
74-
// When archived filter false, and channel is archived
75-
(isChannelArchived(channel) && isArchivedFilterFalse) ||
76-
// When archived filter true, and channel is not archived
77-
(isArchivedFilterTrue && !isChannelArchived(channel))
78-
)
79-
return channels;
80-
return [channel, ...channels];
81-
}
82-
} else {
83-
if (event.cid) {
84-
return moveChannelUp<StreamChatGenerics>({
85-
channels,
86-
channelToMove: targetChannel,
87-
channelToMoveIndexWithinChannels: targetChannelIndex,
88-
sort,
89-
});
90-
}
91-
}
64+
// If channel doesn't exist in existing list, check in activeChannels as well.
65+
// It may happen that channel was hidden using channel.hide(). In that case
66+
// We remove it from `channels` state, but its still being watched and exists in client.activeChannels.
67+
const channelToMove =
68+
targetChannel ??
69+
(event.channel_type &&
70+
event.channel_id &&
71+
client.channel(event.channel_type, event.channel_id));
9272

93-
return [...channels];
73+
// While adding new channels, we need to consider whether they are archived or not.
74+
if (
75+
// When archived filter false, and channel is archived
76+
(isChannelArchived(channelToMove) && isArchivedFilterFalse) ||
77+
// When archived filter true, and channel is not archived
78+
(isArchivedFilterTrue && !isChannelArchived(channelToMove))
79+
) {
80+
return channels;
81+
}
82+
return moveChannelUp<StreamChatGenerics>({
83+
channels,
84+
channelToMove: channelToMove,
85+
channelToMoveIndexWithinChannels: targetChannelIndex,
86+
sort,
87+
});
9488
});
9589
}
9690
};

0 commit comments

Comments
 (0)