@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 201 lines 8.2 KB
Raw
1 import { useGT } from "gt-react";
2 import { workspaceAreaProps } from "../../lib/workspaceFocus";
3 import {
4 useLayoutEffect,
5 useState,
6 type MouseEventHandler,
7 type ReactNode,
8 type RefObject,
9 } from "react";
10 import type { ScopedThreadRef } from "@t3tools/contracts";
11 import { ScrollArea } from "../ui/scroll-area";
12 import { cn } from "../../lib/utils";
13 import { Popover, PopoverPopup, PopoverCreateHandle } from "../ui/popover";
14 import { selectThreadPanelOpen, useRightPanelStore } from "../../rightPanelStore";
15 import { useChatCanvas } from "./ChatCanvasContext";
16 import {
17 THREAD_DETAILS_CARD_GAP,
18 resolveThreadDetailsCardDensity,
19 } from "./threadDetailsCardLayout";
20 import { observeResize } from "../../lib/observeResize";
21 import { requestWorkspaceFocus } from "../../lib/workspaceFocus";
22
23 export function ThreadDetailsCard({
24 threadRef,
25 anchor,
26 handle,
27 contentKey,
28 onContextMenu,
29 children,
30 }: {
31 threadRef: ScopedThreadRef;
32 anchor: RefObject<Element | null>;
33 handle: ReturnType<typeof PopoverCreateHandle>;
34 /** Changes when the set of rendered items changes, so cached heights are measured again. */
35 contentKey: string;
36 onContextMenu?: MouseEventHandler<HTMLDivElement>;
37 children: (density: "full" | "compact" | "essential") => ReactNode;
38 }) {
39 const gt = useGT();
40 const detailsArea = workspaceAreaProps("details", 3, gt("Thread details"));
41 const canvas = useChatCanvas();
42 const preferredPlacement = canvas?.preferredDetailsCard ?? null;
43 const placement = canvas?.detailsCardPlacement ?? null;
44 const mode = placement ? "inline" : "popover";
45 const topInset = canvas?.detailsCardTopInset ?? 0;
46 const inlineOpen = useRightPanelStore((state) =>
47 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "inline"),
48 );
49 const popoverOpen = useRightPanelStore((state) =>
50 selectThreadPanelOpen(state.threadPanelVisibilityByThreadKey, threadRef, "popover"),
51 );
52 const measurementKey = `${threadRef.environmentId}:${threadRef.threadId}:${preferredPlacement?.width ?? "popup"}:${contentKey}`;
53 const [measurements, setMeasurements] = useState({
54 key: measurementKey,
55 heights: { full: 0, compact: 0 },
56 });
57 const contentHeights =
58 measurements.key === measurementKey ? measurements.heights : { full: 0, compact: 0 };
59 const height = placement?.height ?? Math.max(0, (canvas?.container.height ?? 0) - 52);
60 const density = resolveThreadDetailsCardDensity(height, contentHeights);
61 const measureDetailsCard = canvas?.measureDetailsCard;
62 const cardWidth = preferredPlacement?.width;
63 // The mode derives from canvas measurements, the preview frame and the
64 // inline flag, which live in three stores, so no single write site sees the
65 // card leave popover mode. A flag left set would reopen the popover unasked
66 // when the card returns to popover mode.
67 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- closes the popover flag in the right panel store when the derived mode turns inline
68 useLayoutEffect(() => {
69 if (mode === "inline" && popoverOpen)
70 useRightPanelStore.getState().setThreadPanelOpen(threadRef, "popover", false);
71 }, [mode, popoverOpen, threadRef]);
72 const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null);
73 const { environmentId, threadId } = threadRef;
74 // Re-measures when the density or measurement key changes, so each density's
75 // height is measured once it renders.
76 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the card's DOM height via ResizeObserver and reports it to the canvas
77 useLayoutEffect(() => {
78 const element = contentElement;
79 if (!element || density === "essential") return;
80 const measure = () => {
81 const frame = element.closest<HTMLElement>("[data-thread-details-card]");
82 const next = element.offsetHeight + (frame ? frame.offsetHeight - frame.clientHeight : 0);
83 // PR lists, servers, lineage, side chats and automations scroll as they grow. Counting them
84 // toward density would hide the section and workspace controls when the user asks to see more.
85 const lists = element.querySelectorAll<HTMLElement>(
86 "[data-thread-relationships-panel], [data-thread-side-chats-panel], [data-thread-details-pr-list], [data-thread-automations-panel], [data-thread-details-server-overflow]",
87 );
88 const fittingHeight =
89 next - Array.from(lists).reduce((sum, list) => sum + list.offsetHeight, 0);
90 setMeasurements((current) => {
91 const heights = current.key === measurementKey ? current.heights : { full: 0, compact: 0 };
92 return current.key === measurementKey && heights[density] === fittingHeight
93 ? current
94 : { key: measurementKey, heights: { ...heights, [density]: fittingHeight } };
95 });
96 // Only the laid-out DOM knows the full card height, and the canvas keeps preview
97 // frames clear of it, so it is reported from this measure effect.
98 if (density === "full" && cardWidth !== undefined) {
99 measureDetailsCard?.({
100 threadRef: { environmentId, threadId },
101 width: cardWidth,
102 fullContentHeight: next,
103 });
104 }
105 };
106 measure();
107 return observeResize(element, measure);
108 }, [
109 contentElement,
110 density,
111 measurementKey,
112 cardWidth,
113 measureDetailsCard,
114 environmentId,
115 threadId,
116 ]);
117 const card = (
118 <div
119 className={cn(
120 "dropdown-glass isolate contain-paint grid max-h-full grid-rows-[minmax(0,1fr)] overflow-hidden rounded-2xl",
121 mode === "popover" &&
122 "max-h-[min(calc(100dvh-6.5rem),calc(var(--available-height,100dvh)-1rem))]",
123 )}
124 style={placement ? { maxHeight: height } : undefined}
125 onContextMenu={onContextMenu}
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 && inlineOpen ? (
152 <aside
153 {...detailsArea}
154 data-workspace-focus-key={`details:${threadRef.environmentId}:${threadRef.threadId}`}
155 // Skip the return transition to avoid overlapping the rising preview.
156 className={cn(
157 "absolute z-20",
158 topInset > 0 && "transition-[top] duration-150 ease-out motion-reduce:transition-none",
159 )}
160 style={{
161 right: placement.right,
162 top: placement.y,
163 width: placement.width,
164 maxHeight: height,
165 }}
166 data-density={density}
167 data-thread-details-panel="inline"
168 >
169 {card}
170 </aside>
171 ) : null}
172 {/* Rendered in both modes: base-ui keeps a popup that unmounts mid-open
173 mounted, because its exit animation never finishes. Staying in the tree
174 lets it close cleanly when the card switches inline. */}
175 <PopoverPopup
176 {...(mode === "popover" ? detailsArea : {})}
177 role="dialog"
178 data-workspace-focus-key={`details:${threadRef.environmentId}:${threadRef.threadId}`}
179 initialFocus={() => {
180 requestWorkspaceFocus({ area: "details" });
181 return false;
182 }}
183 anchor={anchor}
184 align="end"
185 // Account for the find panel's 8px card padding.
186 alignOffset={THREAD_DETAILS_CARD_GAP - 8}
187 collisionAvoidance={{ side: "shift", align: "shift", fallbackAxisSide: "none" }}
188 side="bottom"
189 sideOffset={topInset}
190 variant="panel"
191 padding="none"
192 >
193 {mode === "popover" ? (
194 <div data-density={density} data-thread-details-panel="popover">
195 {card}
196 </div>
197 ) : null}
198 </PopoverPopup>
199 </Popover>
200 );
201 }