Repository navigation
feat: download manager #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
50a633f
a4c620e
f0de032
e2c63b4
fc594c2
5c22e32
999787e
c240dd2
c08a893
644e5b1
26df532
d34a590
ac210f4
84335aa
69c9fb9
9aa4d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,11 @@ import fs from "node:fs"; | |||||||||||||||||||||||||||||||||||||||||||||
| import { ipcMain, shell } from "electron"; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { downloadsController } from "@/controllers/downloads-controller"; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { deleteDownloadRecord, getDownloadRecord, listDownloads } from "@/saving/downloads"; | ||||||||||||||||||||||||||||||||||||||||||||||
| import { sendMessageToListeners } from "@/ipc/listeners-manager"; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| export function fireDownloadsChanged() { | ||||||||||||||||||||||||||||||||||||||||||||||
| sendMessageToListeners("downloads:on-changed"); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.handle("downloads:list", () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return downloadsController.listDownloads(); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -38,16 +43,21 @@ ipcMain.handle("downloads:open-file", (_event, downloadId: string) => { | |||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+38
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Await and check the result-ipcMain.handle("downloads:open-file", (_event, downloadId: string) => {
+ipcMain.handle("downloads:open-file", async (_event, downloadId: string) => {
const record = getDownloadRecord(downloadId);
if (!record?.savePath || record.state !== "completed") return false;
- shell.openPath(record.savePath);
- return true;
+ const error = await shell.openPath(record.savePath);
+ return error === "";
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.handle("downloads:remove-record", (_event, downloadId: string) => { | ||||||||||||||||||||||||||||||||||||||||||||||
| return deleteDownloadRecord(downloadId); | ||||||||||||||||||||||||||||||||||||||||||||||
| const ok = deleteDownloadRecord(downloadId); | ||||||||||||||||||||||||||||||||||||||||||||||
| if (ok) fireDownloadsChanged(); | ||||||||||||||||||||||||||||||||||||||||||||||
| return ok; | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.handle("downloads:clear-completed", () => { | ||||||||||||||||||||||||||||||||||||||||||||||
| const downloads = listDownloads(); | ||||||||||||||||||||||||||||||||||||||||||||||
| let changed = false; | ||||||||||||||||||||||||||||||||||||||||||||||
| for (const dl of downloads) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (dl.state === "completed" || dl.state === "cancelled") { | ||||||||||||||||||||||||||||||||||||||||||||||
| deleteDownloadRecord(dl.id); | ||||||||||||||||||||||||||||||||||||||||||||||
| changed = true; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| if (changed) fireDownloadsChanged(); | ||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Consider checking deletion result in clear-completed loop.
Track actual deletions ipcMain.handle("downloads:clear-completed", () => {
const downloads = listDownloads();
let changed = false;
for (const dl of downloads) {
if (dl.state === "completed" || dl.state === "cancelled") {
- deleteDownloadRecord(dl.id);
- changed = true;
+ const deleted = deleteDownloadRecord(dl.id);
+ if (deleted) changed = true;
}
}
if (changed) fireDownloadsChanged();
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ipcMain.handle("downloads:check-files-exist", (_event, downloadIds: string[]) => { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,20 +3,11 @@ import { useSpaces } from "@/components/providers/spaces-provider"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Button } from "@/components/ui/button"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { PopoverTrigger } from "@/components/ui/popover"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { cn } from "@/lib/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { filenameFromRecord, isActive } from "@/components/downloads/manager/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { DownloadRecord } from "~/types/downloads"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { DownloadIcon, FileText } from "lucide-react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useCallback, useEffect, useState } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const POLL_MS = 1500; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function filenameFromRecord(r: DownloadRecord): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (r.savePath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const parts = r.savePath.split(/[/\\]/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return parts[parts.length - 1] || r.suggestedFilename; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return r.suggestedFilename; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function relativeTime(ts: number): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const now = Date.now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const diffSec = Math.floor((now - ts) / 1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -44,7 +35,7 @@ function DownloadRow({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setOpen: (open: boolean) => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const filename = filenameFromRecord(dl); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isActive = dl.state === "progressing" || dl.state === "paused"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const active = isActive(dl.state); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handleClick = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (dl.state === "completed" && !fileMissing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -60,12 +51,9 @@ function DownloadRow({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onClick={handleClick} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className="flex items-center gap-3 px-3 py-2.5 rounded-lg hover:bg-accent/50 cursor-pointer transition-colors" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {/* File icon */} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="shrink-0 size-10 rounded-lg bg-muted/80 border border-border/30 flex items-center justify-center"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <FileText className="size-5 text-muted-foreground" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {/* Text */} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="min-w-0 flex-1"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className={cn( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -76,7 +64,7 @@ function DownloadRow({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {filename} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <p className="text-xs text-muted-foreground mt-0.5 truncate leading-snug"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {isActive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {active | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? dl.state === "paused" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ? "Paused" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : "Downloading…" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -102,7 +90,7 @@ export function DownloadsPopover() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const all = await flow.downloads.list(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setDownloads(all); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const idsToCheck = all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((d) => d.state !== "progressing" && d.state !== "paused" && d.savePath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .filter((d) => !isActive(d.state) && d.savePath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .map((d) => d.id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (idsToCheck.length > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const existence = await flow.downloads.checkFilesExist(idsToCheck); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -113,16 +101,18 @@ export function DownloadsPopover() { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, []); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+147
to
+159
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing mount guard - potential setState after unmount. Unlike the Add mount guard+ const mountedRef = useRef(true);
+
+ useEffect(() => {
+ mountedRef.current = true;
+ return () => {
+ mountedRef.current = false;
+ };
+ }, []);
+
const fetchDownloads = useCallback(async () => {
try {
const all = await flow.downloads.list();
+ if (!mountedRef.current) return;
setDownloads(all);
const idsToCheck = all.filter((d) => !isActive(d.state) && d.savePath).map((d) => d.id);
if (idsToCheck.length > 0) {
const existence = await flow.downloads.checkFilesExist(idsToCheck);
- setFileExistence(existence);
+ if (mountedRef.current) setFileExistence(existence);
}
} catch {
// silently ignore
}
}, []);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Fetch on open + listen for changes while open | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!open) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void fetchDownloads(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const id = setInterval(() => void fetchDownloads(), POLL_MS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return () => clearInterval(id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const unsubscribe = flow.downloads.onChanged(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void fetchDownloads(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return unsubscribe; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [open, fetchDownloads]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Show up to 5 most recent, prioritizing active downloads | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const active = downloads.filter((d) => d.state === "progressing" || d.state === "paused"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const recent = downloads.filter((d) => d.state !== "progressing" && d.state !== "paused"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const active = downloads.filter((d) => isActive(d.state)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const recent = downloads.filter((d) => !isActive(d.state)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shown = [...active, ...recent].slice(0, 5); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const hasActive = active.length > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shell.openPathresult not awaited — errors silently swallowedshell.openPath()returns aPromise<string>where an empty string means success and a non-empty string is the OS error message. Because the promise is not awaited, any failure (no associated application, permission denied, file moved/deleted since the record was saved) is silently dropped. The handler always returnstruewhenever it finds a completed record, so the renderer'sif (!ok) toast.error("Could not open file")guard is never triggered — users get no feedback when the open fails.