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

DesktopUpdateStatusIcon.tsx

126 lines3.8 KB

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