π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[web][photos] Share sort menus using ContextMenu
AswinAsok1285e3412597
diff --git a/web/apps/photos/src/components/CollectionSelector.tsx b/web/apps/photos/src/components/CollectionSelector.tsxindex 3986b42d1f..64e03e4b35 100644--- a/web/apps/photos/src/components/CollectionSelector.tsx+++ b/web/apps/photos/src/components/CollectionSelector.tsx@@ -223,7 +223,6 @@ export const CollectionSelector: React.FC<CollectionSelectorProps> = (<CollectionsSortOptionsactiveSortBy={sortBy}onChangeSortBy={setSortBy}- nestedInDialogvariant="v2"/><IconButtondiff --git a/web/apps/photos/src/components/CollectionsSortOptions.tsx b/web/apps/photos/src/components/CollectionsSortOptions.tsxindex 8a0713ccd3..8fa186b9cd 100644--- a/web/apps/photos/src/components/CollectionsSortOptions.tsx+++ b/web/apps/photos/src/components/CollectionsSortOptions.tsx@@ -1,27 +1,14 @@-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,- type Theme,-} from "@mui/material";-import Menu, { type MenuProps } from "@mui/material/Menu";+ SortOptions,+ type SortOptionsAppearanceProps,+} from "@/components/SortOptions";import type { CollectionsSortBy } from "ente-new/photos/services/collection-summary";import { t } from "i18next";-import React, { useRef, useState } from "react";+import React from "react";-interface CollectionsSortOptionsProps {+interface CollectionsSortOptionsProps extends SortOptionsAppearanceProps {activeSortBy: CollectionsSortBy;onChangeSortBy: (by: CollectionsSortBy) => void;- nestedInDialog?: boolean;- transparentTriggerButtonBackground?: boolean;- variant?: "default" | "v2";}type SortCategory = "name" | "creation-time" | "updation-time";@@ -32,244 +19,41 @@ const getSortCategory = (sortBy: CollectionsSortBy): SortCategory => {return "updation-time";};-const isAscending = (sortBy: CollectionsSortBy): boolean =>- sortBy.endsWith("-asc");--const getSortBy = (- category: SortCategory,- ascending: boolean,-): CollectionsSortBy => `${category}-${ascending ? "asc" : "desc"}`;-export const CollectionsSortOptions: React.FC<CollectionsSortOptionsProps> = ({activeSortBy,onChangeSortBy,- nestedInDialog,- transparentTriggerButtonBackground,- variant = "default",-}) => {- const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();- // Apply sort changes after the menu closes to avoid flicker.- const pendingSortByRef = useRef<CollectionsSortBy | undefined>(undefined);- const ariaID = "collection-sort";-- const activeCategory = getSortCategory(activeSortBy);- const activeAscending = isAscending(activeSortBy);-- const handleCategoryClick = (category: SortCategory) => {- let nextSortBy: CollectionsSortBy;- if (category === activeCategory) {- nextSortBy = getSortBy(category, !activeAscending);- } else {- // Names default to ascending; dates default to newest first.- const defaultAscending = category === "name";- nextSortBy = getSortBy(category, defaultAscending);+ ...appearanceProps+}) => (+ <SortOptions<SortCategory>+ {...appearanceProps}+ id="collection-sort"+ activeCategory={getSortCategory(activeSortBy)}+ activeAscending={activeSortBy.endsWith("-asc")}+ onChange={(category, ascending) =>+ onChangeSortBy(`${category}-${ascending ? "asc" : "desc"}`)}- pendingSortByRef.current = nextSortBy;- setAnchorEl(undefined);- };-- const isV2 = variant === "v2";-- const triggerButtonSxProps: IconButtonProps["sx"] = isV2- ? v2TriggerButtonSx- : [- transparentTriggerButtonBackground- ? {}- : { backgroundColor: "fill.faint" },- ];-- const menuPaperSxProps: PaperProps["sx"] | undefined =- !isV2 && 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}- aria-label={isV2 ? t("sort_by") : undefined}- sx={triggerButtonSxProps}- >- <SortIcon sx={isV2 ? { fontSize: 20 } : undefined} />- </IconButton>- <StyledMenu- id={ariaID}- sx={isV2 ? v2MenuSx : undefined}- {...(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" }}- >- <SortCategoryOption- category="name"- activeCategory={activeCategory}- activeAscending={activeAscending}- onClick={handleCategoryClick}- label={t("name")}- ascLabel={t("sort_asc_indicator")}- descLabel={t("sort_desc_indicator")}- />- <SortCategoryOption- category="creation-time"- activeCategory={activeCategory}- activeAscending={activeAscending}- onClick={handleCategoryClick}- label={t("created")}- ascLabel={t("oldest")}- descLabel={t("newest")}- />- <SortCategoryOption- category="updation-time"- activeCategory={activeCategory}- activeAscending={activeAscending}- onClick={handleCategoryClick}- label={t("updated")}- ascLabel={t("oldest")}- descLabel={t("newest")}- />- </StyledMenu>- </>- );-};--interface SortCategoryOptionProps {- category: SortCategory;- activeCategory: SortCategory;- activeAscending: boolean;- onClick: (category: SortCategory) => void;- label: string;- ascLabel: string;- descLabel: string;-}--const SortCategoryOption: React.FC<SortCategoryOptionProps> = ({- category,- activeCategory,- activeAscending,- onClick,- label,- ascLabel,- descLabel,-}) => {- const isSelected = category === activeCategory;- const directionLabel = activeAscending ? ascLabel : descLabel;- 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",- }}- >- <Typography>β’</Typography>- <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" },-}));--const v2TriggerButtonSx = (theme: Theme) => ({- width: 38,- height: 38,- p: 0,- color: "text.base",- backgroundColor: "background.paper",- "&:hover": { backgroundColor: "fill.faintHover" },- ...theme.applyStyles("dark", {- backgroundColor: "rgba(255 255 255 / 0.12)",- }),-});--const v2MenuSx = (theme: Theme) => ({- "& .MuiPaper-root": {- width: 238,- minWidth: 238,- border: "1px solid #ececec",- borderRadius: "16px",- backgroundColor: "background.paper",- boxShadow: "0 4px 4px rgba(0 0 0 / 0.16)",- ...theme.applyStyles("dark", {- borderColor: "rgba(255 255 255 / 0.12)",- backgroundColor: "#282828",- boxShadow: "0 4px 4px rgba(0 0 0 / 0.40)",- }),- },- "& .MuiMenuItem-root": {- minHeight: 44,- height: 44,- boxSizing: "border-box",- py: "12px",- px: "16px",- },- "& .MuiTypography-root": {- fontSize: 14,- lineHeight: "20px",- fontWeight: 500,- },-});+ options={[+ {+ category: "name",+ label: t("name"),+ defaultAscending: true,+ ascendingLabel: t("sort_asc_indicator"),+ descendingLabel: t("sort_desc_indicator"),+ },+ {+ category: "creation-time",+ label: t("created"),+ defaultAscending: false,+ ascendingLabel: t("oldest"),+ descendingLabel: t("newest"),+ },+ {+ category: "updation-time",+ label: t("updated"),+ defaultAscending: false,+ ascendingLabel: t("oldest"),+ descendingLabel: t("newest"),+ },+ ]}+ />+);diff --git a/web/apps/photos/src/components/PeopleSortOptions.tsx b/web/apps/photos/src/components/PeopleSortOptions.tsxindex ca6242a0e7..abed2b3946 100644--- a/web/apps/photos/src/components/PeopleSortOptions.tsx+++ b/web/apps/photos/src/components/PeopleSortOptions.tsx@@ -1,261 +1,40 @@-import type { PeopleSortBy } from "@/utils/people-sort";-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,- type Theme,-} from "@mui/material";-import Menu, { type MenuProps } from "@mui/material/Menu";+ SortOptions,+ type SortOptionsAppearanceProps,+} from "@/components/SortOptions";+import type { PeopleSortBy } from "@/utils/people-sort";import { t } from "i18next";-import React, { useRef, useState } from "react";+import React from "react";-interface PeopleSortOptionsProps {+interface PeopleSortOptionsProps extends SortOptionsAppearanceProps {activeSortBy: PeopleSortBy;onChangeSortBy: (by: PeopleSortBy) => void;- variant?: "default" | "v2";- 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"}`;-export const PeopleSortOptions: React.FC<PeopleSortOptionsProps> = ({activeSortBy,onChangeSortBy,- variant = "default",- nestedInDialog,- transparentTriggerButtonBackground,-}) => {- const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();-- // Apply sort changes after the menu closes to avoid flicker.- 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");+ ...appearanceProps+}) => (+ <SortOptions<PeopleSortCategory>+ {...appearanceProps}+ id="people-sort"+ activeCategory={activeSortBy.startsWith("name") ? "name" : "count"}+ activeAscending={activeSortBy.endsWith("asc")}+ onChange={(category, ascending) =>+ onChangeSortBy(`${category}-${ascending ? "asc" : "desc"}`)}- pendingSortByRef.current = nextSortBy;- setAnchorEl(undefined);- };-- const isV2 = variant === "v2";-- const triggerButtonSxProps: IconButtonProps["sx"] = isV2- ? v2TriggerButtonSx- : [- transparentTriggerButtonBackground- ? {}- : { backgroundColor: "fill.faint" },- ];-- const menuPaperSxProps: PaperProps["sx"] | undefined =- !isV2 && 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}- aria-label={isV2 ? t("sort_by") : undefined}- sx={triggerButtonSxProps}- >- <SortIcon sx={isV2 ? { fontSize: 20 } : undefined} />- </IconButton>- <StyledMenu- id={ariaID}- sx={isV2 ? v2MenuSx : undefined}- {...(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" },-}));--const v2TriggerButtonSx = (theme: Theme) => ({- width: 38,- height: 38,- p: 0,- color: "text.base",- backgroundColor: "background.paper",- "&:hover": { backgroundColor: "fill.faintHover" },- ...theme.applyStyles("dark", {- backgroundColor: "rgba(255 255 255 / 0.12)",- }),-});--const v2MenuSx = (theme: Theme) => ({- "& .MuiPaper-root": {- width: 238,- minWidth: 238,- border: "1px solid #ececec",- borderRadius: "16px",- backgroundColor: "background.paper",- boxShadow: "0 4px 4px rgba(0 0 0 / 0.16)",- ...theme.applyStyles("dark", {- borderColor: "rgba(255 255 255 / 0.12)",- backgroundColor: "#282828",- boxShadow: "0 4px 4px rgba(0 0 0 / 0.40)",- }),- },- "& .MuiMenuItem-root": {- minHeight: 44,- height: 44,- boxSizing: "border-box",- py: "12px",- px: "16px",- },- "& .MuiTypography-root": {- fontSize: 14,- lineHeight: "20px",- fontWeight: 500,- },-});+ options={[+ {+ category: "name",+ label: t("name"),+ defaultAscending: true,+ ascendingLabel: t("sort_asc_indicator"),+ descendingLabel: t("sort_desc_indicator"),+ },+ { category: "count", label: t("photos"), defaultAscending: false },+ ]}+ />+);diff --git a/web/apps/photos/src/components/SortOptions.tsx b/web/apps/photos/src/components/SortOptions.tsxnew file mode 100644index 0000000000..cd867767b7--- /dev/null+++ b/web/apps/photos/src/components/SortOptions.tsx@@ -0,0 +1,179 @@+import { ContextMenu, ContextMenuItem } from "@/components/ContextMenu";+import { ArrowDown02Icon, ArrowUp02Icon } from "@hugeicons/core-free-icons";+import { HugeiconsIcon } from "@hugeicons/react";+import SortIcon from "@mui/icons-material/Sort";+import {+ IconButton,+ Stack,+ type IconButtonProps,+ type Theme,+} from "@mui/material";+import type { MenuProps } from "@mui/material/Menu";+import { t } from "i18next";+import React, { useRef, useState } from "react";++export interface SortOptionsAppearanceProps {+ variant?: "default" | "v2";+ transparentTriggerButtonBackground?: boolean;+}++interface SortOption<Category extends string> {+ category: Category;+ label: string;+ defaultAscending: boolean;+ ascendingLabel?: string;+ descendingLabel?: string;+}++interface SortOptionsProps<+ Category extends string,+> extends SortOptionsAppearanceProps {+ id: string;+ activeCategory: Category;+ activeAscending: boolean;+ options: SortOption<Category>[];+ onChange: (category: Category, ascending: boolean) => void;+}++/** Shared presentation and close-before-sorting behavior for Photos sort menus. */+export function SortOptions<Category extends string>({+ id: ariaID,+ activeCategory,+ activeAscending,+ options,+ onChange,+ variant = "default",+ transparentTriggerButtonBackground,+}: SortOptionsProps<Category>) {+ const [anchorEl, setAnchorEl] = useState<MenuProps["anchorEl"]>();+ // Apply sort changes after the menu closes to avoid flicker.+ const pendingSortRef = useRef<+ { category: Category; ascending: boolean } | undefined+ >(undefined);++ const handleOptionClick = (option: SortOption<Category>) => {+ pendingSortRef.current = {+ category: option.category,+ ascending:+ option.category === activeCategory+ ? !activeAscending+ : option.defaultAscending,+ };+ setAnchorEl(undefined);+ };++ const isV2 = variant === "v2";++ const triggerButtonSxProps: IconButtonProps["sx"] = isV2+ ? v2TriggerButtonSx+ : [+ transparentTriggerButtonBackground+ ? {}+ : { backgroundColor: "fill.faint" },+ ];++ return (+ <>+ <IconButton+ onClick={(event) => setAnchorEl(event.currentTarget)}+ aria-controls={anchorEl ? ariaID : undefined}+ aria-haspopup="true"+ aria-expanded={anchorEl ? "true" : undefined}+ aria-label={isV2 ? t("sort_by") : undefined}+ sx={triggerButtonSxProps}+ >+ <SortIcon sx={isV2 ? { fontSize: 20 } : undefined} />+ </IconButton>+ <ContextMenu+ id={ariaID}+ {...(anchorEl && { anchorEl })}+ open={!!anchorEl}+ onClose={() => setAnchorEl(undefined)}+ slotProps={{+ list: { "aria-labelledby": ariaID },+ transition: {+ onExited: () => {+ const nextSort = pendingSortRef.current;+ if (nextSort) {+ pendingSortRef.current = undefined;+ onChange(nextSort.category, nextSort.ascending);+ }+ },+ },+ }}+ anchorOrigin={{ vertical: "bottom", horizontal: "right" }}+ transformOrigin={{ vertical: "top", horizontal: "right" }}+ >+ {options.map((option) => (+ <SortCategoryOption+ key={option.category}+ isSelected={option.category === activeCategory}+ activeAscending={activeAscending}+ onClick={() => handleOptionClick(option)}+ label={option.label}+ directionLabel={+ activeAscending+ ? option.ascendingLabel+ : option.descendingLabel+ }+ />+ ))}+ </ContextMenu>+ </>+ );+}++interface SortCategoryOptionProps {+ isSelected: boolean;+ activeAscending: boolean;+ onClick: () => void;+ label: string;+ directionLabel?: string;+}++const SortCategoryOption: React.FC<SortCategoryOptionProps> = ({+ isSelected,+ activeAscending,+ onClick,+ label,+ directionLabel,+}) => {+ const arrowIcon = activeAscending ? ArrowUp02Icon : ArrowDown02Icon;++ return (+ <ContextMenuItem onClick={onClick}>+ {label}+ {isSelected && (+ <Stack+ component="span"+ direction="row"+ sx={{+ alignItems: "center",+ gap: 0.75,+ color: "text.muted",+ }}+ >+ {directionLabel && <span>β’</span>}+ {directionLabel && <span>{directionLabel}</span>}+ <HugeiconsIcon+ icon={arrowIcon}+ size={19}+ color="currentColor"+ />+ </Stack>+ )}+ </ContextMenuItem>+ );+};++const v2TriggerButtonSx = (theme: Theme) => ({+ width: 38,+ height: 38,+ p: 0,+ color: "text.base",+ backgroundColor: "background.paper",+ "&:hover": { backgroundColor: "fill.faintHover" },+ ...theme.applyStyles("dark", {+ backgroundColor: "rgba(255 255 255 / 0.12)",+ }),+});