Skip to content

Commit

Permalink
feat(frontend): support tracking chat's creation for widget (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
634750802 authored Jan 15, 2025
1 parent ab557b2 commit f70022e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion frontend/app/src/components/chat/chat-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const ChatsContext = createContext<ChatsProviderValues>({

const ChatControllerContext = createContext<ChatController | null>(null);

export function ChatsProvider ({ children }: { children: ReactNode }) {
export function ChatsProvider ({ onChatCreated, children }: { children: ReactNode, onChatCreated?: (id: string, chat: Chat, controller: ChatController) => void }) {
const bootstrapStatusRef = useLatestRef(useBootstrapStatus());
const [chats, setChats] = useState(() => new Map<string, ChatController>);

Expand All @@ -42,6 +42,7 @@ export function ChatsProvider ({ children }: { children: ReactNode }) {
const controller = new ChatController(...args);
controller.once('created', (chat) => {
setChats(chats => new Map(chats).set(chat.id, controller));
onChatCreated?.(chat.id, chat, controller);
});

return controller;
Expand Down
8 changes: 7 additions & 1 deletion frontend/packages/widget-react/src/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ export const Widget = forwardRef<WidgetInstance, WidgetProps>(({ container, trig
<PortalProvider container={container}>
<BootstrapStatusProvider bootstrapStatus={bootstrapStatus}>
<ExperimentalFeaturesProvider features={experimentalFeatures}>
<ChatsProvider>
<ChatsProvider
onChatCreated={id => {
window.dispatchEvent(new CustomEvent('tidbainewchat', {
detail: { id },
}));
}}
>
<Dialog open={open} onOpenChange={(open) => {
setOpen(open);
if (!open) {
Expand Down

0 comments on commit f70022e

Please sign in to comment.