π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Inline
Manav Rathi3423a7aa5d34
diff --git a/web/apps/photos/src/components/Search/SearchBar/SearchInput.tsx b/web/apps/photos/src/components/Search/SearchBar/SearchInput.tsxdeleted file mode 100644index 3fd3834a4d..0000000000--- a/web/apps/photos/src/components/Search/SearchBar/SearchInput.tsx+++ /dev/null@@ -1,379 +0,0 @@-import { FileType } from "@/media/file-type";-import { PeopleList } from "@/new/photos/components/PeopleList";-import { isMLEnabled } from "@/new/photos/services/ml";-import type {- City,- SearchDateComponents,- SearchPerson,-} from "@/new/photos/services/search/types";-import {- ClipSearchScores,- SearchOption,- SearchQuery,- Suggestion,- SuggestionType,- UpdateSearch,-} from "@/new/photos/services/search/types";-import { labelForSuggestionType } from "@/new/photos/services/search/ui";-import type { LocationTag } from "@/new/photos/services/user-entity";-import { EnteFile } from "@/new/photos/types/file";-import {- FlexWrapper,- FreeFlowText,- Row,- SpaceBetweenFlex,-} from "@ente/shared/components/Container";-import CalendarIcon from "@mui/icons-material/CalendarMonth";-import CloseIcon from "@mui/icons-material/Close";-import FolderIcon from "@mui/icons-material/Folder";-import ImageIcon from "@mui/icons-material/Image";-import LocationIcon from "@mui/icons-material/LocationOn";-import SearchIcon from "@mui/icons-material/SearchOutlined";-import {- Box,- Divider,- IconButton,- Stack,- styled,- Typography,-} from "@mui/material";-import CollectionCard from "components/Collections/CollectionCard";-import { ResultPreviewTile } from "components/Collections/styledComponents";-import { t } from "i18next";-import memoize from "memoize-one";-import pDebounce from "p-debounce";-import { AppContext } from "pages/_app";-import { useCallback, useContext, useEffect, useRef, useState } from "react";-import { components } from "react-select";-import AsyncSelect from "react-select/async";-import { SelectComponents } from "react-select/src/components";-import { InputActionMeta } from "react-select/src/types";-import {- getAutoCompleteSuggestions,- getDefaultOptions,-} from "services/searchService";-import { Collection } from "types/collection";-import { SelectStyles } from "../../../styles/search";-import { SearchInputWrapper } from "./styledComponents";--const { Option, ValueContainer, Menu } = components;--interface SearchInputProps {- isOpen: boolean;- updateSearch: UpdateSearch;- setIsOpen: (value: boolean) => void;- files: EnteFile[];- collections: Collection[];-}--const createComponents = memoize((Option, ValueContainer, Menu, Input) => ({- Option,- ValueContainer,- Menu,- Input,-}));--export const SearchInput: React.FC<SearchInputProps> = (props) => {- const selectRef = useRef(null);- const [value, setValue] = useState<SearchOption>(null);- const appContext = useContext(AppContext);- const handleChange = (value: SearchOption) => {- setValue(value);- setQuery(value?.label);-- blur();- };- const handleInputChange = (value: string, actionMeta: InputActionMeta) => {- if (actionMeta.action === "input-change") {- setQuery(value);- }- };- const [defaultOptions, setDefaultOptions] = useState([]);- const [query, setQuery] = useState("");-- useEffect(() => {- search(value);- }, [value]);-- useEffect(() => {- refreshDefaultOptions();- const t = setInterval(() => refreshDefaultOptions(), 2000);- return () => clearInterval(t);- }, []);-- async function refreshDefaultOptions() {- const defaultOptions = await getDefaultOptions();- setDefaultOptions(defaultOptions);- }-- const resetSearch = () => {- if (props.isOpen) {- appContext.startLoading();- props.updateSearch(null, null);- setTimeout(() => {- appContext.finishLoading();- }, 10);- props.setIsOpen(false);- setValue(null);- setQuery("");- }- };-- const getOptions = useCallback(- pDebounce(- getAutoCompleteSuggestions(props.files, props.collections),- 250,- ),- [props.files, props.collections],- );-- const blur = () => {- selectRef.current?.blur();- };-- const search = (selectedOption: SearchOption) => {- if (!selectedOption) {- return;- }- let search: SearchQuery;- switch (selectedOption.type) {- case SuggestionType.DATE:- search = {- date: selectedOption.value as SearchDateComponents,- };- props.setIsOpen(true);- break;- case SuggestionType.LOCATION:- search = {- location: selectedOption.value as LocationTag,- };- props.setIsOpen(true);- break;- case SuggestionType.CITY:- search = {- city: selectedOption.value as City,- };- props.setIsOpen(true);- break;- case SuggestionType.COLLECTION:- search = { collection: selectedOption.value as number };- setValue(null);- setQuery("");- break;- case SuggestionType.FILE_NAME:- search = { files: selectedOption.value as number[] };- break;- case SuggestionType.FILE_CAPTION:- search = { files: selectedOption.value as number[] };- break;- case SuggestionType.PERSON:- search = { person: selectedOption.value as SearchPerson };- break;- case SuggestionType.FILE_TYPE:- search = { fileType: selectedOption.value as FileType };- break;- case SuggestionType.CLIP:- search = { clip: selectedOption.value as ClipSearchScores };- }- props.updateSearch(search, {- optionName: selectedOption.label,- fileCount: selectedOption.fileCount,- });- };-- // TODO: HACK as AsyncSelect does not support default options reloading on focus/click- // unwanted side effect: placeholder is not shown on focus/click- // https://github.com/JedWatson/react-select/issues/1879- // for correct fix AsyncSelect can be extended to support default options reloading on focus/click- const handleOnFocus = () => {- refreshDefaultOptions();- };-- const MemoizedMenuWithPeople = useCallback(- (props) => (- <MenuWithPeople- {...props}- setValue={setValue}- selectRef={selectRef}- />- ),- [setValue, selectRef],- );-- const components = createComponents(- OptionWithInfo,- ValueContainerWithIcon,- MemoizedMenuWithPeople,- VisibleInput,- );-- return (- <SearchInputWrapper isOpen={props.isOpen}>- <AsyncSelect- ref={selectRef}- value={value}- components={components}- placeholder={<span>{t("search_hint")}</span>}- loadOptions={getOptions}- onChange={handleChange}- onFocus={handleOnFocus}- isClearable- inputValue={query}- onInputChange={handleInputChange}- escapeClearsValue- styles={SelectStyles}- defaultOptions={isMLEnabled() ? defaultOptions : null}- noOptionsMessage={() => null}- />-- {props.isOpen && (- <IconButton onClick={() => resetSearch()} sx={{ ml: 1 }}>- <CloseIcon />- </IconButton>- )}- </SearchInputWrapper>- );-};--const OptionWithInfo = (props) => (- <Option {...props}>- <LabelWithInfo data={props.data} />- </Option>-);--const LabelWithInfo = ({ data }: { data: SearchOption }) => {- return (- !data.hide && (- <>- <Box className="main" px={2} py={1}>- <Typography variant="mini" mb={1}>- {labelForSuggestionType(data.type)}- </Typography>- <SpaceBetweenFlex>- <Box mr={1}>- <FreeFlowText>- <Typography fontWeight={"bold"}>- {data.label}- </Typography>- </FreeFlowText>- <Typography color="text.muted">- {t("photos_count", { count: data.fileCount })}- </Typography>- </Box>-- <Stack direction={"row"} spacing={1}>- {data.previewFiles.map((file) => (- <CollectionCard- key={file.id}- coverFile={file}- onClick={() => null}- collectionTile={ResultPreviewTile}- />- ))}- </Stack>- </SpaceBetweenFlex>- </Box>- <Divider sx={{ mx: 2, my: 1 }} />- </>- )- );-};--const ValueContainerWithIcon: SelectComponents<- SearchOption,- false->["ValueContainer"] = (props) => (- <ValueContainer {...props}>- <FlexWrapper>- <Box- style={{ display: "inline-flex" }}- mr={1.5}- color={(theme) => theme.colors.stroke.muted}- >- {getIconByType(props.getValue()[0]?.type)}- </Box>- {props.children}- </FlexWrapper>- </ValueContainer>-);--const getIconByType = (type: SuggestionType) => {- switch (type) {- case SuggestionType.DATE:- return <CalendarIcon />;- case SuggestionType.LOCATION:- case SuggestionType.CITY:- return <LocationIcon />;- case SuggestionType.COLLECTION:- return <FolderIcon />;- case SuggestionType.FILE_NAME:- return <ImageIcon />;- default:- return <SearchIcon />;- }-};--export const MenuWithPeople = (props) => {- // log.info("props.selectProps.options: ", selectRef);- const peopleSuggestions = props.selectProps.options.filter(- (o) => o.type === SuggestionType.PERSON,- );- const people = peopleSuggestions.map((o) => o.value);-- const indexStatusSuggestion = props.selectProps.options.filter(- (o) => o.type === SuggestionType.INDEX_STATUS,- )[0] as Suggestion;-- const indexStatus = indexStatusSuggestion?.value;- return (- <Menu {...props}>- <Box my={1}>- {isMLEnabled() &&- indexStatus &&- (people && people.length > 0 ? (- <Box>- <Legend>{t("people")}</Legend>- </Box>- ) : (- <Box height={6} />- ))}-- {isMLEnabled() && indexStatus && (- <Box>- <Caption>{indexStatusSuggestion.label}</Caption>- </Box>- )}- {people && people.length > 0 && (- <Row>- <PeopleList- people={people}- maxRows={2}- onSelect={(_, index) => {- props.selectRef.current.blur();- props.setValue(peopleSuggestions[index]);- }}- />- </Row>- )}- </Box>- {props.children}- </Menu>- );-};--const Legend = styled("span")`- font-size: 20px;- color: #ddd;- display: inline;- padding: 0px 12px;-`;--const Caption = styled("span")`- font-size: 12px;- display: inline;- padding: 0px 12px;-`;--const VisibleInput = (props) => (- <components.Input {...props} isHidden={false} />-);diff --git a/web/apps/photos/src/components/Search/SearchBar/index.tsx b/web/apps/photos/src/components/Search/SearchBar/index.tsxindex fbf079c376..3d3a9cf252 100644--- a/web/apps/photos/src/components/Search/SearchBar/index.tsx+++ b/web/apps/photos/src/components/Search/SearchBar/index.tsx@@ -1,13 +1,69 @@-import { UpdateSearch } from "@/new/photos/services/search/types";+import { FileType } from "@/media/file-type";+import { PeopleList } from "@/new/photos/components/PeopleList";+import { isMLEnabled } from "@/new/photos/services/ml";+import type {+ City,+ SearchDateComponents,+ SearchPerson,+} from "@/new/photos/services/search/types";+import {+ ClipSearchScores,+ SearchOption,+ SearchQuery,+ Suggestion,+ SuggestionType,+ UpdateSearch,+} from "@/new/photos/services/search/types";+import { labelForSuggestionType } from "@/new/photos/services/search/ui";+import type { LocationTag } from "@/new/photos/services/user-entity";import { EnteFile } from "@/new/photos/types/file";-import { FluidContainer } from "@ente/shared/components/Container";+import {+ FlexWrapper,+ FluidContainer,+ FreeFlowText,+ Row,+ SpaceBetweenFlex,+} from "@ente/shared/components/Container";+import CalendarIcon from "@mui/icons-material/CalendarMonth";+import CloseIcon from "@mui/icons-material/Close";+import FolderIcon from "@mui/icons-material/Folder";+import ImageIcon from "@mui/icons-material/Image";+import LocationIcon from "@mui/icons-material/LocationOn";import SearchIcon from "@mui/icons-material/Search";-import { IconButton } from "@mui/material";+import {+ Box,+ Divider,+ IconButton,+ Stack,+ styled,+ Typography,+} from "@mui/material";+import CollectionCard from "components/Collections/CollectionCard";+import { ResultPreviewTile } from "components/Collections/styledComponents";+import { t } from "i18next";+import memoize from "memoize-one";+import pDebounce from "p-debounce";+import { AppContext } from "pages/_app";+import { useCallback, useContext, useEffect, useRef, useState } from "react";+import { components } from "react-select";+import AsyncSelect from "react-select/async";+import { SelectComponents } from "react-select/src/components";+import { InputActionMeta } from "react-select/src/types";+import {+ getAutoCompleteSuggestions,+ getDefaultOptions,+} from "services/searchService";import { Collection } from "types/collection";-import { SearchInput } from "./SearchInput";-import { SearchBarWrapper, SearchMobileBox } from "./styledComponents";+import { SelectStyles } from "../../../styles/search";+import {+ SearchBarWrapper,+ SearchInputWrapper,+ SearchMobileBox,+} from "./styledComponents";-interface Props {+const { Option, ValueContainer, Menu } = components;++interface SearchBarProps {updateSearch: UpdateSearch;collections: Collection[];files: EnteFile[];@@ -19,7 +75,7 @@ export default function SearchBar({setIsInSearchMode,isInSearchMode,...props-}: Props) {+}: SearchBarProps) {const showSearchInput = () => setIsInSearchMode(true);return (@@ -37,6 +93,326 @@ export default function SearchBar({);}+interface SearchInputProps {+ isOpen: boolean;+ updateSearch: UpdateSearch;+ setIsOpen: (value: boolean) => void;+ files: EnteFile[];+ collections: Collection[];+}++const createComponents = memoize((Option, ValueContainer, Menu, Input) => ({+ Option,+ ValueContainer,+ Menu,+ Input,+}));++export const SearchInput: React.FC<SearchInputProps> = (props) => {+ const selectRef = useRef(null);+ const [value, setValue] = useState<SearchOption>(null);+ const appContext = useContext(AppContext);+ const handleChange = (value: SearchOption) => {+ setValue(value);+ setQuery(value?.label);++ blur();+ };+ const handleInputChange = (value: string, actionMeta: InputActionMeta) => {+ if (actionMeta.action === "input-change") {+ setQuery(value);+ }+ };+ const [defaultOptions, setDefaultOptions] = useState([]);+ const [query, setQuery] = useState("");++ useEffect(() => {+ search(value);+ }, [value]);++ useEffect(() => {+ refreshDefaultOptions();+ const t = setInterval(() => refreshDefaultOptions(), 2000);+ return () => clearInterval(t);+ }, []);++ async function refreshDefaultOptions() {+ const defaultOptions = await getDefaultOptions();+ setDefaultOptions(defaultOptions);+ }++ const resetSearch = () => {+ if (props.isOpen) {+ appContext.startLoading();+ props.updateSearch(null, null);+ setTimeout(() => {+ appContext.finishLoading();+ }, 10);+ props.setIsOpen(false);+ setValue(null);+ setQuery("");+ }+ };++ const getOptions = useCallback(+ pDebounce(+ getAutoCompleteSuggestions(props.files, props.collections),+ 250,+ ),+ [props.files, props.collections],+ );++ const blur = () => {+ selectRef.current?.blur();+ };++ const search = (selectedOption: SearchOption) => {+ if (!selectedOption) {+ return;+ }+ let search: SearchQuery;+ switch (selectedOption.type) {+ case SuggestionType.DATE:+ search = {+ date: selectedOption.value as SearchDateComponents,+ };+ props.setIsOpen(true);+ break;+ case SuggestionType.LOCATION:+ search = {+ location: selectedOption.value as LocationTag,+ };+ props.setIsOpen(true);+ break;+ case SuggestionType.CITY:+ search = {+ city: selectedOption.value as City,+ };+ props.setIsOpen(true);+ break;+ case SuggestionType.COLLECTION:+ search = { collection: selectedOption.value as number };+ setValue(null);+ setQuery("");+ break;+ case SuggestionType.FILE_NAME:+ search = { files: selectedOption.value as number[] };+ break;+ case SuggestionType.FILE_CAPTION:+ search = { files: selectedOption.value as number[] };+ break;+ case SuggestionType.PERSON:+ search = { person: selectedOption.value as SearchPerson };+ break;+ case SuggestionType.FILE_TYPE:+ search = { fileType: selectedOption.value as FileType };+ break;+ case SuggestionType.CLIP:+ search = { clip: selectedOption.value as ClipSearchScores };+ }+ props.updateSearch(search, {+ optionName: selectedOption.label,+ fileCount: selectedOption.fileCount,+ });+ };++ // TODO: HACK as AsyncSelect does not support default options reloading on focus/click+ // unwanted side effect: placeholder is not shown on focus/click+ // https://github.com/JedWatson/react-select/issues/1879+ // for correct fix AsyncSelect can be extended to support default options reloading on focus/click+ const handleOnFocus = () => {+ refreshDefaultOptions();+ };++ const MemoizedMenuWithPeople = useCallback(+ (props) => (+ <MenuWithPeople+ {...props}+ setValue={setValue}+ selectRef={selectRef}+ />+ ),+ [setValue, selectRef],+ );++ const components = createComponents(+ OptionWithInfo,+ ValueContainerWithIcon,+ MemoizedMenuWithPeople,+ VisibleInput,+ );++ return (+ <SearchInputWrapper isOpen={props.isOpen}>+ <AsyncSelect+ ref={selectRef}+ value={value}+ components={components}+ placeholder={<span>{t("search_hint")}</span>}+ loadOptions={getOptions}+ onChange={handleChange}+ onFocus={handleOnFocus}+ isClearable+ inputValue={query}+ onInputChange={handleInputChange}+ escapeClearsValue+ styles={SelectStyles}+ defaultOptions={isMLEnabled() ? defaultOptions : null}+ noOptionsMessage={() => null}+ />++ {props.isOpen && (+ <IconButton onClick={() => resetSearch()} sx={{ ml: 1 }}>+ <CloseIcon />+ </IconButton>+ )}+ </SearchInputWrapper>+ );+};++const OptionWithInfo = (props) => (+ <Option {...props}>+ <LabelWithInfo data={props.data} />+ </Option>+);++const LabelWithInfo = ({ data }: { data: SearchOption }) => {+ return (+ !data.hide && (+ <>+ <Box className="main" px={2} py={1}>+ <Typography variant="mini" mb={1}>+ {labelForSuggestionType(data.type)}+ </Typography>+ <SpaceBetweenFlex>+ <Box mr={1}>+ <FreeFlowText>+ <Typography fontWeight={"bold"}>+ {data.label}+ </Typography>+ </FreeFlowText>+ <Typography color="text.muted">+ {t("photos_count", { count: data.fileCount })}+ </Typography>+ </Box>++ <Stack direction={"row"} spacing={1}>+ {data.previewFiles.map((file) => (+ <CollectionCard+ key={file.id}+ coverFile={file}+ onClick={() => null}+ collectionTile={ResultPreviewTile}+ />+ ))}+ </Stack>+ </SpaceBetweenFlex>+ </Box>+ <Divider sx={{ mx: 2, my: 1 }} />+ </>+ )+ );+};++const ValueContainerWithIcon: SelectComponents<+ SearchOption,+ false+>["ValueContainer"] = (props) => (+ <ValueContainer {...props}>+ <FlexWrapper>+ <Box+ style={{ display: "inline-flex" }}+ mr={1.5}+ color={(theme) => theme.colors.stroke.muted}+ >+ {getIconByType(props.getValue()[0]?.type)}+ </Box>+ {props.children}+ </FlexWrapper>+ </ValueContainer>+);++const getIconByType = (type: SuggestionType) => {+ switch (type) {+ case SuggestionType.DATE:+ return <CalendarIcon />;+ case SuggestionType.LOCATION:+ case SuggestionType.CITY:+ return <LocationIcon />;+ case SuggestionType.COLLECTION:+ return <FolderIcon />;+ case SuggestionType.FILE_NAME:+ return <ImageIcon />;+ default:+ return <SearchIcon />;+ }+};++export const MenuWithPeople = (props) => {+ // log.info("props.selectProps.options: ", selectRef);+ const peopleSuggestions = props.selectProps.options.filter(+ (o) => o.type === SuggestionType.PERSON,+ );+ const people = peopleSuggestions.map((o) => o.value);++ const indexStatusSuggestion = props.selectProps.options.filter(+ (o) => o.type === SuggestionType.INDEX_STATUS,+ )[0] as Suggestion;++ const indexStatus = indexStatusSuggestion?.value;+ return (+ <Menu {...props}>+ <Box my={1}>+ {isMLEnabled() &&+ indexStatus &&+ (people && people.length > 0 ? (+ <Box>+ <Legend>{t("people")}</Legend>+ </Box>+ ) : (+ <Box height={6} />+ ))}++ {isMLEnabled() && indexStatus && (+ <Box>+ <Caption>{indexStatusSuggestion.label}</Caption>+ </Box>+ )}+ {people && people.length > 0 && (+ <Row>+ <PeopleList+ people={people}+ maxRows={2}+ onSelect={(_, index) => {+ props.selectRef.current.blur();+ props.setValue(peopleSuggestions[index]);+ }}+ />+ </Row>+ )}+ </Box>+ {props.children}+ </Menu>+ );+};++const Legend = styled("span")`+ font-size: 20px;+ color: #ddd;+ display: inline;+ padding: 0px 12px;+`;++const Caption = styled("span")`+ font-size: 12px;+ display: inline;+ padding: 0px 12px;+`;++const VisibleInput = (props) => (+ <components.Input {...props} isHidden={false} />+);+function SearchBarMobile({ show, showSearchInput }) {if (!show) {return <></>;