Open History to see the last commit
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 { Separator } from "../ui/separator";
19import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
20
21function SidebarUpdateReleaseNotesTooltip({
22 state,
23 tooltip,
24}: {
25 readonly state: NonNullable<ReturnType<typeof useDesktopUpdateState>>;
26 readonly tooltip: string;
27}) {
28 if (state.channel !== "nightly" || state.releaseNotes.length === 0) {
29 return <>{tooltip}</>;
30 }
31
32 return (
33 <div className="w-120 max-w-[calc(100vw-2rem)] text-left">
34 <div className="px-1">
35 <div className="text-sm leading-5 font-medium">{tooltip}</div>
36 </div>
37 <div className="pointer-events-auto max-h-[min(28rem,calc(100vh-6rem))] overflow-y-auto px-1 pt-4 pb-1">
38 {state.releaseNotes.map((releaseNote, index) => (
39 <div key={releaseNote.version}>
40 {index > 0 && <Separator className="my-3 bg-border/60" />}
41 <section>
42 <h3 className="text-muted-foreground text-xs leading-4 font-semibold">
43 {index === 0 ? "What's changed" : `Changes in ${releaseNote.version}`}
44 </h3>
45 <ul className="mt-2 space-y-1.5 pl-4 text-xs leading-5 text-popover-foreground/90">
46 {releaseNote.items.map((item, itemIndex) => (
47 <li className="list-disc break-words" key={`${releaseNote.version}-${itemIndex}`}>
48 {item}
49 </li>
50 ))}
51 </ul>
52 </section>
53 </div>
54 ))}
55 </div>
56 </div>
57 );
58}
59
60export function SidebarUpdatePill() {
61 const state = useDesktopUpdateState();
62 const [dismissed, setDismissed] = useState(false);
63
64 const visible = isElectron && shouldShowDesktopUpdateButton(state) && !dismissed;
65 const tooltip = state ? getDesktopUpdateButtonTooltip(state) : "Update available";
66 const disabled = isDesktopUpdateButtonDisabled(state);
67 const action = state ? resolveDesktopUpdateButtonAction(state) : "none";
68
69 const showArm64Warning = isElectron && shouldShowArm64IntelBuildWarning(state);
70 const arm64Description =
71 state && showArm64Warning ? getArm64IntelBuildWarningDescription(state) : null;
72
73 const handleAction = useCallback(() => {
74 const bridge = window.desktopBridge;
75 if (!bridge || !state) return;
76 if (disabled || action === "none") return;
77
78 if (action === "download") {
79 void bridge
80 .downloadUpdate()
81 .then((result) => {
82 if (result.completed) {
83 toastManager.add({
84 type: "success",
85 title: "Update downloaded",
86 description: "Restart the app from the update button to install it.",
87 });
88 }
89 if (!shouldToastDesktopUpdateActionResult(result)) return;
90 const actionError = getDesktopUpdateActionError(result);
91 if (!actionError) return;
92 toastManager.add(
93 stackedThreadToast({
94 type: "error",
95 title: "Could not download update",
96 description: actionError,
97 }),
98 );
99 })
100 .catch((error) => {
101 toastManager.add(
102 stackedThreadToast({
103 type: "error",
104 title: "Could not start update download",
105 description: error instanceof Error ? error.message : "An unexpected error occurred.",
106 }),
107 );
108 });
109 return;
110 }
111
112 if (action === "install") {
113 const confirmed = window.confirm(
114 getDesktopUpdateInstallConfirmationMessage(state, navigator.platform),
115 );
116 if (!confirmed) return;
117 void bridge
118 .installUpdate()
119 .then((result) => {
120 if (!shouldToastDesktopUpdateActionResult(result)) return;
121 const actionError = getDesktopUpdateActionError(result);
122 if (!actionError) return;
123 toastManager.add(
124 stackedThreadToast({
125 type: "error",
126 title: "Could not install update",
127 description: actionError,
128 }),
129 );
130 })
131 .catch((error) => {
132 toastManager.add(
133 stackedThreadToast({
134 type: "error",
135 title: "Could not install update",
136 description: error instanceof Error ? error.message : "An unexpected error occurred.",
137 }),
138 );
139 });
140 }
141 }, [action, disabled, state]);
142
143 if (!visible && !showArm64Warning) return null;
144
145 return (
146 <div className="flex flex-col gap-1">
147 {showArm64Warning && arm64Description && (
148 <Alert variant="warning" className="rounded-2xl border-warning/40 bg-warning/8 text-xs">
149 <TriangleAlertIcon />
150 <AlertTitle>Intel build on Apple Silicon</AlertTitle>
151 <AlertDescription>{arm64Description}</AlertDescription>
152 </Alert>
153 )}
154 {visible && (
155 <div
156 className={`group/update relative flex h-7 w-full items-center rounded-lg bg-primary/15 text-xs font-medium text-primary ${
157 disabled ? " cursor-not-allowed opacity-60" : ""
158 }`}
159 >
160 <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors group-has-[button.update-main:hover]/update:bg-primary/22" />
161 <Tooltip>
162 <TooltipTrigger
163 render={
164 <button
165 type="button"
166 aria-label={tooltip}
167 aria-disabled={disabled || undefined}
168 disabled={disabled}
169 className="update-main relative flex h-full flex-1 items-center gap-2 px-2 enabled:cursor-pointer"
170 onClick={handleAction}
171 >
172 {action === "install" ? (
173 <>
174 <RotateCwIcon className="size-3.5" />
175 <span>Restart to update</span>
176 </>
177 ) : state?.status === "downloading" ? (
178 <>
179 <DownloadIcon className="size-3.5" />
180 <span>
181 Downloading
182 {typeof state.downloadPercent === "number"
183 ? ` (${Math.floor(state.downloadPercent)}%)`
184 : "…"}
185 </span>
186 </>
187 ) : (
188 <>
189 <DownloadIcon className="size-3.5" />
190 <span>Update available</span>
191 </>
192 )}
193 </button>
194 }
195 />
196 <TooltipPopup
197 align="start"
198 className={
199 state?.channel === "nightly" && state.releaseNotes.length > 0
200 ? "max-w-none text-balance"
201 : undefined
202 }
203 side="top"
204 >
205 {state ? (
206 <SidebarUpdateReleaseNotesTooltip state={state} tooltip={tooltip} />
207 ) : (
208 tooltip
209 )}
210 </TooltipPopup>
211 </Tooltip>
212 {action === "download" && (
213 <Tooltip>
214 <TooltipTrigger
215 render={
216 <button
217 type="button"
218 aria-label="Dismiss update"
219 className="mr-1 inline-flex size-5 items-center justify-center rounded-md text-primary/60 transition-colors hover:text-primary"
220 onClick={() => setDismissed(true)}
221 >
222 <XIcon className="size-3.5" />
223 </button>
224 }
225 />
226 <TooltipPopup side="top">Dismiss until next launch</TooltipPopup>
227 </Tooltip>
228 )}
229 </div>
230 )}
231 </div>
232 );
233}