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

SidebarUpdatePill.tsx

177 lines6.5 KB

1import { DownloadIcon, RotateCwIcon, TriangleAlertIcon, XIcon } from "lucide-react";
2import { useCallback, useState } from "react";
3import { isElectron } from "../../env";
4import { useDesktopUpdateState } from "../../state/desktopUpdate";
5import { stackedThreadToast, toastManager } from "../ui/toast";
6import {
7 getArm64IntelBuildWarningDescription,
8 getDesktopUpdateActionError,
9 getDesktopUpdateButtonTooltip,
10 getDesktopUpdateInstallConfirmationMessage,
11 isDesktopUpdateButtonDisabled,
12 resolveDesktopUpdateButtonAction,
13 shouldShowArm64IntelBuildWarning,
14 shouldShowDesktopUpdateButton,
15 shouldToastDesktopUpdateActionResult,
16} from "../desktopUpdate.logic";
17import { Alert, AlertDescription, AlertTitle } from "../ui/alert";
18import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
19​
20export function SidebarUpdatePill() {
21 const state = useDesktopUpdateState();
22 const [dismissed, setDismissed] = useState(false);
23​
24 const visible = isElectron && shouldShowDesktopUpdateButton(state) && !dismissed;
25 const tooltip = state ? getDesktopUpdateButtonTooltip(state) : "Update available";
26 const disabled = isDesktopUpdateButtonDisabled(state);
27 const action = state ? resolveDesktopUpdateButtonAction(state) : "none";
28​
29 const showArm64Warning = isElectron && shouldShowArm64IntelBuildWarning(state);
30 const arm64Description =
31 state && showArm64Warning ? getArm64IntelBuildWarningDescription(state) : null;
32​
33 const handleAction = useCallback(() => {
34 const bridge = window.desktopBridge;
35 if (!bridge || !state) return;
36 if (disabled || action === "none") return;
37​
38 if (action === "download") {
39 void bridge
40 .downloadUpdate()
41 .then((result) => {
42 if (result.completed) {
43 toastManager.add({
44 type: "success",
45 title: "Update downloaded",
46 description: "Restart the app from the update button to install it.",
47 });
48 }
49 if (!shouldToastDesktopUpdateActionResult(result)) return;
50 const actionError = getDesktopUpdateActionError(result);
51 if (!actionError) return;
52 toastManager.add(
53 stackedThreadToast({
54 type: "error",
55 title: "Could not download update",
56 description: actionError,
57 }),
58 );
59 })
60 .catch((error) => {
61 toastManager.add(
62 stackedThreadToast({
63 type: "error",
64 title: "Could not start update download",
65 description: error instanceof Error ? error.message : "An unexpected error occurred.",
66 }),
67 );
68 });
69 return;
70 }
71​
72 if (action === "install") {
73 const confirmed = window.confirm(getDesktopUpdateInstallConfirmationMessage(state));
74 if (!confirmed) return;
75 void bridge
76 .installUpdate()
77 .then((result) => {
78 if (!shouldToastDesktopUpdateActionResult(result)) return;
79 const actionError = getDesktopUpdateActionError(result);
80 if (!actionError) return;
81 toastManager.add(
82 stackedThreadToast({
83 type: "error",
84 title: "Could not install update",
85 description: actionError,
86 }),
87 );
88 })
89 .catch((error) => {
90 toastManager.add(
91 stackedThreadToast({
92 type: "error",
93 title: "Could not install update",
94 description: error instanceof Error ? error.message : "An unexpected error occurred.",
95 }),
96 );
97 });
98 }
99 }, [action, disabled, state]);
100​
101 if (!visible && !showArm64Warning) return null;
102​
103 return (
104 <div className="flex flex-col gap-1">
105 {showArm64Warning && arm64Description && (
106 <Alert variant="warning" className="rounded-2xl border-warning/40 bg-warning/8 text-xs">
107 <TriangleAlertIcon />
108 <AlertTitle>Intel build on Apple Silicon</AlertTitle>
109 <AlertDescription>{arm64Description}</AlertDescription>
110 </Alert>
111 )}
112 {visible && (
113 <div
114 className={`group/update relative flex h-7 w-full items-center rounded-lg bg-primary/15 text-xs font-medium text-primary ${
115 disabled ? " cursor-not-allowed opacity-60" : ""
116 }`}
117 >
118 <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors group-has-[button.update-main:hover]/update:bg-primary/22" />
119 <Tooltip>
120 <TooltipTrigger
121 render={
122 <button
123 type="button"
124 aria-label={tooltip}
125 aria-disabled={disabled || undefined}
126 disabled={disabled}
127 className="update-main relative flex h-full flex-1 items-center gap-2 px-2 enabled:cursor-pointer"
128 onClick={handleAction}
129 >
130 {action === "install" ? (
131 <>
132 <RotateCwIcon className="size-3.5" />
133 <span>Restart to update</span>
134 </>
135 ) : state?.status === "downloading" ? (
136 <>
137 <DownloadIcon className="size-3.5" />
138 <span>
139 Downloading
140 {typeof state.downloadPercent === "number"
141 ? ` (${Math.floor(state.downloadPercent)}%)`
142 : "…"}
143 </span>
144 </>
145 ) : (
146 <>
147 <DownloadIcon className="size-3.5" />
148 <span>Update available</span>
149 </>
150 )}
151 </button>
152 }
153 />
154 <TooltipPopup side="top">{tooltip}</TooltipPopup>
155 </Tooltip>
156 {action === "download" && (
157 <Tooltip>
158 <TooltipTrigger
159 render={
160 <button
161 type="button"
162 aria-label="Dismiss update"
163 className="mr-1 inline-flex size-5 items-center justify-center rounded-md text-primary/60 transition-colors hover:text-primary"
164 onClick={() => setDismissed(true)}
165 >
166 <XIcon className="size-3.5" />
167 </button>
168 }
169 />
170 <TooltipPopup side="top">Dismiss until next launch</TooltipPopup>
171 </Tooltip>
172 )}
173 </div>
174 )}
175 </div>
176 );