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

DesktopUpdateStatusIcon.tsx

130 lines4.1 KB

1import { RefreshIcon } from "~/components/ui/refresh-icon";
2import { CheckIcon, CircleArrowUpIcon, DownloadIcon, RotateCwIcon } from "lucide-react";
3import type { AnimationEventHandler } from "react";
4​
5const DOWNLOAD_PROGRESS_RADIUS = 14;
6const DOWNLOAD_PROGRESS_CIRCUMFERENCE = 2 * Math.PI * DOWNLOAD_PROGRESS_RADIUS;
7​
8export type DesktopUpdateStatusIconState =
9 | "idle"
10 | "checking"
11 | "available"
12 | "downloading"
13 | "downloaded"
14 // Other machines or providers have updates. Matches the server update icon
15 // in Connections settings; the download arrow stays for this app's own file.
16 | "everything";
17​
18function normalizeDesktopUpdateDownloadPercent(percent: number | null): number {
19 if (percent === null || !Number.isFinite(percent)) return 0;
20 return Math.min(100, Math.max(0, percent));
22​
23export function shouldShowDesktopUpdateCheckIcon({
24 isAnimationLatched,
25 isChecking,
26 prefersReducedMotion,
27}: {
28 readonly isAnimationLatched: boolean;
29 readonly isChecking: boolean;
30 readonly prefersReducedMotion: boolean;
31}): boolean {
32 return isChecking || (isAnimationLatched && !prefersReducedMotion);
34​
35export function shouldContinueDesktopUpdateCheckAnimation({
36 isChecking,
37 prefersReducedMotion,
38}: {
39 readonly isChecking: boolean;
40 readonly prefersReducedMotion: boolean;
41}): boolean {
42 return isChecking && !prefersReducedMotion;
44​
45function DesktopUpdateAvailableIcon() {
46 return (
47 <span className="relative grid size-4 place-items-center">
48 <DownloadIcon className="size-4" />
49 <span
50 aria-hidden="true"
51 className="absolute -top-0.5 -right-0.5 size-1.5 rounded-full bg-current ring-2 ring-sidebar-control-surface"
52 />
53 </span>
54 );
56​
57function DesktopUpdateDownloadingIcon({ percent }: { readonly percent: number | null }) {
58 const normalizedPercent = normalizeDesktopUpdateDownloadPercent(percent);
59 const progressOffset = DOWNLOAD_PROGRESS_CIRCUMFERENCE * (1 - normalizedPercent / 100);
60​
61 return (
62 <span className="relative grid size-8 place-items-center">
63 <svg
64 aria-hidden="true"
65 className="pointer-events-none absolute inset-0 size-full -rotate-90"
66 viewBox="0 0 32 32"
67 >
68 <circle
69 cx="16"
70 cy="16"
71 r={DOWNLOAD_PROGRESS_RADIUS}
72 fill="none"
73 className="stroke-current/22"
74 strokeWidth="1.5"
75 />
76 <circle
77 cx="16"
78 cy="16"
79 r={DOWNLOAD_PROGRESS_RADIUS}
80 fill="none"
81 stroke="currentColor"
82 strokeDasharray={DOWNLOAD_PROGRESS_CIRCUMFERENCE}
83 strokeDashoffset={progressOffset}
84 strokeLinecap="round"
85 strokeWidth="1.5"
86 className="transition-[stroke-dashoffset] duration-300 ease-out motion-reduce:transition-none"
87 />
88 </svg>
89 <DownloadIcon className="size-4" />
90 </span>
91 );
93​
94function DesktopUpdateDownloadedIcon() {
95 return (
96 <span className="relative grid size-4 place-items-center">
97 <RotateCwIcon className="size-4" />
98 <span className="absolute -right-1 -bottom-1 grid size-2.5 place-items-center rounded-full bg-foreground text-background ring-2 ring-background">
99 <CheckIcon className="size-2" strokeWidth={3} />
100 </span>
101 </span>
102 );
104​
105export function DesktopUpdateStatusIcon({
106 downloadPercent,
107 isCheckAnimating,
108 onCheckAnimationIteration,
109 status,
110}: {
111 readonly downloadPercent?: number | null;
112 readonly isCheckAnimating?: boolean;
113 readonly onCheckAnimationIteration?: AnimationEventHandler<SVGSVGElement>;
114 readonly status: DesktopUpdateStatusIconState;
115}) {
116 if (status === "available") return <DesktopUpdateAvailableIcon />;
117 if (status === "downloading") {
118 return <DesktopUpdateDownloadingIcon percent={downloadPercent ?? null} />;
119 }
120 if (status === "downloaded") return <DesktopUpdateDownloadedIcon />;
121 if (status === "everything") return <CircleArrowUpIcon className="size-4" />;
122​
123 return (
124 <RefreshIcon
125 className="size-4"
126 refreshing={status === "checking" && isCheckAnimating === true}
127 onAnimationIteration={onCheckAnimationIteration}
128 />
129 );