Skip to content

armgit / ente

Public Imported from GitHub
0

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

Speed up public feed thumbnails

Anand Baburajanc333ec4ba5e3
diff --git a/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
index 2c0a2f4d29..56d30feffe 100644
--- a/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/PublicFeedSidebar.tsx
@@ -26,7 +26,13 @@ import log from "ente-base/log";
import type { EnteFile } from "ente-media/file";
import { FileType } from "ente-media/file-type";
import { t } from "i18next";
-import React, { useCallback, useEffect, useMemo, useState } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
// =============================================================================
// Icons
@@ -636,6 +642,12 @@ export const PublicFeedSidebar: React.FC<PublicFeedSidebarProps> = ({
new Map(),
);
const [isLoading, setIsLoading] = useState(false);
+ const thumbnailCacheRef = useRef(thumbnailCache);
+ const thumbnailLoadsInFlightRef = useRef<Set<number>>(new Set());
+
+ useEffect(() => {
+ thumbnailCacheRef.current = thumbnailCache;
+ }, [thumbnailCache]);
// Build file type cache from files
useEffect(() => {
@@ -646,41 +658,66 @@ export const PublicFeedSidebar: React.FC<PublicFeedSidebarProps> = ({
setFileTypeCache(cache);
}, [files]);
- // Load thumbnails for files that have reactions/comments
+ // Load thumbnails for active feed items in parallel and show each one as
+ // soon as it is ready instead of waiting for the entire batch to finish.
useEffect(() => {
- const loadThumbnails = async () => {
- // Get unique file IDs from comments and reactions
- const fileIDsWithActivity = new Set<number>();
- for (const c of comments) {
- if (c.fileID) fileIDsWithActivity.add(c.fileID);
- }
- for (const r of reactions) {
- if (r.fileID) fileIDsWithActivity.add(r.fileID);
- }
+ if (!open || (comments.length === 0 && reactions.length === 0)) return;
- // Find matching files
- const filesToLoad = files.filter((f) =>
- fileIDsWithActivity.has(f.id),
- );
+ let didCancel = false;
+
+ const fileIDsWithActivity = new Set<number>();
+
+ for (const c of comments) {
+ if (!c.fileID) continue;
+ fileIDsWithActivity.add(c.fileID);
+ }
+
+ for (const r of reactions) {
+ fileIDsWithActivity.add(r.fileID);
+ }
+
+ const filesToLoad = files.filter(
+ (file) =>
+ fileIDsWithActivity.has(file.id) &&
+ !thumbnailCacheRef.current.has(file.id) &&
+ !thumbnailLoadsInFlightRef.current.has(file.id),
+ );
+
+ if (filesToLoad.length === 0) return;
+
+ for (const file of filesToLoad) {
+ thumbnailLoadsInFlightRef.current.add(file.id);
+ }
- const newCache = new Map<number, string>();
- for (const file of filesToLoad) {
+ void Promise.allSettled(
+ filesToLoad.map(async (file) => {
try {
const url =
await downloadManager.renderableThumbnailURL(file);
- if (url) {
- newCache.set(file.id, url);
- }
- } catch {
- // Ignore thumbnail loading errors
+ if (didCancel || !url) return;
+
+ setThumbnailCache((prev) => {
+ if (prev.get(file.id) === url) return prev;
+
+ const next = new Map(prev);
+ next.set(file.id, url);
+ thumbnailCacheRef.current = next;
+ return next;
+ });
+ } catch (e) {
+ log.warn(
+ `Failed to fetch feed thumbnail for file ${file.id}`,
+ e,
+ );
+ } finally {
+ thumbnailLoadsInFlightRef.current.delete(file.id);
}
- }
- setThumbnailCache(newCache);
- };
+ }),
+ );
- if (open && (comments.length > 0 || reactions.length > 0)) {
- void loadThumbnails();
- }
+ return () => {
+ didCancel = true;
+ };
}, [open, comments, reactions, files]);
// Polling interval for refreshing feed data (5 seconds)