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 { useElementState } from "../../hooks/useElementState";
3import { ChatCanvasContext } from "./ChatCanvasContext";
4import { resolveChatCanvasLayout, type ChatCanvasPreview } from "./chatCanvasLayout";
5import type { PreviewMiniPlayerObstacles } from "../preview/previewMiniPlayerLayout";
6import { observeResize } from "../../lib/observeResize";
7​
8export function ChatCanvas({
9 composerOverlayElement,
10 detailsCardTopInset = 0,
11 children,
12 ...props
13}: Omit<ComponentProps<"div">, "className" | "style" | "ref"> & {
14 composerOverlayElement: HTMLElement | null;
15 detailsCardTopInset?: number;
16}) {
17 const elementRef = useRef<HTMLDivElement | null>(null);
18 const widthProbeRef = useRef<HTMLDivElement | null>(null);
19 const [timelineElement, registerTimeline] = useElementState<HTMLElement>();
20 const [preview, setPreview] = useState<ChatCanvasPreview | null>(null);
21 const [detailsCard, setDetailsCard] = useState<PreviewMiniPlayerObstacles["detailsCard"]>(null);
22 const reportDetailsCard = (next: PreviewMiniPlayerObstacles["detailsCard"]) => {
23 setDetailsCard((current) =>
24 current?.left === next?.left &&
25 current?.right === next?.right &&
26 current?.bottom === next?.bottom
27 ? current
28 : next,
29 );
30 };
31 const [measurements, setMeasurements] = useState({
32 width: 0,
33 height: 0,
34 padding: 48,
35 maxChatWidth: 768,
36 minChatWidth: 640,
37 composerHeight: 0,
38 timelineGutter: 0,
39 findBarLeft: 0,
40 findBarRight: 0,
41 findBarBottom: 0,
42 });
43 const reportPreview = (next: ChatCanvasPreview) => {
44 setPreview((current) =>
45 current?.key === next.key &&
46 current.width === next.width &&
47 current.lastInteraction === next.lastInteraction &&
48 current.position?.x === next.position?.x &&
49 current.position?.y === next.position?.y &&
50 current.source.width === next.source.width &&
51 current.source.height === next.source.height
52 ? current
53 : next,
54 );
55 };
56 const clearPreview = (key: string) =>
57 setPreview((current) => (current?.key === key ? null : current));
58 useLayoutEffect(() => {
59 const element = elementRef.current;
60 const probe = widthProbeRef.current;
61 if (!element || !probe) return;
62 const findBar =
63 detailsCardTopInset > 0 ? element.querySelector<HTMLElement>("[data-thread-find-bar]") : null;
64 const measure = () => {
65 const canvasBounds = element.getBoundingClientRect();
66 const findBarBounds = findBar?.getBoundingClientRect();
67 const styles = getComputedStyle(probe);
68 const next = {
69 findBarLeft: findBarBounds ? findBarBounds.left - canvasBounds.left : 0,
70 findBarRight: findBarBounds ? findBarBounds.right - canvasBounds.left : 0,
71 findBarBottom: findBarBounds ? findBarBounds.bottom - canvasBounds.top : 0,
72 width: element.clientWidth,
73 height: element.clientHeight,
74 padding: Number.parseFloat(styles.paddingLeft),
75 maxChatWidth: Number.parseFloat(styles.width),
76 minChatWidth: Number.parseFloat(styles.minWidth),
77 composerHeight: composerOverlayElement?.getBoundingClientRect().height ?? 0,
78 timelineGutter: timelineElement
79 ? (timelineElement.offsetWidth - timelineElement.clientWidth) / 2
80 : 0,
81 };
82 setMeasurements((current) =>
83 Object.keys(next).every(
84 (key) => current[key as keyof typeof current] === next[key as keyof typeof next],
85 )
86 ? current
87 : next,
88 );
89 };
90 measure();
91 const observed: Element[] = [element, probe];
92 if (findBar) observed.push(findBar);
93 if (composerOverlayElement) observed.push(composerOverlayElement);
94 if (timelineElement) observed.push(timelineElement);
95 return observeResize(observed, measure);
96 }, [composerOverlayElement, timelineElement, detailsCardTopInset]);
97 const container = { width: measurements.width, height: measurements.height };
98 const findBar =
99 detailsCardTopInset > 0
100 ? {
101 left: measurements.findBarLeft,
102 right: measurements.findBarRight,
103 bottom: measurements.findBarBottom,
104 }
105 : null;
106 const context = {
107 container,
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 );