Skip to content

Commit

Permalink
feat: add chat init params to chatOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bang9 committed Nov 2, 2023
1 parent 0c49d69 commit 7742853
Showing 1 changed file with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -18,7 +18,6 @@ import { SBUConfig, UIKitConfigProvider } from '@sendbird/uikit-tools';
import type {
PartialDeep,
SendbirdChatSDK,
SendbirdEncryption,
SendbirdGroupChannel,
SendbirdGroupChannelCreateParams,
SendbirdMember,
Expand Down Expand Up @@ -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(),
Expand All @@ -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<T extends Record<string, unknown>>(chatOptions: T): T {
const opts = { ...chatOptions };
chatOmitKeys.forEach((key) => delete opts[key]);
return opts;
}

export type SendbirdUIKitContainerProps = React.PropsWithChildren<{
appId: string;
platformServices: {
Expand All @@ -73,9 +92,9 @@ export type SendbirdUIKitContainerProps = React.PropsWithChildren<{
};
chatOptions: {
localCacheStorage: LocalCacheStorage;
localCacheEncryption?: SendbirdEncryption;
onInitialized?: (sdkInstance: SendbirdChatSDK) => SendbirdChatSDK;
} & Partial<ChatRelatedFeaturesInUIKit>;
} & Partial<ChatOmittedInitParams> &
Partial<ChatRelatedFeaturesInUIKit>;
uikitOptions?: PartialDeep<{
common: SBUConfig['common'];
groupChannel: Omit<SBUConfig['groupChannel']['channel'], UnimplementedFeatures> & {
Expand Down Expand Up @@ -143,7 +162,7 @@ const SendbirdUIKitContainer = ({

const [internalStorage] = useState(() => new InternalLocalCacheStorage(chatOptions.localCacheStorage));
const [sdkInstance, setSdkInstance] = useState<SendbirdChatSDK>(() => {
const sendbird = initializeSendbird(appId, { internalStorage, ...chatOptions });
const sendbird = initializeSendbird(appId, { internalStorage, ...sanitizeChatOptions(chatOptions) });
unsubscribes.current = sendbird.unsubscribes;
return sendbird.chatSDK;
});
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 7742853

Please sign in to comment.