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