Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotdauber committed Jan 8, 2025
1 parent ac88440 commit 9278611
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions packages/react/src/chat/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,37 @@ export function ChatProvider(props: ChatProviderProps): JSX.Element {
headers,
} = props;

const store = useRef<ChatStore>(null);

// biome-ignore lint/correctness/useExhaustiveDependencies: false positive
useEffect(() => {
store.current = createChatStore({
apiUrl,
headers,
projectKey,
chatOptions,
debug,
persistChatHistory: chatOptions?.history,
storeKey,
});
}, [storeKey]);

if (!store.current) {
store.current = createChatStore({
apiUrl,
headers,
projectKey,
chatOptions,
debug,
persistChatHistory: chatOptions?.history,
const store = useRef<{
storeKey: string | undefined;
store: ChatStore | null;
}>({
storeKey: undefined,
store: null,
});

if (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 9278611

Please sign in to comment.