Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Improve public single-file viewer loading flow

Anand Baburajan7e8e5d584d94
diff --git a/mobile/apps/auth/flutter b/mobile/apps/auth/flutter
new file mode 160000
index 0000000000..2663184aa7
--- /dev/null
+++ b/mobile/apps/auth/flutter
@@ -0,0 +1 @@
+Subproject commit 2663184aa79047d0a33a14a3b607954f8fdd8730
diff --git a/web/apps/photos/src/components/PublicAlbumSingleFileViewer.tsx b/web/apps/photos/src/components/PublicAlbumSingleFileViewer.tsx
new file mode 100644
index 0000000000..8883b6c93f
--- /dev/null
+++ b/web/apps/photos/src/components/PublicAlbumSingleFileViewer.tsx
@@ -0,0 +1,489 @@
+import CheckIcon from "@mui/icons-material/Check";
+import ContentCopyIcon from "@mui/icons-material/ContentCopy";
+import DownloadOutlinedIcon from "@mui/icons-material/DownloadOutlined";
+import FullscreenOutlinedIcon from "@mui/icons-material/FullscreenOutlined";
+import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined";
+import MoreVertIcon from "@mui/icons-material/MoreVert";
+import ShareOutlinedIcon from "@mui/icons-material/ShareOutlined";
+import {
+ Box,
+ Button,
+ GlobalStyles,
+ IconButton,
+ Menu,
+ MenuItem,
+ Stack,
+ styled,
+ Typography,
+} from "@mui/material";
+import { EnteLogo } from "ente-base/components/EnteLogo";
+import { ActivityIndicator } from "ente-base/components/mui/ActivityIndicator";
+import { useBaseContext } from "ente-base/context";
+import log from "ente-base/log";
+import type { PublicAlbumsCredentials } from "ente-base/http";
+import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";
+import { FileViewer } from "ente-gallery/components/viewer/FileViewer";
+import { downloadManager } from "ente-gallery/services/download";
+import { downloadAndSaveFiles } from "ente-gallery/services/save";
+import type { EnteFile } from "ente-media/file";
+import { fileFileName } from "ente-media/file-metadata";
+import { FileType } from "ente-media/file-type";
+import { Notification } from "ente-new/photos/components/Notification";
+import { t } from "i18next";
+import { useCallback, useEffect, useMemo, useState } from "react";
+import { getEnteURL } from "utils/public-album";
+
+interface PublicAlbumSingleFileViewerProps {
+ file: EnteFile;
+ albumName: string;
+ publicAlbumsCredentials: PublicAlbumsCredentials;
+ collectionKey: string;
+ enableDownload: boolean;
+ enableComment: boolean;
+ enableJoin?: boolean;
+ onJoinAlbum?: () => void;
+ onVisualFeedback: () => void;
+ onAddSaveGroup: AddSaveGroup;
+}
+
+const bodyClassName = "ente-public-single-file-viewer";
+
+/**
+ * A dedicated public-album single-file viewer mode with a bespoke header/menu.
+ *
+ * This wraps the regular FileViewer but overlays its own controls and blocks
+ * close-by-Escape behavior.
+ */
+export const PublicAlbumSingleFileViewer: React.FC<
+ PublicAlbumSingleFileViewerProps
+> = ({
+ file,
+ albumName,
+ publicAlbumsCredentials,
+ collectionKey,
+ enableDownload,
+ enableComment,
+ enableJoin,
+ onJoinAlbum,
+ onVisualFeedback,
+ onAddSaveGroup,
+}) => {
+ const { onGenericError } = useBaseContext();
+ const [viewerKey, setViewerKey] = useState(0);
+ const [menuAnchorEl, setMenuAnchorEl] = useState<HTMLElement | null>(null);
+ const [showCopiedMessage, setShowCopiedMessage] = useState(false);
+ const needsThumbnailPrime =
+ file.metadata.fileType === FileType.image ||
+ file.metadata.fileType === FileType.livePhoto;
+ const needsOriginalPrime = needsThumbnailPrime;
+ const [primedFileID, setPrimedFileID] = useState<number | undefined>(() =>
+ needsThumbnailPrime ? undefined : file.id,
+ );
+ const isViewerPrimed = !needsThumbnailPrime || primedFileID === file.id;
+
+ useEffect(() => {
+ document.body.classList.add(bodyClassName);
+ return () => document.body.classList.remove(bodyClassName);
+ }, []);
+
+ useEffect(() => {
+ const handleEscape = (event: KeyboardEvent) => {
+ if (event.key !== "Escape") return;
+ event.preventDefault();
+ event.stopPropagation();
+ };
+
+ window.addEventListener("keydown", handleEscape, true);
+ return () => window.removeEventListener("keydown", handleEscape, true);
+ }, []);
+
+ useEffect(() => {
+ if (!needsThumbnailPrime) {
+ setPrimedFileID(file.id);
+ return;
+ }
+ if (primedFileID === file.id) return;
+
+ let isActive = true;
+ void downloadManager
+ .renderableThumbnailURL(file)
+ .catch((error: unknown) =>
+ log.warn(
+ "Failed to prime thumbnail for public single-file viewer",
+ error,
+ ),
+ )
+ .finally(() => {
+ if (isActive) setPrimedFileID(file.id);
+ });
+
+ return () => {
+ isActive = false;
+ };
+ }, [file, file.id, needsThumbnailPrime, primedFileID]);
+
+ useEffect(() => {
+ if (!needsOriginalPrime) return;
+
+ // Give thumbnail fetch a brief head-start, then warm the original.
+ const prefetchTimer = window.setTimeout(() => {
+ void downloadManager
+ .renderableSourceURLs(file)
+ .catch((error: unknown) =>
+ log.warn(
+ "Failed to prime original for public single-file viewer",
+ error,
+ ),
+ );
+ }, 120);
+
+ return () => window.clearTimeout(prefetchTimer);
+ }, [file, needsOriginalPrime]);
+
+ const handleViewerClose = useCallback(() => {
+ window.close();
+ window.setTimeout(() => {
+ if (!window.closed) {
+ // Keep the tab where it is if the browser blocked window.close.
+ window.history.go(1);
+ setViewerKey((k) => k + 1);
+ }
+ }, 0);
+ }, []);
+
+ const handleMenuClose = useCallback(() => setMenuAnchorEl(null), []);
+
+ const handleDownload = useCallback(
+ (targetFile: EnteFile) =>
+ downloadAndSaveFiles(
+ [targetFile],
+ fileFileName(targetFile),
+ onAddSaveGroup,
+ ),
+ [onAddSaveGroup],
+ );
+
+ const handleShare = useCallback(async () => {
+ handleMenuClose();
+ const shareUrl = window.location.href;
+ const shareText = `${albumName}\n${shareUrl}`;
+ const isMobile = window.matchMedia("(width < 720px)").matches;
+
+ if (isMobile && typeof navigator.share === "function") {
+ try {
+ await navigator.share({ text: shareText });
+ return;
+ } catch (error) {
+ if (error instanceof Error && error.name === "AbortError") {
+ return;
+ }
+ }
+ }
+
+ try {
+ await navigator.clipboard.writeText(
+ isMobile ? shareText : shareUrl,
+ );
+ setShowCopiedMessage(true);
+ window.setTimeout(() => setShowCopiedMessage(false), 2000);
+ } catch (error) {
+ onGenericError(error);
+ }
+ }, [albumName, handleMenuClose, onGenericError]);
+
+ const canCopyAsPNG = useMemo(
+ () =>
+ file.metadata.fileType === FileType.image ||
+ file.metadata.fileType === FileType.livePhoto,
+ [file.metadata.fileType],
+ );
+
+ const handleCopyAsPNG = useCallback(async () => {
+ handleMenuClose();
+ if (!canCopyAsPNG) return;
+
+ try {
+ const imageElement =
+ document.querySelector<HTMLImageElement>(
+ ".pswp .pswp__item--active img.pswp__img",
+ ) ??
+ document.querySelector<HTMLImageElement>(".pswp img.pswp__img");
+
+ const imageURL = imageElement?.currentSrc || imageElement?.src;
+ if (!imageURL) return;
+
+ await navigator.clipboard.write([
+ new ClipboardItem({
+ "image/png": createImagePNGBlob(imageURL),
+ }),
+ ]);
+ onVisualFeedback();
+ } catch (error) {
+ onGenericError(error);
+ }
+ }, [canCopyAsPNG, handleMenuClose, onGenericError, onVisualFeedback]);
+
+ const handleOpenInfo = useCallback(() => {
+ handleMenuClose();
+ document
+ .querySelector<HTMLButtonElement>(".pswp .pswp__button--info")
+ ?.click();
+ }, [handleMenuClose]);
+
+ const handleToggleFullscreen = useCallback(() => {
+ handleMenuClose();
+ void (
+ document.fullscreenElement
+ ? document.exitFullscreen()
+ : document.body.requestFullscreen()
+ ).catch(onGenericError);
+ }, [handleMenuClose, onGenericError]);
+
+ const handleTryEnte = useCallback(() => {
+ window.location.href = getEnteURL();
+ }, []);
+
+ return (
+ <>
+ <GlobalStyles
+ styles={{
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__counter`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--zoom`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--close`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--more`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--info`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--ente-logo`]:
+ { display: "none !important" },
+ [`body.${bodyClassName} .pswp-ente-public-album .pswp__button--download`]:
+ { display: "none !important" },
+ }}
+ />
+ <FileViewer
+ key={viewerKey}
+ open
+ onClose={handleViewerClose}
+ files={[file]}
+ initialIndex={0}
+ disableDownload={!enableDownload}
+ onDownload={enableDownload ? handleDownload : undefined}
+ onVisualFeedback={onVisualFeedback}
+ publicAlbumsCredentials={publicAlbumsCredentials}
+ collectionKey={collectionKey}
+ onJoinAlbum={onJoinAlbum}
+ enableComment={enableComment}
+ enableJoin={enableJoin}
+ />
+ {isViewerPrimed && (
+ <>
+ <Box
+ sx={{
+ position: "fixed",
+ top: {
+ xs: "calc(env(titlebar-area-height, 0px) * 0.4 + 12px)",
+ sm: "calc(env(titlebar-area-height, 0px) * 0.4 + 40px)",
+ },
+ left: 0,
+ right: 0,
+ pl: { xs: 1.5, sm: 5.5 },
+ pr: { xs: 0.5, sm: 4 },
+ zIndex: (theme) => theme.zIndex.modal + 1,
+ pointerEvents: "none",
+ }}
+ >
+ <Stack
+ direction="row"
+ justifyContent="space-between"
+ alignItems="center"
+ sx={{ pointerEvents: "auto" }}
+ >
+ <Box
+ component="a"
+ href="https://ente.io"
+ target="_blank"
+ rel="noopener noreferrer"
+ sx={{
+ color: "white",
+ opacity: 0.85,
+ lineHeight: 0,
+ "& svg": {
+ width: "auto",
+ height: { xs: 15, sm: 19 },
+ },
+ "&:hover": { opacity: 1 },
+ }}
+ >
+ <EnteLogo height={15} />
+ </Box>
+ <Stack direction="row" alignItems="center" spacing={1}>
+ <Button
+ variant="contained"
+ onClick={handleTryEnte}
+ sx={{
+ borderRadius: "16px",
+ paddingBlock: "11px",
+ paddingInline: "20px",
+ minHeight: "unset",
+ textTransform: "none",
+ fontWeight: "medium",
+ color: "#fff",
+ backgroundColor: "#08C225",
+ "&:hover": {
+ color: "#fff",
+ backgroundColor: "#07A820",
+ },
+ }}
+ >
+ {t("try_ente")}
+ </Button>
+ <IconButton
+ onClick={(event) =>
+ setMenuAnchorEl(event.currentTarget)
+ }
+ aria-label={t("more")}
+ sx={{
+ color: "white",
+ bgcolor: "rgba(0, 0, 0, 0.4)",
+ "&:hover": {
+ bgcolor: "rgba(0, 0, 0, 0.55)",
+ },
+ }}
+ >
+ <MoreVertIcon />
+ </IconButton>
+ </Stack>
+ </Stack>
+ </Box>
+ <MoreMenu
+ open={!!menuAnchorEl}
+ anchorEl={menuAnchorEl}
+ onClose={handleMenuClose}
+ disableAutoFocusItem
+ slotProps={{ paper: { sx: { mt: "10px" } } }}
+ >
+ {enableDownload && (
+ <MoreMenuItem
+ onClick={() => {
+ handleMenuClose();
+ void handleDownload(file);
+ }}
+ >
+ <MoreMenuItemTitle>{t("download")}</MoreMenuItemTitle>
+ <DownloadOutlinedIcon />
+ </MoreMenuItem>
+ )}
+ <MoreMenuItem onClick={handleOpenInfo}>
+ <MoreMenuItemTitle>{t("info")}</MoreMenuItemTitle>
+ <InfoOutlinedIcon />
+ </MoreMenuItem>
+ <MoreMenuItem onClick={() => void handleShare()}>
+ <MoreMenuItemTitle>{t("share_action")}</MoreMenuItemTitle>
+ <ShareOutlinedIcon />
+ </MoreMenuItem>
+ {canCopyAsPNG && (
+ <MoreMenuItem onClick={() => void handleCopyAsPNG()}>
+ <MoreMenuItemTitle>
+ {t("copy_as_png")}
+ </MoreMenuItemTitle>
+ <ContentCopyIcon
+ sx={{ "&&": { fontSize: "18px" } }}
+ />
+ </MoreMenuItem>
+ )}
+ <MoreMenuItem onClick={handleToggleFullscreen}>
+ <MoreMenuItemTitle>
+ {t("go_fullscreen")}
+ </MoreMenuItemTitle>
+ <FullscreenOutlinedIcon />
+ </MoreMenuItem>
+ </MoreMenu>
+ </>
+ )}
+ {!isViewerPrimed && (
+ <Box
+ sx={{
+ position: "fixed",
+ inset: 0,
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ backgroundColor: "black",
+ zIndex: (theme) => theme.zIndex.modal + 2,
+ }}
+ >
+ <ActivityIndicator />
+ </Box>
+ )}
+ <Notification
+ open={showCopiedMessage}
+ onClose={() => setShowCopiedMessage(false)}
+ horizontal="left"
+ attributes={{
+ color: "secondary",
+ startIcon: <CheckIcon />,
+ title: "Copied!",
+ }}
+ />
+ </>
+ );
+};
+
+const MoreMenu = styled(Menu)(
+ ({ theme }) => `
+ & .MuiPaper-root {
+ background-color: ${theme.vars.palette.fixed.dark.background.paper};
+ }
+ & .MuiList-root {
+ padding-block: 2px;
+ }
+`,
+);
+
+const MoreMenuItem = styled(MenuItem)(
+ ({ theme }) => `
+ min-width: 210px;
+ padding-block: 12px;
+ min-height: auto;
+ gap: 1;
+ justify-content: space-between;
+ align-items: center;
+ color: rgba(255 255 255 / 0.85);
+ &:hover {
+ color: rgba(255 255 255 / 1);
+ background-color: ${theme.vars.palette.fixed.dark.background.paper2}
+ }
+
+ .MuiSvgIcon-root {
+ font-size: 20px;
+ }
+`,
+);
+
+const MoreMenuItemTitle: React.FC<React.PropsWithChildren> = ({ children }) => (
+ <Typography sx={{ fontWeight: "medium" }}>{children}</Typography>
+);
+
+/**
+ * Return an "image/png" blob derived from the given source URL.
+ */
+const createImagePNGBlob = async (imageURL: string): Promise<Blob> =>
+ new Promise((resolve, reject) => {
+ const image = new Image();
+ image.onload = () => {
+ const canvas = document.createElement("canvas");
+ canvas.width = image.width;
+ canvas.height = image.height;
+ canvas.getContext("2d")!.drawImage(image, 0, 0);
+ canvas.toBlob(
+ (blob) =>
+ blob ? resolve(blob) : reject(new Error("toBlob failed")),
+ "image/png",
+ );
+ };
+ image.onerror = reject;
+ image.src = imageURL;
+ });
diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsx
index 56aa4a7827..c7d0cf21c2 100644
--- a/web/apps/photos/src/pages/shared-albums.tsx
+++ b/web/apps/photos/src/pages/shared-albums.tsx
@@ -22,6 +22,7 @@ import { FeedIcon } from "components/Collections/CollectionHeader";
import { DownloadStatusNotifications } from "components/DownloadStatusNotifications";
import { type FileListHeaderOrFooter } from "components/FileList";
import { FileListWithViewer } from "components/FileListWithViewer";
+import { PublicAlbumSingleFileViewer } from "components/PublicAlbumSingleFileViewer";
import { TripLayout } from "components/TripLayout";
import { Upload } from "components/Upload";
import {
@@ -582,6 +583,33 @@ export default function PublicCollectionGallery() {
}
const layout = publicCollection?.pubMagicMetadata?.data.layout || "grouped";
+ const isSingleFileAlbum = publicFiles.length === 1;
+ const shouldShowSingleFileViewer =
+ isSingleFileAlbum && (layout === "grouped" || layout === "continuous");
+
+ if (shouldShowSingleFileViewer) {
+ return (
+ <>
+ <PublicAlbumSingleFileViewer
+ file={publicFiles[0]!}
+ albumName={publicCollection?.name ?? ""}
+ publicAlbumsCredentials={credentials.current}
+ collectionKey={collectionKey.current!}
+ enableDownload={downloadEnabled}
+ enableComment={commentsEnabled}
+ enableJoin={publicCollection?.publicURLs[0]?.enableJoin}
+ onJoinAlbum={handleJoinAlbum}
+ onVisualFeedback={handleVisualFeedback}
+ onAddSaveGroup={onAddSaveGroup}
+ />
+ {blockingLoad && <TranslucentLoadingOverlay />}
+ <DownloadStatusNotifications
+ {...{ saveGroups, onRemoveSaveGroup }}
+ fullWidthOnMobile
+ />
+ </>
+ );
+ }
return (
<FullScreenDropZone
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index f9995a12fb..8560129d48 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -291,6 +291,7 @@
"delete_photos": "Delete photos",
"keep_photos": "Keep photos",
"share_album": "Share album",
+ "share_action": "Share",
"feed": "Feed",
"sharing_with_self": "You cannot share with yourself",
"sharing_already_shared": "You're already sharing this with {{email}}",