Skip to content

Commit f61ace3

Browse files
HoonBaekAhyoungRyu
authored andcommitted
refactor: ChannelSettingsProvider migration (#1229)
[CLNP-5044](https://sendbird.atlassian.net/browse/CLNP-5044) ### Fix ChannelSettingsProvider migration - Created a hook `useChannelSettings` for replacing `useChannelSettingsContext` - Created hooks `useSetChannel` and `useChannelHandler` to separate the logics from the ChannelSettingsProvider
1 parent bec81a0 commit f61ace3

27 files changed

+392
-232
lines changed

rollup.module-exports.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default {
5757

5858
// ChannelSettings
5959
ChannelSettings: 'src/modules/ChannelSettings/index.tsx',
60-
'ChannelSettings/context': 'src/modules/ChannelSettings/context/ChannelSettingsProvider.tsx',
60+
'ChannelSettings/context': 'src/modules/ChannelSettings/context/index.tsx',
6161
'ChannelSettings/hooks/useMenuList': 'src/modules/ChannelSettings/components/ChannelSettingsUI/hooks/useMenuItems.tsx',
6262
'ChannelSettings/components/ChannelProfile': 'src/modules/ChannelSettings/components/ChannelProfile/index.tsx',
6363
'ChannelSettings/components/ChannelSettingsUI': 'src/modules/ChannelSettings/components/ChannelSettingsUI/index.tsx',

src/modules/ChannelSettings/components/ChannelProfile/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import './channel-profile.scss';
22
import React, { useState, useContext, useMemo } from 'react';
33

4-
import { LocalizationContext } from '../../../../lib/LocalizationContext';
4+
import useChannelSettings from '../../context/useChannelSettings';
55
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
6-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
6+
import { LocalizationContext } from '../../../../lib/LocalizationContext';
77

88
import ChannelAvatar from '../../../../ui/ChannelAvatar';
99
import TextButton from '../../../../ui/TextButton';
1010
import Label, {
1111
LabelTypography,
1212
LabelColors,
1313
} from '../../../../ui/Label';
14-
1514
import EditDetailsModal from '../EditDetailsModal';
1615

1716
const ChannelProfile: React.FC = () => {
1817
const state = useSendbirdStateContext();
19-
const channelSettingStore = useChannelSettingsContext();
18+
const { state: { channel } } = useChannelSettings();
2019
const { stringSet } = useContext(LocalizationContext);
2120
const [showModal, setShowModal] = useState(false);
2221

@@ -25,8 +24,6 @@ const ChannelProfile: React.FC = () => {
2524
const isOnline = state?.config?.isOnline;
2625
const disabled = !isOnline;
2726

28-
const channel = channelSettingStore?.channel;
29-
3027
const channelName = useMemo(() => {
3128
if (channel?.name && channel.name !== 'Group Channel') {
3229
return channel.name;

src/modules/ChannelSettings/components/ChannelSettingsUI/MenuListByRole.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import Icon from '../../../../ui/Icon';
88
import { isOperator } from '../../../Channel/context/utils';
99

1010
import MenuItem from './MenuItem';
11-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
1211
import useMenuItems from './hooks/useMenuItems';
12+
import useChannelSettings from '../../context/useChannelSettings';
1313

1414
interface MenuListByRoleProps {
1515
menuItems: ReturnType<typeof useMenuItems>;
1616
}
1717
export const MenuListByRole = ({
1818
menuItems,
1919
}: MenuListByRoleProps) => {
20-
const { channel } = useChannelSettingsContext();
20+
const { state: { channel } } = useChannelSettings();
2121
const menuItemsByRole = isOperator(channel) ? menuItems.operator : menuItems.nonOperator;
2222
// State to track the open accordion key
2323
const [openAccordionKey, setOpenAccordionKey] = useState<string | null>(null);

src/modules/ChannelSettings/components/ChannelSettingsUI/hooks/useMenuItems.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import { IconColors, IconTypes, IconProps } from '../../../../../ui/Icon';
1111
import Badge from '../../../../../ui/Badge';
1212
import { Toggle } from '../../../../../ui/Toggle';
1313
import { LabelColors, LabelTypography, type LabelProps } from '../../../../../ui/Label';
14-
import { useChannelSettingsContext } from '../../../context/ChannelSettingsProvider';
1514

1615
import { MenuItemAction, type MenuItemActionProps } from '../MenuItem';
16+
import useChannelSettings from '../../../context/useChannelSettings';
1717

1818
const kFormatter = (num: number): string | number => {
1919
return Math.abs(num) > 999
@@ -55,7 +55,7 @@ const commonLabelProps = {
5555
export const useMenuItems = (): MenuItems => {
5656
const [frozen, setFrozen] = useState(false);
5757
const { stringSet } = useContext(LocalizationContext);
58-
const { channel, renderUserListItem } = useChannelSettingsContext();
58+
const { state: { channel, renderUserListItem } } = useChannelSettings();
5959

6060
// work around for
6161
// https://sendbird.slack.com/archives/G01290GCDCN/p1595922832000900

src/modules/ChannelSettings/components/ChannelSettingsUI/index.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import './channel-settings-ui.scss';
22

3-
import React, { ReactNode, useContext, useState } from 'react';
3+
import React, { ReactNode, useState } from 'react';
44

55
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
6-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
6+
import useChannelSettings from '../../context/useChannelSettings';
7+
import { useLocalization } from '../../../../lib/LocalizationContext';
8+
import useMenuItems from './hooks/useMenuItems';
79

10+
import { deleteNullish, classnames } from '../../../../utils/utils';
811
import { ChannelSettingsHeader, ChannelSettingsHeaderProps } from './ChannelSettingsHeader';
912

1013
import PlaceHolder, { PlaceHolderTypes } from '../../../../ui/PlaceHolder';
1114
import Label, { LabelTypography, LabelColors } from '../../../../ui/Label';
12-
import { LocalizationContext } from '../../../../lib/LocalizationContext';
1315
import Icon, { IconTypes, IconColors } from '../../../../ui/Icon';
16+
import { UserListItemProps } from '../../../../ui/UserListItem';
17+
1418
import ChannelProfile from '../ChannelProfile';
1519
import LeaveChannelModal from '../LeaveChannel';
16-
import { deleteNullish, classnames } from '../../../../utils/utils';
1720
import MenuItem from './MenuItem';
1821
import MenuListByRole from './MenuListByRole';
19-
import useMenuItems from './hooks/useMenuItems';
20-
import { UserListItemProps } from '../../../../ui/UserListItem';
2122

2223
interface ModerationPanelProps {
2324
menuItems: ReturnType<typeof useMenuItems>;
@@ -37,7 +38,6 @@ export interface ChannelSettingsUIProps {
3738
}
3839

3940
const ChannelSettingsUI = (props: ChannelSettingsUIProps) => {
40-
const { channel, invalidChannel, onCloseClick, loading } = useChannelSettingsContext();
4141
const {
4242
renderHeader = (props: ChannelSettingsHeaderProps) => <ChannelSettingsHeader {...props} />,
4343
renderLeaveChannel,
@@ -46,14 +46,22 @@ const ChannelSettingsUI = (props: ChannelSettingsUIProps) => {
4646
renderPlaceholderError,
4747
renderPlaceholderLoading,
4848
} = deleteNullish(props);
49+
const {
50+
config: { isOnline },
51+
} = useSendbirdStateContext();
52+
const {
53+
state: {
54+
channel,
55+
invalidChannel,
56+
onCloseClick,
57+
loading,
58+
},
59+
} = useChannelSettings();
60+
const { stringSet } = useLocalization();
4961
const menuItems = useMenuItems();
5062

51-
const state = useSendbirdStateContext();
5263
const [showLeaveChannelModal, setShowLeaveChannelModal] = useState(false);
5364

54-
const isOnline = state?.config?.isOnline;
55-
const { stringSet } = useContext(LocalizationContext);
56-
5765
if (loading) {
5866
if (renderPlaceholderLoading) return renderPlaceholderLoading();
5967
return <PlaceHolder type={PlaceHolderTypes.LOADING} />;
@@ -120,6 +128,7 @@ const ChannelSettingsUI = (props: ChannelSettingsUIProps) => {
120128
};
121129

122130
export default ChannelSettingsUI;
131+
/** NOTE: For exportation */
123132
export { OperatorList } from '../ModerationPanel/OperatorList';
124133
export { MemberList } from '../ModerationPanel/MemberList';
125134
export { MutedMemberList } from '../ModerationPanel/MutedMemberList';

src/modules/ChannelSettings/components/EditDetailsModal/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useState, useRef, useContext } from 'react';
22

3-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
43
import { LocalizationContext } from '../../../../lib/LocalizationContext';
54
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
5+
import useChannelSettings from '../../context/useChannelSettings';
66

77
import Modal from '../../../../ui/Modal';
88
import Input, { InputLabel } from '../../../../ui/Input';
@@ -26,11 +26,13 @@ const EditDetails: React.FC<EditDetailsProps> = (props: EditDetailsProps) => {
2626
} = props;
2727

2828
const {
29-
channel,
30-
onChannelModified,
31-
onBeforeUpdateChannel,
32-
setChannelUpdateId,
33-
} = useChannelSettingsContext();
29+
state: {
30+
channel,
31+
onChannelModified,
32+
onBeforeUpdateChannel,
33+
setChannelUpdateId,
34+
},
35+
} = useChannelSettings();
3436
const title = channel?.name;
3537

3638
const state = useSendbirdStateContext();

src/modules/ChannelSettings/components/LeaveChannel/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import React from 'react';
44
import type { GroupChannel } from '@sendbird/chat/groupChannel';
55

66
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
7-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
87
import { noop } from '../../../../utils/utils';
98

109
import Modal from '../../../../ui/Modal';
@@ -15,6 +14,7 @@ import Label, {
1514
LabelTypography,
1615
LabelColors,
1716
} from '../../../../ui/Label';
17+
import useChannelSettings from '../../context/useChannelSettings';
1818

1919
export type LeaveChannelProps = {
2020
onSubmit: () => void;
@@ -27,7 +27,7 @@ const LeaveChannel: React.FC<LeaveChannelProps> = (props: LeaveChannelProps) =>
2727
onCancel = noop,
2828
} = props;
2929

30-
const { channel, onLeaveChannel } = useChannelSettingsContext();
30+
const { state: { channel, onLeaveChannel } } = useChannelSettings();
3131
const { stringSet } = useLocalization();
3232
const state = useSendbirdStateContext();
3333
const logger = state?.config?.logger;

src/modules/ChannelSettings/components/ModerationPanel/AddOperatorsModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import Label, {
1414
} from '../../../../ui/Label';
1515
import { ButtonTypes } from '../../../../ui/Button';
1616
import UserListItem, { UserListItemProps } from '../../../../ui/UserListItem';
17-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
1817
import { Member, MemberListQuery, OperatorFilter } from '@sendbird/chat/groupChannel';
1918
import { useOnScrollPositionChangeDetector } from '../../../../hooks/useOnScrollReachedEndDetector';
19+
import useChannelSettings from '../../context/useChannelSettings';
2020

2121
export interface AddOperatorsModalProps {
2222
onCancel(): void;
@@ -34,7 +34,7 @@ export default function AddOperatorsModal({
3434
const [memberQuery, setMemberQuery] = useState<MemberListQuery | null>(null);
3535
const { stringSet } = useContext(LocalizationContext);
3636

37-
const { channel } = useChannelSettingsContext();
37+
const { state: { channel } } = useChannelSettings();
3838

3939
useEffect(() => {
4040
const memberListQuery = channel?.createMemberListQuery({

src/modules/ChannelSettings/components/ModerationPanel/BannedUserList.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Label, {
1717

1818
import UserListItem, { UserListItemProps } from '../../../../ui/UserListItem';
1919
import BannedUsersModal from './BannedUsersModal';
20-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
2120
import { LocalizationContext } from '../../../../lib/LocalizationContext';
2221
import { UserListItemMenu } from '../../../../ui/UserListItemMenu';
22+
import useChannelSettings from '../../context/useChannelSettings';
2323

2424
interface BannedUserListProps {
2525
renderUserListItem?: (props: UserListItemProps) => ReactNode;
@@ -35,7 +35,7 @@ export const BannedUserList = ({
3535
const [showModal, setShowModal] = useState(false);
3636

3737
const { stringSet } = useContext(LocalizationContext);
38-
const { channel } = useChannelSettingsContext();
38+
const { state: { channel } } = useChannelSettings();
3939

4040
const refreshList = useCallback(() => {
4141
if (!channel) {

src/modules/ChannelSettings/components/ModerationPanel/BannedUsersModal.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import React, {
44
useEffect,
55
useState,
66
} from 'react';
7+
import { BannedUserListQuery, BannedUserListQueryParams, RestrictedUser } from '@sendbird/chat';
78

8-
import Modal from '../../../../ui/Modal';
9-
import UserListItem, { UserListItemProps } from '../../../../ui/UserListItem';
10-
11-
import { noop } from '../../../../utils/utils';
12-
import { useChannelSettingsContext } from '../../context/ChannelSettingsProvider';
9+
import useChannelSettings from '../../context/useChannelSettings';
1310
import { useLocalization } from '../../../../lib/LocalizationContext';
14-
import { BannedUserListQuery, BannedUserListQueryParams, RestrictedUser } from '@sendbird/chat';
1511
import { useOnScrollPositionChangeDetector } from '../../../../hooks/useOnScrollReachedEndDetector';
12+
import { noop } from '../../../../utils/utils';
13+
14+
import Modal from '../../../../ui/Modal';
15+
import UserListItem, { UserListItemProps } from '../../../../ui/UserListItem';
1616
import { UserListItemMenu } from '../../../../ui/UserListItemMenu';
1717

1818
export interface BannedUsersModalProps {
@@ -28,7 +28,7 @@ export function BannedUsersModal({
2828
}: BannedUsersModalProps): ReactElement {
2929
const [members, setMembers] = useState<RestrictedUser[]>([]);
3030
const [memberQuery, setMemberQuery] = useState<BannedUserListQuery | null>(null);
31-
const { channel } = useChannelSettingsContext();
31+
const { state: { channel } } = useChannelSettings();
3232
const { stringSet } = useLocalization();
3333

3434
useEffect(() => {

0 commit comments

Comments
 (0)