-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppUpdateNotification.tsx
More file actions
286 lines (260 loc) · 9.87 KB
/
Copy pathAppUpdateNotification.tsx
File metadata and controls
286 lines (260 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { useState, useEffect, useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Download, RefreshCw, CheckCircle2, AlertCircle, ExternalLink } from "lucide-react";
import ReactMarkdown, { type Components } from "react-markdown";
import remarkGfm from "remark-gfm";
import { Button } from "./ui/button";
import { Progress } from "./ui/progress";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "./ui/dialog";
import type { AppUpdateAvailableEvent, AppUpdateProgress } from "../../shared/types";
const CLAUDE_CODE_CHANGELOG_URL =
"https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md";
// createSafeLink - factory function that creates a SafeLink component with i18n support
const createSafeLink = (opensInNewWindowText: string) => {
return function SafeLink({
href,
children,
...props
}: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
// Validate URL - only allow http, https, and relative links
const isValidUrl =
href &&
(href.startsWith("http://") ||
href.startsWith("https://") ||
href.startsWith("/") ||
href.startsWith("#"));
if (!isValidUrl) {
// For invalid or potentially malicious URLs, render as plain text
return <span className="text-muted-foreground">{children}</span>;
}
// External links get security attributes and accessibility indicator
const isExternal = href?.startsWith("http://") || href?.startsWith("https://");
return (
<a
href={href}
{...props}
{...(isExternal && {
target: "_blank",
rel: "noopener noreferrer",
})}
className="text-primary hover:underline"
>
{children}
{isExternal && <span className="sr-only"> {opensInNewWindowText}</span>}
</a>
);
};
};
/**
* App Update Notification Dialog
* Shows when a new app version is available and handles download/install workflow
*/
export function AppUpdateNotification() {
const { t } = useTranslation(["dialogs", "common"]);
const [isOpen, setIsOpen] = useState(false);
const [updateInfo, setUpdateInfo] = useState<AppUpdateAvailableEvent | null>(null);
const [downloadProgress, setDownloadProgress] = useState<AppUpdateProgress | null>(null);
const [isDownloading, setIsDownloading] = useState(false);
const [isDownloaded, setIsDownloaded] = useState(false);
const [downloadError, setDownloadError] = useState<string | null>(null);
// Create markdown components with translated accessibility text
const markdownComponents: Components = useMemo(
() => ({
a: createSafeLink(t("common:accessibility.opensInNewWindow")),
}),
[t]
);
// Listen for update available event
useEffect(() => {
const cleanup = window.electronAPI.onAppUpdateAvailable((info) => {
setUpdateInfo(info);
setIsOpen(true);
setIsDownloading(false);
setIsDownloaded(false);
setDownloadProgress(null);
setDownloadError(null);
});
return cleanup;
}, []);
// Listen for update downloaded event
useEffect(() => {
const cleanup = window.electronAPI.onAppUpdateDownloaded((_info) => {
setIsDownloading(false);
setIsDownloaded(true);
setDownloadProgress(null);
});
return cleanup;
}, []);
// Listen for download progress
useEffect(() => {
const cleanup = window.electronAPI.onAppUpdateProgress((progress) => {
setDownloadProgress(progress);
});
return cleanup;
}, []);
const handleDownload = async () => {
setIsDownloading(true);
setDownloadError(null);
try {
const result = await window.electronAPI.downloadAppUpdate();
if (!result.success) {
setDownloadError(
result.error || t("dialogs:appUpdate.downloadError", "Failed to download update")
);
setIsDownloading(false);
}
} catch (error) {
console.error("Failed to download app update:", error);
setDownloadError(t("dialogs:appUpdate.downloadError", "Failed to download update"));
setIsDownloading(false);
}
};
const handleInstall = () => {
window.electronAPI.installAppUpdate();
};
const handleDismiss = () => {
setIsOpen(false);
};
if (!updateInfo) {
return null;
}
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Download className="h-5 w-5" />
{t("dialogs:appUpdate.title", "App Update Available")}
</DialogTitle>
<DialogDescription>
{t(
"dialogs:appUpdate.description",
"A new version of Auto Code is ready to download"
)}
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* Version Info */}
<div className="rounded-lg border border-border bg-muted/50 p-4">
<div className="flex items-center justify-between">
<div>
<p className="text-xs text-muted-foreground uppercase tracking-wider mb-1">
{t("dialogs:appUpdate.newVersion", "New Version")}
</p>
<p className="text-base font-medium text-foreground">{updateInfo.version}</p>
{updateInfo.releaseDate && (
<p className="text-xs text-muted-foreground mt-1">
{t("dialogs:appUpdate.released", "Released")}{" "}
{new Date(updateInfo.releaseDate).toLocaleDateString()}
</p>
)}
</div>
{isDownloaded ? (
<CheckCircle2 className="h-6 w-6 text-success" />
) : isDownloading ? (
<RefreshCw className="h-6 w-6 animate-spin text-info" />
) : (
<Download className="h-6 w-6 text-info" />
)}
</div>
</div>
{/* Release Notes */}
{updateInfo.releaseNotes && (
<div className="bg-background rounded-lg p-4 max-h-64 overflow-y-auto border border-border/50">
<div className="prose prose-sm dark:prose-invert max-w-none">
<ReactMarkdown remarkPlugins={[remarkGfm]} components={markdownComponents}>
{updateInfo.releaseNotes}
</ReactMarkdown>
</div>
</div>
)}
{/* Claude Code Changelog Link */}
<Button
variant="link"
size="sm"
className="w-full text-xs text-muted-foreground gap-1"
onClick={() => window.electronAPI?.openExternal?.(CLAUDE_CODE_CHANGELOG_URL)}
aria-label={t(
"dialogs:appUpdate.claudeCodeChangelogAriaLabel",
"View Claude Code Changelog (opens in new window)"
)}
>
{t("dialogs:appUpdate.claudeCodeChangelog", "View Claude Code Changelog")}
<ExternalLink className="h-3 w-3" aria-hidden="true" />
</Button>
{/* Download Progress */}
{isDownloading && downloadProgress && (
<div className="space-y-2">
<div className="flex items-center justify-between text-sm">
<span className="text-muted-foreground">
{t("dialogs:appUpdate.downloading", "Downloading...")}
</span>
<span className="text-foreground font-medium">
{Math.round(downloadProgress.percent)}%
</span>
</div>
<Progress value={downloadProgress.percent} className="h-2" />
<p className="text-xs text-muted-foreground text-right">
{(downloadProgress.transferred / 1024 / 1024).toFixed(2)} MB /{" "}
{(downloadProgress.total / 1024 / 1024).toFixed(2)} MB
</p>
</div>
)}
{/* Download Error */}
{downloadError && (
<div className="flex items-center gap-3 text-sm text-destructive bg-destructive/10 border border-destructive/30 rounded-lg p-3">
<AlertCircle className="h-5 w-5 shrink-0" />
<span>{downloadError}</span>
</div>
)}
{/* Downloaded Success */}
{isDownloaded && (
<div className="flex items-center gap-3 text-sm text-success bg-success/10 border border-success/30 rounded-lg p-3">
<CheckCircle2 className="h-5 w-5 shrink-0" />
<span>
{t(
"dialogs:appUpdate.updateDownloaded",
"Update downloaded successfully! Click Install to restart and apply the update."
)}
</span>
</div>
)}
</div>
<DialogFooter className="flex flex-col sm:flex-row gap-3">
<Button variant="outline" onClick={handleDismiss} disabled={isDownloading}>
{isDownloaded
? t("dialogs:appUpdate.installLater", "Install Later")
: t("dialogs:appUpdate.remindMeLater", "Remind Me Later")}
</Button>
{isDownloaded ? (
<Button onClick={handleInstall}>
<RefreshCw className="mr-2 h-4 w-4" />
{t("dialogs:appUpdate.installAndRestart", "Install and Restart")}
</Button>
) : (
<Button onClick={handleDownload} disabled={isDownloading}>
{isDownloading ? (
<>
<RefreshCw className="mr-2 h-4 w-4 animate-spin" />
{t("dialogs:appUpdate.downloading", "Downloading...")}
</>
) : (
<>
<Download className="mr-2 h-4 w-4" />
{t("dialogs:appUpdate.downloadUpdate", "Download Update")}
</>
)}
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
);
}