Skip to content

armgit / ente

Public Imported from GitHub
0

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

feat(CollectionSortOptions):updating-ui-and-sort-logics

Aswin Asok9c1d59f7c72e
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index 5d506ac4ff..575a9f8cf2 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -11,9 +11,7 @@ import {
isSaveComplete,
type SaveGroup,
} from "ente-gallery/components/utils/save-groups";
-import { sortFiles } from "ente-gallery/utils/file";
import type { Collection } from "ente-media/collection";
-import type { EnteFile } from "ente-media/file";
import {
GalleryBarImpl,
type GalleryBarImplProps,
@@ -322,17 +320,11 @@ const sortCollectionSummaries = (
case "name-desc":
return b.name.localeCompare(a.name);
case "creation-time-asc":
- // Oldest first: when b is newer, b comes after a
- return compareCollectionsLatestFile(
- b.latestFile,
- a.latestFile,
- );
+ // Oldest first: lower ID = created earlier
+ return a.id - b.id;
case "creation-time-desc":
- // Newest first: when a is newer, a comes before b
- return compareCollectionsLatestFile(
- a.latestFile,
- b.latestFile,
- );
+ // 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":
@@ -340,21 +332,3 @@ const sortCollectionSummaries = (
}
})
.sort((a, b) => b.sortPriority - a.sortPriority);
-
-const compareCollectionsLatestFile = (
- first: EnteFile | undefined,
- second: EnteFile | undefined,
-) => {
- if (!first) {
- return 1;
- } else if (!second) {
- return -1;
- } else {
- const sortedFiles = sortFiles([first, second]);
- if (sortedFiles[0]?.id !== first.id) {
- return 1;
- } else {
- return -1;
- }
- }
-};
diff --git a/web/packages/base/locales/en-US/translation.json b/web/packages/base/locales/en-US/translation.json
index 2248f9e9c5..42e7173679 100644
--- a/web/packages/base/locales/en-US/translation.json
+++ b/web/packages/base/locales/en-US/translation.json
@@ -446,6 +446,12 @@
"created_old_to_new": "Created: Old to New",
"updated_new_to_old": "Updated: New to Old",
"updated_old_to_new": "Updated: Old to New",
+ "created": "Created",
+ "updated": "Updated",
+ "sort_asc_indicator": "A-Z",
+ "sort_desc_indicator": "Z-A",
+ "sort_oldest": "Oldest",
+ "sort_newest": "Newest",
"fix_creation_time": "Fix time",
"fix_creation_time_in_progress": "Fixing time",
"fix_creation_time_file_updated": "File time updated",
diff --git a/web/packages/new/package.json b/web/packages/new/package.json
index 9be7311c3e..6b128d0d90 100644
--- a/web/packages/new/package.json
+++ b/web/packages/new/package.json
@@ -10,6 +10,8 @@
"ente-base": "*",
"ente-gallery": "*",
"ente-utils": "*",
+ "@hugeicons/react": "^1.0.5",
+ "@hugeicons/core-free-icons": "^1.0.5",
"react": "^19.2.0",
"react-dom": "^19.2.0"
},
diff --git a/web/packages/new/photos/components/CollectionsSortOptions.tsx b/web/packages/new/photos/components/CollectionsSortOptions.tsx
index eb734fd30f..b3f04b0fc0 100644
--- a/web/packages/new/photos/components/CollectionsSortOptions.tsx
+++ b/web/packages/new/photos/components/CollectionsSortOptions.tsx
@@ -1,11 +1,18 @@
import SortIcon from "@mui/icons-material/Sort";
import {
- OverflowMenu,
- OverflowMenuOption,
-} from "ente-base/components/OverflowMenu";
+ IconButton,
+ MenuItem,
+ Stack,
+ Typography,
+ type IconButtonProps,
+ type PaperProps,
+} from "@mui/material";
+import Menu, { type MenuProps } from "@mui/material/Menu";
import type { CollectionsSortBy } from "ente-new/photos/services/collection-summary";
+import { ArrowDown02Icon, ArrowUp02Icon } from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
import { t } from "i18next";
-import React from "react";
+import React, { useState } from "react";
interface CollectionsSortOptionsProps {
/**
@@ -28,70 +35,189 @@ interface CollectionsSortOptionsProps {
transparentTriggerButtonBackground?: boolean;
}
+/** The three sort categories. */
+type SortCategory = "name" | "creation-time" | "updation-time";
+
+/** Extract the category from a CollectionsSortBy value. */
+const getSortCategory = (sortBy: CollectionsSortBy): SortCategory => {
+ if (sortBy.startsWith("name")) return "name";
+ if (sortBy.startsWith("creation-time")) return "creation-time";
+ return "updation-time";
+};
+
+/** Check if the sort is ascending. */
+const isAscending = (sortBy: CollectionsSortBy): boolean =>
+ sortBy.endsWith("-asc");
+
+/** Get the CollectionsSortBy value for a category and direction. */
+const getSortBy = (
+ category: SortCategory,
+ ascending: boolean,
+): CollectionsSortBy => `${category}-${ascending ? "asc" : "desc"}`;
+
/**
* A button that shows an overflow menu allowing the user to choose from amongst
* the {@link CollectionsSortBy} values that should be used for sorting the
* lists of collections.
*/
export const CollectionsSortOptions: React.FC<CollectionsSortOptionsProps> = ({
+ activeSortBy,
+ onChangeSortBy,
nestedInDialog,
transparentTriggerButtonBackground,
- ...optProps
-}) => (
- <OverflowMenu
- ariaID="collection-sort"
- triggerButtonIcon={<SortIcon />}
- menuPaperSxProps={[
- {
- // The trigger button has a colored background, so add some
- // vertical margin to avoid showing the menu squat under the
- // trigger button.
- marginBlock: 1,
- },
- nestedInDialog ? { backgroundColor: "background.paper2" } : {},
- ]}
- triggerButtonSxProps={[
- transparentTriggerButtonBackground
- ? {}
- : { backgroundColor: "fill.faint" },
- ]}
- >
- <SortByOption {...optProps} sortBy="name-asc">
- {t("name_a_to_z")}
- </SortByOption>
- <SortByOption {...optProps} sortBy="name-desc">
- {t("name_z_to_a")}
- </SortByOption>
- <SortByOption {...optProps} sortBy="creation-time-desc">
- {t("created_new_to_old")}
- </SortByOption>
- <SortByOption {...optProps} sortBy="creation-time-asc">
- {t("created_old_to_new")}
- </SortByOption>
- <SortByOption {...optProps} sortBy="updation-time-desc">
- {t("updated_new_to_old")}
- </SortByOption>
- <SortByOption {...optProps} sortBy="updation-time-asc">
- {t("updated_old_to_new")}
- </SortByOption>
- </OverflowMenu>
-);
+}) => {
+ const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();
+ const ariaID = "collection-sort";
-type SortByOptionProps = Pick<
- CollectionsSortOptionsProps,
- "activeSortBy" | "onChangeSortBy"
-> & { sortBy: CollectionsSortBy };
+ const activeCategory = getSortCategory(activeSortBy);
+ const activeAscending = isAscending(activeSortBy);
-const SortByOption: React.FC<React.PropsWithChildren<SortByOptionProps>> = ({
- activeSortBy,
- onChangeSortBy,
- sortBy,
- children,
-}) => (
- <OverflowMenuOption
- onClick={() => onChangeSortBy(sortBy)}
- selected={activeSortBy === sortBy}
- >
- {children}
- </OverflowMenuOption>
-);
+ const handleCategoryClick = (category: SortCategory) => {
+ if (category === activeCategory) {
+ // Toggle direction if same category
+ onChangeSortBy(getSortBy(category, !activeAscending));
+ } else {
+ // Select new category with default direction
+ const defaultAscending = category === "name"; // Name defaults to A-Z (asc), dates to newest (desc)
+ onChangeSortBy(getSortBy(category, defaultAscending));
+ }
+ setAnchorEl(undefined);
+ };
+
+ const triggerButtonSxProps: IconButtonProps["sx"] = [
+ transparentTriggerButtonBackground
+ ? {}
+ : { backgroundColor: "fill.faint" },
+ ];
+
+ const menuPaperSxProps: PaperProps["sx"] = [
+ { marginBlock: 1, borderRadius: 2, overflow: "hidden" },
+ nestedInDialog ? { backgroundColor: "background.paper2" } : {},
+ ];
+
+ return (
+ <>
+ <IconButton
+ onClick={(event) => setAnchorEl(event.currentTarget)}
+ aria-controls={anchorEl ? ariaID : undefined}
+ aria-haspopup="true"
+ aria-expanded={anchorEl ? "true" : undefined}
+ sx={triggerButtonSxProps}
+ >
+ <SortIcon />
+ </IconButton>
+ <Menu
+ id={ariaID}
+ {...(anchorEl && { anchorEl })}
+ open={!!anchorEl}
+ onClose={() => setAnchorEl(undefined)}
+ slotProps={{
+ paper: { sx: menuPaperSxProps },
+ list: { disablePadding: true, "aria-labelledby": ariaID },
+ }}
+ anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
+ transformOrigin={{ vertical: "top", horizontal: "right" }}
+ >
+ <SortCategoryOption
+ category="name"
+ activeCategory={activeCategory}
+ activeAscending={activeAscending}
+ onClick={handleCategoryClick}
+ label={t("name")}
+ ascLabel={t("sort_asc_indicator")}
+ descLabel={t("sort_desc_indicator")}
+ showBorder
+ />
+ <SortCategoryOption
+ category="creation-time"
+ activeCategory={activeCategory}
+ activeAscending={activeAscending}
+ onClick={handleCategoryClick}
+ label={t("created")}
+ ascLabel={t("sort_oldest")}
+ descLabel={t("sort_newest")}
+ showBorder
+ />
+ <SortCategoryOption
+ category="updation-time"
+ activeCategory={activeCategory}
+ activeAscending={activeAscending}
+ onClick={handleCategoryClick}
+ label={t("updated")}
+ ascLabel={t("sort_oldest")}
+ descLabel={t("sort_newest")}
+ />
+ </Menu>
+ </>
+ );
+};
+
+interface SortCategoryOptionProps {
+ category: SortCategory;
+ activeCategory: SortCategory;
+ activeAscending: boolean;
+ onClick: (category: SortCategory) => void;
+ label: string;
+ ascLabel: string;
+ descLabel: string;
+ showBorder?: boolean;
+}
+
+const SortCategoryOption: React.FC<SortCategoryOptionProps> = ({
+ category,
+ activeCategory,
+ activeAscending,
+ onClick,
+ label,
+ ascLabel,
+ descLabel,
+ showBorder,
+}) => {
+ const isSelected = category === activeCategory;
+ const directionLabel = activeAscending ? ascLabel : descLabel;
+ const ArrowIcon = activeAscending ? ArrowUp02Icon : ArrowDown02Icon;
+
+ return (
+ <MenuItem
+ disableGutters
+ onClick={() => onClick(category)}
+ sx={{
+ minWidth: 220,
+ minHeight: 56,
+ py: 2,
+ px: 2.5,
+ display: "flex",
+ alignItems: "center",
+ borderBottom: showBorder ? "1px solid" : "none",
+ borderColor: "divider",
+ }}
+ >
+ <Stack direction="row" sx={{ alignItems: "center" }}>
+ <Typography
+ sx={{
+ color: isSelected ? "text.primary" : "text.secondary",
+ }}
+ >
+ {label}
+ </Typography>
+ {isSelected && (
+ <Stack
+ direction="row"
+ sx={{
+ alignItems: "center",
+ ml: 1,
+ gap: 0.75,
+ color: "text.muted",
+ }}
+ >
+ <Typography>β€’</Typography>
+ <Typography sx={{ fontSize: "0.875rem" }}>
+ {directionLabel}
+ </Typography>
+ <ArrowIcon size={18} color="currentColor" />
+ </Stack>
+ )}
+ </Stack>
+ </MenuItem>
+ );
+};
diff --git a/web/yarn.lock b/web/yarn.lock
index 40ab80e667..05507e46de 100644
--- a/web/yarn.lock
+++ b/web/yarn.lock
@@ -645,6 +645,16 @@
resolved "https://registry.yarnpkg.com/@fontsource-variable/inter/-/inter-5.2.8.tgz#29b11476f5149f6a443b4df6516e26002d87941a"
integrity sha512-kOfP2D+ykbcX/P3IFnokOhVRNoTozo5/JxhAIVYLpea/UBmCQ/YWPBfWIDuBImXX/15KH+eKh4xpEUyS2sQQGQ==
+"@hugeicons/core-free-icons@^1.0.5":
+ version "1.2.1"
+ resolved "https://registry.yarnpkg.com/@hugeicons/core-free-icons/-/core-free-icons-1.2.1.tgz#c741eaa8bbf1453e9282a64c19a8bbcc8cef5f19"
+ integrity sha512-ho0QdGMkgL+kt+QsZocCsKvJou1rfyVQWARrxIhNLi+9tCKayUUtD9jlHgioaRphmskSl84TxrDm9Ae0G4Uu1g==
+
+"@hugeicons/react@^1.0.5":
+ version "1.1.4"
+ resolved "https://registry.yarnpkg.com/@hugeicons/react/-/react-1.1.4.tgz#1662f11ebb42c7e16fa7e16e04615db666813a06"
+ integrity sha512-gsc3eZyd2fGqRUThW9+lfjxxsOkz6KNVmRXRgJjP32GL0OnnLJnl3hytKt47CBbiQj2xE2kCw+rnP3UQCThcKw==
+
"@humanfs/core@^0.19.1":
version "0.19.1"
resolved "https://registry.yarnpkg.com/@humanfs/core/-/core-0.19.1.tgz#17c55ca7d426733fe3c561906b8173c336b40a77"