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 THREAD_DETAILS_CARD_GAP,
11 resolveThreadDetailsCardDensity,
12 resolveThreadDetailsCardLayout,
13} from "./threadDetailsCardLayout";
14import { observeResize } from "../../lib/observeResize";
15
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 topInset = canvas?.detailsCardTopInset ?? 0;
49 const inlineOpen = useRightPanelStore((state) =>
50 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "inline"),
51 );
52 const popoverOpen = useRightPanelStore((state) =>
53 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "popover"),
54 );
55 const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null);
56 const measurementKey = `${threadRef.environmentId}:${threadRef.threadId}:${preferredPlacement?.width ?? "popup"}`;
57 const [measurements, setMeasurements] = useState({
58 key: measurementKey,
59 heights: { full: 0, compact: 0 },
60 fullContentHeight: 0,
61 });
62 const contentHeights =
63 measurements.key === measurementKey ? measurements.heights : { full: 0, compact: 0 };
64 const height = placement?.height ?? Math.max(0, (canvas?.container.height ?? 0) - 52);
65 const density = resolveThreadDetailsCardDensity(height, contentHeights);
66 const reportDetailsCard = canvas?.reportDetailsCard;
67 const cardLeft = preferredPlacement?.x;
68 const cardRight = preferredPlacement
69 ? preferredPlacement.x + preferredPlacement.width
70 : undefined;
71 const cardBottom =
72 preferredPlacement && measurements.key === measurementKey && measurements.fullContentHeight > 0
73 ? preferredPlacement.y + Math.min(measurements.fullContentHeight, preferredPlacement.height)
74 : undefined;
75 useLayoutEffect(() => {
76 reportDetailsCard?.(
77 inlineOpen && cardLeft !== undefined && cardRight !== undefined && cardBottom !== undefined
78 ? { left: cardLeft, right: cardRight, bottom: cardBottom }
79 : null,
80 );
81 }, [reportDetailsCard, inlineOpen, cardLeft, cardRight, cardBottom]);
82 useLayoutEffect(() => () => reportDetailsCard?.(null), [reportDetailsCard]);
83 useLayoutEffect(() => {
84 onPresentationChange(mode);
85 if (mode === "inline" && popoverOpen)
86 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", false);
87 }, [mode, onPresentationChange, threadRef, popoverOpen]);
88 useLayoutEffect(() => {
89 const element = contentElement;
90 if (!element || density === "essential") return;
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 // PR lists and lineage scroll as they expand. Counting them toward density would hide
95 // the section and workspace controls when the user asks to see more rows.
96 const lists = element.querySelectorAll<HTMLElement>(
97 "[data-thread-relationships-panel], [data-thread-details-pr-list]",
98 );
99 const fittingHeight =
100 next - Array.from(lists).reduce((sum, list) => sum + list.offsetHeight, 0);
101 setMeasurements((current) => {
102 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
103 const fullContentHeight =
104 density === "full"
105 ? next
106 : current.key === measurementKey
107 ? current.fullContentHeight
108 : 0;
109 return current.key === measurementKey &&
110 heights[density] === fittingHeight &&
111 current.fullContentHeight === fullContentHeight
112 ? current
113 : {
114 key: measurementKey,
115 heights: { ...heights, [density]: fittingHeight },
116 fullContentHeight,
117 };
118 });
119 };
120 measure();
121 return observeResize(element, measure);
122 }, [contentElement, density, measurementKey]);
123 const card = (
124 <div
125 className={cn(
126 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-3xl",
127 mode === "popover" &&
128 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
129 )}
130 style={placement ? { maxHeight: height } : undefined}
131 data-thread-details-card
132 >
133 <ScrollArea scrollFade className="min-h-0">
134 <div ref={setContentElement}>{children(density)}</div>
135 </ScrollArea>
136 </div>
137 );
138 return (
139 <Popover
140 handle={handle}
141 open={mode === "popover" && popoverOpen}
142 onOpenChange={(open, details) => {
143 const target =
144 details.reason === "focus-out"
145 ? (details.event as FocusEvent).relatedTarget
146 : details.reason === "outside-press"
147 ? details.event.target
148 : null;
149 if (!open && target instanceof Element && target.closest("[data-thread-find-bar]")) {
150 details.cancel();
151 return;
152 }
153 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open);
154 }}
155 >
156 {placement ? (
157 inlineOpen ? (
158 <aside
159 aria-label="Thread details"
160 // Slides down to make room for the find bar, but returns in the same frame
161 // the bar closes: a floating preview below moves up at once and would
162 // otherwise overlap the card mid-slide.
163 className={cn(
164 "absolute z-20",
165 topInset > 0 &&
166 "transition-[top] duration-150 ease-out motion-reduce:transition-none",
167 )}
168 style={{
169 right: placement.right,
170 top: placement.y,
171 width: placement.width,
172 maxHeight: height,
173 }}
174 data-density={density}
175 data-thread-details-panel="inline"
176 >
177 {card}
178 </aside>
179 ) : null
180 ) : (
181 <PopoverPopup
182 anchor={anchor}
183 align="end"
184 // Lines the card up with the find bar: the panel pads its card by 8px.
185 alignOffset={THREAD_DETAILS_CARD_GAP - 8}
186 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
187 side="bottom"
188 sideOffset={topInset}
189 variant="panel"
190 padding="none"
191 >
192 <div data-density={density} data-thread-details-panel="popover">
193 {card}
194 </div>
195 </PopoverPopup>
196 )}
197 </Popover>
198 );
199}