Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLNP-6034] chore: enable overriding all chat options #222

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,8 @@ export const SendbirdUIKit = Object.freeze({
},
});

export type ChatOmittedInitParams = Omit<
SendbirdChatParams<[GroupChannelModule, OpenChannelModule]>,
(typeof chatOmitKeys)[number]
>;

const chatOmitKeys = [
'appId',
'newInstance',
'modules',
'debugMode',
'appVersion',
'localCacheEnabled',
'useAsyncStorageStore',
'useMMKVStorageStore',
] 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 ChatOmittedInitParams = SendbirdChatParams<[GroupChannelModule, OpenChannelModule]>;

export type SendbirdUIKitContainerProps = React.PropsWithChildren<{
appId: string;
platformServices: {
Expand Down Expand Up @@ -184,7 +166,7 @@ const SendbirdUIKitContainer = (props: SendbirdUIKitContainerProps) => {

const [internalStorage] = useState(() => new InternalLocalCacheStorage(chatOptions.localCacheStorage));
const [sdkInstance, setSdkInstance] = useState<SendbirdChatSDK>(() => {
const sendbird = initializeSendbird(appId, sanitizeChatOptions(chatOptions));
const sendbird = initializeSendbird(appId, chatOptions);
unsubscribes.current = sendbird.unsubscribes;
return sendbird.chatSDK;
});
Expand All @@ -196,7 +178,7 @@ const SendbirdUIKitContainer = (props: SendbirdUIKitContainerProps) => {

useLayoutEffect(() => {
if (!isFirstMount) {
const sendbird = initializeSendbird(appId, sanitizeChatOptions(chatOptions));
const sendbird = initializeSendbird(appId, chatOptions);
setSdkInstance(sendbird.chatSDK);
unsubscribes.current = sendbird.unsubscribes;
}
Expand Down Expand Up @@ -307,24 +289,24 @@ const SendbirdUIKitContainer = (props: SendbirdUIKitContainerProps) => {
);
};

interface InitOptions extends ChatOmittedInitParams {
type InitOptions = Partial<ChatOmittedInitParams> & {
localCacheStorage: LocalCacheStorage;
onInitialized?: (sdk: SendbirdChatSDK) => SendbirdChatSDK;
}
};
const initializeSendbird = (appId: string, options: InitOptions) => {
let chatSDK: SendbirdChatSDK;
const unsubscribes: Array<() => void> = [];
const { localCacheStorage, onInitialized, ...chatInitParams } = options;

const isMMKVStorage = 'getString' in localCacheStorage;
chatSDK = SendbirdChat.init({
...chatInitParams,
appId,
newInstance: true,
modules: [new GroupChannelModule(), new OpenChannelModule()],
localCacheEnabled: true,
useMMKVStorageStore: isMMKVStorage ? (localCacheStorage as MMKV) : undefined,
useAsyncStorageStore: !isMMKVStorage ? (localCacheStorage as AsyncStorageStatic) : undefined,
...chatInitParams,
});

if (onInitialized) {
Expand Down
Loading