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

ChatCanvas.tsx

133 lines4.6 KB

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";
13​
14/** Owns the available conversation space. Floating cards never reserve it themselves. */
15export function ChatCanvas({
16 composerOverlayElement,
17 children,
18 ...props
19}: Omit<ComponentProps<"div">, "className" | "style" | "ref"> & {
20 composerOverlayElement: HTMLElement | null;
21}) {
22 const elementRef = useRef<HTMLDivElement | null>(null);
23 const widthProbeRef = useRef<HTMLDivElement | null>(null);
24 const [timelineElement, registerTimeline] = useState<HTMLElement | null>(null);
25 const [preview, setPreview] = useState<ChatCanvasPreview | null>(null);
26 const [detailsCard, setDetailsCard] = useState<PreviewMiniPlayerObstacles["detailsCard"]>(null);
27 const reportDetailsCard = useCallback((next: PreviewMiniPlayerObstacles["detailsCard"]) => {
28 setDetailsCard((current) =>
29 current?.left === next?.left &&
30 current?.right === next?.right &&
31 current?.bottom === next?.bottom
32 ? current
33 : next,
34 );
35 }, []);
36 const [measurements, setMeasurements] = useState({
37 width: 0,
38 height: 0,
39 padding: 20,
40 maxChatWidth: 768,
41 minChatWidth: 640,
42 composerHeight: 0,
43 timelineGutter: 0,
44 });
45 const reportPreview = useCallback((next: ChatCanvasPreview) => {
46 setPreview((current) =>
47 current?.key === next.key &&
48 current.width === next.width &&
49 current.lastInteraction === next.lastInteraction &&
50 current.position?.x === next.position?.x &&
51 current.position?.y === next.position?.y &&
52 current.source.width === next.source.width &&
53 current.source.height === next.source.height
54 ? current
55 : next,
56 );
57 }, []);
58 const clearPreview = useCallback(
59 (key: string) => setPreview((current) => (current?.key === key ? null : current)),
60 [],
61 );
62 useLayoutEffect(() => {
63 const element = elementRef.current;
64 const probe = widthProbeRef.current;
65 if (!element || !probe) return;
66 const measure = () => {
67 const styles = getComputedStyle(probe);
68 const next = {
69 width: element.clientWidth,
70 height: element.clientHeight,
71 padding: Number.parseFloat(styles.paddingLeft),
72 maxChatWidth: Number.parseFloat(styles.width),
73 minChatWidth: Number.parseFloat(styles.minWidth),
74 composerHeight: composerOverlayElement?.getBoundingClientRect().height ?? 0,
75 timelineGutter: timelineElement
76 ? (timelineElement.offsetWidth - timelineElement.clientWidth) / 2
77 : 0,
78 };
79 setMeasurements((current) =>
80 Object.keys(next).every(
81 (key) => current[key as keyof typeof current] === next[key as keyof typeof next],
82 )
83 ? current
84 : next,
85 );
86 };
87 measure();
88 const observer = new ResizeObserver(measure);
89 observer.observe(element);
90 observer.observe(probe);
91 if (composerOverlayElement) observer.observe(composerOverlayElement);
92 if (timelineElement) observer.observe(timelineElement);
93 return () => observer.disconnect();
94 }, [composerOverlayElement, timelineElement]);
95 const context = useMemo(() => {
96 const container = { width: measurements.width, height: measurements.height };
97 return {
98 container,
99 layout: resolveChatCanvasLayout({ ...measurements, container, preview, detailsCard }),
100 previewKey: preview?.key ?? null,
101 reportPreview,
102 clearPreview,
103 registerTimeline,
104 reportDetailsCard,
105 };
106 }, [measurements, preview, detailsCard, reportPreview, clearPreview, reportDetailsCard]);
107 const { layout } = context;
108 return (
109 <ChatCanvasContext value={context}>
110 <div
111 {...props}
112 ref={elementRef}
113 data-chat-canvas
114 data-preview-overlaps-chat={layout.overlapsChat || undefined}
115 className="relative flex min-h-0 min-w-0 flex-1 flex-col"
116 style={
117 {
118 "--chat-timeline-gutter": `${measurements.timelineGutter}px`,
119 "--chat-lane-inset-start": `${layout.chat.insetStart}px`,
120 "--chat-lane-inset-end": `${layout.chat.insetEnd}px`,
121 } as CSSProperties
122 }
123 >
124 <div
125 ref={widthProbeRef}
126 aria-hidden
127 className="pointer-events-none invisible absolute h-0 w-(--chat-content-max-width) min-w-[40rem] box-content ps-3 sm:ps-5"
128 />
129 {children}
130 </div>
131 </ChatCanvasContext>
132 );