Skip to content

armgit / ente

Public Imported from GitHub
0

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

refractor(memories):update-logics-for-fetching

Aswin Asok266b7f445368
diff --git a/web/apps/memories/src/components/LaneMemoryViewer.tsx b/web/apps/memories/src/components/LaneMemoryViewer.tsx
index 52bf24394f..827635448f 100644
--- a/web/apps/memories/src/components/LaneMemoryViewer.tsx
+++ b/web/apps/memories/src/components/LaneMemoryViewer.tsx
@@ -33,8 +33,8 @@ import {
} from "../utils/lane";
import {
LaneCaptionText,
- LanePlaybackGlyph,
LaneProgressSlider,
+ PlaybackGlyph,
} from "./PublicMemoryControls";
import { PhotoImage, VideoPlayer } from "./PublicMemoryMedia";
import {
@@ -51,7 +51,8 @@ import {
ViewerRoot,
} from "./PublicMemoryViewerShared";
-const LANE_FRAME_INTERVAL_MS = 1500;
+const LANE_PLAYBACK_SPEED_MULTIPLIER = 1.25;
+const LANE_FRAME_INTERVAL_MS = 1200;
const LANE_CARD_TRANSITION_DURATION_MS = 440;
const LANE_COMPACT_LAYOUT_BREAKPOINT_PX = 900;
const LANE_MOBILE_MEDIA_RESERVED_VERTICAL_SPACE_PX = 340;
@@ -354,6 +355,16 @@ export function LaneMemoryViewer({
videoDurationKnown,
]);
+ useEffect(() => {
+ const activeVideo = activeVideoElementRef.current;
+ if (!activeVideo || !isVideo) {
+ return;
+ }
+
+ activeVideo.defaultPlaybackRate = LANE_PLAYBACK_SPEED_MULTIPLIER;
+ activeVideo.playbackRate = LANE_PLAYBACK_SPEED_MULTIPLIER;
+ }, [displayIndex, isVideo]);
+
const handleFullLoad = useCallback(() => {
setFileLoaded(true);
}, []);
@@ -434,6 +445,8 @@ export function LaneMemoryViewer({
return;
}
+ activeVideo.defaultPlaybackRate = LANE_PLAYBACK_SPEED_MULTIPLIER;
+ activeVideo.playbackRate = LANE_PLAYBACK_SPEED_MULTIPLIER;
activeVideo.muted = false;
void activeVideo.play().catch((error: unknown) => {
log.warn(
@@ -833,20 +846,20 @@ export function LaneMemoryViewer({
);
})}
{showPlaybackOverlay && (
- <LaneCornerPlaybackOverlay>
- <LaneCornerPlaybackControl
+ <LaneCenteredPlaybackOverlay>
+ <LaneCenteredPlaybackControl
type="button"
onClick={handlePlaybackToggle}
aria-label={playbackOverlayLabel}
data-memory-control="true"
>
- <LaneCornerPlaybackGlyph>
- <LanePlaybackGlyph
+ <LaneCenteredPlaybackGlyph>
+ <PlaybackGlyph
paused={paused}
/>
- </LaneCornerPlaybackGlyph>
- </LaneCornerPlaybackControl>
- </LaneCornerPlaybackOverlay>
+ </LaneCenteredPlaybackGlyph>
+ </LaneCenteredPlaybackControl>
+ </LaneCenteredPlaybackOverlay>
)}
</LaneCardStack>
</PhotoContainer>
@@ -1285,24 +1298,18 @@ const LaneCaption = styled("div")({
},
});
-const LaneCornerPlaybackOverlay = styled("div")({
+const LaneCenteredPlaybackOverlay = styled("div")({
position: "absolute",
inset: 0,
zIndex: 4,
display: "flex",
- alignItems: "flex-end",
- justifyContent: "flex-end",
- padding: "20px",
- pointerEvents: "none",
- "@media (max-width: 900px)": { padding: "18px" },
- [`@media (max-width: ${MOBILE_LAYOUT_BREAKPOINT_PX}px)`]: {
- padding: "14px",
- },
+ alignItems: "center",
+ justifyContent: "center",
});
-const LaneCornerPlaybackControl = styled("button")({
- width: "58px",
- height: "58px",
+const LaneCenteredPlaybackControl = styled("button")({
+ width: "88px",
+ height: "88px",
borderRadius: "999px",
border: 0,
cursor: "pointer",
@@ -1317,26 +1324,21 @@ const LaneCornerPlaybackControl = styled("button")({
WebkitBackdropFilter: "blur(10px)",
transition:
"transform 150ms ease, background-color 150ms ease, box-shadow 150ms ease",
- pointerEvents: "auto",
"&:hover": {
backgroundColor: "rgba(18, 18, 18, 0.72)",
transform: "scale(1.03)",
},
"&:active": { transform: "scale(0.98)" },
- "@media (max-width: 900px)": { width: "54px", height: "54px" },
- [`@media (max-width: ${MOBILE_LAYOUT_BREAKPOINT_PX}px)`]: {
- width: "48px",
- height: "48px",
- },
+ "@media (max-width: 900px)": { width: "80px", height: "80px" },
});
-const LaneCornerPlaybackGlyph = styled("span")({
+const LaneCenteredPlaybackGlyph = styled("span")({
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
- transform: "scale(1.55)",
+ transform: "scale(1.65)",
transformOrigin: "center",
[`@media (max-width: ${MOBILE_LAYOUT_BREAKPOINT_PX}px)`]: {
- transform: "scale(1.35)",
+ transform: "scale(1.5)",
},
});
diff --git a/web/apps/memories/src/components/MemoryViewer.tsx b/web/apps/memories/src/components/MemoryViewer.tsx
index 1d5b84495d..561d745519 100644
--- a/web/apps/memories/src/components/MemoryViewer.tsx
+++ b/web/apps/memories/src/components/MemoryViewer.tsx
@@ -68,7 +68,7 @@ interface SharedMemoryHeaderProps {
duration: number;
onComplete: () => void;
isVideo: boolean;
- titleBelowProgress?: boolean;
+ showTitle?: boolean;
}
/**
@@ -83,7 +83,7 @@ function SharedMemoryHeader({
duration,
onComplete,
isVideo,
- titleBelowProgress = false,
+ showTitle = true,
}: SharedMemoryHeaderProps) {
const progressIndicator = (
<ProgressIndicator
@@ -99,17 +99,54 @@ function SharedMemoryHeader({
return (
<HeaderSection>
- {titleBelowProgress ? (
- <>
- {progressIndicator}
- <MemoryTitle variant="h6">{title}</MemoryTitle>
- </>
- ) : (
- <>
- <MemoryTitle variant="h6">{title}</MemoryTitle>
- {progressIndicator}
- </>
- )}
+ {showTitle && <MemoryTitle variant="h6">{title}</MemoryTitle>}
+ {progressIndicator}
+ </HeaderSection>
+ );
+}
+
+interface MobileProgressHeaderProps {
+ title: string;
+ date?: string;
+ total: number;
+ current: number;
+ paused: boolean;
+ duration: number;
+ onComplete: () => void;
+ isVideo: boolean;
+}
+
+function MobileProgressHeader({
+ title,
+ date,
+ total,
+ current,
+ paused,
+ duration,
+ onComplete,
+ isVideo,
+}: MobileProgressHeaderProps) {
+ return (
+ <HeaderSection>
+ <MobileFooterMeta>
+ <MobileFooterPrimaryLine variant="h6">
+ {title}
+ </MobileFooterPrimaryLine>
+ {!!date && (
+ <MobileFooterSecondaryLine variant="body">
+ {date}
+ </MobileFooterSecondaryLine>
+ )}
+ </MobileFooterMeta>
+ <ProgressIndicator
+ total={total}
+ current={current}
+ paused={paused}
+ duration={duration}
+ onComplete={onComplete}
+ isVideo={isVideo}
+ minimal
+ />
</HeaderSection>
);
}
@@ -129,6 +166,12 @@ export function MemoryViewer({
const currentFile = files[currentIndex]!;
const [paused, setPaused] = useState(false);
const [fileLoaded, setFileLoaded] = useState(false);
+ const [thumbnailResolvedSession, setThumbnailResolvedSession] = useState<
+ number | null
+ >(null);
+ const [fullLoadUnlockedSession, setFullLoadUnlockedSession] = useState<
+ number | null
+ >(null);
const [videoDurationKnown, setVideoDurationKnown] = useState(false);
const [progressDuration, setProgressDuration] = useState(
IMAGE_AUTO_PROGRESS_DURATION_MS,
@@ -143,14 +186,29 @@ export function MemoryViewer({
const [outgoingIndex, setOutgoingIndex] = useState<number | null>(null);
const previousFileRef = useRef(currentFile);
const previousIndexRef = useRef(currentIndex);
+ const lastPrefetchIndexRef = useRef(currentIndex);
+ const prefetchSessionRef = useRef(0);
const outgoingClearTimeoutRef = useRef<number | null>(null);
const activeVideoElementRef = useRef<HTMLVideoElement | null>(null);
+ // Each navigation gets a fresh prefetch session so revisiting an index
+ // cannot reuse an old unlock signal.
+ if (lastPrefetchIndexRef.current !== currentIndex) {
+ lastPrefetchIndexRef.current = currentIndex;
+ prefetchSessionRef.current += 1;
+ }
+
+ const activePrefetchSession = prefetchSessionRef.current;
+
const isVideo = currentFile.metadata.fileType === FileType.video;
const isMobileLayout = viewport.width <= MOBILE_LAYOUT_BREAKPOINT_PX;
const isTabletShareLayout =
viewport.width > MOBILE_LAYOUT_BREAKPOINT_PX &&
viewport.width < SHARE_COMPACT_LAYOUT_BREAKPOINT_PX;
+ const isCurrentThumbnailResolved =
+ thumbnailResolvedSession === activePrefetchSession;
+ const isCurrentFullLoadUnlocked =
+ fullLoadUnlockedSession === activePrefetchSession;
useEffect(() => {
setPaused(false);
@@ -198,6 +256,13 @@ export function MemoryViewer({
setFileLoaded(true);
}, []);
+ const handleCurrentThumbnailResolved = useCallback(() => {
+ setThumbnailResolvedSession(activePrefetchSession);
+ if (!isVideo) {
+ setFileLoaded(true);
+ }
+ }, [activePrefetchSession, isVideo]);
+
const handleVideoDuration = useCallback((durationSeconds: number) => {
setProgressDuration(durationSeconds * 1000);
setVideoDurationKnown(true);
@@ -251,34 +316,73 @@ export function MemoryViewer({
}, [currentIndex, files.length, onNext]);
useEffect(() => {
- if (!fileLoaded || currentIndex >= files.length - 1) {
+ if (!isCurrentThumbnailResolved) {
return;
}
- const nextFile = files[currentIndex + 1]!;
+ const prefetchIndex = currentIndex;
+ const thumbnailWindow = files.slice(
+ prefetchIndex + 1,
+ prefetchIndex + 3,
+ );
+ const videoWindow = thumbnailWindow.filter(
+ (file) => file.metadata.fileType === FileType.video,
+ );
+ const prefetchSession = activePrefetchSession;
- void (async () => {
- const prefetchTasks: Promise<unknown>[] = [
- downloadManager.renderableThumbnailURL(nextFile),
- ];
+ if (thumbnailWindow.length === 0) {
+ setFullLoadUnlockedSession(prefetchSession);
+ return;
+ }
- if (nextFile.metadata.fileType === FileType.video) {
- prefetchTasks.unshift(
- downloadManager.hlsPlaylistDataForPublicMemory(nextFile),
- );
- }
+ const loadState = { cancelled: false };
+
+ if (videoWindow.length > 0) {
+ void Promise.allSettled(
+ videoWindow.map((file) =>
+ downloadManager.hlsPlaylistDataForPublicMemory(file),
+ ),
+ ).then((results) => {
+ results.forEach((result) => {
+ if (result.status === "rejected") {
+ log.warn(
+ "Failed to prefetch staged memory video playlist",
+ result.reason,
+ );
+ }
+ });
+ });
+ }
- const results = await Promise.allSettled(prefetchTasks);
+ void (async () => {
+ const results = await Promise.allSettled(
+ thumbnailWindow.map((file) =>
+ downloadManager.renderableThumbnailURL(file),
+ ),
+ );
results.forEach((result) => {
if (result.status === "rejected") {
log.warn(
- "Failed to prefetch next memory media",
+ "Failed to prefetch staged memory thumbnail",
result.reason,
);
}
});
+
+ if (!loadState.cancelled) {
+ setFullLoadUnlockedSession(prefetchSession);
+ }
})();
- }, [currentIndex, fileLoaded, files]);
+
+ return () => {
+ loadState.cancelled = true;
+ };
+ }, [
+ activePrefetchSession,
+ currentIndex,
+ files,
+ isCurrentThumbnailResolved,
+ ]);
useEffect(() => {
const updateViewport = () => setViewport(readViewport());
@@ -310,6 +414,15 @@ export function MemoryViewer({
return memoryName || currentFileDate || "Memory";
}, [currentFileDate, memoryName]);
+ const mobileFooterTitle = useMemo(
+ () => memoryName || currentFileDate || "Memory",
+ [currentFileDate, memoryName],
+ );
+ const mobileFooterDate = useMemo(
+ () => (memoryName && currentFileDate ? currentFileDate : undefined),
+ [currentFileDate, memoryName],
+ );
+
const handleRestartPlayback = useCallback(() => {
if (currentIndex > 0) {
onSeek(0);
@@ -486,7 +599,19 @@ export function MemoryViewer({
duration={progressDuration}
onComplete={handleAdvanceOrFinish}
isVideo={isVideo}
- titleBelowProgress={isMobileLayout}
+ />
+ );
+
+ const mobileProgressHeader = (
+ <MobileProgressHeader
+ title={mobileFooterTitle}
+ date={mobileFooterDate}
+ total={files.length}
+ current={currentIndex}
+ paused={isProgressPaused}
+ duration={progressDuration}
+ onComplete={handleAdvanceOrFinish}
+ isVideo={isVideo}
/>
);
@@ -497,11 +622,13 @@ export function MemoryViewer({
<VideoPlayer
file={currentFile}
onReady={handleFullLoad}
+ onThumbnailResolved={handleCurrentThumbnailResolved}
onDuration={handleVideoDuration}
onEnded={handleAdvanceOrFinish}
onPlaybackBlocked={handleVideoPlaybackBlocked}
mediaRef={activeVideoElementRef}
paused={paused}
+ enableFullLoad={isCurrentFullLoadUnlocked}
fillFrame
objectFit="contain"
mediaAspectRatio={resolvedMediaAspectRatio}
@@ -511,6 +638,8 @@ export function MemoryViewer({
<PhotoImage
file={currentFile}
onFullLoad={handleFullLoad}
+ onThumbnailResolved={handleCurrentThumbnailResolved}
+ enableFullLoad={isCurrentFullLoadUnlocked}
fillFrame
objectFit="contain"
mediaAspectRatio={resolvedMediaAspectRatio}
@@ -572,6 +701,7 @@ export function MemoryViewer({
alt="Ente Photos"
/>
</BrandLink>
+ <MobileHeaderSpacer aria-hidden="true" />
<MobileJoinNowButton
variant="contained"
color="accent"
@@ -649,7 +779,7 @@ export function MemoryViewer({
</MediaFrame>
</PhotoContainer>
- {isMobileLayout && sharedHeader}
+ {isMobileLayout && mobileProgressHeader}
</ContentContainer>
</ViewerRoot>
);
@@ -761,14 +891,51 @@ const MemoryTitle = styled(Typography)({
const MobileTopActions = styled("div")({
width: "100%",
- display: "flex",
+ display: "grid",
+ gridTemplateColumns: "auto minmax(0, 1fr) auto",
alignItems: "center",
- justifyContent: "space-between",
- gap: "16px",
+ gap: "12px",
padding: "0 24px",
boxSizing: "border-box",
});
+const MobileHeaderSpacer = styled("div")({ minWidth: 0 });
+
+const MobileFooterMeta = styled("div")({
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ gap: "5px",
+ width: "100%",
+ transform: "translateY(-8px)",
+});
+
+const MobileFooterPrimaryLine = styled(Typography)({
+ color: "rgba(255, 255, 255, 0.9)",
+ fontWeight: 600,
+ fontSize: "19px",
+ lineHeight: 1.08,
+ letterSpacing: "-0.01em",
+ textAlign: "center",
+ whiteSpace: "nowrap",
+ maxWidth: "100%",
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+});
+
+const MobileFooterSecondaryLine = styled(Typography)({
+ color: "rgba(255, 255, 255, 0.72)",
+ fontWeight: 500,
+ fontSize: "14px",
+ lineHeight: 1.18,
+ letterSpacing: "0.01em",
+ textAlign: "center",
+ whiteSpace: "nowrap",
+ maxWidth: "100%",
+ overflow: "hidden",
+ textOverflow: "ellipsis",
+});
+
const TopLeftBrandLink = styled(BrandLink)({
position: "absolute",
left: 0,
diff --git a/web/apps/memories/src/components/PublicMemoryControls.tsx b/web/apps/memories/src/components/PublicMemoryControls.tsx
index a946bfcbf2..0c490fbd1d 100644
--- a/web/apps/memories/src/components/PublicMemoryControls.tsx
+++ b/web/apps/memories/src/components/PublicMemoryControls.tsx
@@ -86,29 +86,29 @@ export const PlaybackGlyph: React.FC<{ paused: boolean }> = ({ paused }) => {
sx={{
width: 0,
height: 0,
- borderTop: "10px solid transparent",
- borderBottom: "10px solid transparent",
- borderLeft: "14px solid white",
- ml: "3px",
+ borderTop: "12px solid transparent",
+ borderBottom: "12px solid transparent",
+ borderLeft: "17px solid white",
+ ml: "4px",
}}
/>
);
}
return (
- <Box sx={{ display: "flex", gap: "6px" }}>
+ <Box sx={{ display: "flex", gap: "7px" }}>
<Box
sx={{
- width: "6px",
- height: "18px",
+ width: "7px",
+ height: "20px",
borderRadius: "2px",
backgroundColor: "white",
}}
/>
<Box
sx={{
- width: "6px",
- height: "18px",
+ width: "7px",
+ height: "20px",
borderRadius: "2px",
backgroundColor: "white",
}}
@@ -130,9 +130,9 @@ export const LanePlaybackGlyph: React.FC<{ paused: boolean }> = ({
sx={{
width: 0,
height: 0,
- borderTop: "5px solid transparent",
- borderBottom: "5px solid transparent",
- borderLeft: "8px solid white",
+ borderTop: "6px solid transparent",
+ borderBottom: "6px solid transparent",
+ borderLeft: "10px solid white",
ml: "2px",
}}
/>
@@ -140,19 +140,19 @@ export const LanePlaybackGlyph: React.FC<{ paused: boolean }> = ({
}
return (
- <Box sx={{ display: "flex", gap: "3px" }}>
+ <Box sx={{ display: "flex", gap: "4px" }}>
<Box
sx={{
- width: "3px",
- height: "10px",
+ width: "4px",
+ height: "12px",
borderRadius: "1px",
backgroundColor: "white",
}}
/>
<Box
sx={{
- width: "3px",
- height: "10px",
+ width: "4px",
+ height: "12px",
borderRadius: "1px",
backgroundColor: "white",
}}
diff --git a/web/apps/memories/src/components/PublicMemoryMedia.tsx b/web/apps/memories/src/components/PublicMemoryMedia.tsx
index dd86349e02..8758c961df 100644
--- a/web/apps/memories/src/components/PublicMemoryMedia.tsx
+++ b/web/apps/memories/src/components/PublicMemoryMedia.tsx
@@ -86,6 +86,8 @@ function MediaLoadingOverlay() {
export interface PhotoImageProps {
file: EnteFile;
onFullLoad?: () => void;
+ onThumbnailResolved?: () => void;
+ enableFullLoad?: boolean;
fillFrame?: boolean;
objectFit?: "contain" | "cover";
thumbnailOnly?: boolean;
@@ -103,6 +105,8 @@ export interface PhotoImageProps {
export function PhotoImage({
file,
onFullLoad,
+ onThumbnailResolved,
+ enableFullLoad = true,
fillFrame,
objectFit = "contain",
thumbnailOnly = false,
@@ -121,13 +125,16 @@ export function PhotoImage({
const [isLoading, setIsLoading] = useState(true);
const imageRef = useRef<HTMLImageElement | null>(null);
const onFullLoadRef = useRef(onFullLoad);
+ const onThumbnailResolvedRef = useRef(onThumbnailResolved);
const onAspectRatioRef = useRef(onAspectRatio);
const mediaReadyRef = useRef(false);
+ const thumbnailResolvedRef = useRef(false);
const hasNotifiedDisplayReadyRef = useRef(false);
+ const hasNotifiedThumbnailResolvedRef = useRef(false);
const lastReportedAspectRatioRef = useRef<number | undefined>(undefined);
- const loadGenerationRef = useRef(0);
onFullLoadRef.current = onFullLoad;
+ onThumbnailResolvedRef.current = onThumbnailResolved;
onAspectRatioRef.current = onAspectRatio;
const signalReady = useCallback(() => {
@@ -139,6 +146,19 @@ export function PhotoImage({
onFullLoadRef.current();
}, []);
+ const signalThumbnailResolved = useCallback(() => {
+ thumbnailResolvedRef.current = true;
+ if (
+ hasNotifiedThumbnailResolvedRef.current ||
+ !onThumbnailResolvedRef.current
+ ) {
+ return;
+ }
+
+ hasNotifiedThumbnailResolvedRef.current = true;
+ onThumbnailResolvedRef.current();
+ }, []);
+
const reportAspectRatio = useCallback((width: number, height: number) => {
if (width <= 0 || height <= 0) {
return;
@@ -159,37 +179,81 @@ export function PhotoImage({
}, []);
useEffect(() => {
- const loadGeneration = loadGenerationRef.current + 1;
- loadGenerationRef.current = loadGeneration;
- const loadState = { cancelled: false };
+ let cancelled = false;
setIsLoading(true);
setThumbnailURL(undefined);
setFullImageURL(undefined);
mediaReadyRef.current = false;
+ thumbnailResolvedRef.current = false;
hasNotifiedDisplayReadyRef.current = false;
+ hasNotifiedThumbnailResolvedRef.current = false;
lastReportedAspectRatioRef.current = undefined;
const loadThumbnail = async () => {
try {
const nextThumbnailURL =
await downloadManager.renderableThumbnailURL(file);
- if (!loadState.cancelled && nextThumbnailURL) {
+ if (cancelled) {
+ return;
+ }
+
+ if (nextThumbnailURL) {
setThumbnailURL(nextThumbnailURL);
- } else if (!loadState.cancelled) {
+ } else if (thumbnailOnly) {
setIsLoading(false);
}
} catch (error) {
log.error("Failed to load thumbnail", error);
- if (!loadState.cancelled) {
+ if (!cancelled && thumbnailOnly) {
setIsLoading(false);
}
+ } finally {
+ if (!cancelled) {
+ signalThumbnailResolved();
+ }
}
};
+ void loadThumbnail();
+
+ return () => {
+ cancelled = true;
+ };
+ }, [file, signalThumbnailResolved, thumbnailOnly]);
+
+ useEffect(() => {
+ if (!onFullLoad) {
+ hasNotifiedDisplayReadyRef.current = false;
+ return;
+ }
+ if (mediaReadyRef.current && !hasNotifiedDisplayReadyRef.current) {
+ hasNotifiedDisplayReadyRef.current = true;
+ onFullLoad();
+ }
+ }, [onFullLoad]);
+
+ useEffect(() => {
+ if (!onThumbnailResolved) {
+ hasNotifiedThumbnailResolvedRef.current = false;
+ return;
+ }
+ if (
+ thumbnailResolvedRef.current &&
+ !hasNotifiedThumbnailResolvedRef.current
+ ) {
+ hasNotifiedThumbnailResolvedRef.current = true;
+ onThumbnailResolved();
+ }
+ }, [onThumbnailResolved]);
+
+ useEffect(() => {
+ if (thumbnailOnly || !enableFullLoad) {
+ return;
+ }
+
+ const loadState = { cancelled: false };
+
const loadFullImage = async () => {
- if (thumbnailOnly) {
- return;
- }
try {
const sourceURLs =
await downloadManager.renderableSourceURLs(file);
@@ -205,7 +269,7 @@ export function PhotoImage({
sourceURLs.type === "livePhoto"
? await sourceURLs.imageURL()
: sourceURLs.imageURL;
- if (loadGeneration !== loadGenerationRef.current) {
+ if (loadState.cancelled) {
return;
}
@@ -218,27 +282,12 @@ export function PhotoImage({
}
};
- void loadThumbnail();
void loadFullImage();
return () => {
loadState.cancelled = true;
- if (loadGenerationRef.current === loadGeneration) {
- loadGenerationRef.current += 1;
- }
};
- }, [file, signalReady, thumbnailOnly]);
-
- useEffect(() => {
- if (!onFullLoad) {
- hasNotifiedDisplayReadyRef.current = false;
- return;
- }
- if (mediaReadyRef.current && !hasNotifiedDisplayReadyRef.current) {
- hasNotifiedDisplayReadyRef.current = true;
- onFullLoad();
- }
- }, [onFullLoad]);
+ }, [enableFullLoad, file, signalReady, thumbnailOnly]);
useEffect(() => {
if (thumbnailOnly || !fullImageURL) {
@@ -326,6 +375,8 @@ export function PhotoImage({
export interface VideoPlayerProps {
file: EnteFile;
onReady?: () => void;
+ onThumbnailResolved?: () => void;
+ enableFullLoad?: boolean;
onDuration?: (durationSeconds: number) => void;
onEnded?: () => void;
onPlaybackBlocked?: () => void;
@@ -348,6 +399,8 @@ export interface VideoPlayerProps {
export function VideoPlayer({
file,
onReady,
+ onThumbnailResolved,
+ enableFullLoad = true,
onDuration,
onEnded,
onPlaybackBlocked,
@@ -376,14 +429,18 @@ export function VideoPlayer({
);
const videoRef = useRef<HTMLVideoElement>(null);
const onReadyRef = useRef(onReady);
+ const onThumbnailResolvedRef = useRef(onThumbnailResolved);
const onDurationRef = useRef(onDuration);
const onEndedRef = useRef(onEnded);
const onPlaybackBlockedRef = useRef(onPlaybackBlocked);
const pausedRef = useRef(paused);
const playbackRequestIDRef = useRef(0);
const hasSignalledReadyRef = useRef(false);
+ const thumbnailResolvedRef = useRef(false);
+ const hasNotifiedThumbnailResolvedRef = useRef(false);
onReadyRef.current = onReady;
+ onThumbnailResolvedRef.current = onThumbnailResolved;
onDurationRef.current = onDuration;
onEndedRef.current = onEnded;
onPlaybackBlockedRef.current = onPlaybackBlocked;
@@ -418,6 +475,19 @@ export function VideoPlayer({
});
}, []);
+ const signalThumbnailResolved = useCallback(() => {
+ thumbnailResolvedRef.current = true;
+ if (
+ hasNotifiedThumbnailResolvedRef.current ||
+ !onThumbnailResolvedRef.current
+ ) {
+ return;
+ }
+
+ hasNotifiedThumbnailResolvedRef.current = true;
+ onThumbnailResolvedRef.current();
+ }, []);
+
const setVideoElementRef = useCallback(
(node: HTMLVideoElement | null) => {
videoRef.current = node;
@@ -438,15 +508,59 @@ export function VideoPlayer({
setHlsData(undefined);
setThumbnailURL(undefined);
hasSignalledReadyRef.current = false;
+ thumbnailResolvedRef.current = false;
+ hasNotifiedThumbnailResolvedRef.current = false;
- const load = async () => {
+ const loadThumbnail = async () => {
try {
const nextThumbnailURL =
await downloadManager.renderableThumbnailURL(file);
- if (!cancelled && nextThumbnailURL) {
+ if (cancelled) {
+ return;
+ }
+
+ if (nextThumbnailURL) {
setThumbnailURL(nextThumbnailURL);
}
+ } catch (error) {
+ log.error("Failed to load video thumbnail", error);
+ } finally {
+ if (!cancelled) {
+ signalThumbnailResolved();
+ }
+ }
+ };
+
+ void loadThumbnail();
+
+ return () => {
+ cancelled = true;
+ };
+ }, [file, invalidatePlaybackRequest, signalThumbnailResolved]);
+
+ useEffect(() => {
+ if (!onThumbnailResolved) {
+ hasNotifiedThumbnailResolvedRef.current = false;
+ return;
+ }
+ if (
+ thumbnailResolvedRef.current &&
+ !hasNotifiedThumbnailResolvedRef.current
+ ) {
+ hasNotifiedThumbnailResolvedRef.current = true;
+ onThumbnailResolved();
+ }
+ }, [onThumbnailResolved]);
+
+ useEffect(() => {
+ if (!enableFullLoad) {
+ return;
+ }
+
+ let cancelled = false;
+ const load = async () => {
+ try {
const nextHlsData =
await downloadManager.hlsPlaylistDataForPublicMemory(file);
if (
@@ -479,7 +593,7 @@ export function VideoPlayer({
return () => {
cancelled = true;
};
- }, [file, invalidatePlaybackRequest]);
+ }, [enableFullLoad, file]);
useEffect(() => {
const video = videoRef.current;