From 7742853837959884feb1e577e4682802486efb9f Mon Sep 17 00:00:00 2001 From: bang9 Date: Thu, 2 Nov 2023 16:23:59 +0900 Subject: [PATCH] feat: add chat init params to chatOptions --- .../src/containers/SendbirdUIKitContainer.tsx | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/packages/uikit-react-native/src/containers/SendbirdUIKitContainer.tsx b/packages/uikit-react-native/src/containers/SendbirdUIKitContainer.tsx index 831274301..668b3a6cd 100644 --- a/packages/uikit-react-native/src/containers/SendbirdUIKitContainer.tsx +++ b/packages/uikit-react-native/src/containers/SendbirdUIKitContainer.tsx @@ -2,7 +2,7 @@ import React, { useLayoutEffect, useMemo, useRef, useState } from 'react'; import { Platform } from 'react-native'; import { SafeAreaProvider } from 'react-native-safe-area-context'; -import Sendbird, { DeviceOsPlatform, SendbirdPlatform, SendbirdProduct } from '@sendbird/chat'; +import SendbirdChat, { DeviceOsPlatform, SendbirdChatParams, SendbirdPlatform, SendbirdProduct } from '@sendbird/chat'; import { GroupChannelModule } from '@sendbird/chat/groupChannel'; import { OpenChannelModule } from '@sendbird/chat/openChannel'; import type { HeaderStyleContextType, UIKitTheme } from '@sendbird/uikit-react-native-foundation'; @@ -18,7 +18,6 @@ import { SBUConfig, UIKitConfigProvider } from '@sendbird/uikit-tools'; import type { PartialDeep, SendbirdChatSDK, - SendbirdEncryption, SendbirdGroupChannel, SendbirdGroupChannelCreateParams, SendbirdMember, @@ -52,7 +51,6 @@ import VERSION from '../version'; import InternalErrorBoundaryContainer from './InternalErrorBoundaryContainer'; const NetInfo = SBUDynamicModule.get('@react-native-community/netinfo', 'warn'); -type UnimplementedFeatures = 'enableVoiceMessage' | 'threadReplySelectType' | 'replyType'; export const SendbirdUIKit = Object.freeze({ VERSION, PLATFORM: Platform.OS.toLowerCase(), @@ -63,6 +61,27 @@ export const SendbirdUIKit = Object.freeze({ }, }); +type UnimplementedFeatures = 'enableVoiceMessage' | 'threadReplySelectType' | 'replyType'; +export type ChatOmittedInitParams = Omit< + SendbirdChatParams<[GroupChannelModule, OpenChannelModule]>, + (typeof chatOmitKeys)[number] +>; + +const chatOmitKeys = [ + 'appId', + 'newInstance', + 'modules', + 'debugMode', + 'appVersion', + 'localCacheEnabled', + 'useAsyncStorageStore', +] as const; +function sanitizeChatOptions>(chatOptions: T): T { + const opts = { ...chatOptions }; + chatOmitKeys.forEach((key) => delete opts[key]); + return opts; +} + export type SendbirdUIKitContainerProps = React.PropsWithChildren<{ appId: string; platformServices: { @@ -73,9 +92,9 @@ export type SendbirdUIKitContainerProps = React.PropsWithChildren<{ }; chatOptions: { localCacheStorage: LocalCacheStorage; - localCacheEncryption?: SendbirdEncryption; onInitialized?: (sdkInstance: SendbirdChatSDK) => SendbirdChatSDK; - } & Partial; + } & Partial & + Partial; uikitOptions?: PartialDeep<{ common: SBUConfig['common']; groupChannel: Omit & { @@ -143,7 +162,7 @@ const SendbirdUIKitContainer = ({ const [internalStorage] = useState(() => new InternalLocalCacheStorage(chatOptions.localCacheStorage)); const [sdkInstance, setSdkInstance] = useState(() => { - const sendbird = initializeSendbird(appId, { internalStorage, ...chatOptions }); + const sendbird = initializeSendbird(appId, { internalStorage, ...sanitizeChatOptions(chatOptions) }); unsubscribes.current = sendbird.unsubscribes; return sendbird.chatSDK; }); @@ -171,7 +190,7 @@ const SendbirdUIKitContainer = ({ useLayoutEffect(() => { if (!isFirstMount) { - const sendbird = initializeSendbird(appId, { internalStorage, ...chatOptions }); + const sendbird = initializeSendbird(appId, { internalStorage, ...sanitizeChatOptions(chatOptions) }); setSdkInstance(sendbird.chatSDK); unsubscribes.current = sendbird.unsubscribes; } @@ -270,25 +289,22 @@ const SendbirdUIKitContainer = ({ ); }; -const initializeSendbird = ( - appId: string, - options: { - internalStorage?: InternalLocalCacheStorage; - onInitialized?: (sdk: SendbirdChatSDK) => SendbirdChatSDK; - localCacheEncryption?: SendbirdEncryption; - }, -) => { +interface InitOptions extends ChatOmittedInitParams { + internalStorage: InternalLocalCacheStorage; + onInitialized?: (sdk: SendbirdChatSDK) => SendbirdChatSDK; +} +const initializeSendbird = (appId: string, options: InitOptions) => { let chatSDK: SendbirdChatSDK; const unsubscribes: Array<() => void> = []; - const { internalStorage, localCacheEncryption, onInitialized } = options; + const { internalStorage, onInitialized, ...chatInitParams } = options; - chatSDK = Sendbird.init({ + chatSDK = SendbirdChat.init({ + ...chatInitParams, appId, newInstance: true, modules: [new GroupChannelModule(), new OpenChannelModule()], - localCacheEnabled: Boolean(internalStorage), + localCacheEnabled: true, useAsyncStorageStore: internalStorage as never, - localCacheEncryption, }); if (onInitialized) {