π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
revert-refractoring
Aswin Asokefcdc1c4a0a9
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex a336a90b06..76126fb59e 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -2322,8 +2322,8 @@ const Page: React.FC = () => {submitButtonTitle={t("create")}onClose={() => {// If the user dismisses the album name dialog without- // submitting, clear any pending single-file add so that- // it doesn't leak into a future album creation.+ // submitting, clear any pending single-file add so that it+ // doesn't leak into a future album creation.pendingSingleFileAdd.current = undefined;albumNameInputVisibilityProps.onClose();}}diff --git a/web/packages/base/components/SingleInputDialog.tsx b/web/packages/base/components/SingleInputDialog.tsxindex ba32e198db..124ef8b43b 100644--- a/web/packages/base/components/SingleInputDialog.tsx+++ b/web/packages/base/components/SingleInputDialog.tsx@@ -14,7 +14,7 @@ import { t } from "i18next";import React, { useCallback } from "react";import { SingleInputForm, type SingleInputFormProps } from "./SingleInputForm";-export type SingleInputDialogProps = ModalVisibilityProps &+type SingleInputDialogProps = ModalVisibilityProps &Omit<SingleInputFormProps, "onCancel"> & {/** Title of the dialog. */title: string;diff --git a/web/packages/new/photos/components/CollectionSelector.tsx b/web/packages/new/photos/components/CollectionSelector.tsxindex 31418b81b4..979a86d726 100644--- a/web/packages/new/photos/components/CollectionSelector.tsx+++ b/web/packages/new/photos/components/CollectionSelector.tsx@@ -27,17 +27,25 @@ import {LargeTileTextOverlay,} from "ente-new/photos/components/Tiles";import { useSettingsSnapshot } from "ente-new/photos/components/utils/use-snapshot";-import type {- CollectionSummaries,- CollectionSummary,+import {+ canAddToCollection,+ canMoveToCollection,+ collectionsSortBy,+ sortCollectionSummaries,+ type CollectionsSortBy,+ type CollectionSummaries,+ type CollectionSummary,} from "ente-new/photos/services/collection-summary";+import { includes } from "ente-utils/type-guards";import { t } from "i18next";-import React, { useRef } from "react";+import React, {+ useCallback,+ useEffect,+ useMemo,+ useRef,+ useState,+} from "react";import { CollectionSelectorV2 } from "./CollectionSelectorV2";-import {- collectionSelectorTitle,- useCollectionSelector,-} from "./useCollectionSelector";export type CollectionSelectorAction =| "upload"@@ -148,35 +156,127 @@ export const CollectionSelector: React.FC<CollectionSelectorProps> = ();};-const CollectionSelectorClassic: React.FC<CollectionSelectorProps> = (- props,-) => {+const CollectionSelectorClassic: React.FC<CollectionSelectorProps> = ({+ open,+ onClose,+ onExited,+ attributes,+ collectionSummaries,+ collectionForCollectionSummaryID,+}) => {// Make the dialog fullscreen if the screen is <= the dialog's max width.const isFullScreen = useMediaQuery("(max-width: 490px)");- const {- attributes,- searchTerm,- setSearchTerm,- sortBy,- setSortBy,- filteredCollections,- searchFilteredCollections,- showCreateButton,- handleExited,- handleCollectionSummaryClick,- handleClose,- } = useCollectionSelector({- ...props,- createCollectionLabel: t("create_albums"),- });-- if (!filteredCollections.length || !attributes) return null;-- const { action, onCreateCollection } = attributes;++ const [searchTerm, setSearchTerm] = useState("");+ const [sortBy, setSortBy] =+ useCollectionSelectorSortByLocalState("name-asc");++ const [filteredCollections, setFilteredCollections] = useState<+ CollectionSummary[]+ >([]);++ const handleExited = useCallback(() => {+ setSearchTerm("");+ onExited?.();+ }, [onExited]);++ useEffect(() => {+ if (!attributes || !open) {+ return;+ }++ const activeCollectionID = attributes.activeCollectionID;+ const filteredCollections = [...collectionSummaries.values()].filter(+ (cs) => {+ if (cs.id === attributes.sourceCollectionSummaryID) {+ return false;+ } else if (+ attributes.action == "add" ||+ attributes.action == "move"+ ) {+ return canAddToCollection(cs) && cs.type != "userFavorites";+ } else if (attributes.action == "upload") {+ return (+ (canAddToCollection(cs) ||+ cs.type == "uncategorized") &&+ cs.type != "userFavorites"+ );+ } else if (attributes.action == "restore") {+ return (+ (canMoveToCollection(cs) ||+ cs.type == "uncategorized") &&+ cs.type != "userFavorites"+ );+ } else {+ // "unhide"+ return (+ canMoveToCollection(cs) && cs.type != "userFavorites"+ );+ }+ },+ );++ const collections = sortCollectionSummaries(filteredCollections, sortBy)+ .sort((a, b) => b.sortPriority - a.sortPriority)+ .sort((a, b) => {+ // Prioritize the active collection (if any) to appear first.+ if (a.id === activeCollectionID) return -1;+ if (b.id === activeCollectionID) return 1;+ return 0;+ });++ if (collections.length === 0) {+ onClose();+ attributes.onCreateCollection();+ handleExited();+ }++ setFilteredCollections(collections);+ }, [collectionSummaries, attributes, open, onClose, sortBy, handleExited]);++ const searchFilteredCollections = useMemo(() => {+ if (!searchTerm.trim()) {+ return filteredCollections;+ }+ const searchLower = searchTerm.toLowerCase();+ return filteredCollections.filter((cs) =>+ cs.name.toLowerCase().includes(searchLower),+ );+ }, [filteredCollections, searchTerm]);++ const showCreateButton = useMemo(() => {+ if (!searchTerm.trim()) {+ return true;+ }+ const searchLower = searchTerm.toLowerCase();+ const createText = t("create_albums").toLowerCase();+ return createText.includes(searchLower);+ }, [searchTerm]);++ if (!filteredCollections.length) {+ return <></>;+ }++ if (!attributes) {+ return <></>;+ }++ const { action, onSelectCollection, onCancel, onCreateCollection } =+ attributes;++ const handleCollectionSummaryClick = async (id: number) => {+ onSelectCollection(await collectionForCollectionSummaryID(id));+ onClose();+ };++ const handleClose = () => {+ onCancel?.();+ onClose();+ };return (<StyledDialog- open={props.open}+ open={open}onClose={handleClose}fullWidthfullScreen={isFullScreen}@@ -196,7 +296,7 @@ const CollectionSelectorClassic: React.FC<CollectionSelectorProps> = (<Stack sx={{ flex: 1 }}><Box><Typography variant="h5">- {collectionSelectorTitle(action)}+ {titleForAction(action)}</Typography><Typographyvariant="small"@@ -275,6 +375,21 @@ const NoResultsContent = styled(DialogContent)`min-height: 154px;`;+const titleForAction = (action: CollectionSelectorAction) => {+ switch (action) {+ case "upload":+ return t("upload_to_album");+ case "add":+ return t("add_to_album");+ case "move":+ return t("move_to_album");+ case "restore":+ return t("restore_to_album");+ case "unhide":+ return t("unhide_to_album");+ }+};+interface CollectionSummaryButtonProps {collectionSummary: CollectionSummary;onClick: (collectionSummaryID: number) => void;@@ -411,3 +526,27 @@ const SearchField: React.FC<SearchFieldProps> = ({ value, onChange }) => {/>);};++/**+ * A hook that maintains the collection selector sort order both as in-memory+ * and local storage state.+ */+const useCollectionSelectorSortByLocalState = (+ initialValue: CollectionsSortBy,+) => {+ const key = "collectionSelectorSortBy";++ const [value, setValue] = useState(initialValue);++ useEffect(() => {+ const value = localStorage.getItem(key);+ if (value && includes(collectionsSortBy, value)) setValue(value);+ }, []);++ const setter = (value: CollectionsSortBy) => {+ localStorage.setItem(key, value);+ setValue(value);+ };++ return [value, setter] as const;+};diff --git a/web/packages/new/photos/components/CollectionSelectorV2.tsx b/web/packages/new/photos/components/CollectionSelectorV2.tsxindex 716e0d944b..79b61ac5d2 100644--- a/web/packages/new/photos/components/CollectionSelectorV2.tsx+++ b/web/packages/new/photos/components/CollectionSelectorV2.tsx@@ -18,14 +18,21 @@ import {} from "@mui/material";import { CollectionsSortOptions } from "ente-new/photos/components/CollectionsSortOptions";import { BaseTileButton, ItemCard } from "ente-new/photos/components/Tiles";-import type { CollectionSummary } from "ente-new/photos/services/collection-summary";-import { t } from "i18next";-import React from "react";-import type { CollectionSelectorProps } from "./CollectionSelector";import {- collectionSelectorTitle,- useCollectionSelector,-} from "./useCollectionSelector";+ canAddToCollection,+ canMoveToCollection,+ collectionsSortBy,+ sortCollectionSummaries,+ type CollectionsSortBy,+ type CollectionSummary,+} from "ente-new/photos/services/collection-summary";+import { includes } from "ente-utils/type-guards";+import { t } from "i18next";+import React, { useCallback, useEffect, useMemo, useState } from "react";+import type {+ CollectionSelectorAction,+ CollectionSelectorProps,+} from "./CollectionSelector";/*** The internal-only ("v2") variant of {@link CollectionSelector}, restyled to@@ -39,32 +46,124 @@ import {export const CollectionSelectorV2: React.FC<CollectionSelectorProps> = (props,) => {- // Make the dialog fullscreen if the screen is <= the dialog's max width.- const isFullScreen = useMediaQuery("(max-width: 490px)");const {+ open,+ onClose,+ onExited,attributes,- searchTerm,- setSearchTerm,- sortBy,- setSortBy,- filteredCollections,- searchFilteredCollections,- showCreateButton,- handleExited,- handleCollectionSummaryClick,- handleClose,- } = useCollectionSelector({- ...props,- createCollectionLabel: t("new_album"),- });+ collectionSummaries,+ collectionForCollectionSummaryID,+ } = props;++ // Make the dialog fullscreen if the screen is <= the dialog's max width.+ const isFullScreen = useMediaQuery("(max-width: 490px)");++ const [searchTerm, setSearchTerm] = useState("");+ const [sortBy, setSortBy] =+ useCollectionSelectorSortByLocalState("name-asc");+ const [filteredCollections, setFilteredCollections] = useState<+ CollectionSummary[]+ >([]);++ const handleExited = useCallback(() => {+ setSearchTerm("");+ onExited?.();+ }, [onExited]);++ useEffect(() => {+ if (!attributes || !open) return;++ const activeCollectionID = attributes.activeCollectionID;+ const selectableCollections = [...collectionSummaries.values()].filter(+ (collectionSummary) => {+ if (+ collectionSummary.id ===+ attributes.sourceCollectionSummaryID+ ) {+ return false;+ }++ const isUserFavorites =+ collectionSummary.type === "userFavorites";+ if (+ attributes.action === "add" ||+ attributes.action === "move"+ ) {+ return (+ canAddToCollection(collectionSummary) &&+ !isUserFavorites+ );+ }+ if (attributes.action === "upload") {+ return (+ (canAddToCollection(collectionSummary) ||+ collectionSummary.type === "uncategorized") &&+ !isUserFavorites+ );+ }+ if (attributes.action === "restore") {+ return (+ (canMoveToCollection(collectionSummary) ||+ collectionSummary.type === "uncategorized") &&+ !isUserFavorites+ );+ }+ return (+ canMoveToCollection(collectionSummary) && !isUserFavorites+ );+ },+ );++ const collections = sortCollectionSummaries(+ selectableCollections,+ sortBy,+ )+ .sort((a, b) => b.sortPriority - a.sortPriority)+ .sort((a, b) => {+ if (a.id === activeCollectionID) return -1;+ if (b.id === activeCollectionID) return 1;+ return 0;+ });++ if (collections.length === 0) {+ onClose();+ attributes.onCreateCollection();+ handleExited();+ }++ setFilteredCollections(collections);+ }, [collectionSummaries, attributes, open, onClose, sortBy, handleExited]);++ const searchFilteredCollections = useMemo(() => {+ if (!searchTerm.trim()) return filteredCollections;+ const searchLower = searchTerm.toLowerCase();+ return filteredCollections.filter((collectionSummary) =>+ collectionSummary.name.toLowerCase().includes(searchLower),+ );+ }, [filteredCollections, searchTerm]);++ const showCreateButton =+ !searchTerm.trim() ||+ t("new_album").toLowerCase().includes(searchTerm.toLowerCase());if (!filteredCollections.length || !attributes) return null;- const { action, onCreateCollection } = attributes;+ const { action, onCancel, onCreateCollection, onSelectCollection } =+ attributes;++ const handleCollectionSummaryClick = async (id: number) => {+ onSelectCollection(await collectionForCollectionSummaryID(id));+ onClose();+ };++ const handleClose = () => {+ onCancel?.();+ onClose();+ };return (<Dialog- open={props.open}+ open={open}onClose={handleClose}fullScreen={isFullScreen}maxWidth={false}@@ -130,6 +229,42 @@ export const CollectionSelectorV2: React.FC<CollectionSelectorProps> = ();};+const collectionSelectorTitle = (action: CollectionSelectorAction) => {+ switch (action) {+ case "upload":+ return t("upload_to_album");+ case "add":+ return t("add_to_album");+ case "move":+ return t("move_to_album");+ case "restore":+ return t("restore_to_album");+ case "unhide":+ return t("unhide_to_album");+ }+};++const useCollectionSelectorSortByLocalState = (+ initialValue: CollectionsSortBy,+) => {+ const key = "collectionSelectorSortBy";+ const [value, setValue] = useState(initialValue);++ useEffect(() => {+ const storedValue = localStorage.getItem(key);+ if (storedValue && includes(collectionsSortBy, storedValue)) {+ setValue(storedValue);+ }+ }, []);++ const setter = (newValue: CollectionsSortBy) => {+ localStorage.setItem(key, newValue);+ setValue(newValue);+ };++ return [value, setter] as const;+};+const surfaceStroke = "#e0e0e0";const surfaceStrokeDark = "rgba(255 255 255 / 0.12)";diff --git a/web/packages/new/photos/components/CollectionsSortOptions.tsx b/web/packages/new/photos/components/CollectionsSortOptions.tsxindex e5b7e34238..90576bec62 100644--- a/web/packages/new/photos/components/CollectionsSortOptions.tsx+++ b/web/packages/new/photos/components/CollectionsSortOptions.tsx@@ -117,7 +117,7 @@ export const CollectionsSortOptions: React.FC<CollectionsSortOptionsProps> = ({aria-controls={anchorEl ? ariaID : undefined}aria-haspopup="true"aria-expanded={anchorEl ? "true" : undefined}- aria-label={t("sort_by")}+ aria-label={isV2 ? t("sort_by") : undefined}sx={triggerButtonSxProps}><SortIcon sx={isV2 ? { fontSize: 20 } : undefined} />diff --git a/web/packages/new/photos/components/useCollectionSelector.ts b/web/packages/new/photos/components/useCollectionSelector.tsdeleted file mode 100644index 3aa709fbed..0000000000--- a/web/packages/new/photos/components/useCollectionSelector.ts+++ /dev/null@@ -1,180 +0,0 @@-import {- canAddToCollection,- canMoveToCollection,- collectionsSortBy,- sortCollectionSummaries,- type CollectionsSortBy,- type CollectionSummary,-} from "ente-new/photos/services/collection-summary";-import { includes } from "ente-utils/type-guards";-import { t } from "i18next";-import { useCallback, useEffect, useMemo, useState } from "react";-import type {- CollectionSelectorAction,- CollectionSelectorProps,-} from "./CollectionSelector";--type UseCollectionSelectorProps = CollectionSelectorProps & {- createCollectionLabel: string;-};--export const useCollectionSelector = ({- open,- onClose,- onExited,- attributes,- collectionSummaries,- collectionForCollectionSummaryID,- createCollectionLabel,-}: UseCollectionSelectorProps) => {- const [searchTerm, setSearchTerm] = useState("");- const [sortBy, setSortBy] =- useCollectionSelectorSortByLocalState("name-asc");- const [filteredCollections, setFilteredCollections] = useState<- CollectionSummary[]- >([]);-- const handleExited = useCallback(() => {- setSearchTerm("");- onExited?.();- }, [onExited]);-- useEffect(() => {- if (!attributes || !open) return;-- const activeCollectionID = attributes.activeCollectionID;- const selectableCollections = [...collectionSummaries.values()].filter(- (collectionSummary) => {- if (- collectionSummary.id ===- attributes.sourceCollectionSummaryID- ) {- return false;- }-- const isUserFavorites =- collectionSummary.type === "userFavorites";- if (- attributes.action === "add" ||- attributes.action === "move"- ) {- return (- canAddToCollection(collectionSummary) &&- !isUserFavorites- );- }- if (attributes.action === "upload") {- return (- (canAddToCollection(collectionSummary) ||- collectionSummary.type === "uncategorized") &&- !isUserFavorites- );- }- if (attributes.action === "restore") {- return (- (canMoveToCollection(collectionSummary) ||- collectionSummary.type === "uncategorized") &&- !isUserFavorites- );- }- return (- canMoveToCollection(collectionSummary) && !isUserFavorites- );- },- );-- const collections = sortCollectionSummaries(- selectableCollections,- sortBy,- )- .sort((a, b) => b.sortPriority - a.sortPriority)- .sort((a, b) => {- if (a.id === activeCollectionID) return -1;- if (b.id === activeCollectionID) return 1;- return 0;- });-- if (collections.length === 0) {- onClose();- attributes.onCreateCollection();- handleExited();- }-- setFilteredCollections(collections);- }, [collectionSummaries, attributes, open, onClose, sortBy, handleExited]);-- const searchFilteredCollections = useMemo(() => {- if (!searchTerm.trim()) return filteredCollections;- const searchLower = searchTerm.toLowerCase();- return filteredCollections.filter((collectionSummary) =>- collectionSummary.name.toLowerCase().includes(searchLower),- );- }, [filteredCollections, searchTerm]);-- const showCreateButton =- !searchTerm.trim() ||- createCollectionLabel.toLowerCase().includes(searchTerm.toLowerCase());-- const handleCollectionSummaryClick = async (id: number) => {- if (!attributes) return;- attributes.onSelectCollection(- await collectionForCollectionSummaryID(id),- );- onClose();- };-- const handleClose = () => {- attributes?.onCancel?.();- onClose();- };-- return {- attributes,- searchTerm,- setSearchTerm,- sortBy,- setSortBy,- filteredCollections,- searchFilteredCollections,- showCreateButton,- handleExited,- handleCollectionSummaryClick,- handleClose,- };-};--export const collectionSelectorTitle = (action: CollectionSelectorAction) => {- switch (action) {- case "upload":- return t("upload_to_album");- case "add":- return t("add_to_album");- case "move":- return t("move_to_album");- case "restore":- return t("restore_to_album");- case "unhide":- return t("unhide_to_album");- }-};--const useCollectionSelectorSortByLocalState = (- initialValue: CollectionsSortBy,-) => {- const key = "collectionSelectorSortBy";- const [value, setValue] = useState(initialValue);-- useEffect(() => {- const storedValue = localStorage.getItem(key);- if (storedValue && includes(collectionsSortBy, storedValue)) {- setValue(storedValue);- }- }, []);-- const setter = (newValue: CollectionsSortBy) => {- localStorage.setItem(key, newValue);- setValue(newValue);- };-- return [value, setter] as const;-};