Skip to content

Commit 0451377

Browse files
authored
feat: Add a new props isUserIdUsedForNickname (#683)
* feat: Add isUserIdUsedForNickname to the public interface * feat: Apply isUserIdUsedForNickname into the connection logic * feat: Set userId to nickname when isUserIdUsedForNickname is true * chore: Fix mockSdk methods to able to connect with any userId string * chore: Add test cases for updating nickname with userId [UIKIT-2537](https://sendbird.atlassian.net/browse/UIKIT-2537)
1 parent 691c02f commit 0451377

File tree

8 files changed

+67
-6
lines changed

8 files changed

+67
-6
lines changed

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ interface SendBirdProviderProps {
125125
isTypingIndicatorEnabledOnChannelList?: boolean;
126126
isMessageReceiptStatusEnabledOnChannelList?: boolean;
127127
uikitOptions?: UIKitOptions;
128+
isUserIdUsedForNickname?: boolean;
128129
}
129130

130131
interface SendBirdStateConfig {
@@ -312,6 +313,7 @@ interface AppProps {
312313
isTypingIndicatorEnabledOnChannelList?: boolean;
313314
isMessageReceiptStatusEnabledOnChannelList?: boolean;
314315
uikitOptions?: UIKitOptions;
316+
isUserIdUsedForNickname?: boolean;
315317
}
316318

317319
interface ApplicationUserListQuery {

src/lib/Sendbird.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export interface SendbirdProviderProps extends CommonUIKitConfigProps {
8686
renderUserProfile?: () => React.ReactElement;
8787
onUserProfileMessage?: () => void;
8888
uikitOptions?: UIKitOptions;
89+
isUserIdUsedForNickname?: boolean;
8990
}
9091

9192
function Sendbird(props: SendbirdProviderProps) {
@@ -146,6 +147,7 @@ const SendbirdSDK = ({
146147
renderUserProfile = null,
147148
onUserProfileMessage = null,
148149
breakpoint = false,
150+
isUserIdUsedForNickname = true,
149151
}: SendbirdProviderProps): React.ReactElement => {
150152
const {
151153
logLevel = '',
@@ -167,6 +169,7 @@ const SendbirdSDK = ({
167169
appId,
168170
userId,
169171
accessToken,
172+
isUserIdUsedForNickname,
170173
}, {
171174
logger,
172175
nickname,

src/lib/hooks/useConnect/__test__/data.mocks.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ export const mockSdk = {
2222
if (userId === mockUser.userId) {
2323
return Promise.resolve(mockUser);
2424
}
25+
if (userId?.length > 0) {
26+
return Promise.resolve({ userId: userId });
27+
}
2528
return Promise.reject();
2629
}),
2730
disconnect: jest.fn().mockImplementation(() => Promise.resolve(true)),
28-
updateCurrentUserInfo: jest.fn().mockImplementation(() => Promise.resolve(mockUser)),
31+
updateCurrentUserInfo: jest.fn().mockImplementation((user) => Promise.resolve(user)),
2932
setSessionHandler: jest.fn(),
3033
addExtension: jest.fn(),
3134
getUIKitConfiguration: jest.fn().mockImplementation(() => Promise.resolve({})),

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

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable global-require */
33
import { SDK_ACTIONS } from '../../../dux/sdk/actionTypes';
44
import { USER_ACTIONS } from '../../../dux/user/actionTypes';
5-
import { getConnectSbError, getMissingParamError, setUpConnection, setUpParams } from '../setupConnection';
5+
import { getMissingParamError, setUpConnection, setUpParams } from '../setupConnection';
66
import { SetupConnectionTypes } from '../types';
77
import { generateSetUpConnectionParams, mockSdk, mockUser, mockUser2 } from './data.mocks';
88

@@ -44,6 +44,48 @@ describe('useConnect/setupConnection', () => {
4444
expect(setUpConnectionProps.sdkDispatcher).toBeCalledWith({ type: SDK_ACTIONS.SDK_ERROR });
4545
});
4646

47+
it('should replace nickname with userId when isUserIdUsedForNickname is true', async () => {
48+
const newUser = {
49+
userId: 'new-userid',
50+
nickname: '',
51+
profileUrl: 'new-user-profile-url',
52+
};
53+
const setUpConnectionProps = generateSetUpConnectionParams();
54+
await setUpConnection({
55+
...setUpConnectionProps,
56+
...newUser,
57+
isUserIdUsedForNickname: true,
58+
});
59+
60+
const updatedUser = { nickname: newUser.userId, profileUrl: newUser.profileUrl };
61+
expect(mockSdk.updateCurrentUserInfo).toHaveBeenCalledWith(updatedUser);
62+
expect(setUpConnectionProps.userDispatcher).toHaveBeenCalledWith({
63+
type: USER_ACTIONS.UPDATE_USER_INFO,
64+
payload: updatedUser,
65+
});
66+
});
67+
68+
it('should not replace nickname with userId when isUserIdUsedForNickname is false', async () => {
69+
const newUser = {
70+
userId: 'new-userid',
71+
nickname: '',
72+
profileUrl: 'new-user-profile-url',
73+
};
74+
const setUpConnectionProps = generateSetUpConnectionParams();
75+
await setUpConnection({
76+
...setUpConnectionProps,
77+
...newUser,
78+
isUserIdUsedForNickname: false,
79+
});
80+
81+
const updatedUser = { nickname: '', profileUrl: newUser.profileUrl };
82+
expect(mockSdk.updateCurrentUserInfo).toHaveBeenCalledWith(updatedUser);
83+
expect(setUpConnectionProps.userDispatcher).toHaveBeenCalledWith({
84+
type: USER_ACTIONS.UPDATE_USER_INFO,
85+
payload: updatedUser,
86+
});
87+
});
88+
4789
it('should call setUpConnection when there is proper SDK', async () => {
4890
const setUpConnectionProps = generateSetUpConnectionParams();
4991
await setUpConnection(setUpConnectionProps);
@@ -107,8 +149,11 @@ describe('useConnect/setupConnection', () => {
107149

108150
it('should call connectCbError if connection fails', async () => {
109151
const setUpConnectionProps = generateSetUpConnectionParams();
110-
setUpConnectionProps.userId = 'unknown';
111-
const errorMessage = getConnectSbError();
152+
setUpConnectionProps.userId = '';
153+
const errorMessage = getMissingParamError({
154+
userId: '',
155+
appId: setUpConnectionProps.appId,
156+
});
112157

113158
await expect(setUpConnection(setUpConnectionProps))
114159
.rejects.toMatch(errorMessage);

src/lib/hooks/useConnect/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ReconnectType, StaticTypes, TriggerTypes } from './types';
44
import { connect } from './connect';
55

66
export default function useConnect(triggerTypes: TriggerTypes, staticTypes: StaticTypes): ReconnectType {
7-
const { userId, appId, accessToken } = triggerTypes;
7+
const { userId, appId, accessToken, isUserIdUsedForNickname } = triggerTypes;
88
const {
99
logger,
1010
nickname,
@@ -36,6 +36,7 @@ export default function useConnect(triggerTypes: TriggerTypes, staticTypes: Stat
3636
sdkDispatcher,
3737
userDispatcher,
3838
initDashboardConfigs,
39+
isUserIdUsedForNickname,
3940
});
4041
} catch (error) {
4142
logger?.error?.('SendbirdProvider | useConnect/useEffect', error);
@@ -59,6 +60,7 @@ export default function useConnect(triggerTypes: TriggerTypes, staticTypes: Stat
5960
sdkDispatcher,
6061
userDispatcher,
6162
initDashboardConfigs,
63+
isUserIdUsedForNickname,
6264
});
6365
} catch (error) {
6466
logger?.error?.('SendbirdProvider | useConnect/reconnect/useCallback', error);

src/lib/hooks/useConnect/setupConnection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export async function setUpConnection({
7373
nickname,
7474
profileUrl,
7575
accessToken,
76+
isUserIdUsedForNickname,
7677
}: SetupConnectionTypes): Promise<void> {
7778
return new Promise((resolve, reject) => {
7879
logger?.info?.('SendbirdProvider | useConnect/setupConnection/init', { userId, appId });
@@ -121,7 +122,7 @@ export async function setUpConnection({
121122
profileUrl,
122123
});
123124
newSdk.updateCurrentUserInfo({
124-
nickname: nickname || user.nickname,
125+
nickname: nickname || user.nickname || (isUserIdUsedForNickname ? user.userId : ''),
125126
profileUrl: profileUrl || user.profileUrl,
126127
}).then((namedUser) => {
127128
logger?.info?.('SendbirdProvider | useConnect/setupConnection/updateCurrentUserInfo success', {

src/lib/hooks/useConnect/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type TriggerTypes = {
1515
appId: string;
1616
// todo: doulbe check this type before merge
1717
accessToken?: string;
18+
isUserIdUsedForNickname?: boolean;
1819
};
1920

2021
export type ConfigureSessionTypes = (sdk: SendbirdChat | SendbirdGroupChat | SendbirdOpenChat) => SessionHandler;

src/modules/App/index.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default function App(props) {
4646
isTypingIndicatorEnabledOnChannelList,
4747
isMessageReceiptStatusEnabledOnChannelList,
4848
uikitOptions,
49+
isUserIdUsedForNickname,
4950
} = props;
5051
const [currentChannel, setCurrentChannel] = useState(null);
5152
return (
@@ -80,6 +81,7 @@ export default function App(props) {
8081
replyType={replyType}
8182
showSearchIcon={showSearchIcon}
8283
uikitOptions={uikitOptions}
84+
isUserIdUsedForNickname={isUserIdUsedForNickname}
8385
>
8486
<AppLayout
8587
isReactionEnabled={isReactionEnabled}
@@ -151,6 +153,7 @@ App.propTypes = {
151153
}),
152154
isTypingIndicatorEnabledOnChannelList: PropTypes.bool,
153155
isMessageReceiptStatusEnabledOnChannelList: PropTypes.bool,
156+
isUserIdUsedForNickname: PropTypes.bool,
154157
};
155158

156159
App.defaultProps = {
@@ -190,4 +193,5 @@ App.defaultProps = {
190193
isVoiceMessageEnabled: undefined,
191194
isTypingIndicatorEnabledOnChannelList: undefined,
192195
isMessageReceiptStatusEnabledOnChannelList: undefined,
196+
isUserIdUsedForNickname: true,
193197
};

0 commit comments

Comments
 (0)