Skip to content

Commit fb7d585

Browse files
committed
Refactor ChatProvider to support customizable platform options
- Introduced a default storage implementation for localStorage operations. - Updated the `useInitializeChat` hook to accept a platform parameter, allowing for more flexible configuration. - Modified the `ChatProvider` component to pass the platform options to the initialization hook, enhancing context management.
1 parent 177cdf4 commit fb7d585

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

react-web/core-integration/ChatProvider.tsx

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,36 @@ import { ComponentRegistry } from "./components";
66
import { TranslationKeysType } from "./locales/en.locale";
77
import { getStr, LangType } from "./locales";
88

9-
const platform: Platform = {
9+
const defaultStorage = {
10+
getItem: async (key: string) => localStorage.getItem(key),
11+
setItem: async (key: string, value: string) => localStorage.setItem(key, value),
12+
removeItem: async (key: string) => localStorage.removeItem(key),
13+
isAvailable: () => true
14+
};
15+
16+
const defaultPlatform: Platform = {
1017
env: {
1118
platform: 'web'
1219
},
13-
storage: {
14-
getItem: async (key: string) => {
15-
return localStorage.getItem(key);
16-
},
17-
setItem: async (key: string, value: string) => {
18-
localStorage.setItem(key, value);
19-
},
20-
removeItem: async (key: string) => {
21-
localStorage.removeItem(key);
22-
}
23-
},
20+
storage: defaultStorage,
21+
};
22+
23+
interface InitializeChatOptions {
24+
options: CoreOptions;
25+
platform?: Partial<Platform>;
2426
}
2527

26-
function useInitializeChat(options: CoreOptions) {
28+
function useInitializeChat({ options, platform: customPlatform }: InitializeChatOptions) {
2729
const context = useMemo(() => {
30+
const platform: Platform = {
31+
env: {
32+
platform: customPlatform?.env?.platform ?? defaultPlatform.env.platform
33+
},
34+
storage: customPlatform?.storage ?? defaultPlatform.storage,
35+
logger: customPlatform?.logger ?? defaultPlatform.logger,
36+
audio: customPlatform?.audio ?? defaultPlatform.audio
37+
};
38+
2839
const config = createConfig(options, platform);
2940
const api = new ApiCaller({
3041
config: config.getConfig(),
@@ -49,7 +60,7 @@ function useInitializeChat(options: CoreOptions) {
4960
config,
5061
platform,
5162
};
52-
}, [options]);
63+
}, [options, customPlatform]);
5364

5465
useEffect(() => {
5566
return () => {
@@ -73,11 +84,15 @@ interface ChatProviderValue extends ReturnType<typeof useInitializeChat> {
7384

7485
const [useChat, SafeProvider] = createSafeContext<ChatProviderValue>();
7586

76-
function ChatProvider({ options, children, components }: {
77-
options: CoreOptions, children: React.ReactNode,
87+
interface ChatProviderProps {
88+
options: CoreOptions;
89+
children: React.ReactNode;
7890
components?: ComponentType[];
79-
}) {
80-
const context = useInitializeChat(options);
91+
platform?: Partial<Platform>;
92+
}
93+
94+
function ChatProvider({ options, children, components, platform }: ChatProviderProps) {
95+
const context = useInitializeChat({ options, platform });
8196

8297
const componentStore = useMemo(
8398
() =>

0 commit comments

Comments
 (0)