fix(core): optimize request chat cards fn (#7331)
This commit is contained in:
@@ -18,10 +18,6 @@ export interface AIChatParams {
|
|||||||
autoSelect?: boolean;
|
autoSelect?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestChatCardsElement = (
|
|
||||||
chatPanel: HTMLElement
|
|
||||||
) => Promise<ChatCards | null>;
|
|
||||||
|
|
||||||
export type ActionEventType =
|
export type ActionEventType =
|
||||||
| 'started'
|
| 'started'
|
||||||
| 'finished'
|
| 'finished'
|
||||||
@@ -76,8 +72,17 @@ export class AIProvider {
|
|||||||
return AIProvider.instance.toggleGeneralAIOnboarding;
|
return AIProvider.instance.toggleGeneralAIOnboarding;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get requestChatCardsElement() {
|
static genRequestChatCardsFn(params: AIChatParams) {
|
||||||
return AIProvider.instance.requestChatCardsElement;
|
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();
|
private static readonly instance = new AIProvider();
|
||||||
@@ -94,14 +99,6 @@ export class AIProvider {
|
|||||||
|
|
||||||
private toggleGeneralAIOnboarding: ((value: boolean) => void) | null = null;
|
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 = {
|
private readonly slots = {
|
||||||
// use case: when user selects "continue in chat" in an ask ai result panel
|
// 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?
|
// do we need to pass the context to the chat panel?
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export type { SidebarTabName } from './multi-tabs/sidebar-tab';
|
export type { SidebarTabName } from './multi-tabs/sidebar-tab';
|
||||||
export { sidebarTabs } from './multi-tabs/sidebar-tabs';
|
export { sidebarTabs, type TabOnLoadFn } from './multi-tabs/sidebar-tabs';
|
||||||
export { MultiTabSidebarBody } from './view/body';
|
export { MultiTabSidebarBody } from './view/body';
|
||||||
export { MultiTabSidebarHeaderSwitcher } from './view/header-switcher';
|
export { MultiTabSidebarHeaderSwitcher } from './view/header-switcher';
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { framePanelTab } from './tabs/frame';
|
|||||||
import { journalTab } from './tabs/journal';
|
import { journalTab } from './tabs/journal';
|
||||||
import { outlineTab } from './tabs/outline';
|
import { outlineTab } from './tabs/outline';
|
||||||
|
|
||||||
|
export type TabOnLoadFn = (component: HTMLElement) => void;
|
||||||
|
|
||||||
// the list of all possible tabs in affine.
|
// the list of all possible tabs in affine.
|
||||||
// order matters (determines the order of the tabs)
|
// order matters (determines the order of the tabs)
|
||||||
export const sidebarTabs: SidebarTab[] = [
|
export const sidebarTabs: SidebarTab[] = [
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import {
|
|||||||
MultiTabSidebarBody,
|
MultiTabSidebarBody,
|
||||||
MultiTabSidebarHeaderSwitcher,
|
MultiTabSidebarHeaderSwitcher,
|
||||||
sidebarTabs,
|
sidebarTabs,
|
||||||
|
type TabOnLoadFn,
|
||||||
} from '../../../modules/multi-tab-sidebar';
|
} from '../../../modules/multi-tab-sidebar';
|
||||||
import {
|
import {
|
||||||
RightSidebarService,
|
RightSidebarService,
|
||||||
@@ -74,9 +75,7 @@ const DetailPageImpl = memo(function DetailPageImpl() {
|
|||||||
const docCollection = workspace.docCollection;
|
const docCollection = workspace.docCollection;
|
||||||
const mode = useLiveData(doc.mode$);
|
const mode = useLiveData(doc.mode$);
|
||||||
const { appSettings } = useAppSettingHelper();
|
const { appSettings } = useAppSettingHelper();
|
||||||
const [tabOnLoad, setTabOnLoad] = useState<
|
const [tabOnLoad, setTabOnLoad] = useState<TabOnLoadFn | null>(null);
|
||||||
((component: HTMLElement) => void) | null
|
|
||||||
>(null);
|
|
||||||
|
|
||||||
const isActiveView = useIsActiveView();
|
const isActiveView = useIsActiveView();
|
||||||
// TODO(@eyhn): remove jotai here
|
// TODO(@eyhn): remove jotai here
|
||||||
@@ -110,12 +109,7 @@ const DetailPageImpl = memo(function DetailPageImpl() {
|
|||||||
// * The right sidebar is not open
|
// * The right sidebar is not open
|
||||||
// * Chat panel is not activated
|
// * Chat panel is not activated
|
||||||
if (!opened || !actived) {
|
if (!opened || !actived) {
|
||||||
const callback = async (chatPanel: HTMLElement) => {
|
const callback = AIProvider.genRequestChatCardsFn(params);
|
||||||
const chatCards = await AIProvider.requestChatCardsElement(chatPanel);
|
|
||||||
if (!chatCards) return;
|
|
||||||
if (chatCards.temporaryParams) return;
|
|
||||||
chatCards.temporaryParams = params;
|
|
||||||
};
|
|
||||||
setTabOnLoad(() => callback);
|
setTabOnLoad(() => callback);
|
||||||
} else {
|
} else {
|
||||||
setTabOnLoad(null);
|
setTabOnLoad(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user