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

ChatCanvas.tsx

148 lines5.3 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 lane: { padding: measurements.padding, minChatWidth: measurements.minChatWidth },
108 layout: resolveChatCanvasLayout({
109 ...measurements,
110 container,
111 preview,
112 detailsCard,
113 findBar,
114 }),
115 previewKey: preview?.key ?? null,
116 reportPreview,
117 clearPreview,
118 registerTimeline,
119 reportDetailsCard,
120 detailsCardTopInset: findBar?.bottom ?? 0,
121 };
122 const { layout } = context;
123 return (
124 <ChatCanvasContext value={context}>
125 <div
126 {...props}
127 ref={elementRef}
128 data-chat-canvas
129 data-preview-overlaps-chat={layout.overlapsChat || undefined}
130 className="relative flex min-h-0 min-w-0 flex-1 flex-col"
131 style={
132 {
133 "--chat-timeline-gutter": `${measurements.timelineGutter}px`,
134 "--chat-lane-inset-start": `${layout.chat.insetStart}px`,
135 "--chat-lane-inset-end": `${layout.chat.insetEnd}px`,
136 } as CSSProperties
137 }
138 >
139 <div
140 ref={widthProbeRef}
141 aria-hidden
142 className="pointer-events-none invisible absolute h-0 w-(--chat-content-max-width) min-w-[40rem] box-content ps-3 sm:ps-12"
143 />
144 {children}
145 </div>
146 </ChatCanvasContext>
147 );