Skip to content

armgit / ente

Public Imported from GitHub
0

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

Share user ID to email mapping from FileViewer to LikesSidebar

Anand Baburajanb38943066543
diff --git a/web/packages/gallery/components/viewer/FileViewer.tsx b/web/packages/gallery/components/viewer/FileViewer.tsx
index 2258986b43..e3db176b69 100644
--- a/web/packages/gallery/components/viewer/FileViewer.tsx
+++ b/web/packages/gallery/components/viewer/FileViewer.tsx
@@ -413,6 +413,12 @@ export const FileViewer: React.FC<FileViewerProps> = ({
Map<number, Map<number, UnifiedReaction[]>>
>(new Map());
+ // Map of user ID to email for displaying reaction/comment authors.
+ // Built from collection owner and sharees when fetching social data.
+ const [userIDToEmail, setUserIDToEmail] = useState<Map<number, string>>(
+ new Map(),
+ );
+
// Ref for fileComments to use in callbacks
const fileCommentsRef = useRef(fileComments);
useEffect(() => {
@@ -443,7 +449,10 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const fileReactionsMap = allReactionsRef.current.get(fileId);
if (!fileReactionsMap) return [];
- const userReactions: { collectionId: number; reactionId: string }[] = [];
+ const userReactions: {
+ collectionId: number;
+ reactionId: string;
+ }[] = [];
for (const [collectionId, reactions] of fileReactionsMap) {
const userFileReaction = reactions.find(
(r) =>
@@ -510,26 +519,23 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const handleCommentsClose = useCallback(() => setOpenComments(false), []);
// Handle a new comment being added
- const handleCommentAdded = useCallback(
- (comment: Comment) => {
- const fileID = comment.fileID;
- if (!fileID) return;
-
- setFileComments((prev) => {
- const next = new Map(prev);
- const fileCommentsMap = new Map(prev.get(fileID) ?? new Map());
- const collectionComments =
- fileCommentsMap.get(comment.collectionID) ?? [];
- fileCommentsMap.set(comment.collectionID, [
- ...collectionComments,
- comment,
- ]);
- next.set(fileID, fileCommentsMap);
- return next;
- });
- },
- [],
- );
+ const handleCommentAdded = useCallback((comment: Comment) => {
+ const fileID = comment.fileID;
+ if (!fileID) return;
+
+ setFileComments((prev) => {
+ const next = new Map(prev);
+ const fileCommentsMap = new Map(prev.get(fileID) ?? new Map());
+ const collectionComments =
+ fileCommentsMap.get(comment.collectionID) ?? [];
+ fileCommentsMap.set(comment.collectionID, [
+ ...collectionComments,
+ comment,
+ ]);
+ next.set(fileID, fileCommentsMap);
+ return next;
+ });
+ }, []);
// Handle a comment being deleted
const handleCommentDeleted = useCallback(
@@ -542,7 +548,8 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const fileCommentsMap = prev.get(fileID);
if (fileCommentsMap) {
const updatedMap = new Map(fileCommentsMap);
- const collectionComments = updatedMap.get(collectionID) ?? [];
+ const collectionComments =
+ updatedMap.get(collectionID) ?? [];
updatedMap.set(
collectionID,
collectionComments.map((c) =>
@@ -590,7 +597,8 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const fileReactionsMap = prev.get(fileID);
if (fileReactionsMap) {
const updatedMap = new Map(fileReactionsMap);
- const collectionReactions = updatedMap.get(collectionID) ?? [];
+ const collectionReactions =
+ updatedMap.get(collectionID) ?? [];
updatedMap.set(
collectionID,
collectionReactions.filter((r) => r.id !== reactionID),
@@ -638,9 +646,12 @@ export const FileViewer: React.FC<FileViewerProps> = ({
if (isGalleryView) {
// Gallery view - only consider shared collections
- const allCollectionIDs = fileNormalCollectionIDsRef.current?.get(fileId) ?? [];
+ const allCollectionIDs =
+ fileNormalCollectionIDsRef.current?.get(fileId) ?? [];
const collectionIDs = allCollectionIDs.filter((id) =>
- collectionSummariesRef.current?.get(id)?.attributes.has("shared"),
+ collectionSummariesRef.current
+ ?.get(id)
+ ?.attributes.has("shared"),
);
if (reactions.length === 0) {
@@ -661,8 +672,11 @@ export const FileViewer: React.FC<FileViewerProps> = ({
log.info(`Added reaction: ${reactionId}`);
setAllReactions((prev) => {
const next = new Map(prev);
- const fileReactionsMap = new Map(prev.get(fileId) ?? new Map());
- const collectionReactions = fileReactionsMap.get(collectionId) ?? [];
+ const fileReactionsMap = new Map(
+ prev.get(fileId) ?? new Map(),
+ );
+ const collectionReactions =
+ fileReactionsMap.get(collectionId) ?? [];
fileReactionsMap.set(collectionId, [
...collectionReactions,
{
@@ -704,11 +718,15 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const fileReactionsMap = prev.get(fileId);
if (fileReactionsMap) {
const updatedMap = new Map(fileReactionsMap);
- for (const [collectionId, collectionReactions] of updatedMap) {
+ for (const [
+ collectionId,
+ collectionReactions,
+ ] of updatedMap) {
updatedMap.set(
collectionId,
collectionReactions.filter(
- (r) => !deletedReactionIds.has(r.id),
+ (r) =>
+ !deletedReactionIds.has(r.id),
),
);
}
@@ -740,11 +758,14 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const fileReactionsMap = prev.get(fileId);
if (fileReactionsMap) {
const updatedMap = new Map(fileReactionsMap);
- const collectionReactions = updatedMap.get(activeCollectionID) ?? [];
+ const collectionReactions =
+ updatedMap.get(activeCollectionID) ?? [];
updatedMap.set(
activeCollectionID,
collectionReactions.filter(
- (r) => r.id !== existingReaction.reactionId,
+ (r) =>
+ r.id !==
+ existingReaction.reactionId,
),
);
next.set(fileId, updatedMap);
@@ -770,8 +791,11 @@ export const FileViewer: React.FC<FileViewerProps> = ({
log.info(`Added reaction: ${reactionId}`);
setAllReactions((prev) => {
const next = new Map(prev);
- const fileReactionsMap = new Map(prev.get(fileId) ?? new Map());
- const collectionReactions = fileReactionsMap.get(activeCollectionID) ?? [];
+ const fileReactionsMap = new Map(
+ prev.get(fileId) ?? new Map(),
+ );
+ const collectionReactions =
+ fileReactionsMap.get(activeCollectionID) ?? [];
fileReactionsMap.set(activeCollectionID, [
...collectionReactions,
{
@@ -826,12 +850,17 @@ export const FileViewer: React.FC<FileViewerProps> = ({
const next = new Map(prev);
const fileReactionsMap = prev.get(fileId);
if (fileReactionsMap) {
- const updatedMap = new Map(fileReactionsMap);
- const collectionReactions = updatedMap.get(albumId) ?? [];
+ const updatedMap = new Map(
+ fileReactionsMap,
+ );
+ const collectionReactions =
+ updatedMap.get(albumId) ?? [];
updatedMap.set(
albumId,
collectionReactions.filter(
- (r) => r.id !== reactionToDelete.reactionId,
+ (r) =>
+ r.id !==
+ reactionToDelete.reactionId,
),
);
next.set(fileId, updatedMap);
@@ -857,8 +886,11 @@ export const FileViewer: React.FC<FileViewerProps> = ({
log.info(`Added reaction: ${reactionId}`);
setAllReactions((prev) => {
const next = new Map(prev);
- const fileReactionsMap = new Map(prev.get(fileId) ?? new Map());
- const collectionReactions = fileReactionsMap.get(albumId) ?? [];
+ const fileReactionsMap = new Map(
+ prev.get(fileId) ?? new Map(),
+ );
+ const collectionReactions =
+ fileReactionsMap.get(albumId) ?? [];
fileReactionsMap.set(albumId, [
...collectionReactions,
{
@@ -922,9 +954,12 @@ export const FileViewer: React.FC<FileViewerProps> = ({
}
setAllReactions((prev) => {
const next = new Map(prev);
- const fileReactionsMap = new Map(prev.get(fileId) ?? new Map());
+ const fileReactionsMap = new Map(
+ prev.get(fileId) ?? new Map(),
+ );
for (const { collectionId, reactionId } of newReactions) {
- const collectionReactions = fileReactionsMap.get(collectionId) ?? [];
+ const collectionReactions =
+ fileReactionsMap.get(collectionId) ?? [];
fileReactionsMap.set(collectionId, [
...collectionReactions,
{
@@ -947,7 +982,12 @@ export const FileViewer: React.FC<FileViewerProps> = ({
}
})();
setOpenLikeAlbumSelector(false);
- }, [fileNormalCollectionIDs, getUserFileReactions, user?.id, collectionSummaries]);
+ }, [
+ fileNormalCollectionIDs,
+ getUserFileReactions,
+ user?.id,
+ collectionSummaries,
+ ]);
const handlePublicLikeModalClose = useCallback(
() => setOpenPublicLikeModal(false),
@@ -1186,8 +1226,13 @@ export const FileViewer: React.FC<FileViewerProps> = ({
// Show for public albums (no logged-in user).
if (!haveUser) return true;
// In collection view: check if that specific collection is shared.
- if (activeCollectionID && activeCollectionID !== 0 && collectionSummaries) {
- const collectionSummary = collectionSummaries.get(activeCollectionID);
+ if (
+ activeCollectionID &&
+ activeCollectionID !== 0 &&
+ collectionSummaries
+ ) {
+ const collectionSummary =
+ collectionSummaries.get(activeCollectionID);
if (collectionSummary?.attributes.has("shared")) return true;
}
return false;
@@ -1234,7 +1279,9 @@ export const FileViewer: React.FC<FileViewerProps> = ({
// Get all collections the file belongs to, filtered to only shared ones
const collectionIDs = fileNormalCollectionIDs?.get(file.id) ?? [];
const allAlbumsForFile = collectionIDs
- .filter((id) => collectionSummaries?.get(id)?.attributes.has("shared"))
+ .filter((id) =>
+ collectionSummaries?.get(id)?.attributes.has("shared"),
+ )
.map((id) => ({
id,
name: collectionNameByID?.get(id) ?? `Album ${id}`,
@@ -1601,6 +1648,7 @@ export const FileViewer: React.FC<FileViewerProps> = ({
try {
const commentsMap = new Map<number, Comment[]>();
const reactionsMap = new Map<number, UnifiedReaction[]>();
+ const newUserIDToEmail = new Map<number, string>();
const collectionIDs = isGalleryView
? (fileNormalCollectionIDs?.get(activeFileID) ?? [])
@@ -1608,12 +1656,28 @@ export const FileViewer: React.FC<FileViewerProps> = ({
for (const collectionId of collectionIDs) {
try {
- const collection = await getCollectionByID(collectionId);
- const { comments, reactions } = await getUnifiedSocialDiff(
- collectionId,
- activeFileID,
- collection.key,
- );
+ const collection =
+ await getCollectionByID(collectionId);
+
+ // Build user ID to email map from collection owner and sharees
+ if (collection.owner.email) {
+ newUserIDToEmail.set(
+ collection.owner.id,
+ collection.owner.email,
+ );
+ }
+ for (const sharee of collection.sharees) {
+ if (sharee.email) {
+ newUserIDToEmail.set(sharee.id, sharee.email);
+ }
+ }
+
+ const { comments, reactions } =
+ await getUnifiedSocialDiff(
+ collectionId,
+ activeFileID,
+ collection.key,
+ );
commentsMap.set(collectionId, comments);
reactionsMap.set(collectionId, reactions);
@@ -1633,6 +1697,14 @@ export const FileViewer: React.FC<FileViewerProps> = ({
next.set(activeFileID, reactionsMap);
return next;
});
+
+ setUserIDToEmail((prev) => {
+ const next = new Map(prev);
+ for (const [id, email] of newUserIDToEmail) {
+ next.set(id, email);
+ }
+ return next;
+ });
} catch (e) {
log.error("Failed to fetch social data", e);
setFileComments((prev) => {
@@ -1773,8 +1845,13 @@ export const FileViewer: React.FC<FileViewerProps> = ({
fileNormalCollectionIDs={fileNormalCollectionIDs}
collectionSummaries={collectionSummaries}
currentUserID={user?.id}
- prefetchedComments={fileComments.get(activeAnnotatedFile.file.id)}
- prefetchedReactions={allReactions.get(activeAnnotatedFile.file.id)}
+ prefetchedComments={fileComments.get(
+ activeAnnotatedFile.file.id,
+ )}
+ prefetchedReactions={allReactions.get(
+ activeAnnotatedFile.file.id,
+ )}
+ prefetchedUserIDToEmail={userIDToEmail}
onCommentAdded={handleCommentAdded}
onCommentDeleted={handleCommentDeleted}
onCommentReactionAdded={handleCommentReactionAdded}
@@ -1787,7 +1864,10 @@ export const FileViewer: React.FC<FileViewerProps> = ({
activeCollectionID={activeCollectionID}
fileNormalCollectionIDs={fileNormalCollectionIDs}
collectionSummaries={collectionSummaries}
- prefetchedReactions={allReactions.get(activeAnnotatedFile.file.id)}
+ prefetchedReactions={allReactions.get(
+ activeAnnotatedFile.file.id,
+ )}
+ prefetchedUserIDToEmail={userIDToEmail}
/>
<LikeAlbumSelectorModal
open={openLikeAlbumSelector}
diff --git a/web/packages/gallery/components/viewer/LikesSidebar.tsx b/web/packages/gallery/components/viewer/LikesSidebar.tsx
index f3b6ef5a9d..24d9b40727 100644
--- a/web/packages/gallery/components/viewer/LikesSidebar.tsx
+++ b/web/packages/gallery/components/viewer/LikesSidebar.tsx
@@ -13,10 +13,15 @@ import { type ModalVisibilityProps } from "ente-base/components/utils/modal";
import log from "ente-base/log";
import { downloadManager } from "ente-gallery/services/download";
import type { EnteFile } from "ente-media/file";
-import { getCollectionByID } from "ente-new/photos/services/collection";
import type { CollectionSummaries } from "ente-new/photos/services/collection-summary";
import { type UnifiedReaction } from "ente-new/photos/services/social";
-import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
// =============================================================================
// Icons
@@ -101,6 +106,10 @@ export interface LikesSidebarProps extends ModalVisibilityProps {
* Pre-fetched reactions by collection ID (includes both file and comment reactions).
*/
prefetchedReactions?: Map<number, UnifiedReaction[]>;
+ /**
+ * Pre-fetched user ID to email mapping.
+ */
+ prefetchedUserIDToEmail?: Map<number, string>;
}
/**
@@ -114,6 +123,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
fileNormalCollectionIDs,
collectionSummaries,
prefetchedReactions,
+ prefetchedUserIDToEmail,
}) => {
const [loading, setLoading] = useState(false);
const [collectionDropdownOpen, setCollectionDropdownOpen] = useState(false);
@@ -123,11 +133,6 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
Map<number, UnifiedReaction[]>
>(new Map());
- // User ID to email mapping built from fetched collections
- const [userIDToEmail, setUserIDToEmail] = useState<Map<number, string>>(
- new Map(),
- );
-
// Selected collection for viewing likes (when in gallery view)
const [selectedCollectionID, setSelectedCollectionID] = useState<
number | undefined
@@ -201,46 +206,23 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
.map((r) => ({
id: r.id,
userID: r.userID,
- userName: userIDToEmail.get(r.userID) ?? `User ${r.userID}`,
+ userName:
+ prefetchedUserIDToEmail?.get(r.userID) ??
+ `User ${r.userID}`,
}));
- }, [selectedCollectionInfo, reactionsByCollection, userIDToEmail]);
+ }, [
+ selectedCollectionInfo,
+ reactionsByCollection,
+ prefetchedUserIDToEmail,
+ ]);
// Load reactions from prefetched data
- const loadReactions = useCallback(async () => {
+ const loadReactions = useCallback(() => {
if (!file || !open || !prefetchedReactions) return;
setLoading(true);
- const newUserIDToEmail = new Map<number, string>();
try {
- // Determine which collections to process
- const collectionIDsToFetch =
- hasCollectionContext && activeCollectionID
- ? [activeCollectionID]
- : fileCollectionIDs;
-
- // Fetch collection info for user emails (lightweight call)
- for (const collectionID of collectionIDsToFetch) {
- try {
- const collection = await getCollectionByID(collectionID);
-
- // Build user ID to email map from collection owner and sharees
- if (collection.owner.email) {
- newUserIDToEmail.set(
- collection.owner.id,
- collection.owner.email,
- );
- }
- for (const sharee of collection.sharees) {
- if (sharee.email) {
- newUserIDToEmail.set(sharee.id, sharee.email);
- }
- }
- } catch {
- // Skip collections that fail to fetch
- }
- }
-
// Use prefetched reactions (filter to only file reactions, not comment reactions)
const filteredReactions = new Map<number, UnifiedReaction[]>();
for (const [collectionID, reactions] of prefetchedReactions) {
@@ -251,20 +233,12 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
}
setReactionsByCollection(filteredReactions);
- setUserIDToEmail(newUserIDToEmail);
} catch (e) {
log.error("Failed to load reactions", e);
} finally {
setLoading(false);
}
- }, [
- file,
- open,
- prefetchedReactions,
- hasCollectionContext,
- activeCollectionID,
- fileCollectionIDs,
- ]);
+ }, [file, open, prefetchedReactions]);
// Track previous open state to detect when sidebar opens
const prevOpenRef = useRef(false);
@@ -273,7 +247,7 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
useEffect(() => {
if (open && !prevOpenRef.current) {
// Sidebar just opened - load reactions and set initial selection
- void loadReactions();
+ loadReactions();
// Set initial selection to album with most likes (gallery view only)
if (
@@ -301,7 +275,13 @@ export const LikesSidebar: React.FC<LikesSidebarProps> = ({
}
}
prevOpenRef.current = open;
- }, [open, loadReactions, hasCollectionContext, prefetchedReactions, file?.id]);
+ }, [
+ open,
+ loadReactions,
+ hasCollectionContext,
+ prefetchedReactions,
+ file?.id,
+ ]);
// Update local state when prefetchedReactions changes (e.g., like/unlike from heart button)
useEffect(() => {
diff --git a/web/packages/gallery/components/viewer/photoswipe.ts b/web/packages/gallery/components/viewer/photoswipe.ts
index 22f9e55224..a28d036655 100644
--- a/web/packages/gallery/components/viewer/photoswipe.ts
+++ b/web/packages/gallery/components/viewer/photoswipe.ts
@@ -90,7 +90,9 @@ export interface FileViewerPhotoSwipeDelegate {
* This is used for gallery view where the file might belong to a shared
* collection even though we're not directly viewing that collection.
*/
- shouldShowSocialButtons: (annotatedFile: FileViewerAnnotatedFile) => boolean;
+ shouldShowSocialButtons: (
+ annotatedFile: FileViewerAnnotatedFile,
+ ) => boolean;
/**
* Called when there is a keydown event, and our PhotoSwipe instance wants
* to know if it should ignore it or handle it.
@@ -1505,10 +1507,9 @@ export class FileViewerPhotoSwipe {
const captionEl =
element.querySelector<HTMLElement>(".pswp__caption")!;
// Get the action buttons container.
- const actionButtonsEl =
- element.querySelector<HTMLElement>(
- ".pswp__action-buttons",
- );
+ const actionButtonsEl = element.querySelector<HTMLElement>(
+ ".pswp__action-buttons",
+ );
const updateSocialButtonsVisibility = () => {
if (!actionButtonsEl) return;