Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2
3import { cn } from "~/lib/utils";
4import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
5
6/** Canonical connection-phase → dot color mapping shared by every status dot. */
7export function connectionPhaseDotClassName(phase: EnvironmentConnectionPhase): string {
8 switch (phase) {
9 case "connected":
10 return "bg-success";
11 case "connecting":
12 case "reconnecting":
13 return "bg-warning";
14 case "unsupported":
15 case "error":
16 return "bg-destructive";
17 default:
18 return "bg-muted-foreground/40";
19 }
20}
21
22/** Ping halo for transitional phases; null renders no ping. */
23export function connectionPhasePingClassName(phase: EnvironmentConnectionPhase): string | null {
24 return phase === "connecting" || phase === "reconnecting" ? "bg-warning/60 duration-2000" : null;
25}
26
27type ConnectionStatusDotProps = {
28 tooltipText?: string | null;
29 dotClassName: string;
30 pingClassName?: string | null;
31};
32
33export function ConnectionStatusDot({
34 tooltipText,
35 dotClassName,
36 pingClassName,
37}: ConnectionStatusDotProps) {
38 const dotContent = (
39 <>
40 {pingClassName ? (
41 <span
42 className={cn(
43 "absolute inline-flex h-full w-full animate-status-ping rounded-full",
44 pingClassName,
45 )}
46 />
47 ) : null}
48 <span className={cn("relative inline-flex size-2 rounded-full", dotClassName)} />
49 </>
50 );
51
52 if (!tooltipText) {
53 return (
54 <span className="relative flex size-3 shrink-0 items-center justify-center">
55 {dotContent}
56 </span>
57 );
58 }
59
60 const dot = (
61 <button
62 type="button"
63 aria-label={tooltipText}
64 className="relative flex size-3 shrink-0 cursor-help items-center justify-center rounded-full outline-hidden"
65 >
66 {dotContent}
67 </button>
68 );
69
70 return (
71 <Tooltip>
72 <TooltipTrigger render={dot} />
73 <TooltipPopup side="top" className="max-w-80 whitespace-pre-wrap leading-tight">
74 {tooltipText}
75 </TooltipPopup>
76 </Tooltip>
77 );
78}