From 56d88598d2d085fd7506f8ccce0f06750b730cd5 Mon Sep 17 00:00:00 2001 From: Elliot Dauber <67391073+elliotdauber@users.noreply.github.com> Date: Wed, 8 Jan 2025 10:39:34 -0800 Subject: [PATCH] create new chat store when store key changes (#484) * create new chat store when store key changes * up * up --- packages/react/src/chat/provider.tsx | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/react/src/chat/provider.tsx b/packages/react/src/chat/provider.tsx index c96c216a..93c28c75 100644 --- a/packages/react/src/chat/provider.tsx +++ b/packages/react/src/chat/provider.tsx @@ -24,28 +24,37 @@ export function ChatProvider(props: ChatProviderProps): JSX.Element { headers, } = props; - const store = useRef(null); + const store = useRef<{ + storeKey: string | undefined; + store: ChatStore | null; + }>({ + storeKey: undefined, + store: null, + }); - if (!store.current) { - store.current = createChatStore({ - apiUrl, - headers, - projectKey, - chatOptions, - debug, - persistChatHistory: chatOptions?.history, + if (!store.current?.store || store.current?.storeKey !== storeKey) { + store.current = { storeKey, - }); + store: createChatStore({ + apiUrl, + headers, + projectKey, + chatOptions, + debug, + persistChatHistory: chatOptions?.history, + storeKey, + }), + }; } // update chat options when they change useEffect(() => { if (!chatOptions) return; - store.current?.getState().setOptions(chatOptions); + store.current?.store?.getState().setOptions(chatOptions); }, [chatOptions]); return ( - + {children} );