Skip to content

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

src/modules/GroupChannel/context/GroupChannelProvider.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,7 @@ const GroupChannelManager :React.FC<React.PropsWithChildren<GroupChannelProvider
163163
onBackClick?.();
164164
},
165165
onChannelUpdated: (channel) => {
166-
// Note: this shouldn't happen ideally, but it happens on re-rendering GroupChannelManager
167-
// even though the next channel and the current channel are the same.
168-
// So added this condition to check if they are the same to prevent unnecessary calling setCurrentChannel action
169-
if (!state.currentChannel?.isEqual(channel)) {
170-
actions.setCurrentChannel(channel);
171-
}
166+
actions.setCurrentChannel(channel);
172167
},
173168
logger: logger as any,
174169
});

src/modules/GroupChannel/context/hooks/useGroupChannel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext, useMemo, useCallback } from 'react';
1+
import { useContext, useCallback, useMemo } from 'react';
22
import type { GroupChannel } from '@sendbird/chat/groupChannel';
33
import type { SendbirdError } from '@sendbird/chat';
44
import { useSyncExternalStore } from 'use-sync-external-store/shim';
@@ -165,7 +165,7 @@ export const useGroupChannel = () => {
165165
nicknamesMap: new Map(
166166
channel.members.map(({ userId, nickname }) => [userId, nickname]),
167167
),
168-
}));
168+
}), true);
169169
}, []);
170170

171171
const handleChannelError = useCallback((error: SendbirdError) => {

src/modules/GroupChannelList/context/GroupChannelListProvider.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
108108
}: GroupChannelListProviderProps) => {
109109
const { state: sendbirdState } = useSendbird();
110110
const { config, stores } = sendbirdState;
111-
const { state } = useGroupChannelList();
111+
const { state, actions } = useGroupChannelList();
112112
const { updateState } = useGroupChannelListStore();
113113
const { sdkStore } = stores;
114114

@@ -126,6 +126,9 @@ export const GroupChannelListManager: React.FC<GroupChannelListProviderProps> =
126126
if (url === selectedChannelUrl) onChannelSelect(null);
127127
});
128128
},
129+
onChannelsUpdated: () => {
130+
actions.setGroupChannels(groupChannels);
131+
},
129132
});
130133

131134
const { refreshing, initialized, groupChannels, refresh, loadMore } = channelListDataSource;

src/modules/GroupChannelList/context/useGroupChannelList.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { useSyncExternalStore } from 'use-sync-external-store/shim';
2-
import { useMemo, useContext } from 'react';
2+
import { useMemo, useContext, useCallback } from 'react';
33
import { GroupChannelListState, GroupChannelListContext } from './GroupChannelListProvider';
44

55
export const useGroupChannelList = () => {
66
const store = useContext(GroupChannelListContext);
77
if (!store) throw new Error('useGroupChannelList must be used within a GroupChannelListProvider');
88

9+
const setGroupChannels = useCallback((channels) => {
10+
store.setState(state => ({
11+
...state,
12+
groupChannels: channels,
13+
}), true);
14+
}, []);
15+
916
const state: GroupChannelListState = useSyncExternalStore(store.subscribe, store.getState);
1017
const actions = useMemo(() => ({
11-
}), [store]);
18+
setGroupChannels,
19+
}), [setGroupChannels]);
1220

1321
return { state, actions };
1422
};

src/utils/storeManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import isEqual from 'lodash/isEqual';
33
// Referrence: https://github.com/pmndrs/zustand
44
export type Store<T> = {
55
getState: () => T;
6-
setState: (partial: Partial<T> | ((state: T) => Partial<T>)) => void;
6+
setState: (partial: Partial<T> | ((state: T) => Partial<T>), force?: boolean) => void;
77
subscribe: (listener: () => void) => () => void;
88
};
99

@@ -30,7 +30,7 @@ export function createStore<T extends object>(initialState: T): Store<T> {
3030
const listeners = new Set<() => void>();
3131
let isUpdating = false;
3232

33-
const setState = (partial: Partial<T> | ((state: T) => Partial<T>)) => {
33+
const setState = (partial: Partial<T> | ((state: T) => Partial<T>), force?: boolean) => {
3434
// Prevent nested updates
3535
if (isUpdating) return;
3636

@@ -39,7 +39,7 @@ export function createStore<T extends object>(initialState: T): Store<T> {
3939
const nextState = typeof partial === 'function' ? partial(state) : partial;
4040
const hasChanged = hasStateChanged(state, nextState);
4141

42-
if (hasChanged) {
42+
if (force || hasChanged) {
4343
state = { ...state, ...nextState };
4444
listeners.forEach((listener) => listener());
4545
}

0 commit comments

Comments
 (0)