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

ThreadDetailsCard.tsx

161 lines6.1 KB

1import { useLayoutEffect, useState, type ReactNode, type RefObject } from "react";
2import type { ScopedThreadRef } from "@t3tools/contracts";
3import { ScrollArea } from "../ui/scroll-area";
4import { cn } from "../../lib/utils";
5import { Popover, PopoverPopup, PopoverCreateHandle } from "../ui/popover";
6import { selectThreadPanelOpen, useRightPanelStore } from "../../rightPanelStore";
7import type { ThreadPanelPresentation } from "../../rightPanelLayout";
8import { useChatCanvas } from "./ChatCanvasContext";
9import {
10 resolveThreadDetailsCardDensity,
11 resolveThreadDetailsCardLayout,
12} from "./threadDetailsCardLayout";
13​
14/** One card owns its placement and folds content only when that content cannot fit. */
15export function ThreadDetailsCard({
16 threadRef,
17 anchor,
18 handle,
19 onPresentationChange,
20 children,
21}: {
22 threadRef: ScopedThreadRef;
23 anchor: RefObject<Element | null>;
24 handle: ReturnType<typeof PopoverCreateHandle>;
25 onPresentationChange: (presentation: ThreadPanelPresentation) => void;
26 children: (density: "full" | "compact" | "essential") => ReactNode;
27}) {
28 const canvas = useChatCanvas();
29 const preferredPlacement = canvas
30 ? resolveThreadDetailsCardLayout({
31 container: canvas.container,
32 lane: canvas.lane,
33 frame: null,
34 })
35 : null;
36 const placement = canvas
37 ? resolveThreadDetailsCardLayout({
38 container: canvas.container,
39 lane: canvas.lane,
40 frame: canvas.layout.frame,
41 overlapsDetailsCard: canvas.layout.overlapsDetailsCard,
42 })
43 : null;
44 const mode = placement ? "inline" : "popover";
45 const inlineOpen = useRightPanelStore((state) =>
46 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "inline"),
47 );
48 const popoverOpen = useRightPanelStore((state) =>
49 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "popover"),
50 );
51 const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null);
52 const measurementKey = `${threadRef.environmentId}:${threadRef.threadId}:${preferredPlacement?.width ?? "popup"}`;
53 const [measurements, setMeasurements] = useState({
54 key: measurementKey,
55 heights: { full: 0, compact: 0 },
56 });
57 const contentHeights =
58 measurements.key === measurementKey ? measurements.heights : { full: 0, compact: 0 };
59 const height = placement?.height ?? Math.max(0, (canvas?.container.height ?? 0) - 52);
60 const density = resolveThreadDetailsCardDensity(height, contentHeights);
61 const reportDetailsCard = canvas?.reportDetailsCard;
62 const cardLeft = preferredPlacement?.x;
63 const cardRight = preferredPlacement
64 ? preferredPlacement.x + preferredPlacement.width
65 : undefined;
66 const cardBottom =
67 preferredPlacement && contentHeights.full > 0
68 ? preferredPlacement.y + Math.min(contentHeights.full, preferredPlacement.height)
69 : undefined;
70 useLayoutEffect(() => {
71 reportDetailsCard?.(
72 inlineOpen && cardLeft !== undefined && cardRight !== undefined && cardBottom !== undefined
73 ? { left: cardLeft, right: cardRight, bottom: cardBottom }
74 : null,
75 );
76 }, [reportDetailsCard, inlineOpen, cardLeft, cardRight, cardBottom]);
77 useLayoutEffect(() => () => reportDetailsCard?.(null), [reportDetailsCard]);
78 useLayoutEffect(() => {
79 onPresentationChange(mode);
80 if (mode === "inline" && popoverOpen)
81 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", false);
82 }, [mode, onPresentationChange, threadRef, popoverOpen]);
83 useLayoutEffect(() => {
84 const element = contentElement;
85 if (!element || density === "essential") return;
86 // Measure the single content tree before the scroll viewport clips it. Retain each
87 // observed height so increasing available space restores the detail it can hold.
88 const measure = () => {
89 const frame = element.closest<HTMLElement>("[data-thread-details-card]");
90 const next = element.offsetHeight + (frame ? frame.offsetHeight - frame.clientHeight : 0);
91 setMeasurements((current) => {
92 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
93 return current.key === measurementKey && heights[density] === next
94 ? current
95 : { key: measurementKey, heights: { ...heights, [density]: next } };
96 });
97 };
98 measure();
99 const observer = new ResizeObserver(measure);
100 observer.observe(element);
101 return () => observer.disconnect();
102 }, [contentElement, density, measurementKey]);
103 const card = (
104 <div
105 className={cn(
106 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-3xl",
107 mode === "popover" &&
108 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
109 )}
110 style={placement ? { maxHeight: height } : undefined}
111 data-thread-details-card
112 >
113 <ScrollArea scrollFade className="min-h-0">
114 <div ref={setContentElement}>{children(density)}</div>
115 </ScrollArea>
116 </div>
117 );
118 return (
119 <Popover
120 handle={handle}
121 open={mode === "popover" && popoverOpen}
122 onOpenChange={(open) =>
123 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open)
124 }
125 >
126 {placement ? (
127 inlineOpen ? (
128 <aside
129 aria-label="Thread details"
130 className="absolute z-20"
131 style={{
132 left: placement.x,
133 top: placement.y,
134 width: placement.width,
135 maxHeight: height,
136 }}
137 data-density={density}
138 data-thread-details-panel="inline"
139 >
140 {card}
141 </aside>
142 ) : null
143 ) : (
144 <PopoverPopup
145 anchor={anchor}
146 align="end"
147 alignOffset={0}
148 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
149 side="bottom"
150 sideOffset={0}
151 variant="panel"
152 padding="none"
153 >
154 <div data-density={density} data-thread-details-panel="popover">
155 {card}
156 </div>
157 </PopoverPopup>
158 )}
159 </Popover>
160 );