Skip to content

Commit 15eabe8

Browse files
authored
fix: Use await inside of the Component, not in global scope (#1113)
[CLNP-3433](https://sendbird.atlassian.net/browse/CLNP-3433) ### Issue ``` Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 2 overrides) ``` ![image](https://github.com/sendbird/sendbird-uikit-react/assets/46333979/5221f359-ea3b-4fa8-baab-1a7810e1e438) [CLNP-3433]: https://sendbird.atlassian.net/browse/CLNP-3433?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 081e6c0 commit 15eabe8

File tree

5 files changed

+65
-63
lines changed

5 files changed

+65
-63
lines changed

src/stories/common/getSampleChannel.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState } from "react";
2+
import SendbirdChat from "@sendbird/chat";
3+
import { GroupChannel, GroupChannelListOrder, GroupChannelModule } from "@sendbird/chat/groupChannel";
4+
import { OpenChannelModule } from "@sendbird/chat/openChannel";
5+
import { useAsyncEffect } from "@sendbird/uikit-tools";
6+
import { STORYBOOK_APP_ID, STORYBOOK_USER_ID } from '../common/const';
7+
8+
type UseSampleChannelParams = {
9+
appId?: string;
10+
userId?: string;
11+
}
12+
export const useSampleChannel = ({
13+
appId = STORYBOOK_APP_ID,
14+
userId = STORYBOOK_USER_ID,
15+
}: UseSampleChannelParams = {}): GroupChannel | null => {
16+
const [channel, setChannel] = useState<GroupChannel | null>(null);
17+
useAsyncEffect(async () => {
18+
try {
19+
const chat = SendbirdChat.init({
20+
appId,
21+
modules: [new GroupChannelModule, new OpenChannelModule],
22+
});
23+
24+
await chat.connect(userId);
25+
const query = chat.groupChannel.createMyGroupChannelListQuery({ limit: 1, order: GroupChannelListOrder.LATEST_LAST_MESSAGE });
26+
const channelList = await query.next();
27+
28+
if (channelList.length > 0) {
29+
setChannel(channelList[0]);
30+
} {
31+
const query = chat.createApplicationUserListQuery({ limit: 10 });
32+
const userList = await query.next();
33+
const newChannel = await chat.groupChannel.createChannel({ invitedUserIds: userList.map(user => user.userId) });
34+
setChannel(newChannel);
35+
}
36+
} catch (err) {
37+
console.warn('Sendbird storybook - useSampleChannel: ', err);
38+
}
39+
}, []);
40+
return channel;
41+
};

src/stories/modules/ChannelSettings.stories.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Meta } from '@storybook/react';
44
import SendbirdProvider from '../../lib/Sendbird';
55
import ChannelSettings from '../../modules/ChannelSettings';
66
import { STORYBOOK_APP_ID, STORYBOOK_USER_ID, STORYBOOK_NICKNAME } from '../common/const';
7-
import { getSampleChannel } from '../common/getSampleChannel';
7+
import { useSampleChannel } from '../common/useSampleChannel';
88

