Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(ai): mentions flashing in chat stream (#3825)

Eric Hayes52861dc89f97
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx b/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
index d1d2fa469d..33925c8414 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/core/StaticMarkdown.tsx
@@ -66,6 +66,7 @@ import {
} from '@core/constant/featureFlags';
import type { MarkNode } from '@lexical/mark';
import type { SearchMatchNode } from '@lexical-core/nodes/SearchMatchNode';
+import { getCachedItemPreview } from '@queries/preview';
import { theme as baseTheme, createTheme } from '../../theme';
import { forceSingleLine, setEditorStateFromMarkdown } from '../../utils';
import { StaticCodeBoxAccessory } from '../accessory/CodeBoxAccessory';
@@ -229,14 +230,24 @@ type NodeComponent<T extends LexicalNode = LexicalNode> = {
type ElementNodeComponent<T extends ElementNode = ElementNode> = ParentProps &
NodeComponent<T>;
+type StaticRenderOptions = {
+ lazy: boolean;
+};
+
type RenderableEntity<T extends LexicalNode = LexicalNode> = {
guard: (node: LexicalNode) => node is T;
- render: (props: NodeComponent<T>) => JSX.Element;
+ render: (
+ props: NodeComponent<T>,
+ options: StaticRenderOptions
+ ) => JSX.Element;
};
type RenderableElement<T extends ElementNode = ElementNode> = {
guard: (node: LexicalNode) => node is T;
- render: (props: ElementNodeComponent<T>) => JSX.Element;
+ render: (
+ props: ElementNodeComponent<T>,
+ options: StaticRenderOptions
+ ) => JSX.Element;
};
const Text: RenderableEntity<TextNode> = {
@@ -280,21 +291,29 @@ const MentionPlaceholder = () => (
const DocumentMention: RenderableEntity<DocumentMentionNode> = {
guard: (node: LexicalNode): node is DocumentMentionNode =>
node.__type === 'document-mention',
- render: (props) => {
+ render: (props, options) => {
const componentProps = props.node.exportComponentProps();
const key = props.node.getKey();
+ const mention = () =>
+ DocumentMentionDecorator({
+ ...componentProps,
+ key,
+ theme: props.theme,
+ });
+ const shouldRenderLazy =
+ options.lazy &&
+ getCachedItemPreview(componentProps.documentId) === undefined;
+
return (
<span class={getTextClassName(props.node, props.theme)}>
- <LazyDecorator
- placeholder={<MentionPlaceholder />}
- render={() =>
- DocumentMentionDecorator({
- ...componentProps,
- key,
- theme: props.theme,
- })
- }
- />
+ {shouldRenderLazy ? (
+ <LazyDecorator
+ placeholder={<MentionPlaceholder />}
+ render={mention}
+ />
+ ) : (
+ mention()
+ )}
</span>
);
},
@@ -750,27 +769,36 @@ const Elements: RenderableElement[] = [
ClassedBlock,
] as const;
-function Render(props: NodeComponent | ElementNodeComponent) {
+function Render(
+ props: (NodeComponent | ElementNodeComponent) & StaticRenderOptions
+) {
let entity = InlineEntities.find((entity) => entity.guard(props.node));
if (entity) {
- return entity.render({
- ...props,
- theme: props.theme,
- });
+ return entity.render(
+ {
+ ...props,
+ theme: props.theme,
+ },
+ { lazy: props.lazy }
+ );
}
const element = Elements.find((entity) => entity.guard(props.node));
if (element) {
let elemNode = props.node as ElementNode;
- return element.render({
- node: elemNode,
- children: MapRender({
- children: elemNode.getChildren(),
+ return element.render(
+ {
+ node: elemNode,
+ children: MapRender({
+ children: elemNode.getChildren(),
+ theme: props.theme,
+ lazy: props.lazy,
+ }),
theme: props.theme,
- }),
- theme: props.theme,
- });
+ },
+ { lazy: props.lazy }
+ );
}
console.error('Static Markdown: no render found for node', props.node);
@@ -780,15 +808,17 @@ function Render(props: NodeComponent | ElementNodeComponent) {
function MapRender(props: {
children: LexicalNode[];
theme: EditorThemeClasses;
+ lazy: boolean;
}) {
return props.children.map((child) => (
- <Render node={child} theme={props.theme} />
+ <Render node={child} theme={props.theme} lazy={props.lazy} />
));
}
function Document(props: {
rootNode: RootNode;
theme: EditorThemeClasses;
+ lazy: boolean;
rootRef?: (ref: HTMLDivElement) => void;
singleLine?: boolean;
}): JSX.Element {
@@ -801,7 +831,11 @@ function Document(props: {
)}
ref={props.rootRef}
>
- <MapRender children={props.rootNode.getChildren()} theme={props.theme} />
+ <MapRender
+ children={props.rootNode.getChildren()}
+ theme={props.theme}
+ lazy={props.lazy}
+ />
</div>
);
}
@@ -809,7 +843,8 @@ function Document(props: {
const context = createContext<{
editor: LexicalEditor | null;
theme: Accessor<EditorThemeClasses>;
-}>({ editor: null, theme: () => baseTheme });
+ lazy: Accessor<boolean>;
+}>({ editor: null, theme: () => baseTheme, lazy: () => true });
export function StaticMarkdown(props: {
markdown: string;
@@ -819,8 +854,13 @@ export function StaticMarkdown(props: {
rootRef?: (ref: HTMLDivElement) => void;
target?: 'internal' | 'external' | 'both';
singleLine?: boolean;
+ lazy?: boolean;
}) {
- let { editor: contextEditor, theme: parentTheme } = useContext(context);
+ let {
+ editor: contextEditor,
+ theme: parentTheme,
+ lazy: parentLazy,
+ } = useContext(context);
let [editorState, setEditorState] = createSignal<EditorState | null>(null);
if (contextEditor === null) {
@@ -834,6 +874,8 @@ export function StaticMarkdown(props: {
return createTheme(props.theme ?? {}, parentTheme());
};
+ const lazy = () => props.lazy ?? parentLazy();
+
const currentEditor = createMemo(() => {
if (contextEditor) {
if (props.setEditorRef) props.setEditorRef(contextEditor);
@@ -878,6 +920,7 @@ export function StaticMarkdown(props: {
return Document({
rootNode: $getRoot(),
theme: mergedTheme(),
+ lazy: lazy(),
rootRef: props.rootRef,
});
});
@@ -889,6 +932,7 @@ export function StaticMarkdown(props: {
export function StaticMarkdownContext(props: {
children: JSX.Element;
theme?: EditorThemeClasses;
+ lazy?: boolean;
}) {
const mergedTheme = () => {
if (!props.theme) return baseTheme;
@@ -900,7 +944,13 @@ export function StaticMarkdownContext(props: {
);
return (
- <context.Provider value={{ editor: editor(), theme: mergedTheme }}>
+ <context.Provider
+ value={{
+ editor: editor(),
+ theme: mergedTheme,
+ lazy: () => props.lazy ?? true,
+ }}
+ >
{props.children}
</context.Provider>
);
diff --git a/js/app/packages/queries/preview/index.ts b/js/app/packages/queries/preview/index.ts
index ccf6016c9f..22c69e550c 100644
--- a/js/app/packages/queries/preview/index.ts
+++ b/js/app/packages/queries/preview/index.ts
@@ -1,4 +1,5 @@
export {
+ getCachedItemPreview,
getItemPreview,
invalidatePreview,
setPreviewFileType,
diff --git a/js/app/packages/queries/preview/preview.ts b/js/app/packages/queries/preview/preview.ts
index 955c2dc0d9..07fef4e9de 100644
--- a/js/app/packages/queries/preview/preview.ts
+++ b/js/app/packages/queries/preview/preview.ts
@@ -110,6 +110,10 @@ function getPreviewData(itemId: string): PreviewItem | undefined {
);
}
+export function getCachedItemPreview(itemId: string): PreviewItem | undefined {
+ return getPreviewData(itemId);
+}
+
/** Directly update preview data in the cache without refetching */
function setPreviewData(itemId: string, updater: Setter<PreviewItem>) {
return queryClient.setQueryData<PreviewItem>(