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 { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m as resolveMessage } from "~/i18n/messages"; |
| 3 | import { useEnvironmentScope } from "../../state/session"; |
| 4 | import { useEnvironmentsWithScope, readEnvironmentScope } from "../../state/session"; |
| 5 | import { |
| 6 | ChevronDownIcon, |
| 7 | CircleXIcon, |
| 8 | EllipsisIcon, |
| 9 | FileJsonIcon, |
| 10 | MinusIcon, |
| 11 | PlusIcon, |
| 12 | SearchIcon, |
| 13 | TriangleAlertIcon, |
| 14 | XIcon, |
| 15 | } from "lucide-react"; |
| 16 | import { useLocation } from "@tanstack/react-router"; |
| 17 | import { |
| 18 | type KeyboardEvent, |
| 19 | type ReactNode, |
| 20 | type RefObject, |
| 21 | useReducer, |
| 22 | useRef, |
| 23 | useState, |
| 24 | } from "react"; |
| 25 | import { flushSync } from "react-dom"; |
| 26 | import { T, useGT, useMessages } from "gt-react"; |
| 27 | import { |
| 28 | AuthOrchestrationOperateScope, |
| 29 | AuthSettingsWriteScope, |
| 30 | type KeybindingCommand, |
| 31 | type KeybindingWhenNode, |
| 32 | type ServerRemoveKeybindingInput, |
| 33 | type ServerUpsertKeybindingInput, |
| 34 | } from "@t3tools/contracts"; |
| 35 | import { |
| 36 | mergeWithDefaultKeybindings, |
| 37 | parseKeybindingWhenExpression, |
| 38 | } from "@t3tools/shared/keybindings"; |
| 39 | import { |
| 40 | isAtomCommandInterrupted, |
| 41 | squashAtomCommandFailure, |
| 42 | } from "@t3tools/client-runtime/state/runtime"; |
| 43 | |
| 44 | import { isElectron } from "../../env"; |
| 45 | import { useOpenInPreferredEditor } from "../../editorPreferences"; |
| 46 | import { formatShortcutLabel } from "../../keybindings"; |
| 47 | import { cn } from "../../lib/utils"; |
| 48 | import { serverEnvironment } from "../../state/server"; |
| 49 | import { useSettingsScope } from "./SettingsScopeContext"; |
| 50 | import { useClientSettings } from "../../hooks/useSettings"; |
| 51 | import { Badge } from "../ui/badge"; |
| 52 | import { Button } from "../ui/button"; |
| 53 | import { Input } from "../ui/input"; |
| 54 | import { InputGroup, InputGroupAddon, InputGroupInput } from "../ui/input-group"; |
| 55 | import { Kbd, KbdGroup } from "../ui/kbd"; |
| 56 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 57 | import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"; |
| 58 | import { |
| 59 | Combobox, |
| 60 | ComboboxTrigger, |
| 61 | ComboboxPopup, |
| 62 | ComboboxSearchInput, |
| 63 | ComboboxEmpty, |
| 64 | ComboboxList, |
| 65 | ComboboxItem, |
| 66 | } from "../ui/combobox"; |
| 67 | import { |
| 68 | SelectButton, |
| 69 | Select, |
| 70 | SelectContent, |
| 71 | SelectItem, |
| 72 | SelectTrigger, |
| 73 | SelectValue, |
| 74 | } from "../ui/select"; |
| 75 | import { Toggle } from "../ui/toggle"; |
| 76 | import { toastManager } from "../ui/toast"; |
| 77 | import { |
| 78 | buildKeybindingRows, |
| 79 | filterKeybindingRows, |
| 80 | buildKeybindingCommandOptions, |
| 81 | buildWhenVariableOptions, |
| 82 | commandLabel, |
| 83 | DEFAULT_WHEN_VARIABLE, |
| 84 | isKnownWhenVariable, |
| 85 | keybindingConflictLabels, |
| 86 | keybindingFromKeyboardEvent, |
| 87 | parseWhenExpressionDraft, |
| 88 | groupKeybindingRows, |
| 89 | type KeybindingCommandOption, |
| 90 | type KeybindingGroup, |
| 91 | type KeybindingRow, |
| 92 | type WhenVariableOption, |
| 93 | unknownWhenVariables, |
| 94 | whenAstToExpression, |
| 95 | whenNodeRemoveLabel, |
| 96 | } from "./KeybindingsSettings.logic"; |
| 97 | import { SettingsGroup } from "./SettingsGroup"; |
| 98 | import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout"; |
| 99 | import { keybindingSearchAnchorId, useSearchableSetting } from "./settingsSearch"; |
| 100 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 101 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 102 | |
| 103 | function KeybindingPill({ value }: { value: string }) { |
| 104 | // Keys dedupe repeated parts; a literal "+" in a shortcut splits into empty strings. |
| 105 | const seenParts = new Map<string, number>(); |
| 106 | const parts = value.split("+").map((part) => { |
| 107 | const seen = seenParts.get(part) ?? 0; |
| 108 | seenParts.set(part, seen + 1); |
| 109 | return { part, key: seen === 0 ? part : `${part}-${seen}` }; |
| 110 | }); |
| 111 | return ( |
| 112 | <KbdGroup> |
| 113 | {parts.map(({ part, key }) => ( |
| 114 | <Kbd key={key}> |
| 115 | {part === "mod" |
| 116 | ? navigator.platform.toLowerCase().includes("mac") |
| 117 | ? "⌘" |
| 118 | : "Ctrl" |
| 119 | : part === "shift" |
| 120 | ? "⇧" |
| 121 | : part === "alt" |
| 122 | ? navigator.platform.toLowerCase().includes("mac") |
| 123 | ? "⌥" |
| 124 | : "Alt" |
| 125 | : part === "ctrl" |
| 126 | ? "⌃" |
| 127 | : part.length === 1 |
| 128 | ? part.toUpperCase() |
| 129 | : part} |
| 130 | </Kbd> |
| 131 | ))} |
| 132 | </KbdGroup> |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | function KeybindingsSearchInput({ |
| 137 | query, |
| 138 | onChange, |
| 139 | inputRef, |
| 140 | }: { |
| 141 | query: string; |
| 142 | onChange: (next: string) => void; |
| 143 | inputRef: RefObject<HTMLInputElement | null>; |
| 144 | }) { |
| 145 | const gt = useGT(); |
| 146 | return ( |
| 147 | <InputGroup className="min-w-0 basis-full **:[input]:h-9 sm:basis-0 sm:flex-1 sm:**:[input]:h-8"> |
| 148 | <InputGroupAddon> |
| 149 | <SearchIcon aria-hidden className="size-3.5" /> |
| 150 | </InputGroupAddon> |
| 151 | <InputGroupInput |
| 152 | ref={inputRef} |
| 153 | type="search" |
| 154 | value={query} |
| 155 | onChange={(event) => onChange(event.currentTarget.value)} |
| 156 | onKeyDown={(event) => { |
| 157 | if (event.key !== "Escape") return; |
| 158 | event.preventDefault(); |
| 159 | if (query.length > 0) { |
| 160 | event.stopPropagation(); |
| 161 | onChange(""); |
| 162 | } else event.currentTarget.blur(); |
| 163 | }} |
| 164 | placeholder={gt("Search keybindings")} |
| 165 | aria-label={gt("Search keybindings")} |
| 166 | size="sm" |
| 167 | /> |
| 168 | </InputGroup> |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | type BooleanOperator = "and" | "or"; |
| 173 | |
| 174 | function flattenWhenChildren( |
| 175 | node: KeybindingWhenNode, |
| 176 | operator: BooleanOperator, |
| 177 | ): KeybindingWhenNode[] { |
| 178 | if (node.type !== operator) return [node]; |
| 179 | return [ |
| 180 | ...flattenWhenChildren(node.left, operator), |
| 181 | ...flattenWhenChildren(node.right, operator), |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | function buildWhenExpressionGroup( |
| 186 | children: readonly KeybindingWhenNode[], |
| 187 | operator: BooleanOperator, |
| 188 | ): KeybindingWhenNode | undefined { |
| 189 | const first = children[0]; |
| 190 | if (!first) return undefined; |
| 191 | return children.slice(1).reduce<KeybindingWhenNode>( |
| 192 | (left, right) => ({ |
| 193 | type: operator, |
| 194 | left, |
| 195 | right, |
| 196 | }), |
| 197 | first, |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | function conditionParts(node: KeybindingWhenNode): { identifier: string; negated: boolean } | null { |
| 202 | if (node.type === "identifier") return { identifier: node.name, negated: false }; |
| 203 | if (node.type === "not" && node.node.type === "identifier") { |
| 204 | return { identifier: node.node.name, negated: true }; |
| 205 | } |
| 206 | return null; |
| 207 | } |
| 208 | |
| 209 | function setConditionIdentifier(node: KeybindingWhenNode, identifier: string): KeybindingWhenNode { |
| 210 | const parts = conditionParts(node); |
| 211 | if (!parts) return node; |
| 212 | const next: KeybindingWhenNode = { type: "identifier", name: identifier }; |
| 213 | return parts.negated ? { type: "not", node: next } : next; |
| 214 | } |
| 215 | |
| 216 | function setConditionNegated(node: KeybindingWhenNode, negated: boolean): KeybindingWhenNode { |
| 217 | const parts = conditionParts(node); |
| 218 | if (!parts) return negated ? { type: "not", node } : node; |
| 219 | const identifier: KeybindingWhenNode = { type: "identifier", name: parts.identifier }; |
| 220 | return negated ? { type: "not", node: identifier } : identifier; |
| 221 | } |
| 222 | |
| 223 | function defaultWhenCondition(): KeybindingWhenNode { |
| 224 | return { type: "identifier", name: DEFAULT_WHEN_VARIABLE }; |
| 225 | } |
| 226 | |
| 227 | function defaultWhenGroup(operator: BooleanOperator = "and"): KeybindingWhenNode { |
| 228 | return { |
| 229 | type: operator, |
| 230 | left: defaultWhenCondition(), |
| 231 | right: { type: "not", node: defaultWhenCondition() }, |
| 232 | }; |
| 233 | } |
| 234 | |
| 235 | function WarningTooltipIcon({ |
| 236 | label, |
| 237 | focusable = true, |
| 238 | className, |
| 239 | children, |
| 240 | }: { |
| 241 | label: string; |
| 242 | focusable?: boolean; |
| 243 | className?: string | undefined; |
| 244 | children: ReactNode; |
| 245 | }) { |
| 246 | return ( |
| 247 | <Tooltip> |
| 248 | <TooltipTrigger |
| 249 | render={ |
| 250 | <span |
| 251 | tabIndex={focusable ? 0 : undefined} |
| 252 | aria-label={label} |
| 253 | className={cn( |
| 254 | "inline-flex size-5 shrink-0 items-center justify-center rounded-sm text-warning outline-none transition-colors hover:bg-warning/10 focus-visible:ring-3 focus-visible:ring-inset focus-visible:ring-warning/25", |
| 255 | className, |
| 256 | )} |
| 257 | /> |
| 258 | } |
| 259 | > |
| 260 | <TriangleAlertIcon className="size-3.5" /> |
| 261 | </TooltipTrigger> |
| 262 | <TooltipPopup side="top">{children}</TooltipPopup> |
| 263 | </Tooltip> |
| 264 | ); |
| 265 | } |
| 266 | |
| 267 | function UnknownWhenVariableWarning({ |
| 268 | identifiers, |
| 269 | focusable = true, |
| 270 | }: { |
| 271 | identifiers: ReadonlyArray<string>; |
| 272 | focusable?: boolean; |
| 273 | }) { |
| 274 | const gt = useGT(); |
| 275 | if (identifiers.length === 0) return null; |
| 276 | const label = |
| 277 | identifiers.length === 1 |
| 278 | ? gt("Unknown condition: {condition}", { condition: identifiers[0] }) |
| 279 | : gt("Unknown conditions: {conditions}", { conditions: identifiers.join(", ") }); |
| 280 | |
| 281 | return ( |
| 282 | <WarningTooltipIcon label={label} focusable={focusable} className="size-4.5"> |
| 283 | <T> |
| 284 | T3 Code does not recognize this condition yet. It can still be saved, but it may not match |
| 285 | unless the runtime provides it. |
| 286 | </T> |
| 287 | </WarningTooltipIcon> |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | function KeybindingConflictWarning({ labels }: { labels: ReadonlyArray<string> }) { |
| 292 | const gt = useGT(); |
| 293 | if (labels.length === 0) return null; |
| 294 | const description = |
| 295 | labels.length === 1 |
| 296 | ? gt("Conflicts with {command}.", { command: labels[0] }) |
| 297 | : labels.length > 3 |
| 298 | ? gt("Conflicts with {commands}, and more.", { commands: labels.slice(0, 3).join(", ") }) |
| 299 | : gt("Conflicts with {commands}.", { commands: labels.slice(0, 3).join(", ") }); |
| 300 | |
| 301 | return ( |
| 302 | <WarningTooltipIcon label={description}> |
| 303 | {description} <T>The most recent matching binding wins when both conditions can apply.</T> |
| 304 | </WarningTooltipIcon> |
| 305 | ); |
| 306 | } |
| 307 | |
| 308 | function WhenVariableSelect({ |
| 309 | value, |
| 310 | variables, |
| 311 | unknownIdentifiers, |
| 312 | onChange, |
| 313 | }: { |
| 314 | value: string; |
| 315 | variables: ReadonlyArray<WhenVariableOption>; |
| 316 | unknownIdentifiers?: ReadonlyArray<string>; |
| 317 | onChange: (value: string) => void; |
| 318 | }) { |
| 319 | const gt = useGT(); |
| 320 | const selected = variables.find((option) => option === value); |
| 321 | const options = |
| 322 | selected || variables.some((option) => option === value) ? variables : [value, ...variables]; |
| 323 | |
| 324 | return ( |
| 325 | <Select value={value} onValueChange={(nextValue) => nextValue && onChange(nextValue)}> |
| 326 | <SelectTrigger size="compact" className="min-w-0 flex-1"> |
| 327 | <SelectValue placeholder={gt("Condition")} /> |
| 328 | {unknownIdentifiers && unknownIdentifiers.length > 0 ? ( |
| 329 | <UnknownWhenVariableWarning identifiers={unknownIdentifiers} focusable={false} /> |
| 330 | ) : null} |
| 331 | </SelectTrigger> |
| 332 | <SelectContent alignItemWithTrigger={false} matchTriggerWidth={false} className="max-h-72"> |
| 333 | {options.map((option) => ( |
| 334 | <SelectItem key={option} value={option} className="w-full"> |
| 335 | <span className="truncate">{option}</span> |
| 336 | </SelectItem> |
| 337 | ))} |
| 338 | </SelectContent> |
| 339 | </Select> |
| 340 | ); |
| 341 | } |
| 342 | |
| 343 | function WhenExpressionRemoveButton({ |
| 344 | label, |
| 345 | className, |
| 346 | onRemove, |
| 347 | }: { |
| 348 | label: string; |
| 349 | className?: string | undefined; |
| 350 | onRemove: () => void; |
| 351 | }) { |
| 352 | return ( |
| 353 | <Tooltip> |
| 354 | <TooltipTrigger |
| 355 | delay={200} |
| 356 | render={ |
| 357 | <Button |
| 358 | type="button" |
| 359 | variant="ghost" |
| 360 | size="icon-sm" |
| 361 | className={cn("size-7", className)} |
| 362 | aria-label={label} |
| 363 | onClick={onRemove} |
| 364 | /> |
| 365 | } |
| 366 | > |
| 367 | <MinusIcon aria-hidden className="size-3.5" /> |
| 368 | </TooltipTrigger> |
| 369 | <TooltipPopup side="top">{label}</TooltipPopup> |
| 370 | </Tooltip> |
| 371 | ); |
| 372 | } |
| 373 | |
| 374 | function WhenExpressionNodeEditor({ |
| 375 | node, |
| 376 | variables, |
| 377 | depth = 0, |
| 378 | onChange, |
| 379 | onRemove, |
| 380 | }: { |
| 381 | node: KeybindingWhenNode; |
| 382 | variables: ReadonlyArray<WhenVariableOption>; |
| 383 | depth?: number; |
| 384 | onChange: (node: KeybindingWhenNode) => void; |
| 385 | onRemove?: () => void; |
| 386 | }) { |
| 387 | const gt = useGT(); |
| 388 | const m = useMessages(); |
| 389 | const condition = conditionParts(node); |
| 390 | |
| 391 | if (condition) { |
| 392 | const unknownIdentifiers = isKnownWhenVariable(condition.identifier) |
| 393 | ? [] |
| 394 | : [condition.identifier]; |
| 395 | |
| 396 | return ( |
| 397 | <div className="flex items-center gap-2 rounded-md border border-border/70 bg-background/60 px-2 py-2"> |
| 398 | <Toggle |
| 399 | pressed={condition.negated} |
| 400 | onPressedChange={(pressed) => onChange(setConditionNegated(node, pressed))} |
| 401 | aria-label={gt("Negate {condition}", { condition: condition.identifier })} |
| 402 | variant="outline" |
| 403 | size="compact" |
| 404 | className="min-w-10" |
| 405 | > |
| 406 | {gt("Not", { $context: "logical negation toggle" })} |
| 407 | </Toggle> |
| 408 | <WhenVariableSelect |
| 409 | value={condition.identifier} |
| 410 | variables={variables} |
| 411 | unknownIdentifiers={unknownIdentifiers} |
| 412 | onChange={(value) => onChange(setConditionIdentifier(node, value))} |
| 413 | /> |
| 414 | {onRemove ? ( |
| 415 | <WhenExpressionRemoveButton |
| 416 | label={whenNodeRemoveLabel(node, depth, m)} |
| 417 | onRemove={onRemove} |
| 418 | /> |
| 419 | ) : null} |
| 420 | </div> |
| 421 | ); |
| 422 | } |
| 423 | |
| 424 | if (node.type === "not") { |
| 425 | return ( |
| 426 | <div |
| 427 | className={cn( |
| 428 | "space-y-2 rounded-lg border border-border/70 bg-muted/20 p-2", |
| 429 | depth > 0 && "border-border/50 bg-background/50", |
| 430 | )} |
| 431 | > |
| 432 | <div className="flex items-center gap-2"> |
| 433 | <Toggle |
| 434 | pressed |
| 435 | onPressedChange={(pressed) => onChange(pressed ? node : node.node)} |
| 436 | aria-label={gt("Negate group")} |
| 437 | variant="outline" |
| 438 | size="compact" |
| 439 | className="min-w-10" |
| 440 | > |
| 441 | {gt("Not", { $context: "logical negation toggle" })} |
| 442 | </Toggle> |
| 443 | {onRemove ? ( |
| 444 | <WhenExpressionRemoveButton |
| 445 | label={whenNodeRemoveLabel(node, depth, m)} |
| 446 | className="ml-auto" |
| 447 | onRemove={onRemove} |
| 448 | /> |
| 449 | ) : null} |
| 450 | </div> |
| 451 | <div className="relative pl-4"> |
| 452 | <span className="absolute top-0 bottom-0 left-1.5 w-px bg-border/70" aria-hidden /> |
| 453 | <span className="absolute top-4 left-1.5 h-px w-2.5 bg-border/70" aria-hidden /> |
| 454 | <WhenExpressionNodeEditor |
| 455 | node={node.node} |
| 456 | variables={variables} |
| 457 | depth={depth + 1} |
| 458 | onChange={(next) => onChange({ type: "not", node: next })} |
| 459 | /> |
| 460 | </div> |
| 461 | </div> |
| 462 | ); |
| 463 | } |
| 464 | |
| 465 | const operator: BooleanOperator = node.type === "or" ? "or" : "and"; |
| 466 | const children = flattenWhenChildren(node, operator); |
| 467 | const childKeyCounts = new Map<string, number>(); |
| 468 | const childEntries = children.map((child) => { |
| 469 | const baseKey = `${child.type}-${whenAstToExpression(child)}`; |
| 470 | const count = childKeyCounts.get(baseKey) ?? 0; |
| 471 | childKeyCounts.set(baseKey, count + 1); |
| 472 | return { child, key: count === 0 ? baseKey : `${baseKey}-${count}` }; |
| 473 | }); |
| 474 | |
| 475 | const updateChild = (target: KeybindingWhenNode, next: KeybindingWhenNode) => { |
| 476 | let didUpdate = false; |
| 477 | const nextChildren = children.map((child) => { |
| 478 | if (!didUpdate && child === target) { |
| 479 | didUpdate = true; |
| 480 | return next; |
| 481 | } |
| 482 | return child; |
| 483 | }); |
| 484 | const nextNode = buildWhenExpressionGroup(nextChildren, operator); |
| 485 | if (nextNode) onChange(nextNode); |
| 486 | }; |
| 487 | |
| 488 | const removeChild = (target: KeybindingWhenNode) => { |
| 489 | let didRemove = false; |
| 490 | const nextChildren = children.filter((child) => { |
| 491 | if (!didRemove && child === target) { |
| 492 | didRemove = true; |
| 493 | return false; |
| 494 | } |
| 495 | return true; |
| 496 | }); |
| 497 | const nextNode = buildWhenExpressionGroup(nextChildren, operator); |
| 498 | if (nextNode) { |
| 499 | onChange(nextNode); |
| 500 | } else { |
| 501 | onChange(defaultWhenCondition()); |
| 502 | } |
| 503 | }; |
| 504 | |
| 505 | const setOperator = (nextOperator: BooleanOperator) => { |
| 506 | if (nextOperator === operator) return; |
| 507 | const nextNode = buildWhenExpressionGroup(children, nextOperator); |
| 508 | if (nextNode) onChange(nextNode); |
| 509 | }; |
| 510 | |
| 511 | const addCondition = () => { |
| 512 | const nextNode = buildWhenExpressionGroup([...children, defaultWhenCondition()], operator); |
| 513 | if (nextNode) onChange(nextNode); |
| 514 | }; |
| 515 | |
| 516 | const addGroup = () => { |
| 517 | const nestedOperator: BooleanOperator = operator === "and" ? "or" : "and"; |
| 518 | const group: KeybindingWhenNode = { |
| 519 | type: nestedOperator, |
| 520 | left: defaultWhenCondition(), |
| 521 | right: { type: "not", node: defaultWhenCondition() }, |
| 522 | }; |
| 523 | const nextNode = buildWhenExpressionGroup([...children, group], operator); |
| 524 | if (nextNode) onChange(nextNode); |
| 525 | }; |
| 526 | |
| 527 | return ( |
| 528 | <div |
| 529 | className={cn( |
| 530 | "space-y-2 rounded-lg border border-border/60 bg-muted/10 p-2", |
| 531 | depth > 0 && "border-border/70 bg-background/55", |
| 532 | )} |
| 533 | > |
| 534 | <div className="flex flex-wrap items-center gap-2"> |
| 535 | <Select value={operator} onValueChange={(value) => setOperator(value as BooleanOperator)}> |
| 536 | <SelectTrigger size="compact" className="w-24"> |
| 537 | <SelectValue /> |
| 538 | </SelectTrigger> |
| 539 | <SelectContent alignItemWithTrigger={false} matchTriggerWidth={false}> |
| 540 | <SelectItem value="and"> |
| 541 | {gt("and", { $context: "logical operator in a when clause" })} |
| 542 | </SelectItem> |
| 543 | <SelectItem value="or"> |
| 544 | {gt("or", { $context: "logical operator in a when clause" })} |
| 545 | </SelectItem> |
| 546 | </SelectContent> |
| 547 | </Select> |
| 548 | <Button type="button" variant="outline" size="compact" onClick={addCondition}> |
| 549 | <PlusIcon className="size-3.5" /> |
| 550 | {gt("Condition", { $context: "button, adds a when-clause condition" })} |
| 551 | </Button> |
| 552 | <Button type="button" variant="outline" size="compact" onClick={addGroup}> |
| 553 | <PlusIcon className="size-3.5" /> |
| 554 | {gt("Group", { $context: "button, adds a group of when-clause conditions" })} |
| 555 | </Button> |
| 556 | {onRemove ? ( |
| 557 | <WhenExpressionRemoveButton |
| 558 | label={whenNodeRemoveLabel(node, depth, m)} |
| 559 | className="ml-auto" |
| 560 | onRemove={onRemove} |
| 561 | /> |
| 562 | ) : null} |
| 563 | </div> |
| 564 | <div className="space-y-2"> |
| 565 | {childEntries.map(({ child, key }) => ( |
| 566 | <div key={key} className="relative pl-4"> |
| 567 | <span |
| 568 | className={cn( |
| 569 | "absolute top-0 bottom-0 left-1.5 w-px", |
| 570 | depth === 0 ? "bg-border" : "bg-border/70", |
| 571 | )} |
| 572 | aria-hidden |
| 573 | /> |
| 574 | <span |
| 575 | className={cn( |
| 576 | "absolute top-4 left-1.5 h-px w-2.5", |
| 577 | depth === 0 ? "bg-border" : "bg-border/70", |
| 578 | )} |
| 579 | aria-hidden |
| 580 | /> |
| 581 | <WhenExpressionNodeEditor |
| 582 | node={child} |
| 583 | variables={variables} |
| 584 | depth={depth + 1} |
| 585 | onChange={(next) => updateChild(child, next)} |
| 586 | onRemove={() => removeChild(child)} |
| 587 | /> |
| 588 | </div> |
| 589 | ))} |
| 590 | </div> |
| 591 | </div> |
| 592 | ); |
| 593 | } |
| 594 | |
| 595 | function WhenExpressionBuilder({ |
| 596 | value, |
| 597 | variables, |
| 598 | onChange, |
| 599 | onValidityChange, |
| 600 | }: { |
| 601 | value: KeybindingWhenNode | undefined; |
| 602 | variables: ReadonlyArray<WhenVariableOption>; |
| 603 | onChange: (value: KeybindingWhenNode | undefined) => void; |
| 604 | onValidityChange?: (valid: boolean) => void; |
| 605 | }) { |
| 606 | const gt = useGT(); |
| 607 | const m = useMessages(); |
| 608 | const expression = whenAstToExpression(value); |
| 609 | const [expressionDraft, setExpressionDraft] = useState(expression); |
| 610 | const parseResult = parseWhenExpressionDraft(expressionDraft, m); |
| 611 | const parseError = parseResult.ok ? null : parseResult.message; |
| 612 | const unknownIdentifiers = parseResult.ok ? unknownWhenVariables(parseResult.value) : []; |
| 613 | |
| 614 | const updateExpressionDraft = (nextExpression: string) => { |
| 615 | setExpressionDraft(nextExpression); |
| 616 | const nextResult = parseWhenExpressionDraft(nextExpression, m); |
| 617 | onValidityChange?.(nextResult.ok); |
| 618 | if (nextResult.ok) { |
| 619 | onChange(nextResult.value); |
| 620 | } |
| 621 | }; |
| 622 | |
| 623 | const updateExpressionValue = (nextValue: KeybindingWhenNode | undefined) => { |
| 624 | setExpressionDraft(whenAstToExpression(nextValue)); |
| 625 | onValidityChange?.(true); |
| 626 | onChange(nextValue); |
| 627 | }; |
| 628 | |
| 629 | const addRootCondition = () => { |
| 630 | if (!value) { |
| 631 | updateExpressionValue(defaultWhenCondition()); |
| 632 | return; |
| 633 | } |
| 634 | updateExpressionValue({ type: "and", left: value, right: defaultWhenCondition() }); |
| 635 | }; |
| 636 | |
| 637 | const addRootGroup = () => { |
| 638 | const group = defaultWhenGroup("or"); |
| 639 | if (!value) { |
| 640 | updateExpressionValue(group); |
| 641 | return; |
| 642 | } |
| 643 | updateExpressionValue({ type: "and", left: value, right: group }); |
| 644 | }; |
| 645 | |
| 646 | return ( |
| 647 | <div className="w-[min(34rem,calc(100vw-2rem))] space-y-3"> |
| 648 | <div className="flex items-start justify-between gap-3"> |
| 649 | <div className="min-w-0"> |
| 650 | <div className="text-sm font-medium text-foreground"> |
| 651 | {gt("When", { $context: "heading for keybinding conditions" })} |
| 652 | </div> |
| 653 | </div> |
| 654 | <div className="flex shrink-0 items-center gap-2"> |
| 655 | <Button type="button" variant="outline" size="compact" onClick={addRootCondition}> |
| 656 | <PlusIcon className="size-3.5" /> |
| 657 | {gt("Condition", { $context: "button, adds a when-clause condition" })} |
| 658 | </Button> |
| 659 | <Button type="button" variant="outline" size="compact" onClick={addRootGroup}> |
| 660 | <PlusIcon className="size-3.5" /> |
| 661 | {gt("Group", { $context: "button, adds a group of when-clause conditions" })} |
| 662 | </Button> |
| 663 | </div> |
| 664 | </div> |
| 665 | |
| 666 | <div className="space-y-1.5"> |
| 667 | <InputGroup> |
| 668 | <InputGroupInput |
| 669 | value={expressionDraft} |
| 670 | onChange={(event) => updateExpressionDraft(event.currentTarget.value)} |
| 671 | placeholder={gt("Always")} |
| 672 | aria-invalid={Boolean(parseError)} |
| 673 | aria-label={gt("When expression")} |
| 674 | size="compact" |
| 675 | font="mono" |
| 676 | /> |
| 677 | {unknownIdentifiers.length > 0 ? ( |
| 678 | <InputGroupAddon align="inline-end"> |
| 679 | <UnknownWhenVariableWarning identifiers={unknownIdentifiers} /> |
| 680 | </InputGroupAddon> |
| 681 | ) : null} |
| 682 | </InputGroup> |
| 683 | {parseError ? ( |
| 684 | <div className="flex items-center gap-1.5 text-2xs text-destructive"> |
| 685 | <CircleXIcon className="size-3.5" /> |
| 686 | {parseError} |
| 687 | </div> |
| 688 | ) : null} |
| 689 | </div> |
| 690 | |
| 691 | <div className="relative"> |
| 692 | {value ? ( |
| 693 | <WhenExpressionNodeEditor |
| 694 | node={value} |
| 695 | variables={variables} |
| 696 | onChange={updateExpressionValue} |
| 697 | onRemove={() => updateExpressionValue(undefined)} |
| 698 | /> |
| 699 | ) : ( |
| 700 | <div className="rounded-md border border-dashed border-border/80 bg-muted/15 p-3"> |
| 701 | <div className="flex flex-wrap gap-2"> |
| 702 | <Button type="button" size="compact" onClick={addRootCondition}> |
| 703 | <PlusIcon className="size-3.5" /> |
| 704 | {gt("Condition", { $context: "button, adds a when-clause condition" })} |
| 705 | </Button> |
| 706 | <Button type="button" variant="outline" size="compact" onClick={addRootGroup}> |
| 707 | <PlusIcon className="size-3.5" /> |
| 708 | {gt("Group", { $context: "button, adds a group of when-clause conditions" })} |
| 709 | </Button> |
| 710 | </div> |
| 711 | </div> |
| 712 | )} |
| 713 | {parseError ? ( |
| 714 | <div className="pointer-events-none absolute inset-0 flex items-center justify-center rounded-lg border border-destructive/30 bg-background/75 p-4 text-center text-xs text-destructive backdrop-blur-xs"> |
| 715 | <T>Fix the expression above to continue editing visually.</T> |
| 716 | </div> |
| 717 | ) : null} |
| 718 | </div> |
| 719 | </div> |
| 720 | ); |
| 721 | } |
| 722 | |
| 723 | type KeybindingRowDraftState = { |
| 724 | keyDraft: string; |
| 725 | whenDraft: KeybindingWhenNode | undefined; |
| 726 | isRecording: boolean; |
| 727 | isWhenDraftValid: boolean; |
| 728 | }; |
| 729 | |
| 730 | function createKeybindingRowDraft(row: KeybindingRow): KeybindingRowDraftState { |
| 731 | return { |
| 732 | keyDraft: row.key, |
| 733 | whenDraft: row.binding.whenAst, |
| 734 | isRecording: false, |
| 735 | isWhenDraftValid: true, |
| 736 | }; |
| 737 | } |
| 738 | |
| 739 | function keybindingRowDraftReducer( |
| 740 | state: KeybindingRowDraftState, |
| 741 | patch: Partial<KeybindingRowDraftState>, |
| 742 | ): KeybindingRowDraftState { |
| 743 | return { ...state, ...patch }; |
| 744 | } |
| 745 | |
| 746 | function rowKeybindingTarget(row: KeybindingRow): ServerRemoveKeybindingInput { |
| 747 | return { |
| 748 | command: row.command, |
| 749 | key: row.key, |
| 750 | ...(row.when.trim().length > 0 ? { when: row.when } : {}), |
| 751 | }; |
| 752 | } |
| 753 | |
| 754 | function useKeybindingRowEditor({ |
| 755 | row, |
| 756 | allRows, |
| 757 | onSave, |
| 758 | }: { |
| 759 | row: KeybindingRow; |
| 760 | allRows: ReadonlyArray<KeybindingRow>; |
| 761 | onSave: (input: ServerUpsertKeybindingInput) => void; |
| 762 | }) { |
| 763 | const m = useMessages(); |
| 764 | const [draft, setDraft] = useReducer(keybindingRowDraftReducer, row, createKeybindingRowDraft); |
| 765 | const pillRef = useRef<HTMLButtonElement>(null); |
| 766 | const { keyDraft, whenDraft, isRecording, isWhenDraftValid } = draft; |
| 767 | const whenDraftExpression = whenAstToExpression(whenDraft); |
| 768 | const isDirty = keyDraft !== row.key || whenDraftExpression !== row.when; |
| 769 | const conflictLabels = keybindingConflictLabels( |
| 770 | allRows, |
| 771 | { |
| 772 | rowId: row.id, |
| 773 | key: keyDraft, |
| 774 | when: whenDraftExpression, |
| 775 | }, |
| 776 | navigator.platform, |
| 777 | m, |
| 778 | ); |
| 779 | |
| 780 | // Giving an unassigned row a shortcut brings back its default condition. |
| 781 | const setKeyDraft = (next: string, patch: Partial<KeybindingRowDraftState> = {}) => |
| 782 | setDraft({ |
| 783 | ...patch, |
| 784 | keyDraft: next, |
| 785 | ...(row.isUnassigned && whenDraftExpression === "false" |
| 786 | ? { whenDraft: parseKeybindingWhenExpression(row.defaultWhen) ?? undefined } |
| 787 | : {}), |
| 788 | }); |
| 789 | |
| 790 | const save = () => { |
| 791 | onSave({ |
| 792 | command: row.command, |
| 793 | key: keyDraft, |
| 794 | when: whenDraftExpression.trim().length > 0 ? whenDraftExpression : undefined, |
| 795 | replace: rowKeybindingTarget(row), |
| 796 | }); |
| 797 | }; |
| 798 | |
| 799 | const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => { |
| 800 | if (event.key === "Tab" && !isRecording) return; |
| 801 | event.preventDefault(); |
| 802 | if (event.key === "Escape") { |
| 803 | // An unhandled Escape closes the settings page. |
| 804 | event.stopPropagation(); |
| 805 | const field = event.currentTarget; |
| 806 | flushSync(() => |
| 807 | setDraft({ |
| 808 | keyDraft: row.key, |
| 809 | isRecording: false, |
| 810 | ...(row.isUnassigned && whenDraftExpression === row.defaultWhen |
| 811 | ? { whenDraft: parseKeybindingWhenExpression(row.when) ?? undefined } |
| 812 | : {}), |
| 813 | }), |
| 814 | ); |
| 815 | // The field may unmount for the pill; focus would otherwise drop to the page. |
| 816 | if (pillRef.current) pillRef.current.focus(); |
| 817 | else field.blur(); |
| 818 | return; |
| 819 | } |
| 820 | const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, { |
| 821 | layoutCharacter: true, |
| 822 | }); |
| 823 | if (!next) return; |
| 824 | setKeyDraft(next, { isRecording: false }); |
| 825 | }; |
| 826 | |
| 827 | return { |
| 828 | keyDraft, |
| 829 | whenDraft, |
| 830 | isRecording, |
| 831 | isWhenDraftValid, |
| 832 | whenDraftExpression, |
| 833 | isDirty, |
| 834 | conflictLabels, |
| 835 | setDraft, |
| 836 | setKeyDraft, |
| 837 | save, |
| 838 | captureKeybinding, |
| 839 | pillRef, |
| 840 | }; |
| 841 | } |
| 842 | |
| 843 | type KeybindingRowEditor = ReturnType<typeof useKeybindingRowEditor>; |
| 844 | |
| 845 | interface KeybindingRowActions { |
| 846 | allRows: ReadonlyArray<KeybindingRow>; |
| 847 | variables: ReadonlyArray<WhenVariableOption>; |
| 848 | onSave: (input: ServerUpsertKeybindingInput) => void; |
| 849 | onReset: (row: KeybindingRow) => void; |
| 850 | onUnassign: (row: KeybindingRow) => void; |
| 851 | onRemove: (row: KeybindingRow) => void; |
| 852 | } |
| 853 | |
| 854 | type KeybindingRowProps = KeybindingRowActions & { |
| 855 | row: KeybindingRow; |
| 856 | isSaving: boolean; |
| 857 | anchorId?: string | undefined; |
| 858 | }; |
| 859 | |
| 860 | function KeybindingKeyControl({ |
| 861 | row, |
| 862 | editor, |
| 863 | isSaving, |
| 864 | pillClassName, |
| 865 | }: { |
| 866 | row: KeybindingRow; |
| 867 | editor: KeybindingRowEditor; |
| 868 | isSaving: boolean; |
| 869 | pillClassName?: string | undefined; |
| 870 | }) { |
| 871 | const { |
| 872 | keyDraft, |
| 873 | isRecording, |
| 874 | isDirty, |
| 875 | isWhenDraftValid, |
| 876 | setDraft, |
| 877 | setKeyDraft, |
| 878 | save, |
| 879 | captureKeybinding, |
| 880 | pillRef, |
| 881 | } = editor; |
| 882 | const gt = useGT(); |
| 883 | const m = useMessages(); |
| 884 | const showPill = !isRecording && keyDraft === row.key && row.key.length > 0 && !isDirty; |
| 885 | |
| 886 | return ( |
| 887 | <> |
| 888 | {isDirty ? ( |
| 889 | <Button |
| 890 | size="sm" |
| 891 | disabled={isSaving || keyDraft.trim().length === 0 || !isWhenDraftValid} |
| 892 | onClick={save} |
| 893 | > |
| 894 | {isSaving ? gt("Saving") : gt("Save", { $context: "verb" })} |
| 895 | </Button> |
| 896 | ) : null} |
| 897 | {showPill ? ( |
| 898 | <button |
| 899 | ref={pillRef} |
| 900 | type="button" |
| 901 | onClick={() => setDraft({ isRecording: true })} |
| 902 | aria-label={gt("Edit shortcut for {command}: {shortcut}", { |
| 903 | command: commandLabel(row.command, m), |
| 904 | shortcut: row.isUnassigned |
| 905 | ? gt("Unassigned", { $context: "keyboard shortcut with no key assigned" }) |
| 906 | : formatShortcutLabel(row.binding.shortcut), |
| 907 | })} |
| 908 | className={cn( |
| 909 | "inline-flex h-8 cursor-pointer items-center rounded-md border border-transparent px-1.5 sm:h-7 outline-none transition-colors hover:border-border/70 hover:bg-accent focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-inset focus-visible:ring-ring/24", |
| 910 | pillClassName, |
| 911 | )} |
| 912 | > |
| 913 | {row.isUnassigned ? ( |
| 914 | <span className="text-sm text-muted-foreground"> |
| 915 | <T context="keyboard shortcut with no key assigned">Unassigned</T> |
| 916 | </span> |
| 917 | ) : ( |
| 918 | <KeybindingPill value={row.key} /> |
| 919 | )} |
| 920 | </button> |
| 921 | ) : ( |
| 922 | <Input |
| 923 | data-keybinding-capture="" |
| 924 | autoFocus={isRecording} |
| 925 | aria-label={gt("Keybinding for {command}", { command: commandLabel(row.command, m) })} |
| 926 | value={isRecording ? "" : keyDraft} |
| 927 | placeholder={isRecording ? gt("Press shortcut") : gt("Unassigned")} |
| 928 | size="sm" |
| 929 | font="mono" |
| 930 | className="w-44" |
| 931 | onFocus={() => setDraft({ isRecording: true })} |
| 932 | onBlur={() => setDraft({ isRecording: false })} |
| 933 | onChange={(event) => setKeyDraft(event.currentTarget.value)} |
| 934 | onKeyDown={captureKeybinding} |
| 935 | /> |
| 936 | )} |
| 937 | </> |
| 938 | ); |
| 939 | } |
| 940 | |
| 941 | function WhenClauseControl({ |
| 942 | editLabel, |
| 943 | expression, |
| 944 | value, |
| 945 | variables, |
| 946 | onChange, |
| 947 | onValidityChange, |
| 948 | }: { |
| 949 | editLabel: string; |
| 950 | expression: string; |
| 951 | value: KeybindingWhenNode | undefined; |
| 952 | variables: ReadonlyArray<WhenVariableOption>; |
| 953 | onChange: (value: KeybindingWhenNode | undefined) => void; |
| 954 | onValidityChange: (valid: boolean) => void; |
| 955 | }) { |
| 956 | // Closing unmounts the builder, which drops an unparsed draft and keeps the last valid value. |
| 957 | const gt = useGT(); |
| 958 | const handleOpenChange = (open: boolean) => { |
| 959 | if (!open) onValidityChange(true); |
| 960 | }; |
| 961 | |
| 962 | return ( |
| 963 | <Popover onOpenChange={handleOpenChange}> |
| 964 | <PopoverTrigger |
| 965 | render={ |
| 966 | <Button |
| 967 | variant={expression ? "ghost" : "ghost-muted"} |
| 968 | size="micro" |
| 969 | className="min-w-0 shrink" |
| 970 | /> |
| 971 | } |
| 972 | aria-label={editLabel} |
| 973 | > |
| 974 | <span className="truncate font-mono">{expression || gt("Always")}</span> |
| 975 | <ChevronDownIcon className="size-3.5 shrink-0 opacity-60" /> |
| 976 | </PopoverTrigger> |
| 977 | <PopoverContent align="start" sideOffset={6}> |
| 978 | <WhenExpressionBuilder |
| 979 | value={value} |
| 980 | variables={variables} |
| 981 | onChange={onChange} |
| 982 | onValidityChange={onValidityChange} |
| 983 | /> |
| 984 | </PopoverContent> |
| 985 | </Popover> |
| 986 | ); |
| 987 | } |
| 988 | |
| 989 | function KeybindingRowMenu({ |
| 990 | row, |
| 991 | isSaving, |
| 992 | onReset, |
| 993 | onUnassign, |
| 994 | onRemove, |
| 995 | }: { |
| 996 | row: KeybindingRow; |
| 997 | isSaving: boolean; |
| 998 | onReset: (row: KeybindingRow) => void; |
| 999 | onUnassign: (row: KeybindingRow) => void; |
| 1000 | onRemove: (row: KeybindingRow) => void; |
| 1001 | }) { |
| 1002 | const gt = useGT(); |
| 1003 | const m = useMessages(); |
| 1004 | const canReset = row.source === "Custom" && row.defaultKey !== null; |
| 1005 | const canUnassign = row.source === "Default"; |
| 1006 | // Reset to default is the way back; removing would drop the default for good. |
| 1007 | const canRemove = row.source !== "Default" && !row.isUnassigned; |
| 1008 | |
| 1009 | return ( |
| 1010 | <Menu> |
| 1011 | <MenuTrigger |
| 1012 | render={ |
| 1013 | <Button |
| 1014 | type="button" |
| 1015 | variant="ghost-muted" |
| 1016 | size="icon-sm" |
| 1017 | disabled={isSaving} |
| 1018 | aria-label={gt("Actions for {command}", { command: commandLabel(row.command, m) })} |
| 1019 | /> |
| 1020 | } |
| 1021 | > |
| 1022 | <EllipsisIcon className="size-3.5" /> |
| 1023 | </MenuTrigger> |
| 1024 | <MenuPopup align="end"> |
| 1025 | {canReset ? ( |
| 1026 | <MenuItem disabled={isSaving} onClick={() => onReset(row)}> |
| 1027 | <T>Reset to default</T> |
| 1028 | </MenuItem> |
| 1029 | ) : null} |
| 1030 | {canUnassign ? ( |
| 1031 | <MenuItem variant="destructive" disabled={isSaving} onClick={() => onUnassign(row)}> |
| 1032 | {gt("Unassign", { $context: "verb, removes the key from a keyboard shortcut" })} |
| 1033 | </MenuItem> |
| 1034 | ) : null} |
| 1035 | {canRemove ? ( |
| 1036 | <MenuItem variant="destructive" disabled={isSaving} onClick={() => onRemove(row)}> |
| 1037 | {gt("Remove", { $context: "verb, removes a keybinding" })} |
| 1038 | </MenuItem> |
| 1039 | ) : null} |
| 1040 | </MenuPopup> |
| 1041 | </Menu> |
| 1042 | ); |
| 1043 | } |
| 1044 | |
| 1045 | function KeybindingSourceBadge({ source }: { source: KeybindingRow["source"] }) { |
| 1046 | const gt = useGT(); |
| 1047 | if (source === "Default") return null; |
| 1048 | return ( |
| 1049 | <Badge variant="outline" size="sm"> |
| 1050 | {source === "Custom" |
| 1051 | ? gt("Custom", { $context: "keybinding source badge" }) |
| 1052 | : gt("Project", { $context: "keybinding source badge" })} |
| 1053 | </Badge> |
| 1054 | ); |
| 1055 | } |
| 1056 | |
| 1057 | function KeybindingRowTitle({ row }: { row: KeybindingRow }) { |
| 1058 | const m = useMessages(); |
| 1059 | return ( |
| 1060 | <Tooltip> |
| 1061 | <TooltipTrigger render={<span className="flex items-center gap-2" />}> |
| 1062 | {commandLabel(row.command, m)} |
| 1063 | <KeybindingSourceBadge source={row.source} /> |
| 1064 | </TooltipTrigger> |
| 1065 | <TooltipPopup side="top">{row.command}</TooltipPopup> |
| 1066 | </Tooltip> |
| 1067 | ); |
| 1068 | } |
| 1069 | |
| 1070 | function KeybindingRowWhen({ |
| 1071 | row, |
| 1072 | editor, |
| 1073 | variables, |
| 1074 | }: { |
| 1075 | row: KeybindingRow; |
| 1076 | editor: KeybindingRowEditor; |
| 1077 | variables: ReadonlyArray<WhenVariableOption>; |
| 1078 | }) { |
| 1079 | const gt = useGT(); |
| 1080 | const m = useMessages(); |
| 1081 | return ( |
| 1082 | <span className="flex h-6 items-center gap-1.5"> |
| 1083 | <span className="text-xs leading-none text-muted-foreground/70"> |
| 1084 | {gt("When", { $context: "heading for keybinding conditions" })} |
| 1085 | </span> |
| 1086 | <WhenClauseControl |
| 1087 | editLabel={gt("Edit when clause for {command}", { command: commandLabel(row.command, m) })} |
| 1088 | expression={editor.whenDraftExpression} |
| 1089 | value={editor.whenDraft} |
| 1090 | variables={variables} |
| 1091 | onChange={(whenDraft) => editor.setDraft({ whenDraft })} |
| 1092 | onValidityChange={(isWhenDraftValid) => editor.setDraft({ isWhenDraftValid })} |
| 1093 | /> |
| 1094 | </span> |
| 1095 | ); |
| 1096 | } |
| 1097 | |
| 1098 | function KeybindingHoverRowMenu(props: { |
| 1099 | row: KeybindingRow; |
| 1100 | isSaving: boolean; |
| 1101 | onReset: (row: KeybindingRow) => void; |
| 1102 | onUnassign: (row: KeybindingRow) => void; |
| 1103 | onRemove: (row: KeybindingRow) => void; |
| 1104 | }) { |
| 1105 | return ( |
| 1106 | <span className="flex items-center opacity-0 transition-opacity group-focus-within/row:opacity-100 group-hover/row:opacity-100 has-data-popup-open:opacity-100 pointer-coarse:opacity-100"> |
| 1107 | <KeybindingRowMenu {...props} /> |
| 1108 | </span> |
| 1109 | ); |
| 1110 | } |
| 1111 | |
| 1112 | function KeybindingSettingsRow(props: KeybindingRowProps) { |
| 1113 | const { row, isSaving, anchorId, allRows, variables, onSave, onReset, onUnassign, onRemove } = |
| 1114 | props; |
| 1115 | const editor = useKeybindingRowEditor({ row, allRows, onSave }); |
| 1116 | |
| 1117 | return ( |
| 1118 | <SettingsRow |
| 1119 | id={anchorId} |
| 1120 | className="group/row rounded-none" |
| 1121 | title={<KeybindingRowTitle row={row} />} |
| 1122 | description={ |
| 1123 | row.isUnassigned && !editor.isDirty ? ( |
| 1124 | <T>No keyboard shortcut assigned. Click Unassigned to set one.</T> |
| 1125 | ) : ( |
| 1126 | <KeybindingRowWhen row={row} editor={editor} variables={variables} /> |
| 1127 | ) |
| 1128 | } |
| 1129 | control={ |
| 1130 | <div className="flex flex-wrap items-center justify-end gap-1.5"> |
| 1131 | <KeybindingConflictWarning labels={editor.conflictLabels} /> |
| 1132 | <KeybindingHoverRowMenu |
| 1133 | row={row} |
| 1134 | isSaving={isSaving} |
| 1135 | onReset={onReset} |
| 1136 | onUnassign={onUnassign} |
| 1137 | onRemove={onRemove} |
| 1138 | /> |
| 1139 | <KeybindingKeyControl |
| 1140 | row={row} |
| 1141 | editor={editor} |
| 1142 | isSaving={isSaving} |
| 1143 | pillClassName="-mr-1.5" |
| 1144 | /> |
| 1145 | </div> |
| 1146 | } |
| 1147 | /> |
| 1148 | ); |
| 1149 | } |
| 1150 | |
| 1151 | function useNewKeybindingDraft({ |
| 1152 | allRows, |
| 1153 | onSave, |
| 1154 | }: { |
| 1155 | allRows: ReadonlyArray<KeybindingRow>; |
| 1156 | onSave: (input: ServerUpsertKeybindingInput) => void; |
| 1157 | }) { |
| 1158 | const m = useMessages(); |
| 1159 | const [commandDraft, setCommandDraft] = useState<KeybindingCommand | "">(""); |
| 1160 | const [draft, setDraft] = useReducer(keybindingRowDraftReducer, { |
| 1161 | keyDraft: "", |
| 1162 | whenDraft: undefined, |
| 1163 | isRecording: false, |
| 1164 | isWhenDraftValid: true, |
| 1165 | }); |
| 1166 | const { keyDraft, whenDraft, isRecording, isWhenDraftValid } = draft; |
| 1167 | const whenDraftExpression = whenAstToExpression(whenDraft); |
| 1168 | const conflictLabels = keybindingConflictLabels( |
| 1169 | allRows, |
| 1170 | { |
| 1171 | rowId: "new", |
| 1172 | key: keyDraft, |
| 1173 | when: whenDraftExpression, |
| 1174 | }, |
| 1175 | navigator.platform, |
| 1176 | m, |
| 1177 | ); |
| 1178 | const commandLabelText = commandDraft ? commandLabel(commandDraft, m) : null; |
| 1179 | const canSave = Boolean(commandDraft) && keyDraft.trim().length > 0 && isWhenDraftValid; |
| 1180 | |
| 1181 | const save = () => { |
| 1182 | if (!commandDraft) return; |
| 1183 | onSave({ |
| 1184 | command: commandDraft, |
| 1185 | key: keyDraft, |
| 1186 | ...(whenDraftExpression.trim().length > 0 ? { when: whenDraftExpression } : {}), |
| 1187 | }); |
| 1188 | }; |
| 1189 | |
| 1190 | const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => { |
| 1191 | if (event.key === "Tab" && !isRecording) return; |
| 1192 | event.preventDefault(); |
| 1193 | if (event.key === "Escape") { |
| 1194 | // An unhandled Escape closes the settings page. |
| 1195 | event.stopPropagation(); |
| 1196 | setDraft({ keyDraft: "", isRecording: false }); |
| 1197 | event.currentTarget.blur(); |
| 1198 | return; |
| 1199 | } |
| 1200 | const next = keybindingFromKeyboardEvent(event.nativeEvent, navigator.platform, { |
| 1201 | layoutCharacter: true, |
| 1202 | }); |
| 1203 | if (!next) return; |
| 1204 | setDraft({ keyDraft: next, isRecording: false }); |
| 1205 | }; |
| 1206 | |
| 1207 | return { |
| 1208 | commandDraft, |
| 1209 | setCommandDraft, |
| 1210 | keyDraft, |
| 1211 | whenDraft, |
| 1212 | whenDraftExpression, |
| 1213 | isRecording, |
| 1214 | conflictLabels, |
| 1215 | commandLabelText, |
| 1216 | canSave, |
| 1217 | setDraft, |
| 1218 | save, |
| 1219 | captureKeybinding, |
| 1220 | }; |
| 1221 | } |
| 1222 | |
| 1223 | type NewKeybindingDraft = ReturnType<typeof useNewKeybindingDraft>; |
| 1224 | |
| 1225 | interface NewKeybindingProps { |
| 1226 | commandOptions: ReadonlyArray<KeybindingCommandOption>; |
| 1227 | allRows: ReadonlyArray<KeybindingRow>; |
| 1228 | variables: ReadonlyArray<WhenVariableOption>; |
| 1229 | isSaving: boolean; |
| 1230 | onSave: (input: ServerUpsertKeybindingInput) => void; |
| 1231 | onCancel: () => void; |
| 1232 | } |
| 1233 | |
| 1234 | function NewKeybindingCommandSelect({ |
| 1235 | draft, |
| 1236 | commandOptions, |
| 1237 | allRows, |
| 1238 | className, |
| 1239 | }: { |
| 1240 | draft: NewKeybindingDraft; |
| 1241 | commandOptions: ReadonlyArray<KeybindingCommandOption>; |
| 1242 | allRows: ReadonlyArray<KeybindingRow>; |
| 1243 | className?: string | undefined; |
| 1244 | }) { |
| 1245 | const gt = useGT(); |
| 1246 | const m = useMessages(); |
| 1247 | const shortcutsByCommand = new Map< |
| 1248 | KeybindingCommand, |
| 1249 | Array<{ id: string; label: string; when: string }> |
| 1250 | >(); |
| 1251 | for (const row of allRows) { |
| 1252 | for (const label of row.previewLabels) { |
| 1253 | const shortcuts = shortcutsByCommand.get(row.command) ?? []; |
| 1254 | shortcuts.push({ id: `${row.id}:${label}`, label, when: row.when }); |
| 1255 | shortcutsByCommand.set(row.command, shortcuts); |
| 1256 | } |
| 1257 | } |
| 1258 | return ( |
| 1259 | <Combobox |
| 1260 | items={commandOptions} |
| 1261 | defaultOpen |
| 1262 | itemToStringLabel={(command) => commandLabel(command, m)} |
| 1263 | filter={(command, query) => |
| 1264 | `${command} ${commandLabel(command, m)}`.toLowerCase().includes(query.trim().toLowerCase()) |
| 1265 | } |
| 1266 | autoHighlight |
| 1267 | value={draft.commandDraft || null} |
| 1268 | onValueChange={(value) => value && draft.setCommandDraft(value)} |
| 1269 | > |
| 1270 | <ComboboxTrigger |
| 1271 | aria-label={gt("Command")} |
| 1272 | render={<SelectButton size="sm" />} |
| 1273 | className={className} |
| 1274 | > |
| 1275 | {draft.commandDraft ? commandLabel(draft.commandDraft, m) : gt("Command")} |
| 1276 | </ComboboxTrigger> |
| 1277 | <ComboboxPopup aria-label={gt("Command")} className="w-96"> |
| 1278 | <ComboboxSearchInput |
| 1279 | autoFocus |
| 1280 | placeholder={gt("Search", { $context: "search field placeholder" })} |
| 1281 | aria-label={gt("Search", { $context: "search field placeholder" })} |
| 1282 | /> |
| 1283 | <ComboboxEmpty>{gt("No results found.")}</ComboboxEmpty> |
| 1284 | <ComboboxList> |
| 1285 | {(command: KeybindingCommandOption) => { |
| 1286 | const shortcuts = shortcutsByCommand.get(command); |
| 1287 | return ( |
| 1288 | <ComboboxItem key={command} value={command}> |
| 1289 | <div className="flex min-w-0 flex-1 items-center justify-between gap-3"> |
| 1290 | <span>{commandLabel(command, m)}</span> |
| 1291 | {shortcuts && ( |
| 1292 | <span className="flex shrink-0 flex-col items-end gap-1"> |
| 1293 | {shortcuts.map((shortcut) => ( |
| 1294 | <Kbd key={shortcut.id} title={shortcut.when || undefined}> |
| 1295 | {shortcut.label} |
| 1296 | </Kbd> |
| 1297 | ))} |
| 1298 | </span> |
| 1299 | )} |
| 1300 | </div> |
| 1301 | </ComboboxItem> |
| 1302 | ); |
| 1303 | }} |
| 1304 | </ComboboxList> |
| 1305 | </ComboboxPopup> |
| 1306 | </Combobox> |
| 1307 | ); |
| 1308 | } |
| 1309 | |
| 1310 | function NewKeybindingKeyInput({ |
| 1311 | draft, |
| 1312 | autoFocus = false, |
| 1313 | className, |
| 1314 | }: { |
| 1315 | draft: NewKeybindingDraft; |
| 1316 | autoFocus?: boolean; |
| 1317 | className?: string | undefined; |
| 1318 | }) { |
| 1319 | const gt = useGT(); |
| 1320 | return ( |
| 1321 | <Input |
| 1322 | data-keybinding-capture="" |
| 1323 | autoFocus={autoFocus} |
| 1324 | aria-label={ |
| 1325 | draft.commandLabelText === null |
| 1326 | ? gt("Keybinding for new keybinding") |
| 1327 | : gt("Keybinding for {command}", { command: draft.commandLabelText }) |
| 1328 | } |
| 1329 | value={draft.isRecording ? "" : draft.keyDraft} |
| 1330 | placeholder={draft.isRecording ? gt("Press shortcut") : gt("Unassigned")} |
| 1331 | size="sm" |
| 1332 | font="mono" |
| 1333 | className={className} |
| 1334 | onFocus={() => draft.setDraft({ isRecording: true })} |
| 1335 | onBlur={() => draft.setDraft({ isRecording: false })} |
| 1336 | onChange={(event) => draft.setDraft({ keyDraft: event.currentTarget.value })} |
| 1337 | onKeyDown={draft.captureKeybinding} |
| 1338 | /> |
| 1339 | ); |
| 1340 | } |
| 1341 | |
| 1342 | function NewKeybindingWhen({ |
| 1343 | draft, |
| 1344 | variables, |
| 1345 | }: { |
| 1346 | draft: NewKeybindingDraft; |
| 1347 | variables: ReadonlyArray<WhenVariableOption>; |
| 1348 | }) { |
| 1349 | const gt = useGT(); |
| 1350 | return ( |
| 1351 | <WhenClauseControl |
| 1352 | editLabel={ |
| 1353 | draft.commandLabelText === null |
| 1354 | ? gt("Edit when clause for new keybinding") |
| 1355 | : gt("Edit when clause for {command}", { command: draft.commandLabelText }) |
| 1356 | } |
| 1357 | expression={draft.whenDraftExpression} |
| 1358 | value={draft.whenDraft} |
| 1359 | variables={variables} |
| 1360 | onChange={(whenDraft) => draft.setDraft({ whenDraft })} |
| 1361 | onValidityChange={(isWhenDraftValid) => draft.setDraft({ isWhenDraftValid })} |
| 1362 | /> |
| 1363 | ); |
| 1364 | } |
| 1365 | |
| 1366 | function NewKeybindingCancelIcon({ |
| 1367 | isSaving, |
| 1368 | onCancel, |
| 1369 | }: { |
| 1370 | isSaving: boolean; |
| 1371 | onCancel: () => void; |
| 1372 | }) { |
| 1373 | const gt = useGT(); |
| 1374 | return ( |
| 1375 | <Tooltip> |
| 1376 | <TooltipTrigger |
| 1377 | render={ |
| 1378 | <Button |
| 1379 | type="button" |
| 1380 | variant="ghost-muted" |
| 1381 | size="icon-sm" |
| 1382 | disabled={isSaving} |
| 1383 | aria-label={gt("Cancel new keybinding")} |
| 1384 | onClick={onCancel} |
| 1385 | /> |
| 1386 | } |
| 1387 | > |
| 1388 | <XIcon className="size-3.5" /> |
| 1389 | </TooltipTrigger> |
| 1390 | <TooltipPopup side="top"> |
| 1391 | <T>Cancel</T> |
| 1392 | </TooltipPopup> |
| 1393 | </Tooltip> |
| 1394 | ); |
| 1395 | } |
| 1396 | |
| 1397 | function NewKeybindingSettingsRow(props: NewKeybindingProps) { |
| 1398 | const { commandOptions, allRows, variables, isSaving, onSave, onCancel } = props; |
| 1399 | const draft = useNewKeybindingDraft({ allRows, onSave }); |
| 1400 | const gt = useGT(); |
| 1401 | |
| 1402 | return ( |
| 1403 | <SettingsRow |
| 1404 | className="bg-muted/15" |
| 1405 | title={gt("New keybinding")} |
| 1406 | description={ |
| 1407 | <span className="flex h-6 items-center gap-1.5"> |
| 1408 | <span className="text-xs leading-none text-muted-foreground/70"> |
| 1409 | {gt("When", { $context: "heading for keybinding conditions" })} |
| 1410 | </span> |
| 1411 | <NewKeybindingWhen draft={draft} variables={variables} /> |
| 1412 | </span> |
| 1413 | } |
| 1414 | control={ |
| 1415 | <div className="flex flex-wrap items-center gap-2"> |
| 1416 | <NewKeybindingCommandSelect |
| 1417 | draft={draft} |
| 1418 | commandOptions={commandOptions} |
| 1419 | allRows={allRows} |
| 1420 | className="w-56" |
| 1421 | /> |
| 1422 | <KeybindingConflictWarning labels={draft.conflictLabels} /> |
| 1423 | <NewKeybindingKeyInput draft={draft} className="w-44" /> |
| 1424 | <Button size="sm" disabled={isSaving || !draft.canSave} onClick={draft.save}> |
| 1425 | {isSaving ? gt("Saving") : gt("Save", { $context: "verb" })} |
| 1426 | </Button> |
| 1427 | <NewKeybindingCancelIcon isSaving={isSaving} onCancel={onCancel} /> |
| 1428 | </div> |
| 1429 | } |
| 1430 | /> |
| 1431 | ); |
| 1432 | } |
| 1433 | |
| 1434 | interface KeybindingsGroupsProps extends KeybindingRowActions { |
| 1435 | groups: ReadonlyArray<KeybindingGroup>; |
| 1436 | anchorIds: ReadonlyMap<string, string>; |
| 1437 | savingCommand: KeybindingCommand | null; |
| 1438 | } |
| 1439 | |
| 1440 | function KeybindingsGroups(props: KeybindingsGroupsProps) { |
| 1441 | const { groups, anchorIds, savingCommand, ...rowActions } = props; |
| 1442 | const m = useMessages(); |
| 1443 | return groups.map((group) => ( |
| 1444 | <SettingsSection key={group.id} id={`keybindings-${group.id}`} title={m(group.title)}> |
| 1445 | {group.rows.map((row) => ( |
| 1446 | <KeybindingSettingsRow |
| 1447 | key={row.id} |
| 1448 | row={row} |
| 1449 | anchorId={anchorIds.get(row.id)} |
| 1450 | isSaving={savingCommand === row.command} |
| 1451 | {...rowActions} |
| 1452 | /> |
| 1453 | ))} |
| 1454 | </SettingsSection> |
| 1455 | )); |
| 1456 | } |
| 1457 | |
| 1458 | function BrowserKeybindingNotice() { |
| 1459 | const gt = useGT(); |
| 1460 | const message = gt( |
| 1461 | "The browser may claim some shortcuts first. The desktop app receives them all.", |
| 1462 | ); |
| 1463 | return ( |
| 1464 | <Tooltip> |
| 1465 | <TooltipTrigger |
| 1466 | delay={200} |
| 1467 | render={ |
| 1468 | <Button size="icon-micro" variant="ghost-muted" aria-label={message}> |
| 1469 | <TriangleAlertIcon className="size-3.5 text-warning" /> |
| 1470 | </Button> |
| 1471 | } |
| 1472 | /> |
| 1473 | <TooltipPopup side="top" className="max-w-72"> |
| 1474 | {message} |
| 1475 | </TooltipPopup> |
| 1476 | </Tooltip> |
| 1477 | ); |
| 1478 | } |
| 1479 | |
| 1480 | function keybindingAnchorIds(rows: ReturnType<typeof buildKeybindingRows>) { |
| 1481 | const ids = new Map<string, string>(); |
| 1482 | const seen = new Set<KeybindingCommand>(); |
| 1483 | for (const row of rows) { |
| 1484 | if (seen.has(row.command)) continue; |
| 1485 | seen.add(row.command); |
| 1486 | ids.set(row.id, keybindingSearchAnchorId(row.command)); |
| 1487 | } |
| 1488 | return ids; |
| 1489 | } |
| 1490 | |
| 1491 | // Cache rows in a separate hook so the compiler does not rebuild them per search keystroke. |
| 1492 | function useKeybindingCatalog() { |
| 1493 | const sendShortcut = useClientSettings((settings) => settings.sendShortcut); |
| 1494 | const serverKeybindings = useSettingsScope().environment?.serverConfig?.keybindings; |
| 1495 | const merged = mergeWithDefaultKeybindings(serverKeybindings ?? []); |
| 1496 | const m = useMessages(); |
| 1497 | return { |
| 1498 | commandOptions: buildKeybindingCommandOptions(merged, m), |
| 1499 | allRows: buildKeybindingRows(merged, navigator.platform, sendShortcut, m), |
| 1500 | }; |
| 1501 | } |
| 1502 | |
| 1503 | export function KeybindingsSettingsPanel() { |
| 1504 | const searchableSetting = useSearchableSetting(); |
| 1505 | const gt = useGT(); |
| 1506 | const m = useMessages(); |
| 1507 | const { environment: primaryEnvironment, connectedEnvironments } = useSettingsScope(); |
| 1508 | const canOpenKeybindingsFile = useEnvironmentScope( |
| 1509 | primaryEnvironment?.environmentId ?? null, |
| 1510 | AuthOrchestrationOperateScope, |
| 1511 | ); |
| 1512 | const writableIds = useEnvironmentsWithScope(connectedEnvironments, AuthSettingsWriteScope); |
| 1513 | const canWriteSettings = |
| 1514 | connectedEnvironments.length > 0 && |
| 1515 | connectedEnvironments.every((target) => writableIds.has(target.environmentId)); |
| 1516 | const keybindingsConfigPath = primaryEnvironment?.serverConfig?.keybindingsConfigPath ?? null; |
| 1517 | const availableEditors = primaryEnvironment?.serverConfig?.availableEditors ?? []; |
| 1518 | const upsertKeybinding = useAtomCommand(serverEnvironment.upsertKeybinding, { |
| 1519 | reportFailure: false, |
| 1520 | }); |
| 1521 | const removeKeybindingMutation = useAtomCommand(serverEnvironment.removeKeybinding, { |
| 1522 | reportFailure: false, |
| 1523 | }); |
| 1524 | const openInPreferredEditor = useOpenInPreferredEditor( |
| 1525 | primaryEnvironment?.environmentId ?? null, |
| 1526 | availableEditors, |
| 1527 | ); |
| 1528 | const [query, setQuery] = useState(""); |
| 1529 | const searchInputRef = useRef<HTMLInputElement>(null); |
| 1530 | const [savingCommand, setSavingCommand] = useState<KeybindingCommand | null>(null); |
| 1531 | const [isAddingBinding, setIsAddingBinding] = useState(false); |
| 1532 | const searchTargetId = useLocation({ select: (location) => location.hash.replace(/^#/, "") }); |
| 1533 | const [handledSearchTargetId, setHandledSearchTargetId] = useState(searchTargetId); |
| 1534 | |
| 1535 | if (searchTargetId !== handledSearchTargetId) { |
| 1536 | setHandledSearchTargetId(searchTargetId); |
| 1537 | if (searchTargetId.startsWith("keybinding-")) setQuery(""); |
| 1538 | } |
| 1539 | const whenVariables = buildWhenVariableOptions(); |
| 1540 | const { commandOptions, allRows } = useKeybindingCatalog(); |
| 1541 | const rows = filterKeybindingRows(allRows, query, m); |
| 1542 | const groups = groupKeybindingRows(rows); |
| 1543 | const anchorIds = keybindingAnchorIds(rows); |
| 1544 | |
| 1545 | const openKeybindingsFile = () => { |
| 1546 | if ( |
| 1547 | !keybindingsConfigPath || |
| 1548 | !primaryEnvironment || |
| 1549 | !readEnvironmentScope(primaryEnvironment.environmentId, AuthOrchestrationOperateScope) |
| 1550 | ) |
| 1551 | return; |
| 1552 | void (async () => { |
| 1553 | const result = await openInPreferredEditor(keybindingsConfigPath); |
| 1554 | if (result._tag === "Success" || isAtomCommandInterrupted(result)) { |
| 1555 | return; |
| 1556 | } |
| 1557 | const error = squashAtomCommandFailure(result); |
| 1558 | toastManager.add({ |
| 1559 | title: gt("Unable to open keybindings file"), |
| 1560 | description: |
| 1561 | error instanceof Error |
| 1562 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 1563 | : gt("The keybindings file was not opened."), |
| 1564 | type: "error", |
| 1565 | }); |
| 1566 | })(); |
| 1567 | }; |
| 1568 | |
| 1569 | const saveKeybinding = (input: ServerUpsertKeybindingInput) => { |
| 1570 | if ( |
| 1571 | !primaryEnvironment || |
| 1572 | !connectedEnvironments.every((target) => |
| 1573 | readEnvironmentScope(target.environmentId, AuthSettingsWriteScope), |
| 1574 | ) |
| 1575 | ) |
| 1576 | return; |
| 1577 | setSavingCommand(input.command); |
| 1578 | const payload: ServerUpsertKeybindingInput = { |
| 1579 | command: input.command, |
| 1580 | key: input.key.trim(), |
| 1581 | ...(input.when?.trim() ? { when: input.when.trim() } : {}), |
| 1582 | ...(input.replace ? { replace: input.replace } : {}), |
| 1583 | }; |
| 1584 | void (async () => { |
| 1585 | const results = await Promise.all( |
| 1586 | connectedEnvironments.map((target) => |
| 1587 | upsertKeybinding({ environmentId: target.environmentId, input: payload }), |
| 1588 | ), |
| 1589 | ); |
| 1590 | setSavingCommand(null); |
| 1591 | const failed = results.find((result) => result._tag === "Failure"); |
| 1592 | if (!failed) { |
| 1593 | setIsAddingBinding(false); |
| 1594 | return; |
| 1595 | } |
| 1596 | if (!isAtomCommandInterrupted(failed)) { |
| 1597 | const error = squashAtomCommandFailure(failed); |
| 1598 | toastManager.add({ |
| 1599 | title: gt("Unable to save keybinding"), |
| 1600 | description: |
| 1601 | error instanceof Error |
| 1602 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 1603 | : gt("The keybinding was not saved."), |
| 1604 | type: "error", |
| 1605 | }); |
| 1606 | } |
| 1607 | })(); |
| 1608 | }; |
| 1609 | |
| 1610 | const removeKeybinding = (row: KeybindingRow) => { |
| 1611 | if ( |
| 1612 | !primaryEnvironment || |
| 1613 | !connectedEnvironments.every((target) => |
| 1614 | readEnvironmentScope(target.environmentId, AuthSettingsWriteScope), |
| 1615 | ) |
| 1616 | ) |
| 1617 | return; |
| 1618 | setSavingCommand(row.command); |
| 1619 | void (async () => { |
| 1620 | const results = await Promise.all( |
| 1621 | connectedEnvironments.map((target) => |
| 1622 | removeKeybindingMutation({ |
| 1623 | environmentId: target.environmentId, |
| 1624 | input: rowKeybindingTarget(row), |
| 1625 | }), |
| 1626 | ), |
| 1627 | ); |
| 1628 | setSavingCommand(null); |
| 1629 | const result = results.find((entry) => entry._tag === "Failure") ?? results[0]; |
| 1630 | if (result?._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 1631 | const error = squashAtomCommandFailure(result); |
| 1632 | toastManager.add({ |
| 1633 | title: gt("Unable to remove keybinding"), |
| 1634 | description: |
| 1635 | error instanceof Error |
| 1636 | ? (thrownErrorMessage(error, resolveMessage) ?? error.message) |
| 1637 | : gt("The keybinding was not removed."), |
| 1638 | type: "error", |
| 1639 | }); |
| 1640 | } |
| 1641 | })(); |
| 1642 | }; |
| 1643 | |
| 1644 | const unassignKeybinding = (row: KeybindingRow) => { |
| 1645 | if (row.source !== "Default") return; |
| 1646 | saveKeybinding({ |
| 1647 | command: row.command, |
| 1648 | key: row.key, |
| 1649 | when: "false", |
| 1650 | replace: rowKeybindingTarget(row), |
| 1651 | }); |
| 1652 | }; |
| 1653 | |
| 1654 | const resetKeybinding = (row: KeybindingRow) => { |
| 1655 | if (!row.defaultKey) return; |
| 1656 | saveKeybinding({ |
| 1657 | command: row.command, |
| 1658 | key: row.defaultKey, |
| 1659 | when: row.defaultWhen.trim().length > 0 ? row.defaultWhen : undefined, |
| 1660 | replace: { |
| 1661 | command: row.command, |
| 1662 | key: row.key, |
| 1663 | ...(row.when.trim().length > 0 ? { when: row.when } : {}), |
| 1664 | }, |
| 1665 | }); |
| 1666 | }; |
| 1667 | |
| 1668 | const cancelAdd = () => setIsAddingBinding(false); |
| 1669 | |
| 1670 | const rowActions: KeybindingRowActions = { |
| 1671 | allRows, |
| 1672 | variables: whenVariables, |
| 1673 | onSave: saveKeybinding, |
| 1674 | onReset: resetKeybinding, |
| 1675 | onUnassign: unassignKeybinding, |
| 1676 | onRemove: removeKeybinding, |
| 1677 | }; |
| 1678 | |
| 1679 | return ( |
| 1680 | <SettingsPageContainer> |
| 1681 | <SettingsSection |
| 1682 | {...searchableSetting("keybindings")} |
| 1683 | headerAction={!isElectron ? <BrowserKeybindingNotice /> : null} |
| 1684 | > |
| 1685 | <div className="flex flex-wrap items-center gap-2 px-3 py-3 sm:px-4"> |
| 1686 | <KeybindingsSearchInput query={query} onChange={setQuery} inputRef={searchInputRef} /> |
| 1687 | <Button |
| 1688 | type="button" |
| 1689 | variant="outline" |
| 1690 | disabled={isAddingBinding || !canWriteSettings} |
| 1691 | onClick={() => setIsAddingBinding(true)} |
| 1692 | > |
| 1693 | <PlusIcon aria-hidden className="size-4" /> |
| 1694 | <T>Add keybinding</T> |
| 1695 | </Button> |
| 1696 | <Tooltip> |
| 1697 | <TooltipTrigger |
| 1698 | render={ |
| 1699 | <Button |
| 1700 | type="button" |
| 1701 | variant="outline" |
| 1702 | size="icon" |
| 1703 | disabled={!keybindingsConfigPath || !canOpenKeybindingsFile} |
| 1704 | onClick={openKeybindingsFile} |
| 1705 | aria-label={gt("Open keybindings.json")} |
| 1706 | > |
| 1707 | <FileJsonIcon aria-hidden className="size-4" /> |
| 1708 | </Button> |
| 1709 | } |
| 1710 | /> |
| 1711 | <TooltipPopup side="top"> |
| 1712 | <T>Open keybindings.json</T> |
| 1713 | </TooltipPopup> |
| 1714 | </Tooltip> |
| 1715 | </div> |
| 1716 | </SettingsSection> |
| 1717 | |
| 1718 | {!canWriteSettings ? ( |
| 1719 | <p className="text-xs text-muted-foreground"> |
| 1720 | {connectedEnvironments.length === 0 |
| 1721 | ? gt("Connect an environment to change keybindings.") |
| 1722 | : gt("This connection can view keybindings but cannot change them.")} |
| 1723 | </p> |
| 1724 | ) : null} |
| 1725 | <div inert={!canWriteSettings} className="flex flex-col gap-8"> |
| 1726 | {isAddingBinding ? ( |
| 1727 | <SettingsGroup> |
| 1728 | <NewKeybindingSettingsRow |
| 1729 | commandOptions={commandOptions} |
| 1730 | allRows={allRows} |
| 1731 | variables={whenVariables} |
| 1732 | isSaving={savingCommand !== null} |
| 1733 | onSave={saveKeybinding} |
| 1734 | onCancel={cancelAdd} |
| 1735 | /> |
| 1736 | </SettingsGroup> |
| 1737 | ) : null} |
| 1738 | |
| 1739 | {groups.length > 0 ? ( |
| 1740 | <KeybindingsGroups |
| 1741 | groups={groups} |
| 1742 | anchorIds={anchorIds} |
| 1743 | savingCommand={savingCommand} |
| 1744 | {...rowActions} |
| 1745 | /> |
| 1746 | ) : ( |
| 1747 | <SettingsGroup> |
| 1748 | <div className="px-4 py-12 text-center text-sm text-muted-foreground"> |
| 1749 | <T>No keybindings match your search.</T> |
| 1750 | </div> |
| 1751 | </SettingsGroup> |
| 1752 | )} |
| 1753 | </div> |
| 1754 | </SettingsPageContainer> |
| 1755 | ); |
| 1756 | } |