Open History to see the last commit
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 topInset: canvas.detailsCardTopInset,
35 })
36 : null;
37 const placement = canvas
38 ? resolveThreadDetailsCardLayout({
39 container: canvas.container,
40 lane: canvas.lane,
41 frame: canvas.layout.frame,
42 overlapsDetailsCard: canvas.layout.overlapsDetailsCard,
43 topInset: canvas.detailsCardTopInset,
44 })
45 : null;
46 const mode = placement ? "inline" : "popover";
47 const inlineOpen = useRightPanelStore((state) =>
48 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "inline"),
49 );
50 const popoverOpen = useRightPanelStore((state) =>
51 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "popover"),
52 );
53 const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null);
54 const measurementKey = `${threadRef.environmentId}:${threadRef.threadId}:${preferredPlacement?.width ?? "popup"}`;
55 const [measurements, setMeasurements] = useState({
56 key: measurementKey,
57 heights: { full: 0, compact: 0 },
58 fullContentHeight: 0,
59 });
60 const contentHeights =
61 measurements.key === measurementKey ? measurements.heights : { full: 0, compact: 0 };
62 const height = placement?.height ?? Math.max(0, (canvas?.container.height ?? 0) - 52);
63 const density = resolveThreadDetailsCardDensity(height, contentHeights);
64 const reportDetailsCard = canvas?.reportDetailsCard;
65 const cardLeft = preferredPlacement?.x;
66 const cardRight = preferredPlacement
67 ? preferredPlacement.x + preferredPlacement.width
68 : undefined;
69 const cardBottom =
70 preferredPlacement && measurements.key === measurementKey && measurements.fullContentHeight > 0
71 ? preferredPlacement.y + Math.min(measurements.fullContentHeight, preferredPlacement.height)
72 : undefined;
73 useLayoutEffect(() => {
74 reportDetailsCard?.(
75 inlineOpen && cardLeft !== undefined && cardRight !== undefined && cardBottom !== undefined
76 ? { left: cardLeft, right: cardRight, bottom: cardBottom }
77 : null,
78 );
79 }, [reportDetailsCard, inlineOpen, cardLeft, cardRight, cardBottom]);
80 useLayoutEffect(() => () => reportDetailsCard?.(null), [reportDetailsCard]);
81 useLayoutEffect(() => {
82 onPresentationChange(mode);
83 if (mode === "inline" && popoverOpen)
84 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", false);
85 }, [mode, onPresentationChange, threadRef, popoverOpen]);
86 useLayoutEffect(() => {
87 const element = contentElement;
88 if (!element || density === "essential") return;
89 // Measure the single content tree before the scroll viewport clips it. Retain each
90 // observed height so increasing available space restores the detail it can hold.
91 const measure = () => {
92 const frame = element.closest<HTMLElement>("[data-thread-details-card]");
93 const next = element.offsetHeight + (frame ? frame.offsetHeight - frame.clientHeight : 0);
94 // Lineage scrolls as it expands. Counting it toward density would hide
95 // the section and workspace controls when the user asks to see more rows.
96 const lineage = element.querySelector<HTMLElement>("[data-thread-relationships-panel]");
97 const fittingHeight = next - (lineage?.offsetHeight ?? 0);
98 setMeasurements((current) => {
99 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
100 const fullContentHeight =
101 density === "full"
102 ? next
103 : current.key === measurementKey
104 ? current.fullContentHeight
105 : 0;
106 return current.key === measurementKey &&
107 heights[density] === fittingHeight &&
108 current.fullContentHeight === fullContentHeight
109 ? current
110 : {
111 key: measurementKey,
112 heights: { ...heights, [density]: fittingHeight },
113 fullContentHeight,
114 };
115 });
116 };
117 measure();
118 const observer = new ResizeObserver(measure);
119 observer.observe(element);
120 return () => observer.disconnect();
121 }, [contentElement, density, measurementKey]);
122 const card = (
123 <div
124 className={cn(
125 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-3xl",
126 mode === "popover" &&
127 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
128 )}
129 style={placement ? { maxHeight: height } : undefined}
130 data-thread-details-card
131 >
132 <ScrollArea scrollFade className="min-h-0">
133 <div ref={setContentElement}>{children(density)}</div>
134 </ScrollArea>
135 </div>
136 );
137 return (
138 <Popover
139 handle={handle}
140 open={mode === "popover" && popoverOpen}
141 onOpenChange={(open) =>
142 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open)
143 }
144 >
145 {placement ? (
146 inlineOpen ? (
147 <aside
148 aria-label="Thread details"
149 className="absolute z-20"
150 style={{
151 left: placement.x,
152 top: placement.y,
153 width: placement.width,
154 maxHeight: height,
155 }}
156 data-density={density}
157 data-thread-details-panel="inline"
158 >
159 {card}
160 </aside>
161 ) : null
162 ) : (
163 <PopoverPopup
164 anchor={anchor}
165 align="end"
166 alignOffset={0}
167 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
168 side="bottom"
169 sideOffset={0}
170 variant="panel"
171 padding="none"
172 >
173 <div data-density={density} data-thread-details-panel="popover">
174 {card}
175 </div>
176 </PopoverPopup>
177 )}
178 </Popover>
179 );
180}