Skip to content

Commit

Permalink
create new chat store when store key changes (#484)
Browse files Browse the repository at this point in the history
* create new chat store when store key changes

* up

* up
  • Loading branch information
elliotdauber authored Jan 8, 2025
1 parent e902618 commit 56d8859
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/react/src/chat/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,37 @@ export function ChatProvider(props: ChatProviderProps): JSX.Element {
headers,
} = props;

const store = useRef<ChatStore>(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 (
<ChatContext.Provider value={store.current}>
<ChatContext.Provider value={store.current?.store}>
{children}
</ChatContext.Provider>
);
Expand Down

0 comments on commit 56d8859

Please sign in to comment.