Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: center artifact card spinner and stabilize streamed char count (#20909)

YuTengjinge2dd39d7db6d
diff --git a/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/index.tsx b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/index.tsx
index dc349657aa0..9318e315f99 100644
--- a/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/index.tsx
+++ b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/index.tsx
@@ -11,6 +11,7 @@ import { dotLoading } from '@/styles/loading';
import { type MarkdownElementProps } from '../../type';
import ArtifactIcon from './Icon';
+import { useArtifactCodeLength } from './useArtifactCodeLength';
const styles = createStaticStyles(({ css, cssVar }) => ({
avatar: css`
@@ -61,7 +62,8 @@ const Render = memo<ArtifactProps>(({ identifier, title, type, language, childre
const isDarkMode = useIsDark();
const hasChildren = !!children;
- const str = ((children as string) || '').toString?.();
+
+ const codeLength = useArtifactCodeLength(id, identifier);
const [isGenerating, isArtifactTagClosed, openArtifact, closeArtifact] = useChatStore((s) => {
return [
@@ -80,7 +82,7 @@ const Render = memo<ArtifactProps>(({ identifier, title, type, language, childre
if (!hasChildren || !isGenerating) return;
openArtifactUI();
- }, [isGenerating, hasChildren, str, identifier, title, type, id, language]);
+ }, [isGenerating, hasChildren, codeLength, identifier, title, type, id, language]);
return (
<Flexbox
@@ -111,15 +113,11 @@ const Render = memo<ArtifactProps>(({ identifier, title, type, language, childre
<Flexbox className={cx(styles.title)}>{title || t('artifact.unknownTitle')}</Flexbox>
)}
{hasChildren && (
- <Flexbox horizontal className={styles.desc}>
+ <Flexbox horizontal align={'center'} className={styles.desc}>
{identifier} ·&nbsp;
- <Flexbox horizontal gap={2}>
- {!isArtifactTagClosed && (
- <div>
- <Spin size="small" />
- </div>
- )}
- {str?.length}
+ <Flexbox horizontal align={'center'} gap={4}>
+ {!isArtifactTagClosed && <Spin size="small" />}
+ {codeLength}
</Flexbox>
</Flexbox>
)}
diff --git a/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.test.ts b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.test.ts
new file mode 100644
index 00000000000..7ff5610a86d
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.test.ts
@@ -0,0 +1,40 @@
+import { renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { useChatStore } from '@/store/chat';
+
+import { useArtifactCodeLength } from './useArtifactCodeLength';
+
+const MESSAGE_ID = 'msg-artifact';
+
+const setMessageContent = (content: string) => {
+ useChatStore.setState({
+ dbMessagesMap: { test: [{ content, id: MESSAGE_ID, role: 'assistant' } as any] },
+ });
+};
+
+describe('useArtifactCodeLength', () => {
+ afterEach(() => {
+ useChatStore.setState({ dbMessagesMap: {} });
+ });
+
+ it('counts inline-markdown syntax that the rendered children lose', () => {
+ // Markdown would turn the paired backticks into inline code, which the rehype plugin drops
+ const code = 'const label = `count: ${n}`;';
+ setMessageContent(
+ `<lobeArtifact identifier="demo" type="text/html" title="Demo">${code}</lobeArtifact>`,
+ );
+
+ const { result } = renderHook(() => useArtifactCodeLength(MESSAGE_ID, 'demo'));
+
+ expect(result.current).toBe(code.length);
+ });
+
+ it('counts the streamed tail of an unclosed artifact', () => {
+ setMessageContent('<lobeArtifact identifier="demo" type="text/html" title="Demo"><svg><rect');
+
+ const { result } = renderHook(() => useArtifactCodeLength(MESSAGE_ID, 'demo'));
+
+ expect(result.current).toBe('<svg><rect'.length);
+ });
+});
diff --git a/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.ts b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.ts
new file mode 100644
index 00000000000..6ccc9792f9a
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/LobeArtifact/Render/useArtifactCodeLength.ts
@@ -0,0 +1,12 @@
+import { useChatStore } from '@/store/chat';
+import { chatPortalSelectors } from '@/store/chat/selectors';
+
+/**
+ * Length of the raw artifact source in the message.
+ *
+ * Do not count the rendered `children` instead: artifact newlines are stripped before markdown
+ * parsing, so paired backticks / `*` inside the code become inline elements that the rehype
+ * plugin drops, and a children-based count jumps back and forth while the artifact streams.
+ */
+export const useArtifactCodeLength = (id: string, identifier: string) =>
+ useChatStore((s) => chatPortalSelectors.artifactCode(id, identifier)(s).length);