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