The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | DndContext, |
| 3 | type DragEndEvent, |
| 4 | KeyboardSensor, |
| 5 | PointerSensor, |
| 6 | closestCenter, |
| 7 | useSensor, |
| 8 | useSensors, |
| 9 | } from "@dnd-kit/core"; |
| 10 | import { restrictToParentElement, restrictToVerticalAxis } from "@dnd-kit/modifiers"; |
| 11 | import { |
| 12 | SortableContext, |
| 13 | arrayMove, |
| 14 | sortableKeyboardCoordinates, |
| 15 | useSortable, |
| 16 | verticalListSortingStrategy, |
| 17 | } from "@dnd-kit/sortable"; |
| 18 | import { CSS } from "@dnd-kit/utilities"; |
| 19 | import { |
| 20 | type ConnectionRoute, |
| 21 | connectionRouteAddress, |
| 22 | connectionRouteId, |
| 23 | connectionRouteLabelText, |
| 24 | connectionRoutes, |
| 25 | isLearned, |
| 26 | pendingRouteOrder, |
| 27 | } from "@t3tools/client-runtime/connection"; |
| 28 | import { GripVerticalIcon, PlusIcon, XIcon } from "lucide-react"; |
| 29 | import { T, useGT, useMessages } from "gt-react"; |
| 30 | import { useState } from "react"; |
| 31 | |
| 32 | import { requestConfirmDialog } from "~/confirmDialog"; |
| 33 | import { environmentCatalog } from "~/connection/catalog"; |
| 34 | import { cn } from "~/lib/utils"; |
| 35 | import type { EnvironmentPresentation } from "~/state/environments"; |
| 36 | import { usePreparedConnection } from "~/state/session"; |
| 37 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 38 | import { Button } from "../ui/button"; |
| 39 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 40 | |
| 41 | export function EnvironmentRoutesList({ |
| 42 | environment, |
| 43 | onAddRoute, |
| 44 | }: { |
| 45 | readonly environment: EnvironmentPresentation; |
| 46 | readonly onAddRoute: () => void; |
| 47 | }) { |
| 48 | const saved = connectionRoutes(environment.entry); |
| 49 | const savedIds = saved.map((route) => connectionRouteId(route.target)); |
| 50 | const [pending, setPending] = useState<ReadonlyArray<string> | null>(null); |
| 51 | const shownPending = pendingRouteOrder(pending, savedIds); |
| 52 | // Dropped during render so a stale order never outlives the save it was for. |
| 53 | if (pending !== null && shownPending === null) setPending(null); |
| 54 | const order: Array<string> = shownPending === null ? savedIds : [...shownPending]; |
| 55 | const byId = new Map(saved.map((route) => [connectionRouteId(route.target), route])); |
| 56 | const routes = order.flatMap((id) => byId.get(id) ?? []); |
| 57 | |
| 58 | const gt = useGT(); |
| 59 | const messages = useMessages(); |
| 60 | const prepared = usePreparedConnection(environment.environmentId); |
| 61 | const activeRouteId = |
| 62 | prepared._tag === "Some" && environment.connection.phase === "connected" |
| 63 | ? connectionRouteId(prepared.value.target) |
| 64 | : null; |
| 65 | const reorder = useAtomCommand(environmentCatalog.reorderRoutes, "Reorder routes"); |
| 66 | const removeRoute = useAtomCommand(environmentCatalog.removeRoute, "Remove route"); |
| 67 | const sensors = useSensors( |
| 68 | useSensor(PointerSensor, { activationConstraint: { distance: 4 } }), |
| 69 | useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }), |
| 70 | ); |
| 71 | |
| 72 | const confirmRemove = async (route: ConnectionRoute) => { |
| 73 | const address = connectionRouteAddress(route); |
| 74 | const confirmed = await requestConfirmDialog( |
| 75 | address === null |
| 76 | ? gt("Remove {route} route?", { route: connectionRouteLabelText(route, messages) }) |
| 77 | : gt("Remove {route} route?\n{address}", { |
| 78 | route: connectionRouteLabelText(route, messages), |
| 79 | address, |
| 80 | }), |
| 81 | { variant: "destructive" }, |
| 82 | ); |
| 83 | if (confirmed !== true) return; |
| 84 | await removeRoute({ |
| 85 | environmentId: environment.environmentId, |
| 86 | routeId: connectionRouteId(route.target), |
| 87 | }); |
| 88 | }; |
| 89 | |
| 90 | const handleDragEnd = (event: DragEndEvent) => { |
| 91 | if (event.over === null || event.active.id === event.over.id) return; |
| 92 | const next = arrayMove( |
| 93 | order, |
| 94 | order.indexOf(String(event.active.id)), |
| 95 | order.indexOf(String(event.over.id)), |
| 96 | ); |
| 97 | setPending(next); |
| 98 | void reorder({ environmentId: environment.environmentId, routeIds: next }).then((result) => { |
| 99 | if (result._tag === "Failure") setPending(null); |
| 100 | }); |
| 101 | }; |
| 102 | |
| 103 | return ( |
| 104 | <div className="mt-2 border-t border-border/50 py-2"> |
| 105 | <DndContext |
| 106 | sensors={sensors} |
| 107 | collisionDetection={closestCenter} |
| 108 | modifiers={[restrictToVerticalAxis, restrictToParentElement]} |
| 109 | onDragEnd={handleDragEnd} |
| 110 | > |
| 111 | <SortableContext items={order} strategy={verticalListSortingStrategy}> |
| 112 | <ol |
| 113 | aria-label={gt("Routes to {environment}, preferred first", { |
| 114 | environment: environment.label, |
| 115 | })} |
| 116 | > |
| 117 | {routes.map((route, index) => ( |
| 118 | <SortableRouteRow |
| 119 | key={connectionRouteId(route.target)} |
| 120 | route={route} |
| 121 | position={index + 1} |
| 122 | inUse={connectionRouteId(route.target) === activeRouteId} |
| 123 | refused={environment.refusedRouteIds.includes(connectionRouteId(route.target))} |
| 124 | removable={routes.length > 1 && !isLearned(route)} |
| 125 | onRemove={() => void confirmRemove(route)} |
| 126 | /> |
| 127 | ))} |
| 128 | </ol> |
| 129 | </SortableContext> |
| 130 | </DndContext> |
| 131 | <div className="-ml-1.5 pt-1"> |
| 132 | <Button size="xs" variant="ghost-muted" onClick={onAddRoute}> |
| 133 | <PlusIcon className="size-3" /> |
| 134 | <T>Add route</T> |
| 135 | </Button> |
| 136 | </div> |
| 137 | </div> |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | function SortableRouteRow({ |
| 142 | route, |
| 143 | position, |
| 144 | inUse, |
| 145 | refused, |
| 146 | removable, |
| 147 | onRemove, |
| 148 | }: { |
| 149 | readonly route: ConnectionRoute; |
| 150 | readonly position: number; |
| 151 | readonly inUse: boolean; |
| 152 | readonly refused: boolean; |
| 153 | readonly removable: boolean; |
| 154 | readonly onRemove: () => void; |
| 155 | }) { |
| 156 | const gt = useGT(); |
| 157 | const messages = useMessages(); |
| 158 | const id = connectionRouteId(route.target); |
| 159 | const label = connectionRouteLabelText(route, messages); |
| 160 | const address = connectionRouteAddress(route); |
| 161 | const { |
| 162 | attributes, |
| 163 | listeners, |
| 164 | setNodeRef, |
| 165 | setActivatorNodeRef, |
| 166 | transform, |
| 167 | transition, |
| 168 | isDragging, |
| 169 | } = useSortable({ id }); |
| 170 | |
| 171 | return ( |
| 172 | <li |
| 173 | ref={setNodeRef} |
| 174 | style={{ transform: CSS.Translate.toString(transform), transition }} |
| 175 | className={cn( |
| 176 | "flex items-center gap-2 py-2", |
| 177 | isDragging && "relative z-10 rounded-md bg-background shadow-md", |
| 178 | )} |
| 179 | > |
| 180 | <div className="min-w-0 flex-1"> |
| 181 | <p className="flex items-center gap-1.5 text-xs font-medium text-foreground"> |
| 182 | {label} |
| 183 | {inUse ? ( |
| 184 | <span className="rounded-sm bg-success/12 px-1 text-2xs font-normal text-success-foreground"> |
| 185 | <T>In use</T> |
| 186 | </span> |
| 187 | ) : null} |
| 188 | {refused ? ( |
| 189 | <span className="rounded-sm bg-destructive/8 px-1 text-2xs font-normal text-destructive-foreground"> |
| 190 | <T>Sign-in refused</T> |
| 191 | </span> |
| 192 | ) : null} |
| 193 | </p> |
| 194 | {address !== null ? ( |
| 195 | <p className="truncate text-2xs text-muted-foreground"> |
| 196 | {address} |
| 197 | {isLearned(route) ? ` · ${gt("found automatically")}` : ""} |
| 198 | </p> |
| 199 | ) : null} |
| 200 | {refused && address !== null ? ( |
| 201 | <p className="text-2xs text-muted-foreground"> |
| 202 | <T>The host refused this device's sign-in. Pair this address again with Add route.</T> |
| 203 | </p> |
| 204 | ) : null} |
| 205 | </div> |
| 206 | <button |
| 207 | type="button" |
| 208 | ref={setActivatorNodeRef} |
| 209 | {...attributes} |
| 210 | {...listeners} |
| 211 | aria-label={gt("Reorder {route}, position {position}", { route: label, position })} |
| 212 | className="flex size-6 shrink-0 cursor-grab touch-none items-center justify-center rounded text-muted-foreground/70 outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring active:cursor-grabbing" |
| 213 | > |
| 214 | <GripVerticalIcon className="size-3.5" /> |
| 215 | </button> |
| 216 | {removable ? ( |
| 217 | <Tooltip> |
| 218 | <TooltipTrigger |
| 219 | render={ |
| 220 | <Button |
| 221 | type="button" |
| 222 | variant="ghost-muted" |
| 223 | size="icon-xs" |
| 224 | aria-label={gt("Remove {route} route", { route: label })} |
| 225 | onClick={onRemove} |
| 226 | /> |
| 227 | } |
| 228 | > |
| 229 | <XIcon className="size-3" /> |
| 230 | </TooltipTrigger> |
| 231 | <TooltipPopup side="top"> |
| 232 | <T>Remove route</T> |
| 233 | </TooltipPopup> |
| 234 | </Tooltip> |
| 235 | ) : null} |
| 236 | </li> |
| 237 | ); |
| 238 | } |