The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(desktop): highlight an edit card's diff only once it is on screen
Tekniumdfcfdba66969
diff --git a/apps/desktop/src/components/chat/diff-lines-seen.test.tsx b/apps/desktop/src/components/chat/diff-lines-seen.test.tsxnew file mode 100644index 00000000000..2406d4dcc88--- /dev/null+++ b/apps/desktop/src/components/chat/diff-lines-seen.test.tsx@@ -0,0 +1,44 @@+import { act, cleanup, render, screen } from '@testing-library/react'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++vi.mock('./syntax-diff', () => ({ default: () => <span data-testid="shiki-diff" /> }))++import { FileDiffPanel } from './diff-lines'++const DIFF = ['--- a/file.ts', '+++ b/file.ts', '@@ -1,2 +1,2 @@', ' const a = 1', '-const b = 2', '+const b = 3'].join('\n')++let intersect: (() => void) | null = null+const realObserver = globalThis.IntersectionObserver++beforeEach(() => {+ intersect = null+ globalThis.IntersectionObserver = class {+ constructor(callback: IntersectionObserverCallback) {+ intersect = () => callback([{ isIntersecting: true } as IntersectionObserverEntry], this as never)+ }+ observe() {}+ unobserve() {}+ disconnect() {}+ takeRecords() {+ return []+ }+ } as unknown as typeof IntersectionObserver+})++afterEach(() => {+ cleanup()+ globalThis.IntersectionObserver = realObserver+})++describe('compact FileDiffPanel highlights only once it is on screen', () => {+ it('keeps the plain colored rows off screen, then highlights on first intersection', async () => {+ const { container } = render(<FileDiffPanel diff={DIFF} path="file.ts" />)++ await act(() => new Promise(resolve => setTimeout(resolve, 50)))+ expect(screen.queryByTestId('shiki-diff')).toBeNull()+ expect(container.textContent).toContain('const b = 3')++ await act(async () => intersect?.())+ expect(await screen.findByTestId('shiki-diff')).toBeTruthy()+ })+})diff --git a/apps/desktop/src/components/chat/diff-lines.tsx b/apps/desktop/src/components/chat/diff-lines.tsxindex 0f5731ec491..2cf45ba74c7 100644--- a/apps/desktop/src/components/chat/diff-lines.tsx+++ b/apps/desktop/src/components/chat/diff-lines.tsx@@ -6,6 +6,7 @@ import type { BundledLanguage, ShikiTransformer, ThemedToken } from 'shiki'import { chunkLines, type LineChunk, useFixedRowWindow } from '@/components/chat/fixed-row-window'import { exceedsHighlightBudget, SHIKI_THEME } from '@/components/chat/shiki-highlighter'import { ErrorBoundary } from '@/components/error-boundary'+import { useSeenOnce } from '@/hooks/use-seen-once'import { shikiLanguageForFilename } from '@/lib/markdown-code'import { cn } from '@/lib/utils'@@ -607,6 +608,13 @@ export function FileDiffPanel({const language = shikiLanguageForFilename(path)const canHighlight = Boolean(language) && !exceedsHighlightBudget(fullText ?? diff)const windowed = showLineNumbers || virtualized+ // Edit-tool cards open their diff by default, so a session full of file edits+ // used to tokenize every diff in the loaded window on each switch (seconds of+ // Shiki on the click path). The compact card keeps the plain colored rows+ // until it is first on screen; the windowed body already highlights only+ // the rows it mounts.+ const boxRef = React.useRef<HTMLDivElement>(null)+ const seen = useSeenOnce(boxRef, canHighlight && !windowed)// Windowed: we own fixed-height rows and render only the visible chunks, so a// large diff never mounts (or Shiki-highlights) every line. Compact tool cards@@ -624,7 +632,7 @@ export function FileDiffPanel({<PreviewDiffRows afterLines={afterRows} beforeLines={beforeRows} chunks={visibleLineChunks} />)- const compactBody = !canHighlight ? (+ const compactBody = !canHighlight || !seen ? (<DiffBody lines={lines} />) : fullText != null ? (<TokenizedDiffBody language={language} lines={lines} />@@ -634,7 +642,7 @@ export function FileDiffPanel({if (!windowed) {return (- <div className={cn(DIFF_BOX_CLASS, className)} data-slot="file-diff-panel">+ <div className={cn(DIFF_BOX_CLASS, className)} data-slot="file-diff-panel" ref={boxRef}>{compactBody}</div>)diff --git a/apps/desktop/src/hooks/use-seen-once.ts b/apps/desktop/src/hooks/use-seen-once.tsnew file mode 100644index 00000000000..5d816aa862c--- /dev/null+++ b/apps/desktop/src/hooks/use-seen-once.ts@@ -0,0 +1,33 @@+import { type RefObject, useEffect, useState } from 'react'++/**+ * Latches true the first time `ref`'s element intersects the viewport (through+ * every clipping scroll ancestor), and stays true. Expensive work that only+ * matters once seen (syntax highlighting) waits on it, so mounting a long+ * transcript does not pay for content the reader never scrolls to.+ * Without IntersectionObserver it reports true at once (nothing to wait on).+ */+export function useSeenOnce(ref: RefObject<Element | null>, enabled = true): boolean {+ const [seen, setSeen] = useState(() => typeof IntersectionObserver !== 'function')++ useEffect(() => {+ const node = ref.current++ if (!enabled || seen || !node) {+ return+ }++ const observer = new IntersectionObserver(entries => {+ if (entries.some(entry => entry.isIntersecting)) {+ observer.disconnect()+ setSeen(true)+ }+ })++ observer.observe(node)++ return () => observer.disconnect()+ }, [enabled, ref, seen])++ return seen+}