perf(desktop): virtualize message timeline and memoize Markdown for instant channel switching (#146)

This commit is contained in:
Wes
2026-03-21 09:43:25 -07:00
committed by GitHub
parent 266d9c6b64
commit 537e54ed6e
8 changed files with 247 additions and 101 deletions

View File

@@ -1,4 +1,4 @@
import type * as React from "react";
import * as React from "react";
import ReactMarkdown, { type Components } from "react-markdown";
import remarkBreaks from "remark-breaks";
import remarkGfm from "remark-gfm";
@@ -181,15 +181,35 @@ function createMarkdownComponents(
} as Components;
}
export function Markdown({
function shallowArrayEqual(a?: string[], b?: string[]): boolean {
if (a === b) return true;
if (!a || !b) return false;
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false;
}
return true;
}
function MarkdownInner({
className,
compact = false,
content,
mentionNames,
tight = false,
}: MarkdownProps) {
const variant = tight ? "tight" : compact ? "compact" : "default";
const variant: MarkdownVariant = tight
? "tight"
: compact
? "compact"
: "default";
const { channels, onOpenChannel } = useChannelNavigation();
const components = React.useMemo(
() => createMarkdownComponents(variant, channels, onOpenChannel),
[variant, channels, onOpenChannel],
);
let processedContent = content;
if (/^(?:\s{2}\n)+/.test(content)) {
@@ -212,7 +232,7 @@ export function Markdown({
)}
>
<ReactMarkdown
components={createMarkdownComponents(variant, channels, onOpenChannel)}
components={components}
remarkPlugins={[
remarkGfm,
remarkBreaks,
@@ -225,3 +245,15 @@ export function Markdown({
</div>
);
}
export const Markdown = React.memo(
MarkdownInner,
(prev, next) =>
prev.content === next.content &&
prev.className === next.className &&
prev.compact === next.compact &&
prev.tight === next.tight &&
shallowArrayEqual(prev.mentionNames, next.mentionNames),
);
Markdown.displayName = "Markdown";