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

SidebarUpdatePill.tsx

184 lines6.8 KB

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 { stackedThreadToast, 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 stackedThreadToast({
60 type: "error",
61 title: "Could not download update",
62 description: actionError,
63 }),
64 );
65 })
66 .catch((error) => {
67 toastManager.add(
68 stackedThreadToast({
69 type: "error",
70 title: "Could not start update download",
71 description: error instanceof Error ? error.message : "An unexpected error occurred.",
72 }),
73 );
74 });
75 return;
76 }
77​
78 if (action === "install") {
79 const confirmed = window.confirm(getDesktopUpdateInstallConfirmationMessage(state));
80 if (!confirmed) return;
81 void bridge
82 .installUpdate()
83 .then((result) => {
84 setDesktopUpdateStateQueryData(queryClient, result.state);
85 if (!shouldToastDesktopUpdateActionResult(result)) return;
86 const actionError = getDesktopUpdateActionError(result);
87 if (!actionError) return;
88 toastManager.add(
89 stackedThreadToast({
90 type: "error",
91 title: "Could not install update",
92 description: actionError,
93 }),
94 );
95 })
96 .catch((error) => {
97 toastManager.add(
98 stackedThreadToast({
99 type: "error",
100 title: "Could not install update",
101 description: error instanceof Error ? error.message : "An unexpected error occurred.",
102 }),
103 );
104 });
105 }
106 }, [action, disabled, queryClient, state]);
107​
108 if (!visible && !showArm64Warning) return null;
109​
110 return (
111 <div className="flex flex-col gap-1">
112 {showArm64Warning && arm64Description && (
113 <Alert variant="warning" className="rounded-2xl border-warning/40 bg-warning/8 text-xs">
114 <TriangleAlertIcon />
115 <AlertTitle>Intel build on Apple Silicon</AlertTitle>
116 <AlertDescription>{arm64Description}</AlertDescription>
117 </Alert>
118 )}
119 {visible && (
120 <div
121 className={`group/update relative flex h-7 w-full items-center rounded-lg bg-primary/15 text-xs font-medium text-primary ${
122 disabled ? " cursor-not-allowed opacity-60" : ""
123 }`}
124 >
125 <div className="pointer-events-none absolute inset-0 rounded-lg transition-colors group-has-[button.update-main:hover]/update:bg-primary/22" />
126 <Tooltip>
127 <TooltipTrigger
128 render={
129 <button
130 type="button"
131 aria-label={tooltip}
132 aria-disabled={disabled || undefined}
133 disabled={disabled}
134 className="update-main relative flex h-full flex-1 items-center gap-2 px-2 enabled:cursor-pointer"
135 onClick={handleAction}
136 >
137 {action === "install" ? (
138 <>
139 <RotateCwIcon className="size-3.5" />
140 <span>Restart to update</span>
141 </>
142 ) : state?.status === "downloading" ? (
143 <>
144 <DownloadIcon className="size-3.5" />
145 <span>
146 Downloading
147 {typeof state.downloadPercent === "number"
148 ? ` (${Math.floor(state.downloadPercent)}%)`
149 : "…"}
150 </span>
151 </>
152 ) : (
153 <>
154 <DownloadIcon className="size-3.5" />
155 <span>Update available</span>
156 </>
157 )}
158 </button>
159 }
160 />
161 <TooltipPopup side="top">{tooltip}</TooltipPopup>
162 </Tooltip>
163 {action === "download" && (
164 <Tooltip>
165 <TooltipTrigger
166 render={
167 <button
168 type="button"
169 aria-label="Dismiss update"
170 className="mr-1 inline-flex size-5 items-center justify-center rounded-md text-primary/60 transition-colors hover:text-primary"
171 onClick={() => setDismissed(true)}
172 >
173 <XIcon className="size-3.5" />
174 </button>
175 }
176 />
177 <TooltipPopup side="top">Dismiss until next launch</TooltipPopup>
178 </Tooltip>
179 )}
180 </div>
181 )}
182 </div>
183 );