Skip to content

Commit 981d757

Browse files
perf(web): the chat canvas compiles without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a702563 commit 981d757

1 file changed

Lines changed: 29 additions & 49 deletions

File tree

‎apps/web/src/components/chat/ChatCanvas.tsx‎

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
useCallback,
3-
useLayoutEffect,
4-
useMemo,
5-
useRef,
6-
useState,
7-
type ComponentProps,
8-
type CSSProperties,
9-
} from "react";
1+
import { useLayoutEffect, useRef, useState, type ComponentProps, type CSSProperties } from "react";
102
import { ChatCanvasContext } from "./ChatCanvasContext";
113
import { resolveChatCanvasLayout, type ChatCanvasPreview } from "./chatCanvasLayout";
124
import type { PreviewMiniPlayerObstacles } from "../preview/previewMiniPlayerLayout";
@@ -26,15 +18,15 @@ export function ChatCanvas({
2618
const [timelineElement, registerTimeline] = useState<HTMLElement | null>(null);
2719
const [preview, setPreview] = useState<ChatCanvasPreview | null>(null);
2820
const [detailsCard, setDetailsCard] = useState<PreviewMiniPlayerObstacles["detailsCard"]>(null);
29-
const reportDetailsCard = useCallback((next: PreviewMiniPlayerObstacles["detailsCard"]) => {
21+
const reportDetailsCard = (next: PreviewMiniPlayerObstacles["detailsCard"]) => {
3022
setDetailsCard((current) =>
3123
current?.left === next?.left &&
3224
current?.right === next?.right &&
3325
current?.bottom === next?.bottom
3426
? current
3527
: next,
3628
);
37-
}, []);
29+
};
3830
const [measurements, setMeasurements] = useState({
3931
width: 0,
4032
height: 0,
@@ -47,7 +39,7 @@ export function ChatCanvas({
4739
findBarRight: 0,
4840
findBarBottom: 0,
4941
});
50-
const reportPreview = useCallback((next: ChatCanvasPreview) => {
42+
const reportPreview = (next: ChatCanvasPreview) => {
5143
setPreview((current) =>
5244
current?.key === next.key &&
5345
current.width === next.width &&
@@ -59,11 +51,9 @@ export function ChatCanvas({
5951
? current
6052
: next,
6153
);
62-
}, []);
63-
const clearPreview = useCallback(
64-
(key: string) => setPreview((current) => (current?.key === key ? null : current)),
65-
[],
66-
);
54+
};
55+
const clearPreview = (key: string) =>
56+
setPreview((current) => (current?.key === key ? null : current));
6757
useLayoutEffect(() => {
6858
const element = elementRef.current;
6959
const probe = widthProbeRef.current;
@@ -103,42 +93,32 @@ export function ChatCanvas({
10393
if (timelineElement) observed.push(timelineElement);
10494
return observeResize(observed, measure);
10595
}, [composerOverlayElement, timelineElement, detailsCardTopInset]);
106-
const context = useMemo(() => {
107-
const container = { width: measurements.width, height: measurements.height };
108-
const findBar =
109-
detailsCardTopInset > 0
110-
? {
111-
left: measurements.findBarLeft,
112-
right: measurements.findBarRight,
113-
bottom: measurements.findBarBottom,
114-
}
115-
: null;
116-
return {
96+
const container = { width: measurements.width, height: measurements.height };
97+
const findBar =
98+
detailsCardTopInset > 0
99+
? {
100+
left: measurements.findBarLeft,
101+
right: measurements.findBarRight,
102+
bottom: measurements.findBarBottom,
103+
}
104+
: null;
105+
const context = {
106+
container,
107+
lane: { padding: measurements.padding, minChatWidth: measurements.minChatWidth },
108+
layout: resolveChatCanvasLayout({
109+
...measurements,
117110
container,
118-
lane: { padding: measurements.padding, minChatWidth: measurements.minChatWidth },
119-
layout: resolveChatCanvasLayout({
120-
...measurements,
121-
container,
122-
preview,
123-
detailsCard,
124-
findBar,
125-
}),
126-
previewKey: preview?.key ?? null,
127-
reportPreview,
128-
clearPreview,
129-
registerTimeline,
130-
reportDetailsCard,
131-
detailsCardTopInset: findBar?.bottom ?? 0,
132-
};
133-
}, [
134-
measurements,
135-
preview,
136-
detailsCard,
111+
preview,
112+
detailsCard,
113+
findBar,
114+
}),
115+
previewKey: preview?.key ?? null,
137116
reportPreview,
138117
clearPreview,
118+
registerTimeline,
139119
reportDetailsCard,
140-
detailsCardTopInset,
141-
]);
120+
detailsCardTopInset: findBar?.bottom ?? 0,
121+
};
142122
const { layout } = context;
143123
return (
144124
<ChatCanvasContext value={context}>

0 commit comments

Comments
 (0)