Skip to content

Commit f3a3706

Browse files
authored
refactor: update useAsyncRequest, ChannelSettingsProvider (#983)
from #981
1 parent ddc4304 commit f3a3706

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/hooks/useAsyncRequest.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ export function useAsyncRequest<T>(request: Request<T>, options?: Options): Retu
2424

2525
const updateWithRequest = async () => {
2626
try {
27-
setState(({ ...draft }) => {
28-
if (options.resetResponseOnRefresh) draft.response = undefined;
29-
draft.error = undefined;
30-
return draft;
31-
});
32-
27+
setState((prev) => ({ loading: true, error: undefined, response: options?.resetResponseOnRefresh ? undefined : prev.response }));
3328
const response = await request();
3429
setState((prev) => ({ ...prev, response, loading: false }));
3530
} catch (error) {

src/modules/ChannelSettings/context/ChannelSettingsProvider.tsx

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,32 @@ interface ChannelSettingsProviderInterface {
4848
queries?: ChannelSettingsQueries;
4949
setChannelUpdateId(uniqId: string): void;
5050
forceUpdateUI(): void;
51-
channel: GroupChannel;
51+
channel: GroupChannel | null;
5252
loading: boolean;
5353
invalidChannel: boolean;
5454
}
5555

56-
const ChannelSettingsContext = React.createContext<ChannelSettingsProviderInterface | null>(undefined);
57-
58-
const ChannelSettingsProvider: React.FC<ChannelSettingsContextProps> = (props: ChannelSettingsContextProps) => {
59-
const {
60-
children,
61-
className,
62-
channelUrl,
63-
onCloseClick,
64-
onLeaveChannel,
65-
onChannelModified,
66-
overrideInviteUser,
67-
onBeforeUpdateChannel,
68-
queries,
69-
} = props;
70-
71-
// fetch store from <SendbirdProvider />
72-
const globalStore = useSendbirdStateContext();
73-
const { config, stores } = globalStore;
56+
const ChannelSettingsContext = React.createContext<ChannelSettingsProviderInterface | null>(null);
57+
58+
const ChannelSettingsProvider = ({
59+
children,
60+
className,
61+
channelUrl,
62+
onCloseClick,
63+
onLeaveChannel,
64+
onChannelModified,
65+
overrideInviteUser,
66+
onBeforeUpdateChannel,
67+
queries,
68+
renderUserProfile,
69+
disableUserProfile,
70+
}: ChannelSettingsContextProps) => {
71+
const { config, stores } = useSendbirdStateContext();
7472
const { sdkStore } = stores;
7573
const { logger, onUserProfileMessage } = config;
7674

7775
// hack to keep track of channel updates by triggering useEffect
78-
const [channelUpdateId, setChannelUpdateId] = useState(uuidv4());
76+
const [channelUpdateId, setChannelUpdateId] = useState(() => uuidv4());
7977
const forceUpdateUI = () => setChannelUpdateId(uuidv4());
8078

8179
const {
@@ -109,7 +107,7 @@ const ChannelSettingsProvider: React.FC<ChannelSettingsContextProps> = (props: C
109107

110108
useEffect(() => {
111109
refresh();
112-
}, [channelUrl, sdkStore.initialized, channelUpdateId]);
110+
}, [channelUrl, channelUpdateId]);
113111

114112
return (
115113
<ChannelSettingsContext.Provider
@@ -129,8 +127,8 @@ const ChannelSettingsProvider: React.FC<ChannelSettingsContextProps> = (props: C
129127
}}
130128
>
131129
<UserProfileProvider
132-
renderUserProfile={props?.renderUserProfile}
133-
disableUserProfile={props?.disableUserProfile ?? config?.disableUserProfile}
130+
renderUserProfile={renderUserProfile}
131+
disableUserProfile={disableUserProfile ?? config?.disableUserProfile}
134132
onUserProfileMessage={onUserProfileMessage}
135133
>
136134
<div className={`sendbird-channel-settings ${className}`}>{children}</div>
@@ -139,7 +137,5 @@ const ChannelSettingsProvider: React.FC<ChannelSettingsContextProps> = (props: C
139137
);
140138
};
141139

142-
export type UseChannelSettingsType = () => ChannelSettingsProviderInterface;
143-
const useChannelSettingsContext: UseChannelSettingsType = () => React.useContext(ChannelSettingsContext);
144-
140+
const useChannelSettingsContext = () => React.useContext(ChannelSettingsContext);
145141
export { ChannelSettingsProvider, useChannelSettingsContext };

0 commit comments

Comments
 (0)