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
| 1 | import { |
| 2 | AuthOrchestrationOperateScope, |
| 3 | type EnvironmentId, |
| 4 | type ScopedThreadRef, |
| 5 | type ThreadId, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { scopeThreadRef } from "@t3tools/client-runtime/environment"; |
| 8 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 9 | import { |
| 10 | isAtomCommandInterrupted, |
| 11 | squashAtomCommandFailure, |
| 12 | } from "@t3tools/client-runtime/state/runtime"; |
| 13 | import { T, useGT, Var } from "gt-react"; |
| 14 | import { ChevronDownIcon } from "lucide-react"; |
| 15 | import { |
| 16 | memo, |
| 17 | useEffect, |
| 18 | useRef, |
| 19 | useState, |
| 20 | type KeyboardEvent as ReactKeyboardEvent, |
| 21 | type MouseEvent as ReactMouseEvent, |
| 22 | } from "react"; |
| 23 | import { isTrailingDoubleClick } from "../Sidebar.logic"; |
| 24 | import { Badge } from "../ui/badge"; |
| 25 | import { MiddleTruncate } from "../ui/middle-truncate"; |
| 26 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 27 | import { toastManager } from "../ui/toast"; |
| 28 | import { useThreadActionMenu } from "~/hooks/useThreadActionMenu"; |
| 29 | import { readLocalApi } from "~/localApi"; |
| 30 | import { threadEnvironment } from "../../state/threads"; |
| 31 | import { canShareWithGuestAtom } from "./guestShareAvailability"; |
| 32 | import { useAtomValue } from "@effect/atom-react"; |
| 33 | import { useOrchestrationCommand } from "../../state/use-orchestration-command"; |
| 34 | import { readEnvironmentScope, useEnvironmentScope } from "../../state/session"; |
| 35 | import { ProjectFavicon } from "../ProjectFavicon"; |
| 36 | import { |
| 37 | WorkspaceBreadcrumb, |
| 38 | WorkspaceBreadcrumbItem, |
| 39 | WorkspaceBreadcrumbSeparator, |
| 40 | WorkspaceBreadcrumbText, |
| 41 | } from "../WorkspaceBreadcrumb"; |
| 42 | import { observeResize } from "~/lib/observeResize"; |
| 43 | import { cn } from "~/lib/utils"; |
| 44 | import { textDirection } from "~/textDirection"; |
| 45 | import { useClientSettings } from "../../hooks/useSettings"; |
| 46 | import { GuestShareIndicator } from "./GuestShareControls"; |
| 47 | |
| 48 | interface ChatHeaderProps { |
| 49 | activeThreadEnvironmentId: EnvironmentId; |
| 50 | activeThreadId: ThreadId; |
| 51 | activeThreadTitle: string; |
| 52 | activeThreadLinkOrigin?: string | null | undefined; |
| 53 | isServerThread: boolean; |
| 54 | activeProject: EnvironmentProject | null; |
| 55 | parentThreadLink: { threadId: ThreadId; title: string } | null; |
| 56 | onOpenThread: (threadId: ThreadId) => void; |
| 57 | rightPanelOpen: boolean; |
| 58 | onNewThreadInProject: () => void; |
| 59 | onOpenProjectSettings?: (() => void) | undefined; |
| 60 | onFindInThread?: ((threadRef: ScopedThreadRef) => void) | undefined; |
| 61 | } |
| 62 | |
| 63 | function resolveRenameCommit(input: { |
| 64 | readonly title: string; |
| 65 | readonly originalTitle: string; |
| 66 | }): { action: "commit"; title: string } | { action: "reject-empty" } | { action: "noop" } { |
| 67 | const trimmed = input.title.trim(); |
| 68 | if (trimmed.length === 0) return { action: "reject-empty" }; |
| 69 | if (trimmed === input.originalTitle) return { action: "noop" }; |
| 70 | return { action: "commit", title: trimmed }; |
| 71 | } |
| 72 | |
| 73 | export const ChatHeader = memo(function ChatHeader({ |
| 74 | activeThreadEnvironmentId, |
| 75 | activeThreadId, |
| 76 | activeThreadTitle, |
| 77 | activeThreadLinkOrigin, |
| 78 | isServerThread, |
| 79 | activeProject, |
| 80 | parentThreadLink, |
| 81 | onOpenThread, |
| 82 | rightPanelOpen, |
| 83 | onNewThreadInProject, |
| 84 | onOpenProjectSettings, |
| 85 | onFindInThread, |
| 86 | }: ChatHeaderProps) { |
| 87 | const gt = useGT(); |
| 88 | const activeProjectName = activeProject?.isScratch ? gt("No project") : activeProject?.title; |
| 89 | const activeProjectCwd = activeProject?.workspaceRoot ?? null; |
| 90 | const interfaceFont = useClientSettings((settings) => settings.fontFamilySans); |
| 91 | const breadcrumbContainerRef = useRef<HTMLDivElement>(null); |
| 92 | const [collapseParentTitle, setCollapseParentTitle] = useState(false); |
| 93 | const activeThreadRef = scopeThreadRef(activeThreadEnvironmentId, activeThreadId); |
| 94 | const canOperateThread = useEnvironmentScope( |
| 95 | activeThreadEnvironmentId, |
| 96 | AuthOrchestrationOperateScope, |
| 97 | ); |
| 98 | const canShareWithGuest = useAtomValue(canShareWithGuestAtom(activeThreadEnvironmentId)); |
| 99 | const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, { |
| 100 | reportFailure: false, |
| 101 | }); |
| 102 | const [renaming, setRenaming] = useState<{ |
| 103 | threadId: ThreadId; |
| 104 | environmentId: EnvironmentId; |
| 105 | title: string; |
| 106 | } | null>(null); |
| 107 | if ( |
| 108 | renaming !== null && |
| 109 | (renaming.threadId !== activeThreadId || |
| 110 | renaming.environmentId !== activeThreadEnvironmentId || |
| 111 | !canOperateThread) |
| 112 | ) { |
| 113 | setRenaming(null); |
| 114 | } |
| 115 | const renamingTitle = |
| 116 | canOperateThread && |
| 117 | renaming?.threadId === activeThreadId && |
| 118 | renaming.environmentId === activeThreadEnvironmentId |
| 119 | ? renaming.title |
| 120 | : null; |
| 121 | const isRenamingTitle = renamingTitle !== null; |
| 122 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- measures the breadcrumb list DOM via ResizeObserver, a frame and font loads to collapse the parent title |
| 123 | useEffect(() => { |
| 124 | const list = breadcrumbContainerRef.current?.querySelector("ol"); |
| 125 | if (!list || !parentThreadLink) return; |
| 126 | const measure = () => { |
| 127 | const gap = Number.parseFloat(getComputedStyle(list).columnGap) || 0; |
| 128 | const width = Array.from(list.children).reduce( |
| 129 | (total, item) => { |
| 130 | const label = item.querySelector('[data-slot="workspace-breadcrumb-text"]'); |
| 131 | const ellipsis = item.querySelector("[data-parent-breadcrumb-ellipsis]"); |
| 132 | return ( |
| 133 | total + |
| 134 | (item.firstElementChild?.getBoundingClientRect().width ?? 0) + |
| 135 | (label && getComputedStyle(label).maxWidth === "none" |
| 136 | ? label.scrollWidth - label.clientWidth |
| 137 | : 0) - |
| 138 | (ellipsis?.getBoundingClientRect().width ?? 0) |
| 139 | ); |
| 140 | }, |
| 141 | gap * (list.children.length - 1), |
| 142 | ); |
| 143 | setCollapseParentTitle(width > list.clientWidth); |
| 144 | }; |
| 145 | measure(); |
| 146 | const frame = requestAnimationFrame(measure); |
| 147 | document.fonts.addEventListener("loadingdone", measure); |
| 148 | const stopObserving = observeResize(list, measure); |
| 149 | return () => { |
| 150 | cancelAnimationFrame(frame); |
| 151 | document.fonts.removeEventListener("loadingdone", measure); |
| 152 | stopObserving(); |
| 153 | }; |
| 154 | }, [ |
| 155 | // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Title, font and rename changes resize the breadcrumb text without resizing the list, so it re-measures. |
| 156 | activeProjectName, |
| 157 | activeThreadTitle, |
| 158 | parentThreadLink, |
| 159 | interfaceFont, |
| 160 | isRenamingTitle, |
| 161 | ]); |
| 162 | const renameCommittedRef = useRef(false); |
| 163 | const startRename = () => { |
| 164 | if ( |
| 165 | !isServerThread || |
| 166 | !readEnvironmentScope(activeThreadEnvironmentId, AuthOrchestrationOperateScope) |
| 167 | ) |
| 168 | return; |
| 169 | renameCommittedRef.current = false; |
| 170 | setRenaming({ |
| 171 | environmentId: activeThreadEnvironmentId, |
| 172 | threadId: activeThreadId, |
| 173 | title: activeThreadTitle, |
| 174 | }); |
| 175 | }; |
| 176 | const commitRename = (title: string) => { |
| 177 | setRenaming(null); |
| 178 | if (!readEnvironmentScope(activeThreadEnvironmentId, AuthOrchestrationOperateScope)) return; |
| 179 | const resolution = resolveRenameCommit({ |
| 180 | title, |
| 181 | originalTitle: renamingTitle ?? activeThreadTitle, |
| 182 | }); |
| 183 | if (resolution.action === "reject-empty") { |
| 184 | toastManager.add({ type: "warning", title: gt("Thread title cannot be empty") }); |
| 185 | return; |
| 186 | } |
| 187 | if (resolution.action === "noop") return; |
| 188 | void updateThreadMetadata({ |
| 189 | environmentId: activeThreadEnvironmentId, |
| 190 | input: { threadId: activeThreadId, title: resolution.title }, |
| 191 | }).then((result) => { |
| 192 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 193 | const error = squashAtomCommandFailure(result); |
| 194 | toastManager.add({ |
| 195 | type: "error", |
| 196 | title: gt("Failed to rename thread"), |
| 197 | description: error instanceof Error ? error.message : gt("An error occurred."), |
| 198 | }); |
| 199 | } |
| 200 | }); |
| 201 | }; |
| 202 | const { openMenu, closeMenu } = useThreadActionMenu({ |
| 203 | threadRef: isServerThread ? activeThreadRef : null, |
| 204 | projectCwd: activeProjectCwd, |
| 205 | onStartRename: startRename, |
| 206 | onFind: onFindInThread, |
| 207 | canShareWithGuest, |
| 208 | }); |
| 209 | const openMenuFromTitle = (event: ReactMouseEvent<HTMLButtonElement>) => { |
| 210 | if (isTrailingDoubleClick(event.detail)) return; |
| 211 | const rect = event.currentTarget.getBoundingClientRect(); |
| 212 | openMenu({ x: rect.left, y: rect.bottom + 4 }); |
| 213 | }; |
| 214 | // A native menu takes the second click, so only the in-page menu lets a double-click reach here. |
| 215 | const handleTitleDoubleClick = (event: ReactMouseEvent) => { |
| 216 | if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return; |
| 217 | if ((event.target as HTMLElement).closest("[data-thread-title-chevron]") !== null) return; |
| 218 | closeMenu(); |
| 219 | startRename(); |
| 220 | }; |
| 221 | const handleHeaderContextMenu = (event: ReactMouseEvent) => { |
| 222 | if (renamingTitle !== null) return; |
| 223 | if (!isServerThread && onOpenProjectSettings === undefined) return; |
| 224 | event.preventDefault(); |
| 225 | if (!isServerThread) { |
| 226 | const api = readLocalApi(); |
| 227 | if (!api) return; |
| 228 | void api.contextMenu |
| 229 | .show([{ id: "project-settings", label: gt("Project settings"), icon: "settings" }], { |
| 230 | x: event.clientX, |
| 231 | y: event.clientY, |
| 232 | }) |
| 233 | .then((action) => { |
| 234 | if (action === "project-settings") onOpenProjectSettings?.(); |
| 235 | }); |
| 236 | return; |
| 237 | } |
| 238 | openMenu({ x: event.clientX, y: event.clientY }); |
| 239 | }; |
| 240 | const handleRenameKeyDown = (event: ReactKeyboardEvent<HTMLInputElement>) => { |
| 241 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 242 | if (event.key === "Enter") { |
| 243 | renameCommittedRef.current = true; |
| 244 | commitRename(event.currentTarget.value); |
| 245 | } else if (event.key === "Escape") { |
| 246 | event.preventDefault(); |
| 247 | renameCommittedRef.current = true; |
| 248 | setRenaming(null); |
| 249 | } |
| 250 | }; |
| 251 | return ( |
| 252 | <div |
| 253 | ref={breadcrumbContainerRef} |
| 254 | className={cn( |
| 255 | "flex min-w-0 flex-1 items-center gap-2 sm:gap-3", |
| 256 | rightPanelOpen ? "pr-10" : "pr-24", |
| 257 | )} |
| 258 | onContextMenu={handleHeaderContextMenu} |
| 259 | > |
| 260 | <WorkspaceBreadcrumb |
| 261 | ariaLabel={gt("Thread breadcrumb")} |
| 262 | className="flex-1 overflow-clip [overflow-clip-margin:2px]" |
| 263 | > |
| 264 | {activeProject ? ( |
| 265 | <> |
| 266 | <WorkspaceBreadcrumbItem className="shrink"> |
| 267 | <Tooltip> |
| 268 | <TooltipTrigger |
| 269 | render={ |
| 270 | <button |
| 271 | type="button" |
| 272 | aria-label={gt("New thread in {project}", { project: activeProjectName })} |
| 273 | onClick={onNewThreadInProject} |
| 274 | className="inline-flex min-w-0 max-w-full cursor-pointer items-center gap-1.5 rounded-sm text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 275 | /> |
| 276 | } |
| 277 | > |
| 278 | <ProjectFavicon project={activeProject} className="size-3.5" /> |
| 279 | <MiddleTruncate |
| 280 | data-slot="workspace-breadcrumb-text" |
| 281 | value={activeProjectName ?? ""} |
| 282 | showTitle={false} |
| 283 | variant="cap-aligned" |
| 284 | /> |
| 285 | </TooltipTrigger> |
| 286 | <TooltipPopup side="top"> |
| 287 | <T> |
| 288 | New thread in <Var>{activeProjectName}</Var> |
| 289 | </T> |
| 290 | </TooltipPopup> |
| 291 | </Tooltip> |
| 292 | </WorkspaceBreadcrumbItem> |
| 293 | <WorkspaceBreadcrumbSeparator> |
| 294 | <WorkspaceBreadcrumbText>/</WorkspaceBreadcrumbText> |
| 295 | </WorkspaceBreadcrumbSeparator> |
| 296 | </> |
| 297 | ) : null} |
| 298 | {parentThreadLink ? ( |
| 299 | <> |
| 300 | <WorkspaceBreadcrumbItem> |
| 301 | <Tooltip> |
| 302 | <TooltipTrigger |
| 303 | render={ |
| 304 | <button |
| 305 | type="button" |
| 306 | aria-label={gt("Open parent thread: {title}", { |
| 307 | title: parentThreadLink.title, |
| 308 | })} |
| 309 | onClick={() => onOpenThread(parentThreadLink.threadId)} |
| 310 | className="inline-flex min-w-0 max-w-full cursor-pointer items-center rounded-sm text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 311 | /> |
| 312 | } |
| 313 | > |
| 314 | <WorkspaceBreadcrumbText |
| 315 | aria-hidden |
| 316 | className={collapseParentTitle ? "w-0" : undefined} |
| 317 | > |
| 318 | {parentThreadLink.title} |
| 319 | </WorkspaceBreadcrumbText> |
| 320 | {collapseParentTitle ? ( |
| 321 | <span aria-hidden data-parent-breadcrumb-ellipsis> |
| 322 | ... |
| 323 | </span> |
| 324 | ) : null} |
| 325 | </TooltipTrigger> |
| 326 | <TooltipPopup side="top">{parentThreadLink.title}</TooltipPopup> |
| 327 | </Tooltip> |
| 328 | </WorkspaceBreadcrumbItem> |
| 329 | <WorkspaceBreadcrumbSeparator> |
| 330 | <WorkspaceBreadcrumbText>/</WorkspaceBreadcrumbText> |
| 331 | </WorkspaceBreadcrumbSeparator> |
| 332 | </> |
| 333 | ) : null} |
| 334 | <WorkspaceBreadcrumbItem current className="min-w-10 flex-1"> |
| 335 | {renamingTitle !== null ? ( |
| 336 | <input |
| 337 | autoFocus |
| 338 | dir="auto" |
| 339 | aria-label={gt("Thread title")} |
| 340 | className="min-w-0 flex-1 rounded-sm bg-transparent text-sm font-medium text-foreground outline-none ring-1 ring-inset ring-ring/50 focus:ring-ring" |
| 341 | defaultValue={renamingTitle} |
| 342 | onBlur={(event) => { |
| 343 | if (renameCommittedRef.current) return; |
| 344 | if ( |
| 345 | event.relatedTarget instanceof HTMLElement && |
| 346 | event.relatedTarget.closest("button") |
| 347 | ) { |
| 348 | setRenaming(null); |
| 349 | return; |
| 350 | } |
| 351 | commitRename(event.currentTarget.value); |
| 352 | }} |
| 353 | onFocus={(event) => event.currentTarget.select()} |
| 354 | onKeyDown={handleRenameKeyDown} |
| 355 | /> |
| 356 | ) : isServerThread ? ( |
| 357 | <Tooltip> |
| 358 | <TooltipTrigger |
| 359 | render={ |
| 360 | <button |
| 361 | type="button" |
| 362 | aria-label={gt("Thread actions for {title}", { title: activeThreadTitle })} |
| 363 | aria-haspopup="menu" |
| 364 | onClick={openMenuFromTitle} |
| 365 | onDoubleClick={canOperateThread ? handleTitleDoubleClick : undefined} |
| 366 | className="group/thread-title inline-flex min-w-0 max-w-full cursor-pointer items-center gap-1 rounded-sm text-left focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 367 | /> |
| 368 | } |
| 369 | > |
| 370 | <h2 className="min-w-0" dir={textDirection(activeThreadTitle)}> |
| 371 | <WorkspaceBreadcrumbText>{activeThreadTitle}</WorkspaceBreadcrumbText> |
| 372 | </h2> |
| 373 | <ChevronDownIcon |
| 374 | aria-hidden |
| 375 | data-thread-title-chevron |
| 376 | className="size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/thread-title:opacity-100 group-focus-visible/thread-title:opacity-100" |
| 377 | /> |
| 378 | </TooltipTrigger> |
| 379 | <TooltipPopup side="top">{activeThreadTitle}</TooltipPopup> |
| 380 | </Tooltip> |
| 381 | ) : ( |
| 382 | <Tooltip> |
| 383 | <TooltipTrigger |
| 384 | render={ |
| 385 | <h2 |
| 386 | aria-label={activeThreadTitle} |
| 387 | className="min-w-0 flex-1" |
| 388 | dir={textDirection(activeThreadTitle)} |
| 389 | /> |
| 390 | } |
| 391 | > |
| 392 | <WorkspaceBreadcrumbText>{activeThreadTitle}</WorkspaceBreadcrumbText> |
| 393 | </TooltipTrigger> |
| 394 | <TooltipPopup side="top">{activeThreadTitle}</TooltipPopup> |
| 395 | </Tooltip> |
| 396 | )} |
| 397 | {renamingTitle === null && activeThreadLinkOrigin ? ( |
| 398 | <Tooltip> |
| 399 | <TooltipTrigger render={<Badge variant="info" size="sm" />}> |
| 400 | <T> |
| 401 | From <Var>{activeThreadLinkOrigin}</Var> |
| 402 | </T> |
| 403 | </TooltipTrigger> |
| 404 | <TooltipPopup side="top"> |
| 405 | <T> |
| 406 | Started by an agent on <Var>{activeThreadLinkOrigin}</Var> through a linked |
| 407 | environment. |
| 408 | </T> |
| 409 | </TooltipPopup> |
| 410 | </Tooltip> |
| 411 | ) : null} |
| 412 | </WorkspaceBreadcrumbItem> |
| 413 | </WorkspaceBreadcrumb> |
| 414 | {isServerThread ? <GuestShareIndicator threadRef={activeThreadRef} /> : null} |
| 415 | </div> |
| 416 | ); |
| 417 | }); |