Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions apps/app/src/components/promptbox/banner/QueuedMessagesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ import {
shiftMentionsToTextRange,
} from "@/components/thread/timeline/ConversationMessageMentions";
import {
buildPromptMentionComponent,
PromptMentionElement,
PromptMentionsContext,
remarkPromptMentions,
substitutePromptMentions,
} from "@/components/ui/markdown-prompt-mentions";
Expand Down Expand Up @@ -234,6 +235,7 @@ const QUEUED_MARKDOWN_COMPONENTS: Components = {
thead: ({ children }) => <span>{children}</span>,
tr: ({ children }) => <span>{children} </span>,
ul: ({ children }) => <span>{children}</span>,
"bb-prompt-mention": PromptMentionElement,
};

function CompactQueuedMarkdownPreview({
Expand All @@ -254,25 +256,24 @@ function CompactQueuedMarkdownPreview({
normalizePromptBlockquoteBoundaries(promptMentionSubstitution.content),
[promptMentionSubstitution.content],
);
const components = useMemo<Components>(
const promptMentions = useMemo(
() => ({
...QUEUED_MARKDOWN_COMPONENTS,
"bb-prompt-mention": buildPromptMentionComponent({
mentions: promptMentionSubstitution.mentions,
resolveMentionLink,
}),
mentions: promptMentionSubstitution.mentions,
resolveMentionLink,
}),
[promptMentionSubstitution.mentions, resolveMentionLink],
);

return (
<span className={queuedMarkdownPreviewClass(compact)}>
<ReactMarkdown
components={components}
remarkPlugins={QUEUED_MARKDOWN_REMARK_PLUGINS}
>
{markdownContent}
</ReactMarkdown>
<PromptMentionsContext.Provider value={promptMentions}>
<ReactMarkdown
components={QUEUED_MARKDOWN_COMPONENTS}
remarkPlugins={QUEUED_MARKDOWN_REMARK_PLUGINS}
>
{markdownContent}
</ReactMarkdown>
</PromptMentionsContext.Provider>
</span>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ describe("ConversationMessageContent streaming split", () => {
]);
expect(markdownRenders).toEqual([
"Para two.\n\n",
"Para three.\n\nPara four",
"Para three.\n\n",
"Para four",
]);

markdownRenders.length = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function setup({
bottomThresholdPx: 4,
deferWritesWhileScrolling: false,
getContentElement: () => content,
getFloorElement: () => null,
getFooterElement: () => null,
getScrollElement: () => (rootReady ? scrollElement : null),
getTopLevelRows: () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function createBottomAnchor(
bottomThresholdPx: 4,
deferWritesWhileScrolling: false,
getContentElement: () => null,
getFloorElement: () => null,
getFooterElement: () => null,
getScrollElement: () => scrollElement,
getTopLevelRows: () => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function useStickyBottomScroll<TElement extends HTMLElement>({
bottomThresholdPx: STICKY_BOTTOM_THRESHOLD_PX,
deferWritesWhileScrolling: isIOSWebKit(navigator),
getContentElement: () => contentRef.current,
getFloorElement: () => null,
getFooterElement: () => null,
getScrollElement: () => scrollRef.current,
getTopLevelRows,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ function installAutoLoadEnvironment() {
bottomThresholdPx: 4,
deferWritesWhileScrolling: false,
getContentElement: () => null,
getFloorElement: () => null,
getFooterElement: () => null,
getScrollElement: () => scrollElement,
getTopLevelRows: () => [],
Expand Down
7 changes: 7 additions & 0 deletions apps/app/src/components/ui/bottom-anchored-scroll-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function BottomAnchoredScrollBody({
const scrollContentRef = useRef<HTMLDivElement>(null);
const pageContentRef = useRef<HTMLDivElement>(null);
const scrollFooterRef = useRef<HTMLDivElement>(null);
const scrollFloorRef = useRef<HTMLDivElement>(null);
const hasFooter = Boolean(footer);
const scrollAnchorCaptureThrottleRef = useRef<{
lastWriteAt: number;
Expand All @@ -131,6 +132,7 @@ export function BottomAnchoredScrollBody({
bottomThresholdPx: BOTTOM_ANCHOR_THRESHOLD_PX,
deferWritesWhileScrolling: isIOSWebKit(navigator),
getContentElement: () => pageContentRef.current,
getFloorElement: () => scrollFloorRef.current,
getFooterElement: () => scrollFooterRef.current,
getScrollElement: () => scrollAreaRef.current,
getTopLevelRows,
Expand Down Expand Up @@ -338,6 +340,11 @@ export function BottomAnchoredScrollBody({
ref={scrollContentRef}
className="flex min-h-full min-w-0 flex-col"
>
<div
ref={scrollFloorRef}
aria-hidden="true"
className="pointer-events-none invisible w-px shrink-0"
/>
<div
ref={pageContentRef}
data-scroll-attached={isAtBottom ? "" : undefined}
Expand Down
57 changes: 57 additions & 0 deletions apps/app/src/components/ui/markdown-preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,63 @@ describe("MarkdownPreview", () => {
expect(screen.getByRole("link", { name: "preview" })).toBe(link);
});

it.each([false, true])(
"keeps links and inline code mounted when link routing and thread mentions change identity (incrementalBlocks=%s)",
(incrementalBlocks) => {
const content =
"See [docs](https://example.com/docs) and `inline_code` here.";
const routing = (): MarkdownLinkRouting => ({
onOpenLink: () => true,
localImage: {
absolutePaths: { kind: "trusted-host" },
resolveSrc: ({ path }) => `/files${path}`,
},
});
const { container, rerender } = render(
<MarkdownPreview
content={content}
incrementalBlocks={incrementalBlocks}
linkRouting={routing()}
threadMentions={{ mentions: [], preserveSoftBreaks: false }}
/>,
);
const link = screen.getByRole("link", { name: "docs" });
const code = container.querySelector("code");

rerender(
<MarkdownPreview
content={content}
incrementalBlocks={incrementalBlocks}
linkRouting={routing()}
threadMentions={{ mentions: [], preserveSoftBreaks: false }}
/>,
);

expect(screen.getByRole("link", { name: "docs" })).toBe(link);
expect(container.querySelector("code")).toBe(code);
},
);

it("routes a link through the latest handler after routing changes", () => {
const content = "See [docs](https://example.com/docs).";
const first = vi.fn(() => true);
const second = vi.fn(() => true);
const { rerender } = render(
<MarkdownPreview content={content} linkRouting={{ onOpenLink: first }} />,
);

rerender(
<MarkdownPreview
content={content}
linkRouting={{ onOpenLink: second }}
/>,
);
fireEvent.click(screen.getByRole("link", { name: "docs" }));

expect(first).not.toHaveBeenCalled();
expect(second).toHaveBeenCalledTimes(1);
});

it("renders inline LaTeX math with KaTeX", async () => {
const { container } = render(
<MarkdownPreview content={"Mass-energy is $$E = mc^2$$ exactly."} />,
Expand Down
Loading
Loading