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