The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | DndContext, |
| 3 | type DragEndEvent, |
| 4 | PointerSensor, |
| 5 | closestCenter, |
| 6 | useSensor, |
| 7 | useSensors, |
| 8 | } from "@dnd-kit/core"; |
| 9 | import { restrictToParentElement, restrictToVerticalAxis } from "@dnd-kit/modifiers"; |
| 10 | import { SortableContext, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable"; |
| 11 | import { CSS } from "@dnd-kit/utilities"; |
| 12 | import { T, useGT } from "gt-react"; |
| 13 | import { ArrowDownIcon, ArrowUpIcon, GripVerticalIcon, XIcon } from "lucide-react"; |
| 14 | |
| 15 | import { cn } from "~/lib/utils"; |
| 16 | import { type BrowserMicrophone, updateMicrophoneOrder } from "../../speech/browserMicrophones"; |
| 17 | import { |
| 18 | type MicrophoneEntry, |
| 19 | forgetMicrophone, |
| 20 | microphoneEntryKey, |
| 21 | moveMicrophone, |
| 22 | preferredMicrophone, |
| 23 | } from "../../speech/microphoneOrder"; |
| 24 | import { Button } from "../ui/button"; |
| 25 | |
| 26 | /** The saved microphone order. Keyboard users reorder with the step buttons. */ |
| 27 | export function MicrophoneOrderList(props: { |
| 28 | readonly order: ReadonlyArray<MicrophoneEntry>; |
| 29 | readonly devices: ReadonlyArray<BrowserMicrophone>; |
| 30 | }) { |
| 31 | const gt = useGT(); |
| 32 | const { order, devices } = props; |
| 33 | const keys = order.map(microphoneEntryKey); |
| 34 | const inUse = microphoneEntryKey(preferredMicrophone(order, devices)); |
| 35 | // Device ids stay hidden until microphone permission, so connection is unknown before it. |
| 36 | const devicesKnown = devices.some((device) => device.deviceId); |
| 37 | const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 4 } })); |
| 38 | const move = (key: string, steps: number) => |
| 39 | updateMicrophoneOrder((current) => moveMicrophone(current, key, steps)); |
| 40 | const handleDragEnd = (event: DragEndEvent) => { |
| 41 | if (event.over === null || event.active.id === event.over.id) return; |
| 42 | const from = keys.indexOf(String(event.active.id)); |
| 43 | const to = keys.indexOf(String(event.over.id)); |
| 44 | if (from !== -1 && to !== -1) move(String(event.active.id), to - from); |
| 45 | }; |
| 46 | |
| 47 | return ( |
| 48 | <DndContext |
| 49 | sensors={sensors} |
| 50 | collisionDetection={closestCenter} |
| 51 | modifiers={[restrictToVerticalAxis, restrictToParentElement]} |
| 52 | onDragEnd={handleDragEnd} |
| 53 | > |
| 54 | <SortableContext items={keys} strategy={verticalListSortingStrategy}> |
| 55 | <ol |
| 56 | aria-label={gt("Microphones, preferred first")} |
| 57 | className="divide-y divide-border/60 rounded-lg border border-border/60" |
| 58 | > |
| 59 | {order.map((entry, index) => { |
| 60 | const key = microphoneEntryKey(entry); |
| 61 | const device = |
| 62 | entry.kind === "device" |
| 63 | ? devices.find((candidate) => candidate.deviceId === entry.deviceId) |
| 64 | : undefined; |
| 65 | return ( |
| 66 | <MicrophoneRow |
| 67 | key={key} |
| 68 | id={key} |
| 69 | name={ |
| 70 | entry.kind === "default" |
| 71 | ? gt("System default") |
| 72 | : entry.label || device?.label || gt("Saved microphone") |
| 73 | } |
| 74 | position={index + 1} |
| 75 | count={order.length} |
| 76 | inUse={key === inUse} |
| 77 | notConnected={entry.kind === "device" && devicesKnown && !device} |
| 78 | onMove={(steps) => move(key, steps)} |
| 79 | onForget={ |
| 80 | entry.kind === "device" |
| 81 | ? () => updateMicrophoneOrder((current) => forgetMicrophone(current, key)) |
| 82 | : undefined |
| 83 | } |
| 84 | /> |
| 85 | ); |
| 86 | })} |
| 87 | </ol> |
| 88 | </SortableContext> |
| 89 | </DndContext> |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | function MicrophoneRow(props: { |
| 94 | readonly id: string; |
| 95 | readonly name: string; |
| 96 | readonly position: number; |
| 97 | readonly count: number; |
| 98 | readonly inUse: boolean; |
| 99 | readonly notConnected: boolean; |
| 100 | readonly onMove: (steps: number) => void; |
| 101 | readonly onForget: (() => void) | undefined; |
| 102 | }) { |
| 103 | const gt = useGT(); |
| 104 | const { name } = props; |
| 105 | const { listeners, setNodeRef, setActivatorNodeRef, transform, transition, isDragging } = |
| 106 | useSortable({ id: props.id }); |
| 107 | return ( |
| 108 | <li |
| 109 | ref={setNodeRef} |
| 110 | style={{ transform: CSS.Translate.toString(transform), transition }} |
| 111 | className={cn( |
| 112 | // Phones put the marker and controls on a second line so names stay readable. |
| 113 | "grid grid-cols-[1rem_minmax(0,1fr)_auto] items-center gap-x-2 py-1.5 ps-3 pe-1.5 @min-[32rem]/settings-row:grid-cols-[1rem_auto_minmax(0,1fr)_auto_auto]", |
| 114 | isDragging && "relative z-10 rounded-lg bg-background shadow-md", |
| 115 | )} |
| 116 | > |
| 117 | <span className="text-xs tabular-nums text-muted-foreground">{props.position}</span> |
| 118 | <span className="col-span-2 text-sm leading-5 break-words @min-[32rem]/settings-row:col-span-1"> |
| 119 | {name} |
| 120 | </span> |
| 121 | <span className="col-start-2 flex @min-[32rem]/settings-row:col-start-auto"> |
| 122 | {props.inUse ? ( |
| 123 | <span className="rounded-sm bg-success/12 px-1 text-2xs text-success-foreground"> |
| 124 | <T>In use</T> |
| 125 | </span> |
| 126 | ) : props.notConnected ? ( |
| 127 | <span className="text-2xs text-muted-foreground"> |
| 128 | <T>Not connected</T> |
| 129 | </span> |
| 130 | ) : null} |
| 131 | </span> |
| 132 | <span |
| 133 | ref={setActivatorNodeRef} |
| 134 | {...listeners} |
| 135 | aria-hidden |
| 136 | className="hidden size-6 shrink-0 cursor-grab touch-none items-center justify-center rounded text-muted-foreground/70 hover:text-foreground active:cursor-grabbing @min-[32rem]/settings-row:flex" |
| 137 | > |
| 138 | <GripVerticalIcon className="size-3.5" /> |
| 139 | </span> |
| 140 | <span className="flex items-center gap-0.5"> |
| 141 | <Button |
| 142 | type="button" |
| 143 | size="icon-xs" |
| 144 | variant="ghost-muted" |
| 145 | disabled={props.position === 1} |
| 146 | aria-label={gt("Move {name} up", { name })} |
| 147 | onClick={() => props.onMove(-1)} |
| 148 | > |
| 149 | <ArrowUpIcon /> |
| 150 | </Button> |
| 151 | <Button |
| 152 | type="button" |
| 153 | size="icon-xs" |
| 154 | variant="ghost-muted" |
| 155 | disabled={props.position === props.count} |
| 156 | aria-label={gt("Move {name} down", { name })} |
| 157 | onClick={() => props.onMove(1)} |
| 158 | > |
| 159 | <ArrowDownIcon /> |
| 160 | </Button> |
| 161 | {props.onForget ? ( |
| 162 | <Button |
| 163 | type="button" |
| 164 | size="icon-xs" |
| 165 | variant="ghost-muted" |
| 166 | aria-label={gt("Forget {name}", { name })} |
| 167 | onClick={props.onForget} |
| 168 | > |
| 169 | <XIcon /> |
| 170 | </Button> |
| 171 | ) : ( |
| 172 | <span aria-hidden className="size-7 sm:size-6" /> |
| 173 | )} |
| 174 | </span> |
| 175 | </li> |
| 176 | ); |
| 177 | } |