Skip to content

Commit 0ec9704

Browse files
authored
fix: Apply the channelListQuery.order to the ChannelList (#942)
[CLNP-2044](https://sendbird.atlassian.net/browse/CLNP-2044) ### Fix * Remove the action, `replace the channel to the top` in the ChannelList * Apply the channelListQuery.order to the ChannelList ### ChangeLog * Apply the channelListQuery.order to the ChannelList
1 parent 9fa0118 commit 0ec9704

File tree

7 files changed

+31
-173
lines changed

7 files changed

+31
-173
lines changed

src/modules/ChannelList/dux/__tests__/channelReplacedToTop.spec.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/modules/ChannelList/dux/actionTypes.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const ON_CHANNEL_UNFROZEN = 'ON_CHANNEL_UNFROZEN';
3131
export const ON_READ_RECEIPT_UPDATED = 'ON_READ_RECEIPT_UPDATED';
3232
export const ON_DELIVERY_RECEIPT_UPDATED = 'ON_DELIVERY_RECEIPT_UPDATED';
3333

34-
export const CHANNEL_REPLACED_TO_TOP = 'CHANNEL_REPLACED_TO_TOP';
3534
export const CHANNEL_LIST_PARAMS_UPDATED = 'CHANNEL_LIST_PARAMS_UPDATED';
3635

3736
type CHANNEL_LIST_PAYLOAD_TYPES = {
@@ -67,7 +66,6 @@ type CHANNEL_LIST_PAYLOAD_TYPES = {
6766
[ON_CHANNEL_UNFROZEN]: GroupChannel;
6867
[ON_READ_RECEIPT_UPDATED]: GroupChannel;
6968
[ON_DELIVERY_RECEIPT_UPDATED]: GroupChannel;
70-
[CHANNEL_REPLACED_TO_TOP]: GroupChannel;
7169
[CHANNEL_LIST_PARAMS_UPDATED]: {
7270
channelListQuery: GroupChannelListQuery;
7371
currentUserId?: string,

src/modules/ChannelList/dux/data.mock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,5 @@ export const creatingChannel = {
412412
members: users,
413413
lastMessage: { createdAt: 1004 },
414414
customType: '',
415+
createdAt: 0,
415416
};

src/modules/ChannelList/dux/reducers.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function channelListReducer(
4646
return {
4747
...state,
4848
currentChannel: channel,
49-
allChannels: getChannelsWithUpsertedChannel(allChannels, channel),
49+
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
5050
};
5151
}
5252
// Do not add to the ChannelList
@@ -72,7 +72,7 @@ export default function channelListReducer(
7272
// Good to [add to/keep in] the ChannelList
7373
return {
7474
...state,
75-
allChannels: getChannelsWithUpsertedChannel(allChannels, channel),
75+
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
7676
};
7777
}
7878
// * Remove the channel from the ChannelList: because the channel is filtered
@@ -113,7 +113,7 @@ export default function channelListReducer(
113113
if (channelListQuery) {
114114
if (filterChannelListParams(channelListQuery, channel, currentUserId)) {
115115
// Good to [add to/keep in] the ChannelList
116-
nextChannels = getChannelsWithUpsertedChannel(allChannels, channel);
116+
nextChannels = getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order);
117117
}
118118
}
119119
// Replace the currentChannel if I left the currentChannel
@@ -150,7 +150,7 @@ export default function channelListReducer(
150150
// Good to [add to/keep in] the ChannelList
151151
return {
152152
...state,
153-
allChannels: getChannelsWithUpsertedChannel(allChannels, channel),
153+
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
154154
};
155155
}
156156
// Filter the channel from the ChannelList
@@ -208,7 +208,7 @@ export default function channelListReducer(
208208
// Good to [add to/keep in] the ChannelList
209209
return {
210210
...state,
211-
allChannels: getChannelsWithUpsertedChannel(allChannels, channel),
211+
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
212212
};
213213
}
214214
// Filter the channel from the ChannelList
@@ -245,7 +245,7 @@ export default function channelListReducer(
245245
// Good to [add to/keep in] the ChannelList
246246
return {
247247
...state,
248-
allChannels: getChannelsWithUpsertedChannel(allChannels, channel),
248+
allChannels: getChannelsWithUpsertedChannel(allChannels, channel, state.channelListQuery?.order),
249249
};
250250
}
251251
// Filter the channel from the ChannelList
@@ -276,24 +276,6 @@ export default function channelListReducer(
276276
}),
277277
};
278278
})
279-
.with({ type: channelListActions.CHANNEL_REPLACED_TO_TOP }, (action) => {
280-
if (state.channelListQuery) {
281-
if (filterChannelListParams(state.channelListQuery, action.payload, state.currentUserId)) {
282-
return {
283-
...state,
284-
allChannels: [
285-
action.payload,
286-
...state.allChannels.filter((channel) => channel?.url !== action.payload.url),
287-
],
288-
};
289-
}
290-
return state;
291-
}
292-
return {
293-
...state,
294-
allChannels: [action.payload, ...state.allChannels.filter((channel) => channel?.url !== action.payload.url)],
295-
};
296-
})
297279
.with({ type: channelListActions.CHANNEL_LIST_PARAMS_UPDATED }, (action) => ({
298280
...state,
299281
channelListQuery: action.payload.channelListQuery,

src/modules/ChannelList/utils.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,6 @@ export const pubSubHandler = (pubSub: SBUGlobalPubSub, channelListDispatcher: Re
302302
}),
303303
);
304304

305-
subscriber.set(
306-
topics.SEND_MESSAGE_START,
307-
pubSub.subscribe(topics.SEND_MESSAGE_START, ({ channel }) => {
308-
if (channel.isGroupChannel()) {
309-
channelListDispatcher({
310-
type: channelActions.CHANNEL_REPLACED_TO_TOP,
311-
payload: channel,
312-
});
313-
}
314-
}),
315-
);
316-
317305
return subscriber;
318306
};
319307

src/utils/__tests__/utils.spec.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
binarySearch,
32
isAdminMessage,
43
isFileMessage,
54
isUrl,
@@ -9,24 +8,6 @@ import {
98
import { AdminMessage, FileMessage, MultipleFilesMessage, UserMessage } from '@sendbird/chat/message';
109
import { isMobileIOS } from '../utils';
1110

12-
describe('Global-utils', () => {
13-
it('should find right index with binarySearch', () => {
14-
const criterionArray = [99, 88, 77, 66, 55, 44, 33, 22, 11, 0];
15-
16-
const targetIndex1 = binarySearch(criterionArray, 100);
17-
expect(targetIndex1).toEqual(0);
18-
const targetIndex2 = binarySearch(criterionArray, 1);
19-
expect(targetIndex2).toEqual(criterionArray.length - 1);
20-
21-
criterionArray.forEach((value, index) => {
22-
const targetIndex = binarySearch(criterionArray, value);
23-
expect(targetIndex).toEqual(index);
24-
const targetIndexPlusOne = binarySearch(criterionArray, value + 1);
25-
expect(targetIndexPlusOne).toEqual(index);
26-
});
27-
});
28-
});
29-
3011
describe('Global-utils: verify message type util functions', () => {
3112
it('should return true for each message', () => {
3213
const mockUserMessage = {

src/utils/index.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import SendbirdChat, { Emoji, EmojiCategory, EmojiContainer, User } from '@sendbird/chat';
2-
import { GroupChannel, Member, SendbirdGroupChat, GroupChannelListQuery } from '@sendbird/chat/groupChannel';
2+
import { GroupChannel, Member, SendbirdGroupChat, GroupChannelListQuery, GroupChannelListOrder } from '@sendbird/chat/groupChannel';
33
import {
44
AdminMessage,
55
BaseMessage,
@@ -757,29 +757,31 @@ export const filterChannelListParams = (params: GroupChannelListQuery, channel:
757757
return true;
758758
};
759759

760-
export const binarySearch = (list: Array<number>, number: number): number => { // [100, 99, 98, 97, ...]
761-
const pivot = Math.floor(list.length / 2);
762-
if (list[pivot] === number) {
763-
return pivot;
764-
}
765-
const leftList = list.slice(0, pivot);
766-
const rightList = list.slice(pivot + 1, list.length);
767-
if (list[pivot] > number) {
768-
return pivot + 1 + (rightList.length === 0 ? 0 : binarySearch(rightList, number));
769-
} else {
770-
return (leftList.length === 0) ? pivot : binarySearch(leftList, number);
771-
}
772-
};
773760
// This is required when channel is displayed on channel list by filter
774-
export const getChannelsWithUpsertedChannel = (channels: Array<GroupChannel>, channel: GroupChannel): Array<GroupChannel> => {
775-
if (channels.some((ch: GroupChannel) => ch.url === channel?.url)) {
776-
return channels.map((ch: GroupChannel) => (ch.url === channel?.url ? channel : ch));
761+
export const getChannelsWithUpsertedChannel = (
762+
_channels: Array<GroupChannel>,
763+
channel: GroupChannel,
764+
order?: GroupChannelListOrder,
765+
): Array<GroupChannel> => {
766+
const compareFunc = match(order)
767+
.with(GroupChannelListOrder.CHANNEL_NAME_ALPHABETICAL, () => (
768+
(a: GroupChannel, b: GroupChannel) => a.name.localeCompare(b.name)
769+
))
770+
.with(GroupChannelListOrder.CHRONOLOGICAL, () => (
771+
(a: GroupChannel, b: GroupChannel) => b.createdAt - a.createdAt
772+
))
773+
.otherwise(() => (
774+
(a: GroupChannel, b: GroupChannel) => (b.lastMessage?.createdAt ?? Number.MIN_SAFE_INTEGER) - (a.lastMessage?.createdAt ?? Number.MIN_SAFE_INTEGER)
775+
));
776+
777+
const channels = [..._channels];
778+
const findingIndex = channels.findIndex((ch) => ch.url === channel.url);
779+
if (findingIndex !== -1) {
780+
channels[findingIndex] = channel;
781+
} else {
782+
channels.push(channel);
777783
}
778-
const targetIndex = binarySearch(
779-
channels.map((channel: GroupChannel) => channel?.lastMessage?.createdAt || channel?.createdAt),
780-
channel?.lastMessage?.createdAt || channel?.createdAt,
781-
);
782-
return [...channels.slice(0, targetIndex), channel, ...channels.slice(targetIndex, channels.length)];
784+
return channels.sort(compareFunc);
783785
};
784786

785787
export const getMatchedUserIds = (word: string, users: Array<User>, _template?: string): boolean => {

0 commit comments

Comments
 (0)