Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat(memories):prefetch-media-and-fix-aspect-ratio

Aswin Asok8612c1a6bb13
diff --git a/web/apps/memories/src/components/MemoryViewer.tsx b/web/apps/memories/src/components/MemoryViewer.tsx
index e44f6df40e..237989b8ff 100644
--- a/web/apps/memories/src/components/MemoryViewer.tsx
+++ b/web/apps/memories/src/components/MemoryViewer.tsx
@@ -7,6 +7,7 @@
*/
import { keyframes } from "@emotion/react";
import { styled, Typography } from "@mui/material";
+import log from "ente-base/log";
import { downloadManager } from "ente-gallery/services/download";
import { fileCreationPhotoDate } from "ente-media/file-metadata";
import { FileType } from "ente-media/file-type";
@@ -221,11 +222,35 @@ export function MemoryViewer({
}, [currentIndex, files.length, onNext]);
useEffect(() => {
- if (currentIndex < files.length - 1) {
- const nextFile = files[currentIndex + 1]!;
- void downloadManager.renderableThumbnailURL(nextFile);
+ if (!fileLoaded || currentIndex >= files.length - 1) {
+ return;
}
- }, [currentIndex, files]);
+
+ const nextFile = files[currentIndex + 1]!;
+
+ void (async () => {
+ const prefetchTasks: Promise<unknown>[] = [
+ downloadManager.renderableThumbnailURL(nextFile),
+ downloadManager.renderableSourceURLs(nextFile),
+ ];
+
+ if (nextFile.metadata.fileType === FileType.video) {
+ prefetchTasks.unshift(
+ downloadManager.hlsPlaylistDataForPublicMemory(nextFile),
+ );
+ }
+
+ const results = await Promise.allSettled(prefetchTasks);
+ results.forEach((result) => {
+ if (result.status === "rejected") {
+ log.warn(
+ "Failed to prefetch next memory media",
+ result.reason,
+ );
+ }
+ });
+ })();
+ }, [currentIndex, fileLoaded, files]);
useEffect(() => {
const updateViewport = () => setViewport(readViewport());
diff --git a/web/apps/memories/src/components/PublicMemoryMedia.tsx b/web/apps/memories/src/components/PublicMemoryMedia.tsx
index 8b18375c2d..c224b3a68c 100644
--- a/web/apps/memories/src/components/PublicMemoryMedia.tsx
+++ b/web/apps/memories/src/components/PublicMemoryMedia.tsx
@@ -25,6 +25,7 @@ import { computeMediaCropStyle } from "../utils/lane";
const DEFAULT_MEDIA_MAX_WIDTH_CSS = "min(1360px, calc(100vw - 32px))";
const DEFAULT_MEDIA_MAX_HEIGHT_CSS = "calc(100dvh - 184px)";
+const ASPECT_RATIO_CHANGE_EPSILON = 0.00001;
const buildMediaStyle = ({
cropRect,
@@ -122,6 +123,7 @@ export function PhotoImage({
const onAspectRatioRef = useRef(onAspectRatio);
const mediaReadyRef = useRef(false);
const hasNotifiedDisplayReadyRef = useRef(false);
+ const lastReportedAspectRatioRef = useRef<number | undefined>(undefined);
onFullLoadRef.current = onFullLoad;
onAspectRatioRef.current = onAspectRatio;
@@ -135,6 +137,25 @@ export function PhotoImage({
onFullLoadRef.current();
}, []);
+ const reportAspectRatio = useCallback((width: number, height: number) => {
+ if (width <= 0 || height <= 0) {
+ return;
+ }
+
+ const nextAspectRatio = width / height;
+ const previousAspectRatio = lastReportedAspectRatioRef.current;
+ if (
+ typeof previousAspectRatio === "number" &&
+ Math.abs(previousAspectRatio - nextAspectRatio) <
+ ASPECT_RATIO_CHANGE_EPSILON
+ ) {
+ return;
+ }
+
+ lastReportedAspectRatioRef.current = nextAspectRatio;
+ onAspectRatioRef.current?.(width, height);
+ }, []);
+
useEffect(() => {
let cancelled = false;
setIsLoading(true);
@@ -142,6 +163,7 @@ export function PhotoImage({
setFullImageURL(undefined);
mediaReadyRef.current = false;
hasNotifiedDisplayReadyRef.current = false;
+ lastReportedAspectRatioRef.current = undefined;
const loadThumbnail = async () => {
try {
@@ -211,9 +233,9 @@ export function PhotoImage({
return;
}
if (image.naturalWidth > 0 && image.naturalHeight > 0) {
- onAspectRatioRef.current?.(image.naturalWidth, image.naturalHeight);
+ reportAspectRatio(image.naturalWidth, image.naturalHeight);
}
- }, [fullImageURL, thumbnailOnly]);
+ }, [fullImageURL, reportAspectRatio, thumbnailOnly]);
const displayURL = fullImageURL ?? thumbnailURL;
const mediaStyle = buildMediaStyle({
@@ -243,16 +265,14 @@ export function PhotoImage({
alt=""
draggable={false}
onLoad={(event) => {
+ reportAspectRatio(
+ event.currentTarget.naturalWidth,
+ event.currentTarget.naturalHeight,
+ );
const isFullImageLoad =
!thumbnailOnly &&
!!fullImageURL &&
event.currentTarget.src === fullImageURL;
- if (isFullImageLoad) {
- onAspectRatioRef.current?.(
- event.currentTarget.naturalWidth,
- event.currentTarget.naturalHeight,
- );
- }
setIsLoading(false);
if (thumbnailOnly || isFullImageLoad) {
signalReady();