Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChatCanvas.tsx

149 lines5.4 KB

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