Skip to content

armgit / ente

Public Imported from GitHub
0

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

refactor(photos):extract-links-content-view

Aswin Asok282097163a8f
diff --git a/web/apps/photos/src/components/gallery/LinksView.tsx b/web/apps/photos/src/components/gallery/LinksView.tsx
new file mode 100644
index 0000000000..c173cff42d
--- /dev/null
+++ b/web/apps/photos/src/components/gallery/LinksView.tsx
@@ -0,0 +1,127 @@
+import { visibleQuickLinks } from "@/utils/quick-links";
+import type { LocalUser } from "ente-accounts/services/user";
+import type { Collection } from "ente-media/collection";
+import type { EnteFile } from "ente-media/file";
+import {
+ PseudoCollectionID,
+ sortCollectionSummaries,
+ type CollectionSummary,
+ type CollectionsSortBy,
+} from "ente-new/photos/services/collection-summary";
+import { t } from "i18next";
+import { useMemo } from "react";
+import type { RemotePullOpts } from ".";
+import { AlbumsOverview } from "./AlbumsOverview";
+import { PublicLinkDetails } from "./PublicLinkDetails";
+
+interface LinksViewProps {
+ user: LocalUser;
+ collections: Collection[];
+ collectionFiles: EnteFile[];
+ collectionSummaries: ReadonlyMap<number, CollectionSummary>;
+ hiddenCollectionIDs: ReadonlySet<number>;
+ hiddenFileIDs: ReadonlySet<number>;
+ tempDeletedFileIDs: ReadonlySet<number>;
+ tempHiddenFileIDs: ReadonlySet<number>;
+ uncategorizedCollectionSummaryID: number;
+ selectedLinkID: number | undefined;
+ searchQuery: string;
+ sortBy: CollectionsSortBy;
+ onCreateAlbum: () => void;
+ setBlockingLoad: (value: boolean) => void;
+ onRemotePull: (opts?: RemotePullOpts) => Promise<void>;
+}
+
+export const LinksView = ({
+ user,
+ collections,
+ collectionFiles,
+ collectionSummaries,
+ hiddenCollectionIDs,
+ hiddenFileIDs,
+ tempDeletedFileIDs,
+ tempHiddenFileIDs,
+ uncategorizedCollectionSummaryID,
+ selectedLinkID,
+ searchQuery,
+ sortBy,
+ onCreateAlbum,
+ setBlockingLoad,
+ onRemotePull,
+}: LinksViewProps) => {
+ const links = useMemo(
+ () => visibleQuickLinks(collections, user.id, hiddenCollectionIDs),
+ [collections, user.id, hiddenCollectionIDs],
+ );
+ const selectedLink = links.find(({ id }) => id === selectedLinkID);
+ const selectedLinkSummary =
+ selectedLink && collectionSummaries.get(selectedLink.id);
+ const selectedLinkFiles = useMemo(
+ () =>
+ selectedLinkID === undefined
+ ? []
+ : collectionFiles.filter(
+ ({ id, collectionID }) =>
+ collectionID === selectedLinkID &&
+ !hiddenFileIDs.has(id) &&
+ !tempDeletedFileIDs.has(id) &&
+ !tempHiddenFileIDs.has(id),
+ ),
+ [
+ selectedLinkID,
+ collectionFiles,
+ hiddenFileIDs,
+ tempDeletedFileIDs,
+ tempHiddenFileIDs,
+ ],
+ );
+ const overviewLinks = useMemo(() => {
+ const summaries = links.flatMap((link) => {
+ const summary = collectionSummaries.get(link.id);
+ return summary ? [summary] : [];
+ });
+ const query = searchQuery.trim().toLowerCase();
+ return sortCollectionSummaries(summaries, sortBy)
+ .sort((a, b) => b.sortPriority - a.sortPriority)
+ .filter(
+ (summary) =>
+ summary.id !== uncategorizedCollectionSummaryID &&
+ summary.id !== PseudoCollectionID.hiddenItems &&
+ summary.id !== PseudoCollectionID.archiveItems &&
+ summary.id !== PseudoCollectionID.trash &&
+ !summary.attributes.has("hideFromCollectionBar") &&
+ summary.name.toLowerCase().includes(query),
+ );
+ }, [
+ links,
+ collectionSummaries,
+ searchQuery,
+ sortBy,
+ uncategorizedCollectionSummaryID,
+ ]);
+
+ return selectedLink ? (
+ <PublicLinkDetails
+ key={selectedLink.id}
+ collection={selectedLink}
+ fileCount={
+ selectedLinkSummary?.fileCount ?? selectedLinkFiles.length
+ }
+ files={selectedLinkFiles}
+ coverFile={selectedLinkSummary?.coverFile}
+ user={user}
+ setBlockingLoad={setBlockingLoad}
+ onRemotePull={onRemotePull}
+ />
+ ) : (
+ <AlbumsOverview
+ albums={overviewLinks}
+ title={t("links")}
+ filter="links"
+ searchQuery={searchQuery}
+ sortBy={sortBy}
+ onCreateAlbum={onCreateAlbum}
+ onRemotePull={onRemotePull}
+ />
+ );
+};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 6aef62e023..5b71f5ccb4 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -33,6 +33,7 @@ import {
import { AlbumsOverview } from "@/components/gallery/AlbumsOverview";
import { DuplicatesView } from "@/components/gallery/DuplicatesView";
import { LargeFilesView } from "@/components/gallery/LargeFilesView";
+import { LinksView } from "@/components/gallery/LinksView";
import { PeopleOverview } from "@/components/gallery/PeopleOverview";
import {
PhotosNavigationContext,
@@ -43,7 +44,6 @@ import {
type PhotosNavigationShellHandle,
} from "@/components/gallery/PhotosNavigationShell";
import type { PlaceSelection } from "@/components/gallery/PlacesList";
-import { PublicLinkDetails } from "@/components/gallery/PublicLinkDetails";
import {
albumShortcutCollectionID,
findCollectionCreatingIfNeeded,
@@ -409,28 +409,6 @@ const Page: React.FC = () => {
),
[quickLinks, normalCollectionSummaries],
);
- const selectedLink = quickLinks.find(({ id }) => id === selectedLinkID);
- const selectedLinkSummary =
- selectedLink && linkSummaries.get(selectedLink.id);
- const selectedLinkFiles = useMemo(
- () =>
- selectedLinkID === undefined
- ? []
- : state.collectionFiles.filter(
- ({ id, collectionID }) =>
- collectionID === selectedLinkID &&
- !hiddenFileIDs.has(id) &&
- !tempDeletedFileIDs.has(id) &&
- !tempHiddenFileIDs.has(id),
- ),
- [
- selectedLinkID,
- state.collectionFiles,
- hiddenFileIDs,
- tempDeletedFileIDs,
- tempHiddenFileIDs,
- ],
- );
const activePerson =
state.view?.type == "people" ? state.view.activePerson : undefined;
const people =
@@ -2321,31 +2299,25 @@ const Page: React.FC = () => {
) : cleanupView === "large-files" ? (
<LargeFilesView onRemoteFilesPull={remoteFilesPull} />
) : !isInSearchMode && barMode == "links" ? (
- selectedLink ? (
- <PublicLinkDetails
- key={selectedLink.id}
- collection={selectedLink}
- fileCount={
- selectedLinkSummary?.fileCount ??
- selectedLinkFiles.length
- }
- files={selectedLinkFiles}
- user={user}
- setBlockingLoad={setBlockingLoad}
- onRemotePull={remotePull}
- coverFile={selectedLinkSummary?.coverFile}
- />
- ) : (
- <AlbumsOverview
- albums={sidebarCollectionSummaries}
- title={t("links")}
- filter="links"
- searchQuery={albumSearchQuery}
- sortBy={collectionsSortBy}
- onCreateAlbum={showAlbumNameInput}
- onRemotePull={remotePull}
- />
- )
+ <LinksView
+ user={user}
+ collections={state.collections}
+ collectionFiles={state.collectionFiles}
+ collectionSummaries={normalCollectionSummaries}
+ hiddenCollectionIDs={hiddenCollectionIDs}
+ hiddenFileIDs={hiddenFileIDs}
+ tempDeletedFileIDs={tempDeletedFileIDs}
+ tempHiddenFileIDs={tempHiddenFileIDs}
+ uncategorizedCollectionSummaryID={
+ state.uncategorizedCollectionSummaryID
+ }
+ selectedLinkID={selectedLinkID}
+ searchQuery={albumSearchQuery}
+ sortBy={collectionsSortBy}
+ onCreateAlbum={showAlbumNameInput}
+ setBlockingLoad={setBlockingLoad}
+ onRemotePull={remotePull}
+ />
) : !isInSearchMode &&
!isFirstLoad &&
!state.collectionFiles.length &&
diff --git a/web/apps/photos/tests/links-view.test.tsx b/web/apps/photos/tests/links-view.test.tsx
new file mode 100644
index 0000000000..b16f4ed614
--- /dev/null
+++ b/web/apps/photos/tests/links-view.test.tsx
@@ -0,0 +1,214 @@
+import type { LocalUser } from "ente-accounts/services/user";
+import { CollectionSubType, type Collection } from "ente-media/collection";
+import type { EnteFile } from "ente-media/file";
+import { ItemVisibility } from "ente-media/file-metadata";
+import type { ComponentProps } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { beforeEach, expect, test, vi } from "vitest";
+import { AlbumsOverview } from "../src/components/gallery/AlbumsOverview";
+import { LinksView } from "../src/components/gallery/LinksView";
+import { PublicLinkDetails } from "../src/components/gallery/PublicLinkDetails";
+import {
+ galleryReducer,
+ initialGalleryState,
+} from "../src/components/gallery/reducer";
+
+vi.mock("@/utils/quick-links", () => import("../src/utils/quick-links"));
+vi.mock("../src/components/gallery/AlbumsOverview", () => ({
+ AlbumsOverview: vi.fn(() => null),
+}));
+vi.mock("../src/components/gallery/PublicLinkDetails", () => ({
+ PublicLinkDetails: vi.fn(() => null),
+}));
+
+beforeEach(() => vi.clearAllMocks());
+
+const link = (id: number, overrides: Partial<Collection> = {}): Collection => ({
+ id,
+ name: `Link ${id}`,
+ type: "album",
+ owner: { id: 1 },
+ key: "test-key",
+ sharees: [],
+ updationTime: id,
+ magicMetadata: {
+ version: 1,
+ count: 1,
+ data: { subType: CollectionSubType.quicklink },
+ },
+ publicURLs: [
+ {
+ url: `https://albums.ente.com/?t=${id}`,
+ validTill: 0,
+ deviceLimit: 0,
+ enableDownload: true,
+ enableCollect: false,
+ enableComment: false,
+ enableJoin: false,
+ passwordEnabled: false,
+ },
+ ],
+ ...overrides,
+});
+
+const file = (id: number, collectionID = 1): EnteFile => ({
+ id,
+ collectionID,
+ ownerID: 1,
+ key: "file-key",
+ file: { decryptionHeader: "" },
+ thumbnail: { decryptionHeader: "" },
+ updationTime: id,
+ metadata: {
+ fileType: 0,
+ title: `${id}.jpg`,
+ creationTime: id,
+ modificationTime: id,
+ },
+});
+
+const props = (
+ collections = [link(1), link(2)],
+ collectionFiles: EnteFile[] = [],
+): ComponentProps<typeof LinksView> => {
+ const user = { id: 1 } as LocalUser;
+ const state = galleryReducer(initialGalleryState, {
+ type: "mount",
+ user,
+ familyData: undefined,
+ collections,
+ collectionFiles,
+ trashItems: [],
+ });
+ return {
+ user,
+ collections,
+ collectionFiles: state.collectionFiles,
+ collectionSummaries: state.normalCollectionSummaries,
+ hiddenCollectionIDs: state.hiddenCollectionIDs,
+ hiddenFileIDs: state.hiddenFileIDs,
+ tempDeletedFileIDs: state.tempDeletedFileIDs,
+ tempHiddenFileIDs: state.tempHiddenFileIDs,
+ uncategorizedCollectionSummaryID:
+ state.uncategorizedCollectionSummaryID,
+ selectedLinkID: undefined,
+ searchQuery: "",
+ sortBy: "updation-time-desc",
+ onCreateAlbum: vi.fn(),
+ setBlockingLoad: vi.fn(),
+ onRemotePull: vi.fn().mockResolvedValue(undefined),
+ };
+};
+
+test("derives the overview from library data with shared sorting and search", () => {
+ const input = props([
+ link(1, { name: "Summer Alpha" }),
+ link(2, { name: "Summer Beta" }),
+ link(3, { name: "Winter" }),
+ link(4, { name: "Summer regular album", magicMetadata: undefined }),
+ link(5, { name: "Summer removed link", publicURLs: [] }),
+ ]);
+ const summaries = new Map(input.collectionSummaries);
+ summaries.set(2, { ...summaries.get(2)!, sortPriority: 7 });
+
+ renderToStaticMarkup(
+ <LinksView
+ {...input}
+ collectionSummaries={summaries}
+ searchQuery=" SUMMER "
+ sortBy="name-asc"
+ />,
+ );
+
+ const overview = vi.mocked(AlbumsOverview).mock.calls[0]![0];
+ expect(overview.albums.map(({ id }) => id)).toEqual([2, 1]);
+ expect(overview.filter).toBe("links");
+ expect(overview.searchQuery).toBe(" SUMMER ");
+ expect(overview.sortBy).toBe("name-asc");
+ expect(overview.onRemotePull).toBe(input.onRemotePull);
+ expect(overview.onCreateAlbum).toBe(input.onCreateAlbum);
+ expect(PublicLinkDetails).not.toHaveBeenCalled();
+ expect(summaries.get(2)?.sortPriority).toBe(7);
+ expect(input.collections.map(({ id }) => id)).toEqual([1, 2, 3, 4, 5]);
+});
+
+test.each([0, 17])(
+ "filters detail files and uses the summary count of %i and its cover",
+ (fileCount) => {
+ const archived = file(6);
+ archived.magicMetadata = {
+ version: 1,
+ count: 1,
+ data: { visibility: ItemVisibility.archived },
+ };
+ const input = props(undefined, [
+ file(1),
+ file(2),
+ file(3),
+ file(4),
+ file(5, 2),
+ archived,
+ ]);
+ const coverFile = file(7);
+ const summaries = new Map(input.collectionSummaries);
+ summaries.set(1, { ...summaries.get(1)!, fileCount, coverFile });
+
+ renderToStaticMarkup(
+ <LinksView
+ {...input}
+ selectedLinkID={1}
+ collectionSummaries={summaries}
+ hiddenFileIDs={new Set([2])}
+ tempDeletedFileIDs={new Set([3])}
+ tempHiddenFileIDs={new Set([4])}
+ />,
+ );
+
+ const details = vi.mocked(PublicLinkDetails).mock.calls[0]![0];
+ expect(details.collection.id).toBe(1);
+ expect(details.files.map(({ id }) => id).sort()).toEqual([1, 6]);
+ expect(details.fileCount).toBe(fileCount);
+ expect(details.coverFile).toBe(coverFile);
+ expect(details.user).toBe(input.user);
+ expect(details.setBlockingLoad).toBe(input.setBlockingLoad);
+ expect(details.onRemotePull).toBe(input.onRemotePull);
+ expect(AlbumsOverview).not.toHaveBeenCalled();
+ },
+);
+
+test("uses the filtered detail file count when no summary exists", () => {
+ const input = props(undefined, [file(1), file(2), file(3, 2)]);
+ renderToStaticMarkup(
+ <LinksView
+ {...input}
+ selectedLinkID={1}
+ collectionSummaries={new Map()}
+ />,
+ );
+ const details = vi.mocked(PublicLinkDetails).mock.calls[0]![0];
+ expect(details.fileCount).toBe(2);
+ expect(details.coverFile).toBeUndefined();
+});
+
+test.each(["removed", "unlinked", "hidden", "shared", "other-owner"])(
+ "returns to the overview when the selected link is %s",
+ (change) => {
+ const updated = link(1);
+ if (change == "unlinked") updated.publicURLs = [];
+ if (change == "shared") updated.sharees = [{ id: 2 }];
+ if (change == "other-owner") updated.owner.id = 2;
+ const input = props(
+ change == "removed" ? [link(2)] : [updated, link(2)],
+ );
+ renderToStaticMarkup(
+ <LinksView
+ {...input}
+ selectedLinkID={1}
+ hiddenCollectionIDs={new Set(change == "hidden" ? [1] : [])}
+ />,
+ );
+ const overview = vi.mocked(AlbumsOverview).mock.calls[0]![0];
+ expect(overview.albums.map(({ id }) => id)).toEqual([2]);
+ expect(PublicLinkDetails).not.toHaveBeenCalled();
+ },
+);