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

SidebarUpdatePill.tsx

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