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 type { |
| 2 | EditorId, |
| 3 | EnvironmentId, |
| 4 | ProjectId, |
| 5 | ProjectScript, |
| 6 | ResolvedKeybindingsConfig, |
| 7 | ThreadId, |
| 8 | } from "@t3tools/contracts"; |
| 9 | |
| 10 | import { scopeProjectRef } from "@t3tools/client-runtime/environment"; |
| 11 | import { isScratchProject } from "@t3tools/client-runtime/state/projects"; |
| 12 | |
| 13 | import type { ComposerThreadTarget, DraftId } from "../../composerDraftStore"; |
| 14 | import { useT3ProjectFileScripts } from "../../hooks/useT3ProjectFileScripts"; |
| 15 | import { type EnvMode, type EnvironmentOption } from "../BranchToolbar.logic"; |
| 16 | import { BranchToolbar } from "../BranchToolbar"; |
| 17 | import GitActionsControl from "../GitActionsControl"; |
| 18 | import ProjectScriptsControl, { |
| 19 | type NewProjectScriptInput, |
| 20 | type ProjectScriptActionResult, |
| 21 | } from "../ProjectScriptsControl"; |
| 22 | import { T, useGT } from "gt-react"; |
| 23 | import { useState, type ComponentProps } from "react"; |
| 24 | import { ThreadDetailsCard } from "./ThreadDetailsCard"; |
| 25 | import { OpenInPicker } from "./OpenInPicker"; |
| 26 | import { ThreadDetailsSection } from "./ThreadDetailsSection"; |
| 27 | import { ThreadAutomationsPanel } from "./ThreadAutomationsPanel"; |
| 28 | import { ThreadRelationshipsPanel } from "./ThreadRelationshipsControl"; |
| 29 | import { ThreadServersPanel } from "./ThreadServersPanel"; |
| 30 | import { |
| 31 | ThreadDetailsContext, |
| 32 | ThreadDetailsUsage, |
| 33 | type ThreadDetailsUsageInput, |
| 34 | } from "./ThreadDetailsUsage"; |
| 35 | import { ThreadDetailsItemsMenuItems } from "./ThreadDetailsItemsMenu"; |
| 36 | import { resolveThreadDetailsVisibility, THREAD_DETAILS_ITEMS } from "./threadDetailsItems"; |
| 37 | import { useClientSettings } from "../../hooks/useSettings"; |
| 38 | import { useProject, useServerConfigs } from "../../state/entities"; |
| 39 | import { ThreadDetailsThreadPrRows } from "./ThreadDetailsPrRows"; |
| 40 | import { cn } from "../../lib/utils"; |
| 41 | import { SlidersHorizontalIcon } from "lucide-react"; |
| 42 | import { ThreadDetailsControl } from "./ThreadDetailsControl"; |
| 43 | import { |
| 44 | THREAD_DETAILS_PANEL_ICON_CLASS, |
| 45 | THREAD_DETAILS_PANEL_LABEL_CLASS, |
| 46 | } from "./threadDetailsPanelStyles"; |
| 47 | import { Menu, MenuPopup } from "../ui/menu"; |
| 48 | |
| 49 | interface ThreadDetailsPanelProps extends Pick< |
| 50 | ComponentProps<typeof ThreadDetailsCard>, |
| 51 | "anchor" | "handle" |
| 52 | > { |
| 53 | forceNewWorktree?: boolean; |
| 54 | environmentId: EnvironmentId; |
| 55 | threadId: ThreadId; |
| 56 | projectId: ProjectId; |
| 57 | worktreePath: string | null; |
| 58 | isServerThread: boolean; |
| 59 | draftId?: DraftId; |
| 60 | composerDraftTarget: ComposerThreadTarget; |
| 61 | activeProjectName: string | undefined; |
| 62 | activeProjectScripts: ReadonlyArray<ProjectScript> | undefined; |
| 63 | preferredScriptId: string | null; |
| 64 | keybindings: ResolvedKeybindingsConfig; |
| 65 | availableEditors: ReadonlyArray<EditorId>; |
| 66 | showOpenInPicker: boolean; |
| 67 | openInShortcutEnabled: boolean; |
| 68 | gitCwd: string | null; |
| 69 | workspaceMutationId: string | null; |
| 70 | isGitRepo: boolean; |
| 71 | envLocked: boolean; |
| 72 | availableEnvironments: readonly EnvironmentOption[]; |
| 73 | autoEnvironmentLabel?: string | undefined; |
| 74 | onAutoEnvironment?: (() => void) | undefined; |
| 75 | onEnvironmentChange?: (environmentId: EnvironmentId) => void; |
| 76 | onEnvModeChange: (mode: EnvMode) => void; |
| 77 | envMode: EnvMode; |
| 78 | activeThreadBranchOverride?: string | null; |
| 79 | onActiveThreadBranchOverrideChange?: (branch: string | null) => void; |
| 80 | startFromOrigin: boolean; |
| 81 | onStartFromOriginChange: (startFromOrigin: boolean) => void; |
| 82 | onCheckoutPullRequestRequest?: (reference: string) => void; |
| 83 | onComposerFocusRequest: () => void; |
| 84 | onOpenChanges?: () => void; |
| 85 | onRunProjectScript: (script: ProjectScript) => void; |
| 86 | onAddProjectScript: (input: NewProjectScriptInput) => Promise<ProjectScriptActionResult>; |
| 87 | onUpdateProjectScript: ( |
| 88 | scriptId: string, |
| 89 | input: NewProjectScriptInput, |
| 90 | ) => Promise<ProjectScriptActionResult>; |
| 91 | onDeleteProjectScript: (scriptId: string) => Promise<ProjectScriptActionResult>; |
| 92 | usage: ThreadDetailsUsageInput; |
| 93 | } |
| 94 | |
| 95 | export function ThreadDetailsPanel(props: ThreadDetailsPanelProps) { |
| 96 | const visible = useClientSettings(resolveThreadDetailsVisibility); |
| 97 | const gt = useGT(); |
| 98 | const [menuPosition, setMenuPosition] = useState<{ x: number; y: number } | null>(null); |
| 99 | const fileScripts = useT3ProjectFileScripts( |
| 100 | props.environmentId, |
| 101 | props.activeProjectScripts && visible.scripts ? props.gitCwd : null, |
| 102 | ); |
| 103 | const activeThreadRef = { environmentId: props.environmentId, threadId: props.threadId }; |
| 104 | const project = useProject(scopeProjectRef(props.environmentId, props.projectId)); |
| 105 | const serverConfig = useServerConfigs().get(props.environmentId); |
| 106 | // A No project thread's folder sits inside the shared scratch root, so it must never get git. |
| 107 | // Until the config and project load, a No project thread looks like any other folder. |
| 108 | const offersGit = |
| 109 | props.gitCwd !== null && |
| 110 | serverConfig !== undefined && |
| 111 | project !== null && |
| 112 | !isScratchProject(project, serverConfig.scratchWorkspaceRoot); |
| 113 | const branchToolbarProps = { |
| 114 | showGitControls: props.isGitRepo, |
| 115 | environmentId: props.environmentId, |
| 116 | threadId: props.threadId, |
| 117 | projectId: props.projectId, |
| 118 | worktreePath: props.worktreePath, |
| 119 | isServerThread: props.isServerThread, |
| 120 | ...(props.draftId ? { draftId: props.draftId } : {}), |
| 121 | onEnvModeChange: props.onEnvModeChange, |
| 122 | startFromOrigin: props.startFromOrigin, |
| 123 | onStartFromOriginChange: props.onStartFromOriginChange, |
| 124 | envMode: props.envMode, |
| 125 | ...(props.activeThreadBranchOverride !== undefined |
| 126 | ? { activeThreadBranchOverride: props.activeThreadBranchOverride } |
| 127 | : {}), |
| 128 | ...(props.onActiveThreadBranchOverrideChange |
| 129 | ? { onActiveThreadBranchOverrideChange: props.onActiveThreadBranchOverrideChange } |
| 130 | : {}), |
| 131 | envLocked: props.envLocked, |
| 132 | forceNewWorktree: props.forceNewWorktree ?? false, |
| 133 | onComposerFocusRequest: props.onComposerFocusRequest, |
| 134 | ...(props.onCheckoutPullRequestRequest |
| 135 | ? { onCheckoutPullRequestRequest: props.onCheckoutPullRequestRequest } |
| 136 | : {}), |
| 137 | }; |
| 138 | |
| 139 | return ( |
| 140 | <ThreadDetailsCard |
| 141 | threadRef={{ environmentId: props.environmentId, threadId: props.threadId }} |
| 142 | anchor={props.anchor} |
| 143 | handle={props.handle} |
| 144 | contentKey={THREAD_DETAILS_ITEMS.filter((item) => visible[item.id]) |
| 145 | .map((item) => item.id) |
| 146 | .join(",")} |
| 147 | onContextMenu={(event) => { |
| 148 | // Popups the card opens bubble here through React; they, fields, links, and selected text |
| 149 | // keep their own menus. |
| 150 | const target = event.target; |
| 151 | const selection = window.getSelection(); |
| 152 | if ( |
| 153 | !(target instanceof Element) || |
| 154 | !event.currentTarget.contains(target) || |
| 155 | target.closest("input, textarea, [contenteditable], a[href]") || |
| 156 | (selection && |
| 157 | !selection.isCollapsed && |
| 158 | (event.currentTarget.contains(selection.anchorNode) || |
| 159 | event.currentTarget.contains(selection.focusNode))) |
| 160 | ) |
| 161 | return; |
| 162 | event.preventDefault(); |
| 163 | setMenuPosition({ x: event.clientX, y: event.clientY }); |
| 164 | }} |
| 165 | > |
| 166 | {(density) => { |
| 167 | // Section flags follow the rows that will render, so no empty section steals the divider rule. |
| 168 | const show = { |
| 169 | environment: density === "full" && visible.environment, |
| 170 | openIn: density !== "essential" && visible.openIn && props.showOpenInPicker, |
| 171 | scripts: visible.scripts && props.activeProjectScripts !== undefined, |
| 172 | branch: offersGit && props.isGitRepo && visible.branch, |
| 173 | // Without a branch selector, linked pull requests still need somewhere to show. |
| 174 | pullRequests: !(offersGit && props.isGitRepo) && visible.branch, |
| 175 | git: |
| 176 | offersGit && |
| 177 | props.activeProjectName !== undefined && |
| 178 | (visible.gitActions || (visible.changes && props.isGitRepo)), |
| 179 | }; |
| 180 | return ( |
| 181 | <> |
| 182 | <div className="group/details-items"> |
| 183 | {show.environment || show.openIn || show.scripts ? ( |
| 184 | <ThreadDetailsSection |
| 185 | headingId="thread-details-workspace-heading" |
| 186 | title={gt("Workspace")} |
| 187 | separated={false} |
| 188 | showHeading={false} |
| 189 | > |
| 190 | <div className="flex flex-col"> |
| 191 | {show.environment ? ( |
| 192 | <BranchToolbar |
| 193 | layout="panel" |
| 194 | panelSection="workspace" |
| 195 | availableEnvironments={props.availableEnvironments} |
| 196 | {...(props.onEnvironmentChange |
| 197 | ? { onEnvironmentChange: props.onEnvironmentChange } |
| 198 | : {})} |
| 199 | autoEnvironmentLabel={props.autoEnvironmentLabel} |
| 200 | onAutoEnvironment={props.onAutoEnvironment} |
| 201 | {...branchToolbarProps} |
| 202 | /> |
| 203 | ) : null} |
| 204 | |
| 205 | {show.openIn ? ( |
| 206 | <OpenInPicker |
| 207 | keybindings={props.keybindings} |
| 208 | environmentId={props.environmentId} |
| 209 | availableEditors={props.availableEditors} |
| 210 | openInCwd={props.gitCwd} |
| 211 | enableShortcut={props.openInShortcutEnabled} |
| 212 | displayMode="panel" |
| 213 | /> |
| 214 | ) : null} |
| 215 | |
| 216 | {show.scripts && props.activeProjectScripts ? ( |
| 217 | <ProjectScriptsControl |
| 218 | environmentId={props.environmentId} |
| 219 | displayMode="panel" |
| 220 | scripts={props.activeProjectScripts} |
| 221 | fileScripts={fileScripts} |
| 222 | preferredScriptId={props.preferredScriptId} |
| 223 | onRunScript={props.onRunProjectScript} |
| 224 | onAddScript={props.onAddProjectScript} |
| 225 | onUpdateScript={props.onUpdateProjectScript} |
| 226 | onDeleteScript={props.onDeleteProjectScript} |
| 227 | /> |
| 228 | ) : null} |
| 229 | </div> |
| 230 | </ThreadDetailsSection> |
| 231 | ) : null} |
| 232 | |
| 233 | {show.branch || show.pullRequests || show.git ? ( |
| 234 | <ThreadDetailsSection |
| 235 | headingId="thread-details-version-control-heading" |
| 236 | title={gt("Version Control")} |
| 237 | showHeading={false} |
| 238 | separated={density === "full"} |
| 239 | > |
| 240 | <div className="flex flex-col"> |
| 241 | {show.branch ? ( |
| 242 | <BranchToolbar layout="panel" panelSection="branch" {...branchToolbarProps} /> |
| 243 | ) : null} |
| 244 | {show.pullRequests ? ( |
| 245 | <ThreadDetailsThreadPrRows |
| 246 | threadRef={activeThreadRef} |
| 247 | project={project} |
| 248 | composerTarget={props.composerDraftTarget} |
| 249 | /> |
| 250 | ) : null} |
| 251 | {show.git && props.gitCwd ? ( |
| 252 | <GitActionsControl |
| 253 | compact={density !== "full"} |
| 254 | showActions={visible.gitActions} |
| 255 | showChanges={visible.changes} |
| 256 | gitCwd={props.gitCwd} |
| 257 | workspaceMutationId={props.workspaceMutationId} |
| 258 | activeThreadRef={activeThreadRef} |
| 259 | {...(props.draftId ? { draftId: props.draftId } : {})} |
| 260 | {...(props.onOpenChanges ? { onOpenChanges: props.onOpenChanges } : {})} |
| 261 | /> |
| 262 | ) : null} |
| 263 | </div> |
| 264 | </ThreadDetailsSection> |
| 265 | ) : null} |
| 266 | |
| 267 | {density === "full" && !props.draftId && visible.servers ? ( |
| 268 | <ThreadServersPanel environmentId={props.environmentId} threadId={props.threadId} /> |
| 269 | ) : null} |
| 270 | |
| 271 | {density === "full" && !props.draftId && visible.automations ? ( |
| 272 | <ThreadAutomationsPanel |
| 273 | environmentId={props.environmentId} |
| 274 | threadId={props.threadId} |
| 275 | /> |
| 276 | ) : null} |
| 277 | |
| 278 | {density === "full" && !props.draftId && (visible.lineage || visible.sideChats) ? ( |
| 279 | <ThreadRelationshipsPanel |
| 280 | environmentId={props.environmentId} |
| 281 | threadId={props.threadId} |
| 282 | showLineage={visible.lineage} |
| 283 | showSideChats={visible.sideChats} |
| 284 | /> |
| 285 | ) : null} |
| 286 | |
| 287 | {visible.usage ? <ThreadDetailsUsage {...props.usage} /> : null} |
| 288 | |
| 289 | {visible.context && props.usage.contextWindow ? ( |
| 290 | <ThreadDetailsContext usage={props.usage.contextWindow} /> |
| 291 | ) : null} |
| 292 | |
| 293 | {/* Shown only when no section above has content, so an emptied card can be refilled. */} |
| 294 | <div className="p-2 group-has-[>section>:last-child:not(:empty)]/details-items:hidden"> |
| 295 | <ThreadDetailsControl |
| 296 | size="xs" |
| 297 | variant="ghost" |
| 298 | part="row" |
| 299 | panel |
| 300 | onClick={(event) => { |
| 301 | const rect = event.currentTarget.getBoundingClientRect(); |
| 302 | setMenuPosition({ x: rect.left, y: rect.bottom }); |
| 303 | }} |
| 304 | > |
| 305 | <SlidersHorizontalIcon className={THREAD_DETAILS_PANEL_ICON_CLASS} aria-hidden /> |
| 306 | <span className={cn("ml-0.5", THREAD_DETAILS_PANEL_LABEL_CLASS)}> |
| 307 | <T>Choose what to show</T> |
| 308 | </span> |
| 309 | </ThreadDetailsControl> |
| 310 | </div> |
| 311 | </div> |
| 312 | |
| 313 | <Menu |
| 314 | open={menuPosition !== null} |
| 315 | onOpenChange={(open) => !open && setMenuPosition(null)} |
| 316 | > |
| 317 | <MenuPopup |
| 318 | anchor={ |
| 319 | menuPosition |
| 320 | ? { |
| 321 | getBoundingClientRect: () => |
| 322 | new DOMRect(menuPosition.x, menuPosition.y, 0, 0), |
| 323 | } |
| 324 | : undefined |
| 325 | } |
| 326 | align="start" |
| 327 | side="bottom" |
| 328 | > |
| 329 | <ThreadDetailsItemsMenuItems /> |
| 330 | </MenuPopup> |
| 331 | </Menu> |
| 332 | </> |
| 333 | ); |
| 334 | }} |
| 335 | </ThreadDetailsCard> |
| 336 | ); |
| 337 | } |