Skip to content

armgit / ente

Public Imported from GitHub
0

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

Fix public album social loading states

Anand Baburajanc278ff209fb2
diff --git a/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
index 9c90f07c96..9fb4397829 100644
--- a/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/CommentsSidebar.tsx
@@ -361,6 +361,11 @@ export interface CommentsSidebarProps extends ModalVisibilityProps {
* The file whose comments are being displayed.
*/
file?: EnteFile;
+ /**
+ * True while the parent is fetching the initial social payload for the
+ * current file.
+ */
+ isSocialDataLoading?: boolean;
/**
* The currently active collection ID (when viewing from within a collection).
*/
@@ -447,6 +452,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
open,
onClose,
file,
+ isSocialDataLoading = false,
activeCollectionID,
currentUserID,
prefetchedComments,
@@ -486,6 +492,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
const commentsContainerRef = useRef<HTMLDivElement>(null);
// Ref to preserve isLiked state during context menu close animation
const contextMenuIsLikedRef = useRef(false);
+ const showLoading = loading || isSocialDataLoading;
// Comments grouped by collection: collectionID -> comments
const [commentsByCollection, setCommentsByCollection] = useState<
@@ -537,7 +544,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
!highlightCommentID ||
hasScrolledToHighlightRef.current ||
!commentsContainerRef.current ||
- loading ||
+ showLoading ||
comments.length === 0
) {
return;
@@ -616,7 +623,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
if (cleanupTimeout) clearTimeout(cleanupTimeout);
if (blinkInterval) clearInterval(blinkInterval);
};
- }, [open, highlightCommentID, loading, comments]);
+ }, [open, highlightCommentID, showLoading, comments]);
// Reset state when the file changes to avoid showing stale data
useEffect(() => {
@@ -1332,7 +1339,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
...theme.applyStyles("dark", { color: "#fff" }),
})}
>
- {loading
+ {showLoading
? "Loading..."
: `${sortedComments.length} ${t("comments")}`}
</Typography>
@@ -1441,7 +1448,7 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
...theme.applyStyles("dark", { color: "#fff" }),
})}
>
- {loading
+ {showLoading
? "Loading..."
: `${sortedComments.length} ${t("comments")}`}
</Typography>
@@ -1452,9 +1459,9 @@ export const CommentsSidebar: React.FC<CommentsSidebarProps> = ({
</Header>
<CommentsContainer ref={commentsContainerRef}>
- {loading ? (
+ {showLoading ? (
<LoadingContainer>
- <CircularProgress size={32} />
+ <CircularProgress size={24} />
</LoadingContainer>
) : sortedComments.length === 0 ? (
<EmptyMessage>{t("no_comments_yet")}</EmptyMessage>
diff --git a/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx b/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
index 5580e2aac0..cdd6151811 100644
--- a/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/FileViewer.tsx
@@ -209,6 +209,8 @@ export const FileViewer: React.FC<FileViewerProps> = ({
() =>
initialAnonUserNames ? new Map(initialAnonUserNames) : new Map(),
);
+ const [publicSocialDataStatusByFileID, setPublicSocialDataStatusByFileID] =
+ useState<Map<number, "loading" | "ready">>(new Map());
useEffect(() => {
if (!initialAnonUserNames?.size) return;
@@ -904,6 +906,16 @@ export const FileViewer: React.FC<FileViewerProps> = ({
}, [allReactions, files, open]);
const activeFileID = activeAnnotatedFile?.file.id;
+ const shouldFetchPublicSocialData =
+ !!open &&
+ enableComment &&
+ !!activeFileID &&
+ !!publicAlbumsCredentials &&
+ !!collectionKey;
+ const isInitialPublicSocialDataLoading =
+ !!activeFileID &&
+ shouldFetchPublicSocialData &&
+ publicSocialDataStatusByFileID.get(activeFileID) !== "ready";
// Fetch social data (comments + reactions) for public albums (when viewing as anonymous user).
useEffect(() => {
@@ -919,6 +931,14 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const file = files.find((f) => f.id === activeFileID);
if (!file) return;
+ setPublicSocialDataStatusByFileID((prev) => {
+ if (prev.get(activeFileID) === "ready") return prev;
+
+ const next = new Map(prev);
+ next.set(activeFileID, "loading");
+ return next;
+ });
+
void (async () => {
try {
// Fetch both comments and reactions in a single API call
@@ -1011,6 +1031,14 @@ export const FileViewer: React.FC<FileViewerProps> = ({
}
} catch (e) {
log.error("Failed to fetch public social data", e);
+ } finally {
+ setPublicSocialDataStatusByFileID((prev) => {
+ if (prev.get(activeFileID) === "ready") return prev;
+
+ const next = new Map(prev);
+ next.set(activeFileID, "ready");
+ return next;
+ });
}
})();
}, [
@@ -1249,6 +1277,7 @@ export const FileViewer: React.FC<FileViewerProps> = ({
onClose={handleCommentsClose}
file={activeAnnotatedFile.file}
activeCollectionID={activeCollectionID}
+ isSocialDataLoading={isInitialPublicSocialDataLoading}
prefetchedComments={fileComments.get(
activeAnnotatedFile.file.id,
)}
@@ -1272,6 +1301,7 @@ export const FileViewer: React.FC<FileViewerProps> = ({
onClose={handleLikesClose}
file={activeAnnotatedFile.file}
activeCollectionID={activeCollectionID}
+ isSocialDataLoading={isInitialPublicSocialDataLoading}
prefetchedReactions={allReactions.get(
activeAnnotatedFile.file.id,
)}
diff --git a/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx b/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
index 1632d74c9e..5f10c6247f 100644
--- a/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
+++ b/web/apps/albums/src/public-album/viewer/components/LikesSidebar.tsx
@@ -114,6 +114,11 @@ export interface LikesSidebarProps extends ModalVisibilityProps {
* The file whose likes are being displayed.
*/
file?: EnteFile;
+ /**
+ * True while the parent is fetching the initial social payload for the
+ * current file.
+ */
+ isSocialDataLoading?: boolean;
/**
* The currently active collection ID (when viewing from within a collection).
*/
@@ -143,6 +148,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
open,
onClose,
file,
+ isSocialDataLoading = false,
activeCollectionID,
currentUserID,
prefetchedReactions,
@@ -151,6 +157,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
}) => {
const [loading, setLoading] = useState(false);
const [collectionDropdownOpen, setCollectionDropdownOpen] = useState(false);
+ const showLoading = loading || isSocialDataLoading;
// Reactions grouped by collection: collectionID -> reactions
const [reactionsByCollection, setReactionsByCollection] = useState<
@@ -433,7 +440,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
...theme.applyStyles("dark", { color: "#fff" }),
})}
>
- {loading
+ {showLoading
? t("loading")
: t("like_count", { count: likers.length })}
</Typography>
@@ -541,7 +548,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
...theme.applyStyles("dark", { color: "#fff" }),
})}
>
- {loading
+ {showLoading
? t("loading")
: t("like_count", { count: likers.length })}
</Typography>
@@ -552,9 +559,9 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
</Header>
<LikersContainer>
- {loading ? (
+ {showLoading ? (
<LoadingContainer>
- <CircularProgress size={32} />
+ <CircularProgress size={24} />
</LoadingContainer>
) : likers.length === 0 ? (
<EmptyMessage>{t("no_likes_yet")}</EmptyMessage>