Open History to see the last commit
1import {
2 useCallback,
3 useLayoutEffect,
4 useMemo,
5 useRef,
6 useState,
7 type ComponentProps,
8 type CSSProperties,
9} from "react";
10import { ChatCanvasContext } from "./ChatCanvasContext";
11import { resolveChatCanvasLayout, type ChatCanvasPreview } from "./chatCanvasLayout";
12import type { PreviewMiniPlayerObstacles } from "../preview/previewMiniPlayerLayout";
13import { observeResize } from "../../lib/observeResize";
14
15/**
16 * Owns the available conversation space. Cards only report where they sit; the
17 * canvas decides when chat moves over to make room for them.
18 */
19export function ChatCanvas({
20 composerOverlayElement,
21 detailsCardTopInset = 0,
22 children,
23 ...props
24}: Omit<ComponentProps<"div">, "className" | "style" | "ref"> & {
25 composerOverlayElement: HTMLElement | null;
26 detailsCardTopInset?: number;
27}) {
28 const elementRef = useRef<HTMLDivElement | null>(null);
29 const widthProbeRef = useRef<HTMLDivElement | null>(null);
30 const [timelineElement, registerTimeline] = useState<HTMLElement | null>(null);
31 const [preview, setPreview] = useState<ChatCanvasPreview | null>(null);
32 const [detailsCard, setDetailsCard] = useState<PreviewMiniPlayerObstacles["detailsCard"]>(null);
33 const reportDetailsCard = useCallback((next: PreviewMiniPlayerObstacles["detailsCard"]) => {
34 setDetailsCard((current) =>
35 current?.left === next?.left &&
36 current?.right === next?.right &&
37 current?.bottom === next?.bottom
38 ? current
39 : next,
40 );
41 }, []);
42 const [measurements, setMeasurements] = useState({
43 width: 0,
44 height: 0,
45 padding: 48,
46 maxChatWidth: 768,
47 minChatWidth: 640,
48 composerHeight: 0,
49 timelineGutter: 0,
50 findBarLeft: 0,
51 findBarRight: 0,
52 findBarBottom: 0,
53 });
54 const reportPreview = useCallback((next: ChatCanvasPreview) => {
55 setPreview((current) =>
56 current?.key === next.key &&
57 current.width === next.width &&
58 current.lastInteraction === next.lastInteraction &&
59 current.position?.x === next.position?.x &&
60 current.position?.y === next.position?.y &&
61 current.source.width === next.source.width &&
62 current.source.height === next.source.height
63 ? current
64 : next,
65 );
66 }, []);
67 const clearPreview = useCallback(
68 (key: string) => setPreview((current) => (current?.key === key ? null : current)),
69 [],
70 );
71 useLayoutEffect(() => {
72 const element = elementRef.current;
73 const probe = widthProbeRef.current;
74 if (!element || !probe) return;
75 const findBar =
76 detailsCardTopInset > 0 ? element.querySelector<HTMLElement>("[data-thread-find-bar]") : null;
77 const measure = () => {
78 const canvasBounds = element.getBoundingClientRect();
79 const findBarBounds = findBar?.getBoundingClientRect();
80 const styles = getComputedStyle(probe);
81 const next = {
82 findBarLeft: findBarBounds ? findBarBounds.left - canvasBounds.left : 0,
83 findBarRight: findBarBounds ? findBarBounds.right - canvasBounds.left : 0,
84 findBarBottom: findBarBounds ? findBarBounds.bottom - canvasBounds.top : 0,
85 width: element.clientWidth,
86 height: element.clientHeight,
87 padding: Number.parseFloat(styles.paddingLeft),
88 maxChatWidth: Number.parseFloat(styles.width),
89 minChatWidth: Number.parseFloat(styles.minWidth),
90 composerHeight: composerOverlayElement?.getBoundingClientRect().height ?? 0,
91 timelineGutter: timelineElement
92 ? (timelineElement.offsetWidth - timelineElement.clientWidth) / 2
93 : 0,
94 };
95 setMeasurements((current) =>
96 Object.keys(next).every(
97 (key) => current[key as keyof typeof current] === next[key as keyof typeof next],
98 )
99 ? current
100 : next,
101 );
102 };
103 measure();
104 const observed: Element[] = [element, probe];
105 if (findBar) observed.push(findBar);
106 if (composerOverlayElement) observed.push(composerOverlayElement);
107 if (timelineElement) observed.push(timelineElement);
108 return observeResize(observed, measure);
109 }, [composerOverlayElement, timelineElement, detailsCardTopInset]);
110 const context = useMemo(() => {
111 const container = { width: measurements.width, height: measurements.height };
112 const findBar =
113 detailsCardTopInset > 0
114 ? {
115 left: measurements.findBarLeft,
116 right: measurements.findBarRight,
117 bottom: measurements.findBarBottom,
118 }
119 : null;
120 return {
121 container,
122 lane: { padding: measurements.padding, minChatWidth: measurements.minChatWidth },
123 layout: resolveChatCanvasLayout({
124 ...measurements,
125 container,
126 preview,
127 detailsCard,
128 findBar,
129 }),
130 previewKey: preview?.key ?? null,
131 reportPreview,
132 clearPreview,
133 registerTimeline,
134 reportDetailsCard,
135 detailsCardTopInset: findBar?.bottom ?? 0,
136 };
137 }, [
138 measurements,
139 preview,
140 detailsCard,
141 reportPreview,
142 clearPreview,
143 reportDetailsCard,
144 detailsCardTopInset,
145 ]);
146 const { layout } = context;
147 return (
148 <ChatCanvasContext value={context}>
149 <div
150 {...props}
151 ref={elementRef}
152 data-chat-canvas
153 data-preview-overlaps-chat={layout.overlapsChat || undefined}
154 className="relative flex min-h-0 min-w-0 flex-1 flex-col"
155 style={
156 {
157 "--chat-timeline-gutter": `${measurements.timelineGutter}px`,
158 "--chat-lane-inset-start": `${layout.chat.insetStart}px`,
159 "--chat-lane-inset-end": `${layout.chat.insetEnd}px`,
160 } as CSSProperties
161 }
162 >
163 <div
164 ref={widthProbeRef}
165 aria-hidden
166 className="pointer-events-none invisible absolute h-0 w-(--chat-content-max-width) min-w-[40rem] box-content ps-3 sm:ps-12"
167 />
168 {children}
169 </div>
170 </ChatCanvasContext>
171 );
172}