π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
feat(gallery):add-sort-to-people-tab
Aswin Asok468d3e44f209
diff --git a/web/apps/photos/src/components/Collections/AllPeople.tsx b/web/apps/photos/src/components/Collections/AllPeople.tsxindex 2b78c15c64..b921a0bd71 100644--- a/web/apps/photos/src/components/Collections/AllPeople.tsx+++ b/web/apps/photos/src/components/Collections/AllPeople.tsx@@ -1,6 +1,4 @@// TODO: Audit this file.-import { ArrowDown02Icon, ArrowUp02Icon } from "@hugeicons/core-free-icons";-import { HugeiconsIcon } from "@hugeicons/react";import AddIcon from "@mui/icons-material/Add";import CloseIcon from "@mui/icons-material/Close";import EditIcon from "@mui/icons-material/Edit";@@ -9,26 +7,20 @@ import MoreHorizIcon from "@mui/icons-material/MoreHoriz";import PushPinIcon from "@mui/icons-material/PushPin";import PushPinOutlinedIcon from "@mui/icons-material/PushPinOutlined";import SearchIcon from "@mui/icons-material/Search";-import SortIcon from "@mui/icons-material/Sort";import {Box,Dialog,DialogContent,DialogTitle,Divider,- IconButton,InputAdornment,- MenuItem,Stack,TextField,Tooltip,Typography,styled,useMediaQuery,- type IconButtonProps,- type PaperProps,} from "@mui/material";-import Menu, { type MenuProps } from "@mui/material/Menu";import { FilledIconButton } from "ente-base/components/mui";import { DialogCloseIconButton } from "ente-base/components/mui/DialogCloseIconButton";import {@@ -38,6 +30,10 @@ import {import { SingleInputDialog } from "ente-base/components/SingleInputDialog";import { useBaseContext } from "ente-base/context";import { SlideUpTransition } from "ente-new/photos/components/mui/SlideUpTransition";+import {+ PeopleSortOptions,+ type PeopleSortBy,+} from "ente-new/photos/components/PeopleSortOptions";import {ItemCard,LargeTileButton,@@ -69,12 +65,6 @@ import {type ListChildComponentProps,} from "react-window";-export type PeopleSortBy =- | "count-desc"- | "count-asc"- | "name-asc"- | "name-desc";-interface AllPeopleProps {open: boolean;onClose: () => void;@@ -132,21 +122,16 @@ export const AllPeople: React.FC<AllPeopleProps> = ({});};- const sortedPeople = useMemo(- () => [...people].sort(personComparator(peopleSortBy)),- [people, peopleSortBy],- );-const filteredPeople = useMemo(() => {if (!searchTerm.trim()) {- return sortedPeople;+ return people;}const searchLower = searchTerm.toLowerCase();- return sortedPeople.filter((person) =>+ return people.filter((person) =>person.name?.toLowerCase().includes(searchLower),);- }, [searchTerm, sortedPeople]);+ }, [searchTerm, people]);return (<>@@ -349,194 +334,6 @@ const SearchField: React.FC<SearchFieldProps> = ({ value, onChange }) => {);};-type PeopleSortCategory = "name" | "count";--const getPeopleSortCategory = (sortBy: PeopleSortBy): PeopleSortCategory =>- sortBy.startsWith("name") ? "name" : "count";--const isPeopleSortAscending = (sortBy: PeopleSortBy) => sortBy.endsWith("asc");--const getPeopleSortBy = (- category: PeopleSortCategory,- ascending: boolean,-): PeopleSortBy => `${category}-${ascending ? "asc" : "desc"}` as PeopleSortBy;--interface PeopleSortOptionsProps {- activeSortBy: PeopleSortBy;- onChangeSortBy: (by: PeopleSortBy) => void;- nestedInDialog?: boolean;- transparentTriggerButtonBackground?: boolean;-}--const PeopleSortOptions: React.FC<PeopleSortOptionsProps> = ({- activeSortBy,- onChangeSortBy,- nestedInDialog,- transparentTriggerButtonBackground,-}) => {- const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();- const pendingSortByRef = useRef<PeopleSortBy | undefined>(undefined);- const ariaID = "people-sort";-- const activeCategory = getPeopleSortCategory(activeSortBy);- const activeAscending = isPeopleSortAscending(activeSortBy);-- const handleCategoryClick = (category: PeopleSortCategory) => {- let nextSortBy: PeopleSortBy;- if (category === activeCategory) {- nextSortBy = getPeopleSortBy(category, !activeAscending);- } else {- nextSortBy = getPeopleSortBy(category, category === "name");- }- pendingSortByRef.current = nextSortBy;- setAnchorEl(undefined);- };-- const triggerButtonSxProps: IconButtonProps["sx"] = [- transparentTriggerButtonBackground- ? {}- : { backgroundColor: "fill.faint" },- ];-- const menuPaperSxProps: PaperProps["sx"] | undefined = nestedInDialog- ? { backgroundColor: "background.paper2" }- : undefined;-- return (- <>- <IconButton- onClick={(event) => setAnchorEl(event.currentTarget)}- aria-controls={anchorEl ? ariaID : undefined}- aria-haspopup="true"- aria-expanded={anchorEl ? "true" : undefined}- sx={triggerButtonSxProps}- >- <SortIcon />- </IconButton>- <StyledMenu- id={ariaID}- {...(anchorEl && { anchorEl })}- open={!!anchorEl}- onClose={() => setAnchorEl(undefined)}- slotProps={{- paper: menuPaperSxProps ? { sx: menuPaperSxProps } : {},- list: { disablePadding: true, "aria-labelledby": ariaID },- transition: {- onExited: () => {- const nextSortBy = pendingSortByRef.current;- if (nextSortBy) {- pendingSortByRef.current = undefined;- onChangeSortBy(nextSortBy);- }- },- },- }}- anchorOrigin={{ vertical: "bottom", horizontal: "right" }}- transformOrigin={{ vertical: "top", horizontal: "right" }}- >- <PeopleSortCategoryOption- category="name"- activeCategory={activeCategory}- activeAscending={activeAscending}- onClick={handleCategoryClick}- label={t("name")}- directionLabel={- activeAscending- ? t("sort_asc_indicator")- : t("sort_desc_indicator")- }- />- <PeopleSortCategoryOption- category="count"- activeCategory={activeCategory}- activeAscending={activeAscending}- onClick={handleCategoryClick}- label={t("photos")}- />- </StyledMenu>- </>- );-};--interface PeopleSortCategoryOptionProps {- category: PeopleSortCategory;- activeCategory: PeopleSortCategory;- activeAscending: boolean;- onClick: (category: PeopleSortCategory) => void;- label: string;- directionLabel?: string;-}--const PeopleSortCategoryOption: React.FC<PeopleSortCategoryOptionProps> = ({- category,- activeCategory,- activeAscending,- onClick,- label,- directionLabel,-}) => {- const isSelected = category === activeCategory;- const arrowIcon = activeAscending ? ArrowUp02Icon : ArrowDown02Icon;-- return (- <StyledMenuItem onClick={() => onClick(category)}>- <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",- }}- >- {directionLabel && <Typography>β’</Typography>}- {directionLabel && (- <Typography sx={{ fontSize: "0.9rem" }}>- {directionLabel}- </Typography>- )}- <HugeiconsIcon- icon={arrowIcon}- size={19}- color="currentColor"- />- </Stack>- )}- </Stack>- </StyledMenuItem>- );-};--const StyledMenu = styled(Menu)(({ theme }) => ({- "& .MuiPaper-root": {- backgroundColor: theme.vars.palette.background.elevatedPaper,- minWidth: 220,- width: 220,- borderRadius: 12,- boxShadow: theme.vars.palette.boxShadow.menu,- marginTop: 6,- },- "& .MuiList-root": { padding: theme.spacing(1) },-}));--const StyledMenuItem = styled(MenuItem)(({ theme }) => ({- display: "flex",- alignItems: "center",- gap: 12,- borderRadius: 10,- "& + &": { marginTop: 4 },- "&.Mui-selected": { backgroundColor: theme.vars.palette.fill.faint },-}));-interface AllPeopleContentProps {people: Person[];hasSearchQuery: boolean;@@ -978,54 +775,3 @@ const PersonPickerCard: React.FC<PersonPickerCardProps> = ({</LargeTileTextOverlay></ItemCard>);--const personComparator =- (sortBy: PeopleSortBy) =>- (a: Person, b: Person): number => {- if (a.isPinned !== b.isPinned) {- return a.isPinned ? -1 : 1;- }-- const sectionRankDiff = personSectionRank(a) - personSectionRank(b);-- if (sortBy.startsWith("name")) {- const aName = a.name?.trim();- const bName = b.name?.trim();- if (!!aName !== !!bName) {- return aName ? -1 : 1;- }- if (aName && bName) {- const cmp = aName.localeCompare(bName, undefined, {- sensitivity: "base",- });- if (cmp) {- return sortBy === "name-asc" ? cmp : -cmp;- }- }- return b.fileIDs.length - a.fileIDs.length;- }-- if (sectionRankDiff) {- return sectionRankDiff;- }-- const countDiff = a.fileIDs.length - b.fileIDs.length;- if (countDiff) {- return sortBy === "count-asc" ? countDiff : -countDiff;- }-- const aName = a.name?.trim();- const bName = b.name?.trim();- if (!!aName !== !!bName) {- return aName ? -1 : 1;- }- if (aName && bName) {- return aName.localeCompare(bName, undefined, {- sensitivity: "base",- });- }- return 0;- };--const personSectionRank = (person: Person) =>- person.type === "cgroup" ? 0 : 1;diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsxindex fe7037299c..054f7fe43a 100644--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx@@ -1,6 +1,6 @@// TODO: Audit this fileimport { AllAlbums } from "components/Collections/AllAlbums";-import { AllPeople, type PeopleSortBy } from "components/Collections/AllPeople";+import { AllPeople } from "components/Collections/AllPeople";import {CollectionShare,type CollectionShareIntent,@@ -14,6 +14,10 @@ import {type SaveGroup,} from "ente-gallery/components/utils/save-groups";import type { Collection } from "ente-media/collection";+import {+ sortPeople,+ type PeopleSortBy,+} from "ente-new/photos/components/PeopleSortOptions";import {GalleryBarImpl,type GalleryBarImplProps,@@ -45,6 +49,8 @@ type GalleryBarAndListHeaderProps = Omit<| "onSelectCollectionID"| "collectionsSortBy"| "onChangeCollectionsSortBy"+ | "peopleSortBy"+ | "onChangePeopleSortBy"| "onShowAllAlbums"| "onShowAllPeople"> & {@@ -173,6 +179,10 @@ export const GalleryBarAndListHeader: React.FC<).sort((a, b) => b.sortPriority - a.sortPriority),[collectionsSortBy, toShowCollectionSummaries],);+ const sortedPeople = useMemo(+ () => sortPeople(people, peopleSortBy),+ [people, peopleSortBy],+ );const isActiveCollectionDownloadInProgress = useCallback(() => {const group = saveGroups.find(@@ -278,13 +288,15 @@ export const GalleryBarAndListHeader: React.FC<mode,onChangeMode,activeCollectionID,- people,+ people: sortedPeople,activePerson,onSelectPerson,collectionsSortBy,+ peopleSortBy,}}onSelectCollectionID={setActiveCollectionID}onChangeCollectionsSortBy={setCollectionsSortBy}+ onChangePeopleSortBy={setPeopleSortBy}onShowAllAlbums={showAllAlbums}onShowAllPeople={showAllPeople}collectionSummaries={sortedCollectionSummaries.filter(@@ -305,7 +317,7 @@ export const GalleryBarAndListHeader: React.FC</><AllPeople{...allPeopleVisibilityProps}- people={people}+ people={sortedPeople}onSelectPerson={onSelectPerson}peopleSortBy={peopleSortBy}onChangePeopleSortBy={setPeopleSortBy}diff --git a/web/packages/new/photos/components/PeopleSortOptions.tsx b/web/packages/new/photos/components/PeopleSortOptions.tsxnew file mode 100644index 0000000000..d9641ebdcd--- /dev/null+++ b/web/packages/new/photos/components/PeopleSortOptions.tsx@@ -0,0 +1,269 @@+import { ArrowDown02Icon, ArrowUp02Icon } from "@hugeicons/core-free-icons";+import { HugeiconsIcon } from "@hugeicons/react";+import SortIcon from "@mui/icons-material/Sort";+import {+ IconButton,+ MenuItem,+ Stack,+ Typography,+ styled,+ type IconButtonProps,+ type PaperProps,+} from "@mui/material";+import Menu, { type MenuProps } from "@mui/material/Menu";+import type { Person } from "ente-new/photos/services/ml/people";+import { t } from "i18next";+import React, { useRef, useState } from "react";++export type PeopleSortBy =+ | "count-desc"+ | "count-asc"+ | "name-asc"+ | "name-desc";++interface PeopleSortOptionsProps {+ activeSortBy: PeopleSortBy;+ onChangeSortBy: (by: PeopleSortBy) => void;+ nestedInDialog?: boolean;+ transparentTriggerButtonBackground?: boolean;+}++type PeopleSortCategory = "name" | "count";++const getPeopleSortCategory = (sortBy: PeopleSortBy): PeopleSortCategory =>+ sortBy.startsWith("name") ? "name" : "count";++const isPeopleSortAscending = (sortBy: PeopleSortBy) => sortBy.endsWith("asc");++const getPeopleSortBy = (+ category: PeopleSortCategory,+ ascending: boolean,+): PeopleSortBy => `${category}-${ascending ? "asc" : "desc"}` as PeopleSortBy;++export const PeopleSortOptions: React.FC<PeopleSortOptionsProps> = ({+ activeSortBy,+ onChangeSortBy,+ nestedInDialog,+ transparentTriggerButtonBackground,+}) => {+ const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();+ const pendingSortByRef = useRef<PeopleSortBy | undefined>(undefined);+ const ariaID = "people-sort";++ const activeCategory = getPeopleSortCategory(activeSortBy);+ const activeAscending = isPeopleSortAscending(activeSortBy);++ const handleCategoryClick = (category: PeopleSortCategory) => {+ let nextSortBy: PeopleSortBy;+ if (category === activeCategory) {+ nextSortBy = getPeopleSortBy(category, !activeAscending);+ } else {+ nextSortBy = getPeopleSortBy(category, category === "name");+ }+ pendingSortByRef.current = nextSortBy;+ setAnchorEl(undefined);+ };++ const triggerButtonSxProps: IconButtonProps["sx"] = [+ transparentTriggerButtonBackground+ ? {}+ : { backgroundColor: "fill.faint" },+ ];++ const menuPaperSxProps: PaperProps["sx"] | undefined = nestedInDialog+ ? { backgroundColor: "background.paper2" }+ : undefined;++ return (+ <>+ <IconButton+ onClick={(event) => setAnchorEl(event.currentTarget)}+ aria-controls={anchorEl ? ariaID : undefined}+ aria-haspopup="true"+ aria-expanded={anchorEl ? "true" : undefined}+ sx={triggerButtonSxProps}+ >+ <SortIcon />+ </IconButton>+ <StyledMenu+ id={ariaID}+ {...(anchorEl && { anchorEl })}+ open={!!anchorEl}+ onClose={() => setAnchorEl(undefined)}+ slotProps={{+ paper: menuPaperSxProps ? { sx: menuPaperSxProps } : {},+ list: { disablePadding: true, "aria-labelledby": ariaID },+ transition: {+ onExited: () => {+ const nextSortBy = pendingSortByRef.current;+ if (nextSortBy) {+ pendingSortByRef.current = undefined;+ onChangeSortBy(nextSortBy);+ }+ },+ },+ }}+ anchorOrigin={{ vertical: "bottom", horizontal: "right" }}+ transformOrigin={{ vertical: "top", horizontal: "right" }}+ >+ <PeopleSortCategoryOption+ category="name"+ activeCategory={activeCategory}+ activeAscending={activeAscending}+ onClick={handleCategoryClick}+ label={t("name")}+ directionLabel={+ activeAscending+ ? t("sort_asc_indicator")+ : t("sort_desc_indicator")+ }+ />+ <PeopleSortCategoryOption+ category="count"+ activeCategory={activeCategory}+ activeAscending={activeAscending}+ onClick={handleCategoryClick}+ label={t("photos")}+ />+ </StyledMenu>+ </>+ );+};++interface PeopleSortCategoryOptionProps {+ category: PeopleSortCategory;+ activeCategory: PeopleSortCategory;+ activeAscending: boolean;+ onClick: (category: PeopleSortCategory) => void;+ label: string;+ directionLabel?: string;+}++const PeopleSortCategoryOption: React.FC<PeopleSortCategoryOptionProps> = ({+ category,+ activeCategory,+ activeAscending,+ onClick,+ label,+ directionLabel,+}) => {+ const isSelected = category === activeCategory;+ const arrowIcon = activeAscending ? ArrowUp02Icon : ArrowDown02Icon;++ return (+ <StyledMenuItem onClick={() => onClick(category)}>+ <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",+ }}+ >+ {directionLabel && <Typography>β’</Typography>}+ {directionLabel && (+ <Typography sx={{ fontSize: "0.9rem" }}>+ {directionLabel}+ </Typography>+ )}+ <HugeiconsIcon+ icon={arrowIcon}+ size={19}+ color="currentColor"+ />+ </Stack>+ )}+ </Stack>+ </StyledMenuItem>+ );+};++const StyledMenu = styled(Menu)(({ theme }) => ({+ "& .MuiPaper-root": {+ backgroundColor: theme.vars.palette.background.elevatedPaper,+ minWidth: 220,+ width: 220,+ borderRadius: 12,+ boxShadow: theme.vars.palette.boxShadow.menu,+ marginTop: 6,+ },+ "& .MuiList-root": { padding: theme.spacing(1) },+}));++const StyledMenuItem = styled(MenuItem)(({ theme }) => ({+ display: "flex",+ alignItems: "center",+ gap: 12,+ padding: theme.spacing(1.5, 2),+ borderRadius: 8,+ color: theme.vars.palette.text.base,+ fontSize: 15,+ "&:hover": { backgroundColor: theme.vars.palette.fill.faintHover },+ "& .MuiListItemIcon-root": { minWidth: 0, color: "inherit" },+ "& .MuiListItemText-root": { margin: 0 },+ "& .MuiListItemText-primary": { color: "inherit", fontSize: "inherit" },+}));++export const sortPeople = (people: Person[], sortBy: PeopleSortBy): Person[] =>+ [...people].sort(personComparator(sortBy));++const personComparator =+ (sortBy: PeopleSortBy) =>+ (a: Person, b: Person): number => {+ if (a.isPinned !== b.isPinned) {+ return a.isPinned ? -1 : 1;+ }++ const sectionRankDiff = personSectionRank(a) - personSectionRank(b);++ if (sortBy.startsWith("name")) {+ const aName = a.name?.trim();+ const bName = b.name?.trim();+ if (!!aName !== !!bName) {+ return aName ? -1 : 1;+ }+ if (aName && bName) {+ const cmp = aName.localeCompare(bName, undefined, {+ sensitivity: "base",+ });+ if (cmp) {+ return sortBy === "name-asc" ? cmp : -cmp;+ }+ }+ return b.fileIDs.length - a.fileIDs.length;+ }++ if (sectionRankDiff) {+ return sectionRankDiff;+ }++ const countDiff = a.fileIDs.length - b.fileIDs.length;+ if (countDiff) {+ return sortBy === "count-asc" ? countDiff : -countDiff;+ }++ const aName = a.name?.trim();+ const bName = b.name?.trim();+ if (!!aName !== !!bName) {+ return aName ? -1 : 1;+ }+ if (aName && bName) {+ return aName.localeCompare(bName, undefined, {+ sensitivity: "base",+ });+ }+ return 0;+ };++const personSectionRank = (person: Person) =>+ person.type === "cgroup" ? 0 : 1;diff --git a/web/packages/new/photos/components/gallery/BarImpl.tsx b/web/packages/new/photos/components/gallery/BarImpl.tsxindex 52b685092e..604f92ed46 100644--- a/web/packages/new/photos/components/gallery/BarImpl.tsx+++ b/web/packages/new/photos/components/gallery/BarImpl.tsx@@ -11,6 +11,10 @@ import { Ellipsized2LineTypography } from "ente-base/components/Typography";import { useIsSmallWidth } from "ente-base/components/utils/hooks";import { CollectionsSortOptions } from "ente-new/photos/components/CollectionsSortOptions";import { StarIcon } from "ente-new/photos/components/icons/StarIcon";+import {+ PeopleSortOptions,+ type PeopleSortBy,+} from "ente-new/photos/components/PeopleSortOptions";import {BarItemTile,ItemCard,@@ -39,8 +43,8 @@ import React, {import AutoSizer from "react-virtualized-auto-sizer";import {FixedSizeList,- type ListChildComponentProps,areEqual,+ type ListChildComponentProps,} from "react-window";import { isMLSupported } from "../../services/ml";import type { GalleryBarMode } from "./reducer";@@ -88,6 +92,14 @@ export interface GalleryBarImplProps {* Called when the user changes the sorting scheme.*/onChangeCollectionsSortBy: (by: CollectionsSortBy) => void;+ /**+ * The scheme that should be used for sorting the people.+ */+ peopleSortBy: PeopleSortBy;+ /**+ * Called when the user changes the people sorting scheme.+ */+ onChangePeopleSortBy: (by: PeopleSortBy) => void;/*** The list of people that should be shown in the bar.*/@@ -112,6 +124,8 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({onShowAllPeople,collectionsSortBy,onChangeCollectionsSortBy,+ peopleSortBy,+ onChangePeopleSortBy,people,activePerson,onSelectPerson,@@ -228,12 +242,18 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({direction="row"sx={{ alignItems: "center", gap: 1, minHeight: "64px" }}>- {mode != "people" && (+ {mode != "people" ? (<CollectionsSortOptionsactiveSortBy={collectionsSortBy}onChangeSortBy={onChangeCollectionsSortBy}transparentTriggerButtonBackground/>+ ) : (+ <PeopleSortOptions+ activeSortBy={peopleSortBy}+ onChangeSortBy={onChangePeopleSortBy}+ transparentTriggerButtonBackground+ />)}<IconButtononClick={mode == "people" ? onShowAllPeople : onShowAllAlbums}@@ -248,11 +268,16 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({direction="row"sx={{ alignItems: "center", gap: 1, height: "64px" }}>- {mode != "people" && (+ {mode != "people" ? (<CollectionsSortOptionsactiveSortBy={collectionsSortBy}onChangeSortBy={onChangeCollectionsSortBy}/>+ ) : (+ <PeopleSortOptions+ activeSortBy={peopleSortBy}+ onChangeSortBy={onChangePeopleSortBy}+ />)}<FilledIconButtononClick={mode == "people" ? onShowAllPeople : onShowAllAlbums}