99
const meta: Meta<typeof ChannelSettings> = {
1010
title: '1.Module/ChannelSettings',
@@ -74,7 +74,9 @@ const meta: Meta<typeof ChannelSettings> = {
7474
};
7575
export default meta;
7676

77-
export const Default = (args): React.ReactElement => {
77+
export const Default = (): React.ReactElement => {
78+
const channel = useSampleChannel();
79+
7880
return (
7981
<div style={{ height: 500 }}>
8082
<SendbirdProvider
@@ -84,14 +86,10 @@ export const Default = (args): React.ReactElement => {
8486
breakpoint={/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)}
8587
>
8688
<ChannelSettings
87-
{...args}
89+
channelUrl={channel?.url ?? ''}
90+
disableUserProfile={false}
8891
/>
8992
</SendbirdProvider>
9093
</div>
9194
);
9295
};
93-
const channel = await getSampleChannel({ appId: STORYBOOK_APP_ID, userId: STORYBOOK_USER_ID });
94-
Default.args = {
95-
channelUrl: channel.url,
96-
disableUserProfile: false,
97-
};

src/stories/modules/GroupChannel.stories.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Meta } from '@storybook/react';
44
import SendbirdProvider from '../../lib/Sendbird';
55
import GroupChannel from '../../modules/GroupChannel';
66
import { STORYBOOK_APP_ID, STORYBOOK_USER_ID, STORYBOOK_NICKNAME } from '../common/const';
7-
import { getSampleChannel } from '../common/getSampleChannel';
7+
import { useSampleChannel } from '../common/useSampleChannel';
88

99
const meta: Meta<typeof GroupChannel> = {
1010
title: '1.Module/GroupChannel',
@@ -103,7 +103,9 @@ const meta: Meta<typeof GroupChannel> = {
103103
};
104104
export default meta;
105105

106-
export const Default = (args): React.ReactElement => {
106+
export const Default = (): React.ReactElement => {
107+
const channel = useSampleChannel();
108+
107109
return (
108110
<div style={{ height: 500 }}>
109111
<SendbirdProvider
@@ -113,20 +115,17 @@ export const Default = (args): React.ReactElement => {
113115
breakpoint={/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)}
114116
>
115117
<GroupChannel
116-
{...args}
118+
{...{
119+
channelUrl: channel?.url ?? '',
120+
isReactionEnabled: true,
121+
isMessageGroupingEnabled: true,
122+
showSearchIcon: true,
123+
replyType: 'THREAD',
124+
isMultipleFilesMessageEnabled: true,
125+
disableUserProfile: false,
126+
}}
117127
/>
118128
</SendbirdProvider>
119129
</div>
120130
);
121131
};
122-
const channel = await getSampleChannel({ appId: STORYBOOK_APP_ID, userId: STORYBOOK_USER_ID });
123-
Default.args = {
124-
channelUrl: channel.url,
125-
isReactionEnabled: true,
126-
isMessageGroupingEnabled: true,
127-
showSearchIcon: true,
128-
replyType: 'THREAD',
129-
threadReplySelectType: 'THREAD',
130-
isMultipleFilesMessageEnabled: true,
131-
disableUserProfile: false,
132-
};

src/stories/modules/MessageSearch.stories.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Meta } from '@storybook/react';
44
import SendbirdProvider from '../../lib/Sendbird';
55
import MessageSearch from '../../modules/MessageSearch';
66
import { STORYBOOK_APP_ID, STORYBOOK_USER_ID } from '../common/const';
7-
import { getSampleChannel } from '../common/getSampleChannel';
7+
import { useSampleChannel } from '../common/useSampleChannel';
88

99
const meta: Meta<typeof MessageSearch> = {
1010
title: '1.Module/MessageSearch',
@@ -61,20 +61,18 @@ const meta: Meta<typeof MessageSearch> = {
6161
};
6262
export default meta;
6363

64-
export const Default = (args): React.ReactElement => {
64+
export const Default = (): React.ReactElement => {
65+
const channel = useSampleChannel();
66+
6567
return (
6668
<div style={{ height: 520 }}>
6769
<SendbirdProvider
6870
appId={STORYBOOK_APP_ID}
6971
userId={STORYBOOK_USER_ID}
7072
breakpoint={/iPhone|iPad|iPod|Android/i.test(navigator.userAgent)}
7173
>
72-
<MessageSearch {...args} />
74+
<MessageSearch channelUrl={channel?.url ?? ''} />
7375
</SendbirdProvider>
7476
</div>
7577
);
7678
};
77-
const channel = await getSampleChannel({ appId: STORYBOOK_APP_ID, userId: STORYBOOK_USER_ID });
78-
Default.args = {
79-
channelUrl: channel.url,
80-
};

0 commit comments

Comments
 (0)