Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DeviceToolVersions.tsx

89 lines3.4 KB

1import type { ReactNode } from "react";
2import type { DeviceToolVersions as ToolVersions } from "@t3tools/contracts";
3import { Popover, PopoverPopup, PopoverTitle, PopoverTrigger } from "~/components/ui/popover";
4​
5export function DeviceToolVersions({
6 tools,
7 action,
8 kind,
9 owner,
10 error,
11}: {
12 tools: ToolVersions | undefined;
13 action?: ReactNode;
14 kind?: keyof ToolVersions;
15 owner?: string | undefined;
16 error?: string | undefined;
17}) {
18 const selected = kind ? tools?.[kind] : undefined;
19 const version =
20 selected?.runningVersion ??
21 (selected?.installedVersions.includes(selected.requiredVersion)
22 ? selected.requiredVersion
23 : selected?.installedVersions
24 .toSorted((a, b) => a.localeCompare(b, undefined, { numeric: true }))
25 .at(-1));
26 const label = kind === "hub" ? "Device hub" : "Agent device";
27 return (
28 <Popover>
29 <PopoverTrigger
30 aria-label={
31 kind
32 ? `${label}: ${version ? `version ${version}` : selected ? "not installed" : "version unknown"}. Show details`
33 : undefined
34 }
35 className="rounded text-xs text-muted-foreground underline decoration-muted-foreground/40 underline-offset-4 hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"
36 >
37 {kind
38 ? version
39 ? `v${version}`
40 : selected
41 ? "Not installed"
42 : "Version unknown"
43 : error
44 ? "Versions unavailable"
45 : "Versions"}
46 </PopoverTrigger>
47 <PopoverPopup align="end" className="w-80">
48 <PopoverTitle className="text-sm">{kind ? label : "Device tools"}</PopoverTitle>
49 {tools ? (
50 <div className="mt-4 divide-y divide-border/50">
51 {(
52 [
53 ["Device hub", tools.hub],
54 ["Agent device", tools.agent],
55 ] as const
56 )
57 .filter(([name]) => !kind || name === label)
58 .map(([name, tool]) => (
59 <div key={name} className="space-y-2 py-3 first:pt-0 last:pb-0">
60 {!kind ? <p className="text-xs font-medium">{name}</p> : null}
61 <dl className="grid grid-cols-[auto_1fr] gap-x-6 gap-y-1 text-xs">
62 <dt className="text-muted-foreground">Running</dt>
63 <dd className="text-right font-mono">{tool.runningVersion ?? "Not running"}</dd>
64 <dt className="text-muted-foreground">Required</dt>
65 <dd className="text-right font-mono">{tool.requiredVersion}</dd>
66 <dt className="text-muted-foreground">Installed</dt>
67 <dd className="text-right font-mono break-words">
68 {tool.installedVersions.join(", ") || "None"}
69 </dd>
70 </dl>
71 </div>
72 ))}
73 </div>
74 ) : (
75 <p className="mt-3 text-xs text-muted-foreground">Versions have not been checked.</p>
76 )}
77 <p className="mt-4 border-t border-border/50 pt-3 text-xs text-muted-foreground">
78 {owner ? `Managed by ${owner}. ` : ""}Tools update automatically on this host when needed.
79 </p>
80 {error ? (
81 <p role="status" className="mt-2 text-xs text-destructive">
82 {error}
83 </p>
84 ) : null}
85 {action ? <div className="mt-3">{action}</div> : null}
86 </PopoverPopup>
87 </Popover>
88 );