Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadDetailsCard.tsx

178 lines6.8 KB

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