Skip to content

Commit 1b2ce8e

Browse files
authored
feat: add sdkInitParams prop (#692)
Addresses https://sendbird.atlassian.net/browse/AC-217 �Added a new prop `sdkInitParams` that allows passing custom parameters when `sdk.init(params)` is called from outside of UIKit. e.g. ``` <SendbirdProvider sdkInitParams={{ appStateToggleEnabled: false, debugMode: true, // more options can be found here https://sendbird.com/docs/chat/v4/javascript/ref/interfaces/_sendbird_chat.SendbirdChatParams.html }} /> ```
1 parent 7eb6aed commit 1b2ce8e

File tree

10 files changed

+51
-3
lines changed

10 files changed

+51
-3
lines changed

scripts/index_d_ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
declare module "SendbirdUIKitGlobal" {
77
import type React from 'react';
88
import type SendbirdChat from '@sendbird/chat';
9-
import { GroupChannelModule, ModuleNamespaces, OpenChannelModule } from '@sendbird/chat/lib/__definition';
9+
import { GroupChannelModule, ModuleNamespaces, OpenChannelModule, SendbirdChatParams, Module } from '@sendbird/chat/lib/__definition';
1010
import { SBUConfig } from '@sendbird/uikit-tools';
1111
import type {
1212
SendbirdError,
@@ -113,6 +113,7 @@ declare module "SendbirdUIKitGlobal" {
113113
isTypingIndicatorEnabledOnChannelList?: boolean;
114114
isMessageReceiptStatusEnabledOnChannelList?: boolean;
115115
uikitOptions?: UIKitOptions;
116+
sdkInitParams?: SendbirdChatParams<Module[]>;
116117
}
117118

118119
export type Logger = {
@@ -292,6 +293,7 @@ declare module "SendbirdUIKitGlobal" {
292293
isTypingIndicatorEnabledOnChannelList?: boolean;
293294
isMessageReceiptStatusEnabledOnChannelList?: boolean;
294295
uikitOptions?: UIKitOptions;
296+
sdkInitParams?: SendbirdChatParams<Module[]>;
295297
}
296298

297299
export interface SendBirdStateConfig {

src/lib/Sendbird.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { useMarkAsDeliveredScheduler } from './hooks/useMarkAsDeliveredScheduler
3535
import { getCaseResolvedReplyType, getCaseResolvedThreadReplySelectType } from './utils/resolvedReplyType';
3636
import { useUnmount } from '../hooks/useUnmount';
3737
import { disconnectSdk } from './hooks/useConnect/disconnectSdk';
38-
import { UIKitOptions, CommonUIKitConfigProps } from './types';
38+
import { UIKitOptions, CommonUIKitConfigProps, SendbirdChatInitParams } from './types';
3939

4040
export type UserListQueryType = {
4141
hasNext?: boolean;
@@ -87,6 +87,7 @@ export interface SendbirdProviderProps extends CommonUIKitConfigProps {
8787
onUserProfileMessage?: () => void;
8888
uikitOptions?: UIKitOptions;
8989
isUserIdUsedForNickname?: boolean;
90+
sdkInitParams?: SendbirdChatInitParams;
9091
}
9192

9293
function Sendbird(props: SendbirdProviderProps) {
@@ -148,6 +149,7 @@ const SendbirdSDK = ({
148149
onUserProfileMessage = null,
149150
breakpoint = false,
150151
isUserIdUsedForNickname = true,
152+
sdkInitParams,
151153
}: SendbirdProviderProps): React.ReactElement => {
152154
const {
153155
logLevel = '',
@@ -177,6 +179,7 @@ const SendbirdSDK = ({
177179
configureSession,
178180
customApiHost,
179181
customWebSocketHost,
182+
sdkInitParams,
180183
sdk,
181184
sdkDispatcher,
182185
userDispatcher,

src/lib/hooks/useConnect/__test__/setupConnection.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,23 @@ describe('useConnect/setupConnection/setUpParams', () => {
202202
});
203203
expect(newSdk).toEqual(mockSdk);
204204
});
205+
206+
it('should call init with sdkInitParams', async () => {
207+
const setUpConnectionProps = generateSetUpConnectionParams();
208+
const { appId, sdkInitParams } = setUpConnectionProps;
209+
const newSdk = setUpParams({ appId, sdkInitParams });
210+
// @ts-ignore
211+
expect(require('@sendbird/chat').init).toBeCalledWith({
212+
appId,
213+
newInstance: true,
214+
modules: [
215+
// @ts-ignore
216+
new (require('@sendbird/chat/groupChannel').GroupChannelModule)(),
217+
// @ts-ignore
218+
new (require('@sendbird/chat/openChannel').OpenChannelModule)(),
219+
],
220+
sdkInitParams,
221+
});
222+
expect(newSdk).toEqual(mockSdk);
223+
});
205224
});

src/lib/hooks/useConnect/connect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export async function connect({
1616
profileUrl,
1717
accessToken,
1818
sdk,
19+
sdkInitParams,
1920
}: ConnectTypes): Promise<void> {
2021
await disconnectSdk({
2122
logger,
@@ -36,5 +37,6 @@ export async function connect({
3637
nickname,
3738
profileUrl,
3839
accessToken,
40+
sdkInitParams,
3941
});
4042
}

src/lib/hooks/useConnect/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function useConnect(triggerTypes: TriggerTypes, staticTypes: Stat
1616
sdkDispatcher,
1717
userDispatcher,
1818
initDashboardConfigs,
19+
sdkInitParams,
1920
} = staticTypes;
2021
logger?.info?.('SendbirdProvider | useConnect', { ...triggerTypes, ...staticTypes });
2122

@@ -37,6 +38,7 @@ export default function useConnect(triggerTypes: TriggerTypes, staticTypes: Stat
3738
userDispatcher,
3839
initDashboardConfigs,
3940
isUserIdUsedForNickname,
41+
sdkInitParams,
4042
});
4143
} catch (error) {
4244
logger?.error?.('SendbirdProvider | useConnect/useEffect', error);
@@ -61,6 +63,7 @@ export default function useConnect(triggerTypes: TriggerTypes, staticTypes: Stat
6163
userDispatcher,
6264
initDashboardConfigs,
6365
isUserIdUsedForNickname,
66+
sdkInitParams,
6467
});
6568
} catch (error) {
6669
logger?.error?.('SendbirdProvider | useConnect/reconnect/useCallback', error);

src/lib/hooks/useConnect/setupConnection.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { USER_ACTIONS } from '../../dux/user/actionTypes';
88
import { isTextuallyNull } from '../../../utils';
99

1010
import { SetupConnectionTypes } from './types';
11+
import { SendbirdChatInitParams } from '../../types';
1112

1213
const APP_VERSION_STRING = '__react_dev_mode__';
1314

@@ -25,10 +26,12 @@ export function setUpParams({
2526
appId,
2627
customApiHost,
2728
customWebSocketHost,
29+
sdkInitParams,
2830
}: {
2931
appId: string;
3032
customApiHost?: string;
3133
customWebSocketHost?: string;
34+
sdkInitParams?: SendbirdChatInitParams;
3235
}): SendbirdChat {
3336
const params = {
3437
appId,
@@ -37,6 +40,7 @@ export function setUpParams({
3740
new OpenChannelModule(),
3841
],
3942
newInstance: true,
43+
...sdkInitParams,
4044
};
4145
if (customApiHost) {
4246
params['customApiHost'] = customApiHost;
@@ -74,6 +78,7 @@ export async function setUpConnection({
7478
profileUrl,
7579
accessToken,
7680
isUserIdUsedForNickname,
81+
sdkInitParams,
7782
}: SetupConnectionTypes): Promise<void> {
7883
return new Promise((resolve, reject) => {
7984
logger?.info?.('SendbirdProvider | useConnect/setupConnection/init', { userId, appId });
@@ -84,6 +89,7 @@ export async function setUpConnection({
8489
appId,
8590
customApiHost,
8691
customWebSocketHost,
92+
sdkInitParams,
8793
});
8894

8995
if (configureSession && typeof configureSession === 'function') {

src/lib/hooks/useConnect/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { UserActionTypes } from '../../dux/user/actionTypes';
77

88
import { Logger } from '../../SendbirdState';
99

10+
import { SendbirdChatInitParams } from '../../types';
11+
1012
type SdkDispatcher = React.Dispatch<SdkActionTypes>;
1113
type UserDispatcher = React.Dispatch<UserActionTypes>;
1214

@@ -31,6 +33,7 @@ export type StaticTypes = {
3133
sdkDispatcher: SdkDispatcher;
3234
userDispatcher: UserDispatcher;
3335
initDashboardConfigs: (sdk: SendbirdChat) => Promise<void>;
36+
sdkInitParams?: SendbirdChatInitParams;
3437
};
3538

3639
export type ConnectTypes = TriggerTypes & StaticTypes;

src/lib/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type SendbirdChat from '@sendbird/chat';
2-
import type { User } from '@sendbird/chat';
2+
import type { User, SendbirdChatParams } from '@sendbird/chat';
33
import type {
44
GroupChannel,
55
GroupChannelCreateParams,
@@ -29,6 +29,8 @@ import { PartialDeep } from '../utils/typeHelpers/partialDeep';
2929

3030
import { SBUConfig } from '@sendbird/uikit-tools';
3131

32+
import { Module } from '@sendbird/chat/lib/__definition';
33+
3234
// note to SDK team:
3335
// using enum inside .d.ts won’t work for jest, but const enum will work.
3436
export const Role = {
@@ -244,3 +246,5 @@ export type UIKitOptions = PartialDeep<{
244246
groupChannelSettings: SBUConfig['groupChannel']['setting'];
245247
openChannel: SBUConfig['openChannel']['channel'];
246248
}>;
249+
250+
export type SendbirdChatInitParams = SendbirdChatParams<Module[]>;

src/modules/App/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function App(props) {
4747
isMessageReceiptStatusEnabledOnChannelList,
4848
uikitOptions,
4949
isUserIdUsedForNickname,
50+
sdkInitParams,
5051
} = props;
5152
const [currentChannel, setCurrentChannel] = useState(null);
5253
return (
@@ -82,6 +83,7 @@ export default function App(props) {
8283
showSearchIcon={showSearchIcon}
8384
uikitOptions={uikitOptions}
8485
isUserIdUsedForNickname={isUserIdUsedForNickname}
86+
sdkInitParams={sdkInitParams}
8587
>
8688
<AppLayout
8789
isReactionEnabled={isReactionEnabled}
@@ -154,6 +156,7 @@ App.propTypes = {
154156
isTypingIndicatorEnabledOnChannelList: PropTypes.bool,
155157
isMessageReceiptStatusEnabledOnChannelList: PropTypes.bool,
156158
isUserIdUsedForNickname: PropTypes.bool,
159+
sdkInitParams: PropTypes.shape({}),
157160
};
158161

159162
App.defaultProps = {
@@ -194,4 +197,5 @@ App.defaultProps = {
194197
isTypingIndicatorEnabledOnChannelList: undefined,
195198
isMessageReceiptStatusEnabledOnChannelList: undefined,
196199
isUserIdUsedForNickname: true,
200+
sdkInitParams: undefined,
197201
};

src/modules/App/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
UserListQuery,
1010
RenderUserProfileProps,
1111
SendBirdProviderConfig,
12+
SendbirdChatInitParams,
1213
} from '../../types';
1314

1415
export interface AppLayoutProps {
@@ -75,4 +76,5 @@ export default interface AppProps {
7576
disableAutoSelect?: boolean;
7677
isTypingIndicatorEnabledOnChannelList?: boolean;
7778
isMessageReceiptStatusEnabledOnChannelList?: boolean;
79+
sdkInitParams?: SendbirdChatInitParams;
7880
}

0 commit comments

Comments
 (0)