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 { ComposerSelectControl } from "./chat/ComposerControl"; |
| 2 | import { ComposerContextLabel } from "./ComposerContextLabel"; |
| 3 | import { Tooltip, TooltipTrigger, TooltipPopup } from "./ui/tooltip"; |
| 4 | import { useAtomValue } from "@effect/atom-react"; |
| 5 | import { shortcutLabelForCommand } from "../keybindings"; |
| 6 | import { primaryServerKeybindingsAtom } from "../state/server"; |
| 7 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 8 | import { PlusIcon, ScaleIcon } from "lucide-react"; |
| 9 | import { memo } from "react"; |
| 10 | import { T, useGT } from "gt-react"; |
| 11 | |
| 12 | import { ADD_TO_MACHINE_LABEL_CLASS, type EnvironmentOption } from "./BranchToolbar.logic"; |
| 13 | import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon"; |
| 14 | import { useComposerMenuProps } from "./chat/composerEventScope"; |
| 15 | import { |
| 16 | Select, |
| 17 | SelectGroup, |
| 18 | SelectGroupLabel, |
| 19 | SelectItem, |
| 20 | SelectPopup, |
| 21 | SelectValue, |
| 22 | } from "./ui/select"; |
| 23 | |
| 24 | interface BranchToolbarEnvironmentSelectorProps { |
| 25 | autoEnvironmentLabel?: string | undefined; |
| 26 | onAutoEnvironment?: (() => void) | undefined; |
| 27 | envLocked: boolean; |
| 28 | environmentId: EnvironmentId; |
| 29 | availableEnvironments: readonly EnvironmentOption[]; |
| 30 | onEnvironmentChange?: (environmentId: EnvironmentId) => void; |
| 31 | onAddToMachine?: (() => void) | undefined; |
| 32 | } |
| 33 | |
| 34 | // Action values cannot collide with server-minted environment IDs. |
| 35 | const AUTO_VALUE = "action:auto"; |
| 36 | const ADD_MACHINE_VALUE = "action:add-machine"; |
| 37 | const environmentValue = (id: EnvironmentId) => `env:${id}`; |
| 38 | |
| 39 | export const BranchToolbarEnvironmentSelector = memo(function BranchToolbarEnvironmentSelector({ |
| 40 | autoEnvironmentLabel, |
| 41 | onAutoEnvironment, |
| 42 | envLocked, |
| 43 | environmentId, |
| 44 | availableEnvironments, |
| 45 | onEnvironmentChange, |
| 46 | onAddToMachine, |
| 47 | }: BranchToolbarEnvironmentSelectorProps) { |
| 48 | const gt = useGT(); |
| 49 | const composerFloatingLayerProps = useComposerMenuProps(); |
| 50 | const shortcutLabel = shortcutLabelForCommand( |
| 51 | useAtomValue(primaryServerKeybindingsAtom), |
| 52 | "composer.host", |
| 53 | ); |
| 54 | const activeEnvironment = |
| 55 | availableEnvironments.find((env) => env.environmentId === environmentId) ?? null; |
| 56 | |
| 57 | const environmentItems = [ |
| 58 | ...(onAutoEnvironment |
| 59 | ? [{ value: AUTO_VALUE, label: autoEnvironmentLabel ?? gt("Auto balance") }] |
| 60 | : []), |
| 61 | ...availableEnvironments.map((env) => ({ |
| 62 | value: environmentValue(env.environmentId), |
| 63 | label: env.label, |
| 64 | })), |
| 65 | ...(onAddToMachine ? [{ value: ADD_MACHINE_VALUE, label: gt("Add to another machine") }] : []), |
| 66 | ]; |
| 67 | |
| 68 | // Match the xs control's height and padding: the composer strip has no min-height, |
| 69 | // and its glass seam assumes that height even when this label is the only control. |
| 70 | if (envLocked || (onEnvironmentChange === undefined && onAddToMachine === undefined)) { |
| 71 | const lockedRow = ( |
| 72 | <span |
| 73 | className="inline-flex h-7 min-w-0 max-w-full items-center gap-1.5 border border-transparent px-1.75 font-normal text-muted-foreground/70 text-sm sm:h-6 sm:text-xs" |
| 74 | data-composer-context-control |
| 75 | > |
| 76 | <EnvironmentMachineIcon |
| 77 | kind={activeEnvironment?.machine ?? "server"} |
| 78 | className="size-3 shrink-0" |
| 79 | /> |
| 80 | <ComposerContextLabel>{activeEnvironment?.label ?? gt("Run on")}</ComposerContextLabel> |
| 81 | </span> |
| 82 | ); |
| 83 | return ( |
| 84 | <Tooltip> |
| 85 | <TooltipTrigger render={lockedRow} /> |
| 86 | <TooltipPopup>{activeEnvironment?.label ?? gt("Run on")}</TooltipPopup> |
| 87 | </Tooltip> |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return ( |
| 92 | <Select |
| 93 | modal={false} |
| 94 | value={autoEnvironmentLabel ? AUTO_VALUE : environmentValue(environmentId)} |
| 95 | onValueChange={(value) => { |
| 96 | if (value === ADD_MACHINE_VALUE) { |
| 97 | onAddToMachine?.(); |
| 98 | } else if (value === AUTO_VALUE) { |
| 99 | onAutoEnvironment?.(); |
| 100 | } else if (value?.startsWith("env:")) { |
| 101 | onEnvironmentChange?.(value.slice(4) as EnvironmentId); |
| 102 | } |
| 103 | }} |
| 104 | items={environmentItems} |
| 105 | > |
| 106 | <Tooltip> |
| 107 | <TooltipTrigger |
| 108 | render={ |
| 109 | <ComposerSelectControl |
| 110 | size="xs" |
| 111 | className="min-w-0 max-w-full" |
| 112 | aria-label={gt("Run on")} |
| 113 | data-composer-shortcut="composer.host" |
| 114 | data-composer-context-control |
| 115 | /> |
| 116 | } |
| 117 | > |
| 118 | {autoEnvironmentLabel ? ( |
| 119 | <ScaleIcon className="size-3 shrink-0" aria-hidden="true" /> |
| 120 | ) : ( |
| 121 | <EnvironmentMachineIcon |
| 122 | kind={activeEnvironment?.machine ?? "server"} |
| 123 | className="size-3 shrink-0" |
| 124 | /> |
| 125 | )} |
| 126 | <ComposerContextLabel> |
| 127 | <SelectValue /> |
| 128 | </ComposerContextLabel> |
| 129 | </TooltipTrigger> |
| 130 | <TooltipPopup> |
| 131 | {autoEnvironmentLabel ?? activeEnvironment?.label ?? gt("Run on")} |
| 132 | {shortcutLabel ? ` · ${shortcutLabel}` : null} |
| 133 | </TooltipPopup> |
| 134 | </Tooltip> |
| 135 | <SelectPopup alignItemWithTrigger={false} {...composerFloatingLayerProps}> |
| 136 | <SelectGroup> |
| 137 | <SelectGroupLabel> |
| 138 | <T>Run on</T> |
| 139 | </SelectGroupLabel> |
| 140 | {onAutoEnvironment && ( |
| 141 | <SelectItem |
| 142 | value={AUTO_VALUE} |
| 143 | onClick={() => { |
| 144 | if (autoEnvironmentLabel) onAutoEnvironment?.(); |
| 145 | }} |
| 146 | > |
| 147 | <span className="inline-flex items-center gap-1.5"> |
| 148 | <ScaleIcon className="size-3" aria-hidden="true" /> |
| 149 | {autoEnvironmentLabel ?? gt("Auto balance")} |
| 150 | </span> |
| 151 | </SelectItem> |
| 152 | )} |
| 153 | {availableEnvironments.map((env) => ( |
| 154 | <SelectItem |
| 155 | key={env.environmentId} |
| 156 | value={environmentValue(env.environmentId)} |
| 157 | disabled={onEnvironmentChange === undefined && env.environmentId !== environmentId} |
| 158 | > |
| 159 | <span className="inline-flex items-center gap-1.5"> |
| 160 | <EnvironmentMachineIcon kind={env.machine} className="size-3" /> |
| 161 | {env.label} |
| 162 | </span> |
| 163 | </SelectItem> |
| 164 | ))} |
| 165 | {onAddToMachine ? ( |
| 166 | <SelectItem value={ADD_MACHINE_VALUE}> |
| 167 | <span className={ADD_TO_MACHINE_LABEL_CLASS}> |
| 168 | <PlusIcon aria-hidden="true" className="size-3" /> |
| 169 | <T>Add to another machine</T> |
| 170 | </span> |
| 171 | </SelectItem> |
| 172 | ) : null} |
| 173 | </SelectGroup> |
| 174 | </SelectPopup> |
| 175 | </Select> |
| 176 | ); |
| 177 | }); |