Skip to content

armgit / ente

Public Imported from GitHub
0

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

refractor(CollectionSort):uses-same-logical-function

Aswin Asokf43f5ed1981c
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index 575a9f8cf2..dc738fb4ff 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -21,11 +21,11 @@ import {
GalleryItemsSummary,
} from "ente-new/photos/components/gallery/ListHeader";
import { PeopleHeader } from "ente-new/photos/components/gallery/PeopleHeader";
-import type { CollectionSummary } from "ente-new/photos/services/collection-summary";
import {
collectionsSortBy,
haveOnlySystemCollections,
PseudoCollectionID,
+ sortCollectionSummaries,
type CollectionsSortBy,
type CollectionSummaries,
} from "ente-new/photos/services/collection-summary";
@@ -142,7 +142,7 @@ export const GalleryBarAndListHeader: React.FC<
sortCollectionSummaries(
[...toShowCollectionSummaries.values()],
collectionsSortBy,
- ),
+ ).sort((a, b) => b.sortPriority - a.sortPriority),
[collectionsSortBy, toShowCollectionSummaries],
);
@@ -307,28 +307,3 @@ const useCollectionsSortByLocalState = (initialValue: CollectionsSortBy) => {
return [value, setter] as const;
};
-
-const sortCollectionSummaries = (
- collectionSummaries: CollectionSummary[],
- by: CollectionsSortBy,
-) =>
- collectionSummaries
- .sort((a, b) => {
- switch (by) {
- case "name-asc":
- return a.name.localeCompare(b.name);
- case "name-desc":
- return b.name.localeCompare(a.name);
- case "creation-time-asc":
- // Oldest first: lower ID = created earlier
- return a.id - b.id;
- case "creation-time-desc":
- // Newest first: higher ID = created later
- return b.id - a.id;
- case "updation-time-asc":
- return (a.updationTime ?? 0) - (b.updationTime ?? 0);
- case "updation-time-desc":
- return (b.updationTime ?? 0) - (a.updationTime ?? 0);
- }
- })
- .sort((a, b) => b.sortPriority - a.sortPriority);
diff --git a/web/packages/new/photos/components/CollectionSelector.tsx b/web/packages/new/photos/components/CollectionSelector.tsx
index 1e49da011d..b36089d0bd 100644
--- a/web/packages/new/photos/components/CollectionSelector.tsx
+++ b/web/packages/new/photos/components/CollectionSelector.tsx
@@ -15,9 +15,7 @@ import {
} from "@mui/material";
import { FilledIconButton } from "ente-base/components/mui";
import type { ModalVisibilityProps } from "ente-base/components/utils/modal";
-import { sortFiles } from "ente-gallery/utils/file";
import type { Collection } from "ente-media/collection";
-import type { EnteFile } from "ente-media/file";
import { CollectionsSortOptions } from "ente-new/photos/components/CollectionsSortOptions";
import {
ItemCard,
@@ -29,6 +27,7 @@ import {
canAddToCollection,
canMoveToCollection,
collectionsSortBy,
+ sortCollectionSummaries,
type CollectionsSortBy,
type CollectionSummaries,
type CollectionSummary,
@@ -151,8 +150,8 @@ export const CollectionSelector: React.FC<CollectionSelectorProps> = ({
}
const activeCollectionID = attributes.activeCollectionID;
- const collections = [...collectionSummaries.values()]
- .filter((cs) => {
+ const filteredCollections = [...collectionSummaries.values()].filter(
+ (cs) => {
if (cs.id === attributes.sourceCollectionSummaryID) {
return false;
} else if (attributes.action == "add") {
@@ -175,31 +174,10 @@ export const CollectionSelector: React.FC<CollectionSelectorProps> = ({
canMoveToCollection(cs) && cs.type != "userFavorites"
);
}
- })
- .sort((a, b) => {
- switch (sortBy) {
- case "name-asc":
- return a.name.localeCompare(b.name);
- case "name-desc":
- return b.name.localeCompare(a.name);
- case "creation-time-asc":
- return compareCollectionsLatestFile(
- a.latestFile,
- b.latestFile,
- true,
- );
- case "creation-time-desc":
- return compareCollectionsLatestFile(
- a.latestFile,
- b.latestFile,
- false,
- );
- case "updation-time-asc":
- return (a.updationTime ?? 0) - (b.updationTime ?? 0);
- case "updation-time-desc":
- return (b.updationTime ?? 0) - (a.updationTime ?? 0);
- }
- })
+ },
+ );
+
+ const collections = sortCollectionSummaries(filteredCollections, sortBy)
.sort((a, b) => b.sortPriority - a.sortPriority)
.sort((a, b) => {
// Prioritize the active collection (if any) to appear first.
@@ -494,22 +472,3 @@ const useCollectionSelectorSortByLocalState = (
return [value, setter] as const;
};
-
-const compareCollectionsLatestFile = (
- first: EnteFile | undefined,
- second: EnteFile | undefined,
- sortAsc: boolean,
-) => {
- if (!first) {
- return 1;
- } else if (!second) {
- return -1;
- } else {
- const sortedFiles = sortFiles([first, second], sortAsc);
- if (sortedFiles[0]?.id !== first.id) {
- return 1;
- } else {
- return -1;
- }
- }
-};
diff --git a/web/packages/new/photos/services/collection-summary.ts b/web/packages/new/photos/services/collection-summary.ts
index bef7dd05aa..605da44ec7 100644
--- a/web/packages/new/photos/services/collection-summary.ts
+++ b/web/packages/new/photos/services/collection-summary.ts
@@ -226,3 +226,34 @@ export const canAddToCollection = ({ attributes }: CollectionSummary) =>
export const canMoveToCollection = ({ attributes }: CollectionSummary) =>
!attributes.has("system") && !attributes.has("sharedIncoming");
+
+/**
+ * Sort an array of collection summaries by the specified sort order.
+ *
+ * @param collectionSummaries The array of collection summaries to sort
+ * @param by The sort order to apply
+ * @returns A new sorted array of collection summaries
+ */
+export const sortCollectionSummaries = (
+ collectionSummaries: CollectionSummary[],
+ by: CollectionsSortBy,
+): CollectionSummary[] => {
+ return [...collectionSummaries].sort((a, b) => {
+ switch (by) {
+ case "name-asc":
+ return a.name.localeCompare(b.name);
+ case "name-desc":
+ return b.name.localeCompare(a.name);
+ case "creation-time-asc":
+ // Oldest first: lower ID = created earlier
+ return a.id - b.id;
+ case "creation-time-desc":
+ // Newest first: higher ID = created later
+ return b.id - a.id;
+ case "updation-time-asc":
+ return (a.updationTime ?? 0) - (b.updationTime ?? 0);
+ case "updation-time-desc":
+ return (b.updationTime ?? 0) - (a.updationTime ?? 0);
+ }
+ });
+};