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 { ThreadDetailsControl } from "./ThreadDetailsControl"; |
| 2 | import { |
| 3 | AuthOrchestrationOperateScope, |
| 4 | buildRemoteOpenUrl, |
| 5 | EditorId, |
| 6 | type EnvironmentId, |
| 7 | type ResolvedKeybindingsConfig, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 10 | import { memo, use, useRef } from "react"; |
| 11 | import { BackgroundThreadContext } from "./backgroundThread"; |
| 12 | import { shortcutLabelForCommand } from "../../keybindings"; |
| 13 | import { OPEN_FAVORITE_EDITOR_ATTRIBUTE } from "./OpenInPickerShortcut"; |
| 14 | import { usePreferredEditor } from "../../editorPreferences"; |
| 15 | import { editorLabelForPlatform } from "../../editorLabels"; |
| 16 | import type { MessageResolver } from "../../i18n/messages"; |
| 17 | import { |
| 18 | openRemoteEditorUrl, |
| 19 | useRemoteCapableEditors, |
| 20 | useRemoteOpenHint, |
| 21 | useRemoteOpenState, |
| 22 | } from "../../remoteOpen"; |
| 23 | import { useEnvironment } from "../../state/environments"; |
| 24 | import { ChevronDownIcon, FolderClosedIcon, SquareArrowOutUpRightIcon } from "lucide-react"; |
| 25 | |
| 26 | import { Group, GroupSeparator } from "../ui/group"; |
| 27 | import { |
| 28 | Menu, |
| 29 | MenuItem, |
| 30 | MenuItemLabel, |
| 31 | MenuPopup, |
| 32 | MenuShortcut, |
| 33 | MenuSub, |
| 34 | MenuSubTrigger, |
| 35 | MenuSubPopup, |
| 36 | MenuTrigger, |
| 37 | } from "../ui/menu"; |
| 38 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 39 | import { |
| 40 | AntigravityIcon, |
| 41 | CursorIcon, |
| 42 | FileExplorerIcon, |
| 43 | FinderIcon, |
| 44 | Icon, |
| 45 | KiroIcon, |
| 46 | TraeIcon, |
| 47 | VisualStudioCode, |
| 48 | VisualStudioCodeInsiders, |
| 49 | VSCodium, |
| 50 | Zed, |
| 51 | } from "../Icons"; |
| 52 | import { |
| 53 | AquaIcon, |
| 54 | CLionIcon, |
| 55 | DataGripIcon, |
| 56 | DataSpellIcon, |
| 57 | GoLandIcon, |
| 58 | IntelliJIdeaIcon, |
| 59 | PhpStormIcon, |
| 60 | PyCharmIcon, |
| 61 | RiderIcon, |
| 62 | RubyMineIcon, |
| 63 | RustRoverIcon, |
| 64 | WebStormIcon, |
| 65 | } from "../JetBrainsIcons"; |
| 66 | import { announce } from "~/lib/announce"; |
| 67 | import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils"; |
| 68 | import { shellEnvironment } from "~/state/shell"; |
| 69 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 70 | import { |
| 71 | THREAD_DETAILS_PANEL_CHEVRON_CLASS, |
| 72 | THREAD_DETAILS_PANEL_ICON_CLASS, |
| 73 | THREAD_DETAILS_PANEL_LABEL_CLASS, |
| 74 | THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS, |
| 75 | THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS, |
| 76 | } from "./threadDetailsPanelStyles"; |
| 77 | import { readEnvironmentScope, useEnvironmentScope } from "~/state/session"; |
| 78 | import { useComposerMenuState } from "./useComposerMenuState"; |
| 79 | |
| 80 | type OpenInOption = { |
| 81 | label: string; |
| 82 | Icon: Icon; |
| 83 | value: EditorId; |
| 84 | kind: "brand" | "generic"; |
| 85 | }; |
| 86 | |
| 87 | const resolveOpenInOptions = ( |
| 88 | platform: string, |
| 89 | availableEditors: ReadonlyArray<EditorId>, |
| 90 | resolve: MessageResolver, |
| 91 | ) => { |
| 92 | const baseOptions: ReadonlyArray<Omit<OpenInOption, "label">> = [ |
| 93 | { |
| 94 | Icon: CursorIcon, |
| 95 | value: "cursor", |
| 96 | kind: "brand", |
| 97 | }, |
| 98 | { |
| 99 | Icon: TraeIcon, |
| 100 | value: "trae", |
| 101 | kind: "brand", |
| 102 | }, |
| 103 | { |
| 104 | Icon: KiroIcon, |
| 105 | value: "kiro", |
| 106 | kind: "brand", |
| 107 | }, |
| 108 | { |
| 109 | Icon: VisualStudioCode, |
| 110 | value: "vscode", |
| 111 | kind: "brand", |
| 112 | }, |
| 113 | { |
| 114 | Icon: VisualStudioCodeInsiders, |
| 115 | value: "vscode-insiders", |
| 116 | kind: "brand", |
| 117 | }, |
| 118 | { |
| 119 | Icon: VSCodium, |
| 120 | value: "vscodium", |
| 121 | kind: "brand", |
| 122 | }, |
| 123 | { |
| 124 | Icon: Zed, |
| 125 | value: "zed", |
| 126 | kind: "brand", |
| 127 | }, |
| 128 | { |
| 129 | Icon: AntigravityIcon, |
| 130 | value: "antigravity", |
| 131 | kind: "brand", |
| 132 | }, |
| 133 | { |
| 134 | Icon: IntelliJIdeaIcon, |
| 135 | value: "idea", |
| 136 | kind: "brand", |
| 137 | }, |
| 138 | { |
| 139 | Icon: AquaIcon, |
| 140 | value: "aqua", |
| 141 | kind: "brand", |
| 142 | }, |
| 143 | { |
| 144 | Icon: CLionIcon, |
| 145 | value: "clion", |
| 146 | kind: "brand", |
| 147 | }, |
| 148 | { |
| 149 | Icon: DataGripIcon, |
| 150 | value: "datagrip", |
| 151 | kind: "brand", |
| 152 | }, |
| 153 | { |
| 154 | Icon: DataSpellIcon, |
| 155 | value: "dataspell", |
| 156 | kind: "brand", |
| 157 | }, |
| 158 | { |
| 159 | Icon: GoLandIcon, |
| 160 | value: "goland", |
| 161 | kind: "brand", |
| 162 | }, |
| 163 | { |
| 164 | Icon: PhpStormIcon, |
| 165 | value: "phpstorm", |
| 166 | kind: "brand", |
| 167 | }, |
| 168 | { |
| 169 | Icon: PyCharmIcon, |
| 170 | value: "pycharm", |
| 171 | kind: "brand", |
| 172 | }, |
| 173 | { |
| 174 | Icon: RiderIcon, |
| 175 | value: "rider", |
| 176 | kind: "brand", |
| 177 | }, |
| 178 | { |
| 179 | Icon: RubyMineIcon, |
| 180 | value: "rubymine", |
| 181 | kind: "brand", |
| 182 | }, |
| 183 | { |
| 184 | Icon: RustRoverIcon, |
| 185 | value: "rustrover", |
| 186 | kind: "brand", |
| 187 | }, |
| 188 | { |
| 189 | Icon: WebStormIcon, |
| 190 | value: "webstorm", |
| 191 | kind: "brand", |
| 192 | }, |
| 193 | { |
| 194 | Icon: isMacPlatform(platform) |
| 195 | ? FinderIcon |
| 196 | : isWindowsPlatform(platform) |
| 197 | ? FileExplorerIcon |
| 198 | : FolderClosedIcon, |
| 199 | value: "file-manager", |
| 200 | kind: isMacPlatform(platform) || isWindowsPlatform(platform) ? "brand" : "generic", |
| 201 | }, |
| 202 | ]; |
| 203 | const availableEditorSet = new Set(availableEditors); |
| 204 | return baseOptions |
| 205 | .filter((option) => availableEditorSet.has(option.value)) |
| 206 | .map((option) => ({ |
| 207 | ...option, |
| 208 | label: editorLabelForPlatform(option.value, platform, resolve), |
| 209 | })); |
| 210 | }; |
| 211 | |
| 212 | function getOpenInIconClass(kind: OpenInOption["kind"]) { |
| 213 | return cn(kind === "brand" ? "text-foreground opacity-100" : "text-muted-foreground"); |
| 214 | } |
| 215 | |
| 216 | export const OpenInPicker = memo(function OpenInPicker({ |
| 217 | environmentId, |
| 218 | keybindings, |
| 219 | availableEditors, |
| 220 | openInCwd, |
| 221 | openInCwdIsFile = false, |
| 222 | presentation = "toolbar", |
| 223 | compact = false, |
| 224 | enableShortcut = true, |
| 225 | displayMode = "toolbar", |
| 226 | revealFileManager = false, |
| 227 | }: { |
| 228 | environmentId: EnvironmentId; |
| 229 | keybindings: ResolvedKeybindingsConfig; |
| 230 | availableEditors: ReadonlyArray<EditorId>; |
| 231 | openInCwd: string | null; |
| 232 | openInCwdIsFile?: boolean; |
| 233 | presentation?: "toolbar" | "menu"; |
| 234 | compact?: boolean; |
| 235 | enableShortcut?: boolean; |
| 236 | displayMode?: "toolbar" | "panel"; |
| 237 | revealFileManager?: boolean; |
| 238 | }) { |
| 239 | const gt = useGT(); |
| 240 | const messages = useMessages(); |
| 241 | const isPanel = displayMode === "panel"; |
| 242 | const ActionGroup = isPanel ? "div" : Group; |
| 243 | const panelAnchorRef = useRef<HTMLDivElement | null>(null); |
| 244 | const openInEditorMutation = useAtomCommand(shellEnvironment.openInEditor, "open in editor"); |
| 245 | const remote = useRemoteOpenState(environmentId); |
| 246 | const canOperateHost = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope); |
| 247 | const isHostEditorDenied = remote.mode === "local-exec" && !canOperateHost; |
| 248 | const canOpenEditor = remote.mode !== "remote-unavailable" && !isHostEditorDenied; |
| 249 | const [menuOpen, setMenuOpen] = useComposerMenuState(isHostEditorDenied); |
| 250 | const remoteCapableEditors = useRemoteCapableEditors(); |
| 251 | const [remoteHintSeen, markRemoteHintSeen] = useRemoteOpenHint(); |
| 252 | const environmentLabel = useEnvironment(environmentId)?.label ?? null; |
| 253 | // Remote editors run on the viewing machine; only desktop can probe that PATH. |
| 254 | const effectiveEditors = remote.mode === "local-exec" ? availableEditors : remoteCapableEditors; |
| 255 | const [preferredEditor, setPreferredEditor] = usePreferredEditor(effectiveEditors); |
| 256 | |
| 257 | const openInEditor = async (editorId: EditorId | null): Promise<boolean> => { |
| 258 | if (!openInCwd) return false; |
| 259 | const editor = editorId ?? preferredEditor; |
| 260 | if (!editor) return false; |
| 261 | if (remote.mode === "remote-unavailable") return false; |
| 262 | if (remote.mode === "remote-links") { |
| 263 | const url = buildRemoteOpenUrl({ |
| 264 | editor, |
| 265 | ...remote.host, |
| 266 | absolutePath: openInCwd, |
| 267 | isFile: openInCwdIsFile, |
| 268 | }); |
| 269 | if (url === undefined) return false; |
| 270 | // Only record hint-seen/preferred when the shell actually accepted |
| 271 | // the URL (an older desktop build can refuse the editor scheme). |
| 272 | const opened = await openRemoteEditorUrl(url); |
| 273 | if (!opened) return false; |
| 274 | markRemoteHintSeen(); |
| 275 | setPreferredEditor(editor); |
| 276 | return true; |
| 277 | } |
| 278 | if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return false; |
| 279 | const result = openInEditorMutation({ |
| 280 | environmentId, |
| 281 | input: { |
| 282 | cwd: openInCwd, |
| 283 | editor, |
| 284 | ...(revealFileManager && editor === "file-manager" ? { reveal: true } : {}), |
| 285 | }, |
| 286 | }); |
| 287 | setPreferredEditor(editor); |
| 288 | return (await result)._tag === "Success"; |
| 289 | }; |
| 290 | |
| 291 | const openFavoriteEditorShortcutLabel = shortcutLabelForCommand( |
| 292 | keybindings, |
| 293 | "editor.openFavorite", |
| 294 | ); |
| 295 | |
| 296 | const backgroundThread = use(BackgroundThreadContext); |
| 297 | const options = resolveOpenInOptions(navigator.platform, effectiveEditors, messages); |
| 298 | const primaryOption = options.find(({ value }) => value === preferredEditor) ?? null; |
| 299 | const primaryLabel = isPanel |
| 300 | ? primaryOption |
| 301 | ? gt("Open in {editor}", { editor: primaryOption.label }) |
| 302 | : gt("Open in editor") |
| 303 | : gt("Open", { $context: "verb, opens the project in the preferred editor" }); |
| 304 | |
| 305 | const unavailableReason = |
| 306 | remote.mode === "remote-unavailable" |
| 307 | ? environmentLabel !== null |
| 308 | ? gt("No SSH route to {environment}", { environment: environmentLabel }) |
| 309 | : gt("No SSH route to this machine") |
| 310 | : remote.mode === "remote-links" && options.length === 0 |
| 311 | ? environmentLabel !== null |
| 312 | ? gt("Open from a computer to edit on {environment}", { environment: environmentLabel }) |
| 313 | : gt("Open from a computer to edit on this machine") |
| 314 | : null; |
| 315 | |
| 316 | const editorItems = ( |
| 317 | <> |
| 318 | {unavailableReason !== null ? ( |
| 319 | <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled> |
| 320 | {unavailableReason} |
| 321 | </MenuItem> |
| 322 | ) : ( |
| 323 | <> |
| 324 | {options.length === 0 && ( |
| 325 | <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled> |
| 326 | <T>No installed editors found</T> |
| 327 | </MenuItem> |
| 328 | )} |
| 329 | {options.map(({ label, Icon, value, kind }) => ( |
| 330 | <MenuItem |
| 331 | density={presentation === "menu" ? "touch" : "default"} |
| 332 | disabled={!openInCwd || !canOpenEditor} |
| 333 | key={value} |
| 334 | onClick={() => { |
| 335 | if (presentation === "menu") { |
| 336 | void openInEditor(value); |
| 337 | } else { |
| 338 | setPreferredEditor(value); |
| 339 | } |
| 340 | }} |
| 341 | > |
| 342 | <Icon aria-hidden="true" className={getOpenInIconClass(kind)} /> |
| 343 | <MenuItemLabel>{label}</MenuItemLabel> |
| 344 | {value === preferredEditor && openFavoriteEditorShortcutLabel && ( |
| 345 | <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut> |
| 346 | )} |
| 347 | </MenuItem> |
| 348 | ))} |
| 349 | {remote.mode === "remote-links" && !remoteHintSeen && ( |
| 350 | <MenuItem density={presentation === "menu" ? "touch" : "default"} disabled> |
| 351 | {environmentLabel !== null |
| 352 | ? gt("Opens over SSH. Needs your key on {environment}", { |
| 353 | environment: environmentLabel, |
| 354 | }) |
| 355 | : gt("Opens over SSH. Needs your key on this machine")} |
| 356 | </MenuItem> |
| 357 | )} |
| 358 | </> |
| 359 | )} |
| 360 | </> |
| 361 | ); |
| 362 | if (presentation === "menu") { |
| 363 | return ( |
| 364 | <> |
| 365 | {primaryOption && ( |
| 366 | <MenuItem |
| 367 | density={presentation === "menu" ? "touch" : "default"} |
| 368 | |
| 369 | disabled={!openInCwd || !canOpenEditor} |
| 370 | onClick={() => openInEditor(preferredEditor)} |
| 371 | > |
| 372 | <primaryOption.Icon className={cn("size-4", getOpenInIconClass(primaryOption.kind))} /> |
| 373 | <MenuItemLabel> |
| 374 | <T> |
| 375 | Open in <Var>{primaryOption.label}</Var> |
| 376 | </T> |
| 377 | </MenuItemLabel> |
| 378 | {openFavoriteEditorShortcutLabel && ( |
| 379 | <MenuShortcut>{openFavoriteEditorShortcutLabel}</MenuShortcut> |
| 380 | )} |
| 381 | </MenuItem> |
| 382 | )} |
| 383 | <MenuSub> |
| 384 | <MenuSubTrigger density="touch" disabled={isHostEditorDenied}> |
| 385 | <SquareArrowOutUpRightIcon className="size-4" /> |
| 386 | <MenuItemLabel> |
| 387 | <T>Open in…</T> |
| 388 | </MenuItemLabel> |
| 389 | </MenuSubTrigger> |
| 390 | <MenuSubPopup>{editorItems}</MenuSubPopup> |
| 391 | </MenuSub> |
| 392 | </> |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | const primaryControlLabel = compact ? gt("Open file in preferred editor") : primaryLabel; |
| 397 | const primaryControl = ( |
| 398 | <ThreadDetailsControl |
| 399 | aria-label={primaryControlLabel} |
| 400 | // Tooltip replaces the button data-slot, so toolbar theming needs a separate hook. |
| 401 | data-toolbar-control="" |
| 402 | size={isPanel ? "sm" : "xs"} |
| 403 | variant={isPanel ? "ghost" : "outline"} |
| 404 | part="primary" |
| 405 | panel={isPanel} |
| 406 | disabled={!preferredEditor || !openInCwd || !canOpenEditor} |
| 407 | {...(enableShortcut && !backgroundThread ? { [OPEN_FAVORITE_EDITOR_ATTRIBUTE]: "" } : {})} |
| 408 | onClick={() => { |
| 409 | if (!preferredEditor) return; |
| 410 | const label = editorLabelForPlatform(preferredEditor, navigator.platform); |
| 411 | void openInEditor(preferredEditor).then((opened) => { |
| 412 | if (opened) announce(gt("Opened in {editor}", { editor: label })); |
| 413 | }); |
| 414 | }} |
| 415 | > |
| 416 | {primaryOption?.Icon ? ( |
| 417 | <primaryOption.Icon |
| 418 | aria-hidden="true" |
| 419 | className={cn( |
| 420 | isPanel ? THREAD_DETAILS_PANEL_ICON_CLASS : "size-3.5", |
| 421 | getOpenInIconClass(primaryOption.kind), |
| 422 | )} |
| 423 | /> |
| 424 | ) : isPanel ? ( |
| 425 | <SquareArrowOutUpRightIcon aria-hidden="true" className={THREAD_DETAILS_PANEL_ICON_CLASS} /> |
| 426 | ) : null} |
| 427 | <span |
| 428 | className={cn( |
| 429 | compact |
| 430 | ? "sr-only" |
| 431 | : "sr-only @3xl/header-actions:not-sr-only @3xl/header-actions:ml-0.5", |
| 432 | isPanel && cn("not-sr-only ml-0 min-w-0 truncate", THREAD_DETAILS_PANEL_LABEL_CLASS), |
| 433 | )} |
| 434 | > |
| 435 | {primaryLabel} |
| 436 | </span> |
| 437 | </ThreadDetailsControl> |
| 438 | ); |
| 439 | |
| 440 | return ( |
| 441 | <ActionGroup |
| 442 | aria-label={gt("Open in editor")} |
| 443 | role="group" |
| 444 | {...(isPanel |
| 445 | ? { className: THREAD_DETAILS_PANEL_SPLIT_GROUP_CLASS, ref: panelAnchorRef } |
| 446 | : {})} |
| 447 | > |
| 448 | <Tooltip> |
| 449 | <TooltipTrigger render={primaryControl} /> |
| 450 | <TooltipPopup side="top">{primaryControlLabel}</TooltipPopup> |
| 451 | </Tooltip> |
| 452 | {isPanel ? ( |
| 453 | <span aria-hidden="true" className={THREAD_DETAILS_PANEL_SPLIT_SEPARATOR_CLASS} /> |
| 454 | ) : ( |
| 455 | <GroupSeparator {...(!compact ? { className: "hidden @3xl/header-actions:block" } : {})} /> |
| 456 | )} |
| 457 | <Menu open={menuOpen} onOpenChange={setMenuOpen}> |
| 458 | <Tooltip> |
| 459 | <TooltipTrigger |
| 460 | render={ |
| 461 | <MenuTrigger |
| 462 | disabled={isHostEditorDenied} |
| 463 | render={ |
| 464 | <ThreadDetailsControl |
| 465 | aria-label={gt("Choose editor")} |
| 466 | data-toolbar-control="" |
| 467 | size={isPanel ? "sm" : "icon-xs"} |
| 468 | variant={isPanel ? "ghost" : "outline"} |
| 469 | part="secondary" |
| 470 | panel={isPanel} |
| 471 | /> |
| 472 | } |
| 473 | /> |
| 474 | } |
| 475 | > |
| 476 | <ChevronDownIcon |
| 477 | aria-hidden="true" |
| 478 | className={isPanel ? THREAD_DETAILS_PANEL_CHEVRON_CLASS : "size-4"} |
| 479 | /> |
| 480 | </TooltipTrigger> |
| 481 | <TooltipPopup side="top"> |
| 482 | <T>Choose editor</T> |
| 483 | </TooltipPopup> |
| 484 | </Tooltip> |
| 485 | <MenuPopup |
| 486 | align="end" |
| 487 | {...(isPanel ? { anchor: panelAnchorRef } : {})} |
| 488 | className={isPanel ? "w-(--anchor-width)" : undefined} |
| 489 | > |
| 490 | {editorItems} |
| 491 | </MenuPopup> |
| 492 | </Menu> |
| 493 | </ActionGroup> |
| 494 | ); |
| 495 | }); |