@devries-cameron20 / t3code Mirror · read-only / commits / c298a401f2
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 17:00 UTC

perf(web): diff file boundaries observe visibility once instead of on every render (#25134)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 25, 2026 at 16:35 UTC c298a401f2d68693003d8027b08bc915da739f9e
5 files changed +94 -20
apps/web/src/components/diffs/DiffFileLoadingBoundary.browser.test.tsx new
+49
@@ -0,0 +1,49 @@
1 +import { describe, expect, it, vi } from "vite-plus/test";
2 +
3 +import { render } from "../../../test/render";
4 +import { DiffFileLoadingBoundary } from "./DiffFileLoadingBoundary";
5 +
6 +// Like the real observer, every observe() reports the target's current state; this boundary
7 +// is always in range.
8 +class InRangeIntersectionObserver {
9 + static observeCount = 0;
10 + constructor(readonly callback: IntersectionObserverCallback) {}
11 + observe(target: Element) {
12 + InRangeIntersectionObserver.observeCount += 1;
13 + this.callback(
14 + [{ target, isIntersecting: true } as IntersectionObserverEntry],
15 + this as unknown as IntersectionObserver,
16 + );
17 + }
18 + unobserve() {}
19 + disconnect() {}
20 +}
21 +
22 +function stubIntersectionObserver() {
23 + InRangeIntersectionObserver.observeCount = 0;
24 + vi.stubGlobal("IntersectionObserver", InRangeIntersectionObserver);
25 + return { [Symbol.dispose]: () => vi.unstubAllGlobals() };
26 +}
27 +
28 +describe("DiffFileLoadingBoundary", () => {
29 + it("observes once and loads once while its parent re-renders with a new load", async () => {
30 + using _observer = stubIntersectionObserver();
31 + const load = vi.fn();
32 + // DiffPanel hands over a new loadNextFiles whenever any file patch changes.
33 + await using screen = await render(<DiffFileLoadingBoundary load={() => load()} count={8} />, {
34 + router: false, // the boundary does not touch routing
35 + });
36 + await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={8} />);
37 + await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={8} />);
38 + await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={8} />);
39 +
40 + await expect.element(screen.getByRole("status", { name: "Loading diff…" })).toBeInTheDocument();
41 + expect(InRangeIntersectionObserver.observeCount).toBe(1);
42 + expect(load).toHaveBeenCalledTimes(1);
43 +
44 + // A settled batch leaves the boundary in range, so it asks for the next one.
45 + await screen.rerender(<DiffFileLoadingBoundary load={() => load()} count={4} />);
46 + expect(InRangeIntersectionObserver.observeCount).toBe(1);
47 + expect(load).toHaveBeenCalledTimes(2);
48 + });
49 +});
apps/web/src/components/diffs/DiffFileLoadingBoundary.tsx
+20 -14
@@ -1,22 +1,28 @@
1 +import { useEffect, useState } from "react";
2 +
3 +import { useStableCallback } from "~/hooks/useStableCallback";
4 import { observeIntersection } from "~/lib/observeIntersection";
5 import { DiffFileHeaderSkeleton } from "../DiffPanelShell";
6
7 export function DiffFileLoadingBoundary({ load, count }: { load: () => void; count: number }) {
8 + const [isNear, setIsNear] = useState(false);
9 + const loadLatest = useStableCallback(load);
10 + // A fresh observe redelivers the current intersection, so this ref must keep its identity
11 + // across renders: it captures nothing but the state setter.
12 + const observe = (node: HTMLDivElement | null) => {
13 + if (!node) return;
14 + return observeIntersection(node, (entry) => setIsNear(entry.isIntersecting), {
15 + rootMargin: "600px",
16 + });
17 + };
18 + // A settled batch shrinks `count` without moving the boundary out of range, and no new
19 + // intersection entry arrives for that, so ask for the next batch whenever it changes.
20 + useEffect(() => {
21 + if (isNear && count > 0) loadLatest();
22 + }, [isNear, count, loadLatest]);
23 +
24 return (
6 - <div
7 - ref={(node) => {
8 - if (!node) return;
9 - return observeIntersection(
10 - node,
11 - (entry) => {
12 - if (entry.isIntersecting) load();
13 - },
14 - { rootMargin: "600px" },
15 - );
16 - }}
17 - role="status"
18 - aria-label="Loading diff…"
19 - >
25 + <div ref={observe} role="status" aria-label="Loading diff…">
26 {Array.from({ length: Math.min(count, 4) }, (_, index) => (
27 <div key={index} aria-hidden className="border-b border-border/40">
28 <DiffFileHeaderSkeleton titleWidth="medium" />
apps/web/src/components/diffs/useReviewFilePatches.ts
+4 -1
@@ -92,8 +92,11 @@ export function useReviewFilePatches({
92 const added = indices.filter((index) => !previous.includes(index));
93 return added.length === 0 ? current : { scope, indices: [...previous, ...added] };
94 });
95 - const loadNextFiles = () =>
95 + // The first unsettled file already requested means a batch is in flight; wait for it.
96 + const loadNextFiles = () => {
97 + if (queries.some(({ index }) => index === settledFileCount)) return;
98 requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index));
99 + };
100 const requestFile = (index: number) => requestFiles([index]);
101 const renderableFiles = source
102 ? files.map((file, index): FileDiffMetadata => {
apps/web/src/components/permissions/usePermissionStatus.browser.test.tsx
+16 -1
@@ -6,11 +6,13 @@ import { usePermissionStatus } from "./usePermissionStatus";
6 function PermissionProbe({
7 check,
8 label,
9 + enabled = true,
10 }: {
11 readonly check: () => Promise<{ screen: boolean }>;
12 readonly label: string;
13 + readonly enabled?: boolean;
14 }) {
13 - const { status } = usePermissionStatus(async () => check(), { screen: false });
15 + const { status } = usePermissionStatus(async () => check(), { screen: false }, enabled);
16 return (
17 <p>
18 {label}: {status.screen ? "granted" : "missing"}
@@ -32,4 +34,17 @@ describe("usePermissionStatus", () => {
34 await expect.element(screen.getByText("third: missing")).toBeInTheDocument();
35 expect(check).toHaveBeenCalledTimes(1);
36 });
37 +
38 + it("checks with the latest check after the caller passes a new one", async () => {
39 + const first = vi.fn(() => new Promise<{ screen: boolean }>(() => {}));
40 + const second = vi.fn(async () => ({ screen: true }));
41 + await using screen = await render(<PermissionProbe check={first} label="probe" />, {
42 + router: false, // the hook does not touch routing
43 + });
44 + await screen.rerender(<PermissionProbe check={second} label="probe" enabled={false} />);
45 + await screen.rerender(<PermissionProbe check={second} label="probe" />);
46 +
47 + await expect.element(screen.getByText("probe: granted")).toBeInTheDocument();
48 + expect(first).toHaveBeenCalledTimes(1);
49 + });
50 });
apps/web/src/components/permissions/usePermissionStatus.ts
+5 -4
@@ -1,5 +1,6 @@
1 import { useState, useSyncExternalStore } from "react";
2
3 +import { useStableCallback } from "~/hooks/useStableCallback";
4 import { getDocumentActivity, subscribeDocumentActivity } from "~/lib/documentActivity";
5
6 const POLL_INTERVAL_MS = 1500;
@@ -61,16 +62,16 @@ export function createPermissionPoller<Id extends string>(
62 const noopSubscribe = () => () => {};
63
64 /**
64 - * `check` and `initialStatus` are read once, when the component mounts, so the
65 - * subscription only restarts when `enabled` flips. Key the component if what it
66 - * checks can change.
65 + * Each check calls the latest `check`, since callers close over props. `initialStatus` is
66 + * read once, when the component mounts, and the subscription only restarts when `enabled` flips.
67 */
68 export function usePermissionStatus<Id extends string>(
69 check: () => Promise<Record<Id, boolean>>,
70 initialStatus: Record<Id, boolean>,
71 enabled = true,
72 ) {
73 - const [poller] = useState(() => createPermissionPoller(check, initialStatus));
73 + const checkLatest = useStableCallback(check);
74 + const [poller] = useState(() => createPermissionPoller(checkLatest, initialStatus));
75 const { status, error } = useSyncExternalStore(
76 enabled ? poller.subscribe : noopSubscribe,
77 poller.getSnapshot,