The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { resolveTimelineMinimapPreview, type TimelineMinimapItem } from "./timelineMinimapItems"; |
| 2 | import { useRef, useState, type MouseEvent } from "react"; |
| 3 | import { T, useGT } from "gt-react"; |
| 4 | import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"; |
| 5 | import { Button } from "../ui/button"; |
| 6 | import { |
| 7 | resolveTimelineMinimapHeightStyle, |
| 8 | resolveTimelineMinimapIndexFromPointer, |
| 9 | resolveTimelineMinimapNavigationInteractive, |
| 10 | resolveTimelineMinimapTopPercent, |
| 11 | TIMELINE_MINIMAP_MIN_ITEMS, |
| 12 | } from "./MessagesTimeline.logic"; |
| 13 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 14 | import { cn } from "~/lib/utils"; |
| 15 | |
| 16 | // A release outside the window can skip `mouseup`, so the next button-free |
| 17 | // `mousemove` also counts. |
| 18 | function onMouseButtonsReleased(onRelease: () => void): () => void { |
| 19 | const check = (event: globalThis.MouseEvent) => { |
| 20 | if (event.buttons !== 0) return; |
| 21 | stop(); |
| 22 | onRelease(); |
| 23 | }; |
| 24 | const stop = () => { |
| 25 | window.removeEventListener("mouseup", check); |
| 26 | window.removeEventListener("mousemove", check); |
| 27 | }; |
| 28 | window.addEventListener("mouseup", check); |
| 29 | window.addEventListener("mousemove", check); |
| 30 | return stop; |
| 31 | } |
| 32 | |
| 33 | export function TimelineMinimap({ |
| 34 | hasPersistentGutter, |
| 35 | hitStripWidth, |
| 36 | currentIndex, |
| 37 | atEnd, |
| 38 | items, |
| 39 | stripMap, |
| 40 | onWheel, |
| 41 | onSelect, |
| 42 | }: { |
| 43 | hasPersistentGutter: boolean; |
| 44 | hitStripWidth: number; |
| 45 | currentIndex: number | null; |
| 46 | atEnd: boolean; |
| 47 | items: ReadonlyArray<TimelineMinimapItem>; |
| 48 | stripMap: Map<string, HTMLSpanElement>; |
| 49 | onWheel: (event: WheelEvent) => void; |
| 50 | onSelect: (item: TimelineMinimapItem) => void; |
| 51 | }) { |
| 52 | const gt = useGT(); |
| 53 | const [activeIndex, setActiveIndex] = useState<number | null>(null); |
| 54 | const wheelTargetRef = (node: HTMLDivElement | null) => { |
| 55 | if (!node) return; |
| 56 | node.addEventListener("wheel", onWheel, { passive: false }); |
| 57 | return () => node.removeEventListener("wheel", onWheel); |
| 58 | }; |
| 59 | |
| 60 | const pressedOnStripRef = useRef(false); |
| 61 | const [passThrough, setPassThrough] = useState(false); |
| 62 | |
| 63 | const resolvedActiveIndex = |
| 64 | activeIndex !== null && activeIndex < items.length ? activeIndex : null; |
| 65 | const activeItem = resolveTimelineMinimapPreview( |
| 66 | resolvedActiveIndex === null ? null : (items[resolvedActiveIndex] ?? null), |
| 67 | ); |
| 68 | const navigationInteractive = |
| 69 | resolveTimelineMinimapNavigationInteractive(hitStripWidth) && !passThrough; |
| 70 | const activeTopPercent = |
| 71 | resolvedActiveIndex === null |
| 72 | ? 0 |
| 73 | : resolveTimelineMinimapTopPercent(resolvedActiveIndex, items.length); |
| 74 | const resolvedCurrentIndex = |
| 75 | currentIndex !== null && currentIndex >= 0 && currentIndex < items.length ? currentIndex : null; |
| 76 | const previousItem = |
| 77 | resolvedCurrentIndex === null ? null : (items[resolvedCurrentIndex - 1] ?? null); |
| 78 | const nextItem = |
| 79 | resolvedCurrentIndex === null || atEnd ? null : (items[resolvedCurrentIndex + 1] ?? null); |
| 80 | |
| 81 | const resolveActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => { |
| 82 | const rect = event.currentTarget.getBoundingClientRect(); |
| 83 | return resolveTimelineMinimapIndexFromPointer({ |
| 84 | itemCount: items.length, |
| 85 | railTop: rect.top, |
| 86 | railHeight: rect.height, |
| 87 | pointerY: event.clientY, |
| 88 | }); |
| 89 | }; |
| 90 | |
| 91 | const updateActiveIndexFromPointer = (event: MouseEvent<HTMLElement>) => { |
| 92 | const nextIndex = resolveActiveIndexFromPointer(event); |
| 93 | setActiveIndex(nextIndex); |
| 94 | }; |
| 95 | |
| 96 | const moveActiveIndex = (delta: number) => { |
| 97 | setActiveIndex((current) => { |
| 98 | const base = current ?? 0; |
| 99 | return Math.max(0, Math.min(items.length - 1, base + delta)); |
| 100 | }); |
| 101 | }; |
| 102 | |
| 103 | if (items.length < TIMELINE_MINIMAP_MIN_ITEMS) { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | return ( |
| 108 | <div |
| 109 | className={cn( |
| 110 | "group/minimap pointer-events-none absolute inset-y-0 left-0 z-40 w-18", |
| 111 | hasPersistentGutter |
| 112 | ? "opacity-100" |
| 113 | : "hidden opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100 [@media(pointer:fine)]:block", |
| 114 | )} |
| 115 | data-testid="timeline-minimap" |
| 116 | data-persistent-gutter={hasPersistentGutter ? "true" : "false"} |
| 117 | ref={wheelTargetRef} |
| 118 | > |
| 119 | <div className="relative h-full w-full select-none"> |
| 120 | <div |
| 121 | className={cn( |
| 122 | "absolute top-1/2 left-3 -translate-y-1/2", |
| 123 | hitStripWidth > 0 && !passThrough ? "pointer-events-auto" : "pointer-events-none", |
| 124 | )} |
| 125 | onMouseDown={() => { |
| 126 | if (pressedOnStripRef.current) return; |
| 127 | pressedOnStripRef.current = true; |
| 128 | onMouseButtonsReleased(() => { |
| 129 | pressedOnStripRef.current = false; |
| 130 | }); |
| 131 | }} |
| 132 | onMouseMove={(event) => { |
| 133 | if (event.buttons === 0 || pressedOnStripRef.current || passThrough) return; |
| 134 | setPassThrough(true); |
| 135 | onMouseButtonsReleased(() => setPassThrough(false)); |
| 136 | }} |
| 137 | style={{ |
| 138 | height: resolveTimelineMinimapHeightStyle(items.length), |
| 139 | width: hitStripWidth, |
| 140 | }} |
| 141 | > |
| 142 | <TimelineMinimapNavigationButton |
| 143 | direction="previous" |
| 144 | disabled={previousItem === null} |
| 145 | interactive={navigationInteractive} |
| 146 | onClick={() => { |
| 147 | if (previousItem) onSelect(previousItem); |
| 148 | }} |
| 149 | /> |
| 150 | <button |
| 151 | aria-label={gt("Jump to message: {text}", { |
| 152 | text: activeItem?.userText ?? gt("User message"), |
| 153 | })} |
| 154 | className="absolute inset-y-0 left-0 w-full cursor-pointer bg-transparent before:absolute before:inset-x-0 before:-inset-y-1 before:content-[''] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70" |
| 155 | onBlur={() => setActiveIndex(null)} |
| 156 | onClick={(event) => { |
| 157 | const nextIndex = resolveActiveIndexFromPointer(event); |
| 158 | const selectedItem = nextIndex === null ? null : (items[nextIndex] ?? null); |
| 159 | if (selectedItem) { |
| 160 | onSelect(selectedItem); |
| 161 | } |
| 162 | event.currentTarget.blur(); |
| 163 | }} |
| 164 | onFocus={() => setActiveIndex((current) => current ?? resolvedCurrentIndex ?? 0)} |
| 165 | onKeyDown={(event) => { |
| 166 | if (event.key === "ArrowDown") { |
| 167 | event.preventDefault(); |
| 168 | moveActiveIndex(1); |
| 169 | } else if (event.key === "ArrowUp") { |
| 170 | event.preventDefault(); |
| 171 | moveActiveIndex(-1); |
| 172 | } else if (event.key === "Home") { |
| 173 | event.preventDefault(); |
| 174 | setActiveIndex(0); |
| 175 | } else if (event.key === "End") { |
| 176 | event.preventDefault(); |
| 177 | setActiveIndex(items.length - 1); |
| 178 | } else if (event.key === "Enter" || event.key === " ") { |
| 179 | event.preventDefault(); |
| 180 | if (activeItem) { |
| 181 | onSelect(activeItem); |
| 182 | } |
| 183 | } |
| 184 | }} |
| 185 | onMouseLeave={() => setActiveIndex(null)} |
| 186 | onMouseMove={(event) => { |
| 187 | if (event.buttons !== 0 && !pressedOnStripRef.current) return; |
| 188 | updateActiveIndexFromPointer(event); |
| 189 | }} |
| 190 | onMouseDown={(event) => event.preventDefault()} |
| 191 | type="button" |
| 192 | > |
| 193 | {items.map((item, index) => { |
| 194 | const top = `${resolveTimelineMinimapTopPercent(index, items.length)}%`; |
| 195 | const activeDistance = |
| 196 | resolvedActiveIndex === null ? null : Math.abs(index - resolvedActiveIndex); |
| 197 | return ( |
| 198 | // Compositor-friendly on purpose: in-view state and the hover |
| 199 | // fisheye animate constantly (every scroll tick and streaming |
| 200 | // update flips a band of strips), so the strip animates only |
| 201 | // transform and opacity. |
| 202 | <span |
| 203 | aria-hidden="true" |
| 204 | className={cn( |
| 205 | "pointer-events-none absolute left-0 h-0.5 w-6 origin-left -translate-y-1/2 rounded-full transition-transform duration-150 after:absolute after:inset-0 after:rounded-full after:bg-foreground/55 after:opacity-0 after:transition-opacity after:duration-150 data-[in-view=true]:after:opacity-100", |
| 206 | activeDistance === 0 ? "bg-muted-foreground/60" : "bg-muted-foreground/25", |
| 207 | activeDistance === 0 |
| 208 | ? "scale-x-100" |
| 209 | : activeDistance === 1 |
| 210 | ? "scale-x-[0.667]" |
| 211 | : activeDistance === 2 |
| 212 | ? "scale-x-[0.417]" |
| 213 | : "scale-x-[0.333]", |
| 214 | )} |
| 215 | data-in-view="false" |
| 216 | data-minimap-strip |
| 217 | key={item.id} |
| 218 | ref={(node) => { |
| 219 | if (node) { |
| 220 | stripMap.set(item.id, node); |
| 221 | } else { |
| 222 | stripMap.delete(item.id); |
| 223 | } |
| 224 | }} |
| 225 | style={{ top }} |
| 226 | /> |
| 227 | ); |
| 228 | })} |
| 229 | {activeItem ? ( |
| 230 | // Display-only: it overlaps the message column, and the strip keeps tracking beneath it. |
| 231 | <span |
| 232 | className="pointer-events-none absolute left-8 w-80 -translate-y-1/2" |
| 233 | style={{ top: `${activeTopPercent}%` }} |
| 234 | > |
| 235 | <span className="dropdown-glass block rounded-xl p-3 text-left text-popover-foreground shadow-xl shadow-black/25"> |
| 236 | <span className="block max-w-full overflow-hidden text-ellipsis whitespace-nowrap text-sm font-medium leading-5"> |
| 237 | {activeItem.userText ?? <T>User message</T>} |
| 238 | </span> |
| 239 | {activeItem.assistantText ? ( |
| 240 | <span |
| 241 | className="mt-1 max-h-[3.75rem] overflow-hidden text-muted-foreground text-sm leading-5" |
| 242 | style={{ |
| 243 | display: "-webkit-box", |
| 244 | WebkitBoxOrient: "vertical", |
| 245 | WebkitLineClamp: 3, |
| 246 | }} |
| 247 | > |
| 248 | {activeItem.assistantText} |
| 249 | </span> |
| 250 | ) : null} |
| 251 | </span> |
| 252 | </span> |
| 253 | ) : null} |
| 254 | </button> |
| 255 | <TimelineMinimapNavigationButton |
| 256 | direction="next" |
| 257 | disabled={nextItem === null} |
| 258 | interactive={navigationInteractive} |
| 259 | onClick={() => { |
| 260 | if (nextItem) onSelect(nextItem); |
| 261 | }} |
| 262 | /> |
| 263 | </div> |
| 264 | </div> |
| 265 | </div> |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | function TimelineMinimapNavigationButton({ |
| 270 | direction, |
| 271 | disabled, |
| 272 | interactive, |
| 273 | onClick, |
| 274 | }: { |
| 275 | direction: "previous" | "next"; |
| 276 | disabled: boolean; |
| 277 | interactive: boolean; |
| 278 | onClick: () => void; |
| 279 | }) { |
| 280 | const gt = useGT(); |
| 281 | const previous = direction === "previous"; |
| 282 | const label = previous ? gt("Previous turn") : gt("Next turn"); |
| 283 | const Icon = previous ? ChevronUpIcon : ChevronDownIcon; |
| 284 | |
| 285 | return ( |
| 286 | <Tooltip> |
| 287 | <TooltipTrigger |
| 288 | render={ |
| 289 | <span |
| 290 | className={cn( |
| 291 | "absolute left-1 z-10 inline-flex -translate-x-1/2 opacity-0 transition-opacity duration-150 hover:opacity-100 focus-within:opacity-100", |
| 292 | interactive ? "pointer-events-auto" : "pointer-events-none", |
| 293 | previous ? "bottom-[calc(100%+6px)]" : "top-[calc(100%+6px)]", |
| 294 | )} |
| 295 | /> |
| 296 | } |
| 297 | > |
| 298 | <Button |
| 299 | aria-label={label} |
| 300 | disabled={disabled} |
| 301 | onClick={onClick} |
| 302 | size="icon-micro" |
| 303 | type="button" |
| 304 | variant="ghost-muted" |
| 305 | > |
| 306 | <Icon className="size-4 text-foreground/90" /> |
| 307 | </Button> |
| 308 | </TooltipTrigger> |
| 309 | <TooltipPopup side={previous ? "top" : "bottom"}>{label}</TooltipPopup> |
| 310 | </Tooltip> |
| 311 | ); |
| 312 | } |