Open History to see the last commit
1import type { LinkableMachine } from "@t3tools/client-runtime/link-request";
2import { useId } from "react";
3
4import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
5import { Badge } from "../ui/badge";
6import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
7
8/** The machine choice for linking by an address typed in. */
9export const ANOTHER_ADDRESS = "another-address";
10
11/** Whether a machine is linked already, or how this app knows it when not connected to it. */
12export function LinkMachineBadge({ machine }: { readonly machine: LinkableMachine }) {
13 if (machine.linked) {
14 return (
15 <Badge variant="success" size="sm">
16 Linked
17 </Badge>
18 );
19 }
20 if (machine.connected) return null;
21 return (
22 <Badge variant="outline" size="sm">
23 {machine.connectOnly ? "T3 Connect" : "Not connected"}
24 </Badge>
25 );
26}
27
28function MachineName({ machine }: { readonly machine: LinkableMachine }) {
29 return (
30 <span className="flex min-w-0 items-center gap-2">
31 <EnvironmentMachineIcon kind={machine.machine} className="size-4 shrink-0 opacity-80" />
32 <span className="min-w-0 truncate">{machine.label}</span>
33 </span>
34 );
35}
36
37/** The user's other machines, including those on their T3 Connect account, then an address. */
38export function LinkMachineSelect({
39 machines,
40 value,
41 onChange,
42 disabled,
43}: {
44 readonly machines: ReadonlyArray<LinkableMachine>;
45 readonly value: string | null;
46 readonly onChange: (value: string) => void;
47 readonly disabled: boolean;
48}) {
49 const labelId = useId();
50 return (
51 <div className="space-y-1.5">
52 <span id={labelId} className="block text-xs font-medium text-foreground">
53 Machine
54 </span>
55 <Select
56 value={value}
57 disabled={disabled}
58 onValueChange={(next) => {
59 if (next !== null) onChange(next as string);
60 }}
61 >
62 <SelectTrigger aria-labelledby={labelId}>
63 <SelectValue>
64 {(selected: string | null) => {
65 if (selected === ANOTHER_ADDRESS) return "Another address…";
66 const machine = machines.find((candidate) => candidate.environmentId === selected);
67 return machine === undefined ? "Choose a machine" : <MachineName machine={machine} />;
68 }}
69 </SelectValue>
70 </SelectTrigger>
71 <SelectPopup alignItemWithTrigger={false}>
72 {machines.map((machine) => (
73 <SelectItem key={machine.environmentId} value={machine.environmentId} hideIndicator>
74 <span className="flex min-w-0 items-center justify-between gap-3">
75 <MachineName machine={machine} />
76 <LinkMachineBadge machine={machine} />
77 </span>
78 </SelectItem>
79 ))}
80 <SelectItem value={ANOTHER_ADDRESS} hideIndicator>
81 Another address…
82 </SelectItem>
83 </SelectPopup>
84 </Select>
85 </div>
86 );
87}