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