diff --git a/app/components/DesktopEventHandler.tsx b/app/components/DesktopEventHandler.tsx index 28ca23249c..ac3f02ae8a 100644 --- a/app/components/DesktopEventHandler.tsx +++ b/app/components/DesktopEventHandler.tsx @@ -3,6 +3,7 @@ import { useTranslation } from "react-i18next"; import { useHistory } from "react-router-dom"; import { toast } from "sonner"; import KeyboardShortcuts from "~/scenes/KeyboardShortcuts"; +import { showDownloadToast } from "~/components/DownloadToast"; import { useDesktopTitlebar } from "~/hooks/useDesktopTitlebar"; import useStores from "~/hooks/useStores"; import Desktop from "~/utils/Desktop"; @@ -41,6 +42,8 @@ export default function DesktopEventHandler() { }); }); + Desktop.bridge?.onDownloadComplete(showDownloadToast); + Desktop.bridge?.focus(() => { window.document.body.classList.remove("backgrounded"); }); diff --git a/app/components/DownloadToast.tsx b/app/components/DownloadToast.tsx new file mode 100644 index 0000000000..e110164a05 --- /dev/null +++ b/app/components/DownloadToast.tsx @@ -0,0 +1,89 @@ +import { t } from "i18next"; +import { useCallback } from "react"; +import { useTranslation } from "react-i18next"; +import { toast } from "sonner"; +import styled from "styled-components"; +import { s } from "@shared/styles"; +import Flex from "~/components/Flex"; +import Desktop from "~/utils/Desktop"; + +type DownloadedFile = { + /** The user facing name of the downloaded file. */ + fileName: string; + /** The absolute path the file was saved to. */ + filePath: string; +}; + +/** + * Displays a toast notifying that a file finished downloading, with actions to + * open the file or reveal it in the file manager. Desktop app only. + * + * @param file the name and path of the downloaded file. + */ +export function showDownloadToast(file: DownloadedFile) { + const toastId = `download-${file.filePath}`; + + toast.success(t("Download complete"), { + id: toastId, + description: file.fileName, + action: , + }); +} + +function DownloadToastActions({ + file, + toastId, +}: { + file: DownloadedFile; + toastId: string; +}) { + const { t } = useTranslation(); + + const handleOpen = useCallback(() => { + toast.dismiss(toastId); + void Desktop.bridge?.openDownload(file.filePath); + }, [file.filePath, toastId]); + + const handleShowInFolder = useCallback(() => { + toast.dismiss(toastId); + void Desktop.bridge?.showDownloadInFolder(file.filePath); + }, [file.filePath, toastId]); + + // The name given to the file manager differs by operating system. + const showInFolderLabel = Desktop.isMacApp() + ? t("Show in Finder") + : Desktop.isWindowsApp() + ? t("Show in Explorer") + : t("Show in folder"); + + return ( + + + {t("Open")} + + + {showInFolderLabel} + + + ); +} + +const Action = styled.button` + height: 24px; + padding: 0 8px; + flex-shrink: 0; + border: 1px solid ${s("divider")}; + border-radius: 4px; + background: transparent; + color: ${s("toastText")}; + font-family: inherit; + font-size: 12px; + font-weight: 500; + line-height: 1; + white-space: nowrap; + cursor: var(--pointer); + + &:hover { + background: ${s("sidebarActiveBackground")}; + } +`; diff --git a/app/typings/window.d.ts b/app/typings/window.d.ts index a14e960737..3045f5c3fd 100644 --- a/app/typings/window.d.ts +++ b/app/typings/window.d.ts @@ -142,6 +142,27 @@ declare global { */ onReplaceInPage: (callback: () => void) => void; + /** + * Registers a callback to be called when a file download completes. The + * callback receives the name and saved path of the file, which can be + * passed back to `openDownload` or `showDownloadInFolder`. + */ + onDownloadComplete: ( + callback: (file: { fileName: string; filePath: string }) => void + ) => void; + + /** + * Opens a downloaded file with the default application for its type. + * Resolves with whether the file was opened. + */ + openDownload: (filePath: string) => Promise; + + /** + * Reveals a downloaded file in the file manager of the operating system. + * Resolves with whether the file was revealed. + */ + showDownloadInFolder: (filePath: string) => Promise; + /** * Get whether the app is configured to launch at login. */