L-Sun
2025-01-21 16:00:49 +00:00
parent d4aeac5d35
commit 29995e498a
4 changed files with 102 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
import { handleInlineAskAIAction } from '@affine/core/blocksuite/presets/ai';
import { pageAIGroups } from '@affine/core/blocksuite/presets/ai/_common/config';
import { DocsService } from '@affine/core/modules/doc';
import { EditorService } from '@affine/core/modules/editor';
import { FeatureFlagService } from '@affine/core/modules/feature-flag';
@@ -5,6 +7,7 @@ import { TemplateDocService } from '@affine/core/modules/template-doc';
import { TemplateListMenu } from '@affine/core/modules/template-doc/view/template-list-menu';
import { useI18n } from '@affine/i18n';
import track from '@affine/track';
import { PageRootBlockComponent } from '@blocksuite/affine/blocks';
import type { Store } from '@blocksuite/affine/store';
import {
AiIcon,
@@ -62,6 +65,7 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
[doc.id, templateDocService.list]
)
);
const enableAI = useLiveData(featureFlagService.flags.enable_ai.$);
const enableTemplateDoc = useLiveData(
featureFlagService.flags.enable_template_doc.$
);
@@ -85,10 +89,29 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
setTemplateMenuOpen(open);
}, []);
const showAI = false;
const startWithAI = useCallback(() => {
const std = editorService.editor.editorContainer$.value?.std;
if (!std) return;
const rootBlockId = std.host.doc.root?.id;
if (!rootBlockId) return;
const rootComponent = std.view.getBlock(rootBlockId);
if (!(rootComponent instanceof PageRootBlockComponent)) return;
const { id, created } = rootComponent.focusFirstParagraph();
if (created) {
std.view.viewUpdated.once(v => {
if (v.id === id) handleInlineAskAIAction(std.host, pageAIGroups);
});
} else {
handleInlineAskAIAction(std.host, pageAIGroups);
}
}, [editorService.editor]);
const showTemplate = !isTemplate && enableTemplateDoc;
if (!showAI && !showTemplate) {
if (!enableAI && !showTemplate) {
return null;
}
@@ -96,10 +119,12 @@ const StarterBarNotEmpty = ({ doc }: { doc: Store }) => {
<div className={styles.root}>
{t['com.affine.page-starter-bar.start']()}
<ul className={styles.badges}>
{showAI ? (
{enableAI ? (
<Badge
data-testid="start-with-ai-badge"
icon={<AiIcon />}
text={t['com.affine.page-starter-bar.ai']()}
onClick={startWithAI}
/>
) : null}