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
15export function ChatCanvas({
16 composerOverlayElement,
17 detailsCardTopInset = 0,
18 children,
19 ...props
20}: Omit<ComponentProps<"div">, "className" | "style" | "ref"> & {
21 composerOverlayElement: HTMLElement | null;
22 detailsCardTopInset?: number;
23}) {
24 const elementRef = useRef<HTMLDivElement | null>(null);
25 const widthProbeRef = useRef<HTMLDivElement | null>(null);
26 const [timelineElement, registerTimeline] = useState<HTMLElement | null>(null);
27 const [preview, setPreview] = useState<ChatCanvasPreview | null>(null);
28 const [detailsCard, setDetailsCard] = useState<PreviewMiniPlayerObstacles["detailsCard"]>(null);
29 const reportDetailsCard = useCallback((next: PreviewMiniPlayerObstacles["detailsCard"]) => {
30 setDetailsCard((current) =>
31 current?.left === next?.left &&
32 current?.right === next?.right &&
33 current?.bottom === next?.bottom
34 ? current
35 : next,
36 );
37 }, []);
38 const [measurements, setMeasurements] = useState({
39 width: 0,
40 height: 0,
41 padding: 48,
42 maxChatWidth: 768,
43 minChatWidth: 640,
44 composerHeight: 0,
45 timelineGutter: 0,
46 findBarLeft: 0,
47 findBarRight: 0,
48 findBarBottom: 0,
49 });
50 const reportPreview = useCallback((next: ChatCanvasPreview) => {
51 setPreview((current) =>
52 current?.key === next.key &&
53 current.width === next.width &&
54 current.lastInteraction === next.lastInteraction &&
55 current.position?.x === next.position?.x &&
56 current.position?.y === next.position?.y &&
57 current.source.width === next.source.width &&
58 current.source.height === next.source.height
59 ? current
60 : next,
61 );
62 }, []);
63 const clearPreview = useCallback(
64 (key: string) => setPreview((current) => (current?.key === key ? null : current)),
65 [],
66 );
67 useLayoutEffect(() => {
68 const element = elementRef.current;
69 const probe = widthProbeRef.current;
70 if (!element || !probe) return;
71 const findBar =
72 detailsCardTopInset > 0 ? element.querySelector<HTMLElement>("[data-thread-find-bar]") : null;
73 const measure = () => {
74 const canvasBounds = element.getBoundingClientRect();
75 const findBarBounds = findBar?.getBoundingClientRect();
76 const styles = getComputedStyle(probe);
77 const next = {
78 findBarLeft: findBarBounds ? findBarBounds.left - canvasBounds.left : 0,
79 findBarRight: findBarBounds ? findBarBounds.right - canvasBounds.left : 0,
80 findBarBottom: findBarBounds ? findBarBounds.bottom - canvasBounds.top : 0,
81 width: element.clientWidth,
82 height: element.clientHeight,
83 padding: Number.parseFloat(styles.paddingLeft),
84 maxChatWidth: Number.parseFloat(styles.width),
85 minChatWidth: Number.parseFloat(styles.minWidth),
86 composerHeight: composerOverlayElement?.getBoundingClientRect().height ?? 0,
87 timelineGutter: timelineElement
88 ? (timelineElement.offsetWidth - timelineElement.clientWidth) / 2
89 : 0,
90 };
91 setMeasurements((current) =>
92 Object.keys(next).every(
93 (key) => current[key as keyof typeof current] === next[key as keyof typeof next],
94 )
95 ? current
96 : next,
97 );
98 };
99 measure();
100 const observed: Element[] = [element, probe];
101 if (findBar) observed.push(findBar);
102 if (composerOverlayElement) observed.push(composerOverlayElement);
103 if (timelineElement) observed.push(timelineElement);
104 return observeResize(observed, measure);
105 }, [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 {
117 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,
137 reportPreview,
138 clearPreview,
139 reportDetailsCard,
140 detailsCardTopInset,
141 ]);
142 const { layout } = context;
143 return (
144 <ChatCanvasContext value={context}>
145 <div
146 {...props}
147 ref={elementRef}
148 data-chat-canvas
149 data-preview-overlaps-chat={layout.overlapsChat || undefined}
150 className="relative flex min-h-0 min-w-0 flex-1 flex-col"
151 style={
152 {
153 "--chat-timeline-gutter": `${measurements.timelineGutter}px`,
154 "--chat-lane-inset-start": `${layout.chat.insetStart}px`,
155 "--chat-lane-inset-end": `${layout.chat.insetEnd}px`,
156 } as CSSProperties
157 }
158 >
159 <div
160 ref={widthProbeRef}
161 aria-hidden
162 className="pointer-events-none invisible absolute h-0 w-(--chat-content-max-width) min-w-[40rem] box-content ps-3 sm:ps-12"
163 />
164 {children}
165 </div>
166 </ChatCanvasContext>
167 );
168}