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(getDesktopUpdateInstallConfirmationMessage(state));
114 if (!confirmed) return;
115 void bridge
116 .installUpdate()
117 .then((result) => {
118 if (!shouldToastDesktopUpdateActionResult(result)) return;
119 const actionError = getDesktopUpdateActionError(result);
120 if (!actionError) return;
121 toastManager.add(
122 stackedThreadToast({
123 type: "error",
124 title: "Could not install update",
125 description: actionError,
126 }),
127 );
128 })
129 .catch((error) => {
130 toastManager.add(
131 stackedThreadToast({
132 type: "error",
133 title: "Could not install update",
134 description: error instanceof Error ? error.message : "An unexpected error occurred.",
135 }),
136 );
137 });
138 }
139 }, [action, disabled, state]);
140
141 if (!visible && !showArm64Warning) return null;
142
143 return (
144 <div className="flex flex-col gap-1">
145 {showArm64Warning && arm64Description && (
146 <Alert variant="warning" className="rounded-2xl border-warning/40 bg-warning/8 text-xs">
147 <TriangleAlertIcon />
148 <AlertTitle>Intel build on Apple Silicon</AlertTitle>
149 <AlertDescription>{arm64Description}</AlertDescription>
150 </Alert>
151 )}
152 {visible && (
153 <div
154 className={`group/update relative flex h-7 w-full items-center rounded-lg bg-primary/15 text-xs font-medium text-primary ${
155 disabled ? " cursor-not-allowed opacity-60" : ""
156 }`}
157 >
158 <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors group-has-[button.update-main:hover]/update:bg-primary/22" />
159 <Tooltip>
160 <TooltipTrigger
161 render={
162 <button
163 type="button"
164 aria-label={tooltip}
165 aria-disabled={disabled || undefined}
166 disabled={disabled}
167 className="update-main relative flex h-full flex-1 items-center gap-2 px-2 enabled:cursor-pointer"
168 onClick={handleAction}
169 >
170 {action === "install" ? (
171 <>
172 <RotateCwIcon className="size-3.5" />
173 <span>Restart to update</span>
174 </>
175 ) : state?.status === "downloading" ? (
176 <>
177 <DownloadIcon className="size-3.5" />
178 <span>
179 Downloading
180 {typeof state.downloadPercent === "number"
181 ? ` (${Math.floor(state.downloadPercent)}%)`
182 : "…"}
183 </span>
184 </>
185 ) : (
186 <>
187 <DownloadIcon className="size-3.5" />
188 <span>Update available</span>
189 </>
190 )}
191 </button>
192 }
193 />
194 <TooltipPopup
195 align="start"
196 className={
197 state?.channel === "nightly" && state.releaseNotes.length > 0
198 ? "max-w-none text-balance"
199 : undefined
200 }
201 side="top"
202 >
203 {state ? (
204 <SidebarUpdateReleaseNotesTooltip state={state} tooltip={tooltip} />
205 ) : (
206 tooltip
207 )}
208 </TooltipPopup>
209 </Tooltip>
210 {action === "download" && (
211 <Tooltip>
212 <TooltipTrigger
213 render={
214 <button
215 type="button"
216 aria-label="Dismiss update"
217 className="mr-1 inline-flex size-5 items-center justify-center rounded-md text-primary/60 transition-colors hover:text-primary"
218 onClick={() => setDismissed(true)}
219 >
220 <XIcon className="size-3.5" />
221 </button>
222 }
223 />
224 <TooltipPopup side="top">Dismiss until next launch</TooltipPopup>
225 </Tooltip>
226 )}
227 </div>
228 )}
229 </div>
230 );
231}