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 const lineage = element.querySelector<HTMLElement>("[data-thread-relationships-panel]");
95 const fittingHeight = next - (lineage?.offsetHeight ?? 0);
96 setMeasurements((current) => {
97 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
98 const fullContentHeight =
99 density === "full"
100 ? next
101 : current.key === measurementKey
102 ? current.fullContentHeight
103 : 0;
104 return current.key === measurementKey &&
105 heights[density] === fittingHeight &&
106 current.fullContentHeight === fullContentHeight
107 ? current
108 : {
109 key: measurementKey,
110 heights: { ...heights, [density]: fittingHeight },
111 fullContentHeight,
112 };
113 });
114 };
115 measure();
116 return observeResize(element, measure);
117 }, [contentElement, density, measurementKey]);
118 const card = (
119 <div
120 className={cn(
121 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-3xl",
122 mode === "popover" &&
123 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
124 )}
125 style={placement ? { maxHeight: height } : undefined}
126 data-thread-details-card
127 >
128 <ScrollArea scrollFade className="min-h-0">
129 <div ref={setContentElement}>{children(density)}</div>
130 </ScrollArea>
131 </div>
132 );
133 return (
134 <Popover
135 handle={handle}
136 open={mode === "popover" && popoverOpen}
137 onOpenChange={(open, details) => {
138 const target =
139 details.reason === "focus-out"
140 ? (details.event as FocusEvent).relatedTarget
141 : details.reason === "outside-press"
142 ? details.event.target
143 : null;
144 if (!open && target instanceof Element && target.closest("[data-thread-find-bar]")) {
145 details.cancel();
146 return;
147 }
148 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open);
149 }}
150 >
151 {placement ? (
152 inlineOpen ? (
153 <aside
154 aria-label="Thread details"
155 // Slides down to make room for the find bar, but returns in the same frame
156 // the bar closes: a floating preview below moves up at once and would
157 // otherwise overlap the card mid-slide.
158 className={cn(
159 "absolute z-20",
160 topInset > 0 &&
161 "transition-[top] duration-150 ease-out motion-reduce:transition-none",
162 )}
163 style={{
164 right: placement.right,
165 top: placement.y,
166 width: placement.width,
167 maxHeight: height,
168 }}
169 data-density={density}
170 data-thread-details-panel="inline"
171 >
172 {card}
173 </aside>
174 ) : null
175 ) : (
176 <PopoverPopup
177 anchor={anchor}
178 align="end"
179 // Lines the card up with the find bar: the panel pads its card by 8px.
180 alignOffset={THREAD_DETAILS_CARD_GAP - 8}
181 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
182 side="bottom"
183 sideOffset={topInset}
184 variant="panel"
185 padding="none"
186 >
187 <div data-density={density} data-thread-details-panel="popover">
188 {card}
189 </div>
190 </PopoverPopup>
191 )}
192 </Popover>
193 );
194}