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

ThreadDetailsCard.tsx

174 lines6.9 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 { useChatCanvas } from "./ChatCanvasContext";
8import {
9 THREAD_DETAILS_CARD_GAP,
10 resolveThreadDetailsCardDensity,
11} from "./threadDetailsCardLayout";
12import { observeResize } from "../../lib/observeResize";
13​
14export function ThreadDetailsCard({
15 threadRef,
16 anchor,
17 handle,
18 children,
19}: {
20 threadRef: ScopedThreadRef;
21 anchor: RefObject<Element | null>;
22 handle: ReturnType<typeof PopoverCreateHandle>;
23 children: (density: "full" | "compact" | "essential") => ReactNode;
24}) {
25 const canvas = useChatCanvas();
26 const preferredPlacement = canvas?.preferredDetailsCard ?? null;
27 const placement = canvas?.detailsCardPlacement ?? null;
28 const mode = placement ? "inline" : "popover";
29 const topInset = canvas?.detailsCardTopInset ?? 0;
30 const inlineOpen = useRightPanelStore((state) =>
31 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "inline"),
32 );
33 const popoverOpen = useRightPanelStore((state) =>
34 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "popover"),
35 );
36 const measurementKey = `${threadRef.environmentId}:${threadRef.threadId}:${preferredPlacement?.width ?? "popup"}`;
37 const [measurements, setMeasurements] = useState({
38 key: measurementKey,
39 heights: { full: 0, compact: 0 },
40 });
41 const contentHeights =
42 measurements.key === measurementKey ? measurements.heights : { full: 0, compact: 0 };
43 const height = placement?.height ?? Math.max(0, (canvas?.container.height ?? 0) - 52);
44 const density = resolveThreadDetailsCardDensity(height, contentHeights);
45 const measureDetailsCard = canvas?.measureDetailsCard;
46 const cardWidth = preferredPlacement?.width;
47 // The mode derives from canvas measurements, the preview frame and the
48 // inline flag, which live in three stores, so no single write site sees the
49 // card leave popover mode. A flag left set would reopen the popover unasked
50 // when the card returns to popover mode.
51 useLayoutEffect(() => {
52 if (mode === "inline" && popoverOpen)
53 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", false);
54 }, [mode, popoverOpen, threadRef]);
55 const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null);
56 const { environmentId, threadId } = threadRef;
57 // Re-measures when the density or measurement key changes, so each density's
58 // height is measured once it renders.
59 useLayoutEffect(() => {
60 const element = contentElement;
61 if (!element || density === "essential") return;
62 const measure = () => {
63 const frame = element.closest<HTMLElement>("[data-thread-details-card]");
64 const next = element.offsetHeight + (frame ? frame.offsetHeight - frame.clientHeight : 0);
65 // PR lists and lineage scroll as they expand. Counting them toward density would hide
66 // the section and workspace controls when the user asks to see more rows.
67 const lists = element.querySelectorAll<HTMLElement>(
68 "[data-thread-relationships-panel], [data-thread-details-pr-list]",
69 );
70 const fittingHeight =
71 next - Array.from(lists).reduce((sum, list) => sum + list.offsetHeight, 0);
72 setMeasurements((current) => {
73 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
74 return current.key === measurementKey && heights[density] === fittingHeight
75 ? current
76 : { key: measurementKey, heights: { ...heights, [density]: fittingHeight } };
77 });
78 // Only the laid-out DOM knows the full card height, and the canvas keeps preview
79 // frames clear of it, so it is reported from this measure effect.
80 if (density === "full" && cardWidth !== undefined) {
81 measureDetailsCard?.({
82 threadRef: { environmentId, threadId },
83 width: cardWidth,
84 fullContentHeight: next,
85 });
86 }
87 };
88 measure();
89 return observeResize(element, measure);
90 }, [
91 contentElement,
92 density,
93 measurementKey,
94 cardWidth,
95 measureDetailsCard,
96 environmentId,
97 threadId,
98 ]);
99 const card = (
100 <div
101 className={cn(
102 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-2xl",
103 mode === "popover" &&
104 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
105 )}
106 style={placement ? { maxHeight: height } : undefined}
107 data-thread-details-card
108 >
109 <ScrollArea scrollFade className="min-h-0">
110 <div ref={setContentElement}>{children(density)}</div>
111 </ScrollArea>
112 </div>
113 );
114 return (
115 <Popover
116 handle={handle}
117 open={mode === "popover" && popoverOpen}
118 onOpenChange={(open, details) => {
119 const target =
120 details.reason === "focus-out"
121 ? (details.event as FocusEvent).relatedTarget
122 : details.reason === "outside-press"
123 ? details.event.target
124 : null;
125 if (!open && target instanceof Element && target.closest("[data-thread-find-bar]")) {
126 details.cancel();
127 return;
128 }
129 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", open);
130 }}
131 >
132 {placement && inlineOpen ? (
133 <aside
134 aria-label="Thread details"
135 // Skip the return transition to avoid overlapping the rising preview.
136 className={cn(
137 "absolute z-20",
138 topInset > 0 && "transition-[top] duration-150 ease-out motion-reduce:transition-none",
139 )}
140 style={{
141 right: placement.right,
142 top: placement.y,
143 width: placement.width,
144 maxHeight: height,
145 }}
146 data-density={density}
147 data-thread-details-panel="inline"
148 >
149 {card}
150 </aside>
151 ) : null}
152 {/* Rendered in both modes: base-ui keeps a popup that unmounts mid-open
153 mounted, because its exit animation never finishes. Staying in the tree
154 lets it close cleanly when the card switches inline. */}
155 <PopoverPopup
156 anchor={anchor}
157 align="end"
158 // Account for the find panel's 8px card padding.
159 alignOffset={THREAD_DETAILS_CARD_GAP - 8}
160 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
161 side="bottom"
162 sideOffset={topInset}
163 variant="panel"
164 padding="none"
165 >
166 {mode === "popover" ? (
167 <div data-density={density} data-thread-details-panel="popover">
168 {card}
169 </div>
170 ) : null}
171 </PopoverPopup>
172 </Popover>
173 );