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