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 { T } from "gt-react"; |
| 2 | import { ArrowDownIcon, ArrowUpIcon } from "lucide-react"; |
| 3 | import { type ComponentProps, type ReactNode, type RefObject } from "react"; |
| 4 | |
| 5 | import { Command, CommandFooter, CommandInput, CommandPanel } from "./ui/command"; |
| 6 | import { Kbd, KbdGroup } from "./ui/kbd"; |
| 7 | |
| 8 | /** |
| 9 | * A view replacing another inside an open dialog mounts with focus on the body, |
| 10 | * left there by the removed input. Claim it so typing cannot reach the composer |
| 11 | * behind the modal. On first open the dialog's own initial focus handles it. |
| 12 | */ |
| 13 | function claimFocusInOpenDialog(input: HTMLInputElement) { |
| 14 | const active = input.ownerDocument.activeElement; |
| 15 | if ( |
| 16 | active === null || |
| 17 | active === input.ownerDocument.body || |
| 18 | input.closest('[role="dialog"]')?.contains(active) |
| 19 | ) { |
| 20 | input.focus(); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export interface PendingInputSelection { |
| 25 | readonly value: string; |
| 26 | readonly start: number | null; |
| 27 | readonly end: number | null; |
| 28 | readonly direction: "forward" | "backward" | "none" | null; |
| 29 | } |
| 30 | |
| 31 | function restorePendingSelection( |
| 32 | input: HTMLInputElement, |
| 33 | pendingSelectionRef: RefObject<PendingInputSelection | null> | undefined, |
| 34 | ) { |
| 35 | const selection = pendingSelectionRef?.current; |
| 36 | if (!pendingSelectionRef || !selection) return; |
| 37 | pendingSelectionRef.current = null; |
| 38 | if (input.value !== selection.value) return; |
| 39 | input.setSelectionRange(selection.start, selection.end, selection.direction ?? undefined); |
| 40 | } |
| 41 | |
| 42 | type CommandPaletteContentProps = Omit<ComponentProps<typeof Command>, "children"> & { |
| 43 | readonly children: ReactNode; |
| 44 | readonly escapeLabel?: ReactNode; |
| 45 | readonly footerActionLabel?: ReactNode; |
| 46 | readonly footerTrailing?: ReactNode; |
| 47 | readonly inputAccessory?: ReactNode; |
| 48 | readonly inputProps: ComponentProps<typeof CommandInput>; |
| 49 | readonly pendingSelectionRef?: RefObject<PendingInputSelection | null>; |
| 50 | readonly panelSize?: "list" | "tall-list" | "fill"; |
| 51 | readonly showBackHint?: boolean; |
| 52 | readonly testId?: string; |
| 53 | }; |
| 54 | |
| 55 | export function CommandPaletteContent({ |
| 56 | children, |
| 57 | escapeLabel, |
| 58 | footerActionLabel, |
| 59 | footerTrailing, |
| 60 | inputAccessory, |
| 61 | inputProps, |
| 62 | pendingSelectionRef, |
| 63 | panelSize = "list", |
| 64 | showBackHint, |
| 65 | testId, |
| 66 | ...commandProps |
| 67 | }: CommandPaletteContentProps) { |
| 68 | return ( |
| 69 | <div className="contents" data-testid={testId}> |
| 70 | <Command {...commandProps}> |
| 71 | <div className="relative"> |
| 72 | <CommandInput |
| 73 | autoFocus={false} |
| 74 | {...inputProps} |
| 75 | ref={(input) => { |
| 76 | if (!input) return; |
| 77 | restorePendingSelection(input, pendingSelectionRef); |
| 78 | claimFocusInOpenDialog(input); |
| 79 | }} |
| 80 | /> |
| 81 | {inputAccessory} |
| 82 | </div> |
| 83 | <CommandPanel |
| 84 | className={ |
| 85 | panelSize === "fill" |
| 86 | ? "flex min-h-0 flex-1 flex-col" |
| 87 | : panelSize === "tall-list" |
| 88 | ? "max-h-[min(34rem,76vh)]" |
| 89 | : "flex max-h-[min(28rem,70vh)] min-h-0 flex-col" |
| 90 | } |
| 91 | > |
| 92 | {children} |
| 93 | </CommandPanel> |
| 94 | <CommandFooter className="max-sm:flex-col max-sm:items-start"> |
| 95 | <div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1.5"> |
| 96 | <KbdGroup> |
| 97 | <Kbd> |
| 98 | <ArrowUpIcon /> |
| 99 | </Kbd> |
| 100 | <Kbd> |
| 101 | <ArrowDownIcon /> |
| 102 | </Kbd> |
| 103 | <span> |
| 104 | <T>Navigate</T> |
| 105 | </span> |
| 106 | </KbdGroup> |
| 107 | {footerActionLabel !== undefined ? ( |
| 108 | <KbdGroup> |
| 109 | <Kbd>Enter</Kbd> |
| 110 | <span>{footerActionLabel}</span> |
| 111 | </KbdGroup> |
| 112 | ) : null} |
| 113 | {showBackHint ? ( |
| 114 | <KbdGroup> |
| 115 | <Kbd>Backspace</Kbd> |
| 116 | <span> |
| 117 | <T>Back</T> |
| 118 | </span> |
| 119 | </KbdGroup> |
| 120 | ) : null} |
| 121 | <KbdGroup> |
| 122 | <Kbd>Esc</Kbd> |
| 123 | <span>{escapeLabel ?? <T>Close</T>}</span> |
| 124 | </KbdGroup> |
| 125 | </div> |
| 126 | {footerTrailing} |
| 127 | </CommandFooter> |
| 128 | </Command> |
| 129 | </div> |
| 130 | ); |
| 131 | } |