fix(core): optimize request chat cards fn (#7331)

This commit is contained in:
Fangdun Tsai
2024-06-26 23:50:45 +08:00
committed by GitHub
parent dcf766f0ee
commit a5662e3de2
4 changed files with 17 additions and 24 deletions
@@ -18,10 +18,6 @@ export interface AIChatParams {
autoSelect?: boolean;
}
type RequestChatCardsElement = (
chatPanel: HTMLElement
) => Promise<ChatCards | null>;
export type ActionEventType =
| 'started'
| 'finished'
@@ -76,8 +72,17 @@ export class AIProvider {
return AIProvider.instance.toggleGeneralAIOnboarding;
}
static get requestChatCardsElement() {
return AIProvider.instance.requestChatCardsElement;
static genRequestChatCardsFn(params: AIChatParams) {
return async (chatPanel: HTMLElement) => {
const chatCards: ChatCards | null = await new Promise(resolve =>
requestAnimationFrame(() =>
resolve(chatPanel.querySelector('chat-cards'))
)
);
if (!chatCards) return;
if (chatCards.temporaryParams) return;
chatCards.temporaryParams = params;
};
}
private static readonly instance = new AIProvider();
@@ -94,14 +99,6 @@ export class AIProvider {
private toggleGeneralAIOnboarding: ((value: boolean) => void) | null = null;
private readonly requestChatCardsElement: RequestChatCardsElement = (
chatPanel: HTMLElement
) => {
return new Promise(resolve => {
resolve(chatPanel.querySelector<ChatCards>('chat-cards'));
});
};
private readonly slots = {
// use case: when user selects "continue in chat" in an ask ai result panel
// do we need to pass the context to the chat panel?