From 358a249c629c0bebff9cfb031eb065b3c267d6c0 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 22 Feb 2022 13:50:48 +0530 Subject: [PATCH] add search object logic --- src/components/PhotoFrame.tsx | 7 +++++++ src/components/SearchBar.tsx | 19 ++++++++++++++++++- src/services/searchService.ts | 13 +++++++++++++ src/types/gallery/index.ts | 3 ++- src/types/search/index.ts | 5 +++-- 5 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/components/PhotoFrame.tsx b/src/components/PhotoFrame.tsx index c5bb1cc31..753995e61 100644 --- a/src/components/PhotoFrame.tsx +++ b/src/components/PhotoFrame.tsx @@ -192,6 +192,13 @@ const PhotoFrame = ({ return false; } + if ( + search.thing && + search.thing.files.indexOf(item.id) === -1 + ) { + return false; + } + if (isSharedFile(item) && !isSharedCollection) { return false; } diff --git a/src/components/SearchBar.tsx b/src/components/SearchBar.tsx index 2e38bb617..3c7e4d493 100644 --- a/src/components/SearchBar.tsx +++ b/src/components/SearchBar.tsx @@ -12,6 +12,7 @@ import { searchCollection, searchFiles, searchLocation, + searchObject, } from 'services/searchService'; import { getFormattedDate } from 'utils/search'; import constants from 'utils/strings/constants'; @@ -32,7 +33,7 @@ import { FILE_TYPE } from 'constants/file'; import { GalleryContext } from 'pages/gallery'; import { AppContext } from 'pages/_app'; import { Col } from 'react-bootstrap'; -import { Person } from 'types/machineLearning'; +import { Person, Thing } from 'types/machineLearning'; import { IndexStatus } from 'types/machineLearning/ui'; import { PeopleList } from './MachineLearning/PeopleList'; @@ -202,6 +203,18 @@ export default function SearchBar(props: Props) { } as Suggestion) ) ); + const objectResults = await searchObject(searchPhrase); + + options.push( + ...objectResults.map( + (searchResult) => + ({ + type: SuggestionType.OBJECT, + value: searchResult, + label: searchResult.class, + } as Suggestion) + ) + ); return options; }; @@ -238,6 +251,10 @@ export default function SearchBar(props: Props) { props.setSearch({ person: selectedOption.value as Person }); props.setOpen(true); break; + case SuggestionType.OBJECT: + props.setSearch({ thing: selectedOption.value as Thing }); + props.setOpen(true); + break; } }; const resetSearch = () => { diff --git a/src/services/searchService.ts b/src/services/searchService.ts index ef936e2b8..5ece3c4eb 100644 --- a/src/services/searchService.ts +++ b/src/services/searchService.ts @@ -16,6 +16,8 @@ import { Suggestion, SuggestionType, } from 'types/search'; +import ObjectService from './machineLearning/objectService'; +import { Thing } from 'types/machineLearning'; const ENDPOINT = getEndpoint(); @@ -171,3 +173,14 @@ export function searchFiles(searchPhrase: string, files: EnteFile[]) { .filter(({ title }) => title.toLowerCase().includes(searchPhrase)) .slice(0, 4); } + +export async function searchObject(searchPhrase: string) { + const clusteredObjects = await ObjectService.getClusteredObjects(); + const matches = [] as Thing[]; + for (const [key, value] of clusteredObjects.entries()) { + if (key.toLocaleLowerCase().includes(searchPhrase)) { + matches.push({ class: key, files: value }); + } + } + return matches; +} diff --git a/src/types/gallery/index.ts b/src/types/gallery/index.ts index 391a57184..1d7b2f52f 100644 --- a/src/types/gallery/index.ts +++ b/src/types/gallery/index.ts @@ -1,7 +1,7 @@ import { SetDialogMessage } from 'components/MessageDialog'; import { Collection } from 'types/collection'; import { EnteFile } from 'types/file'; -import { Person } from 'types/machineLearning'; +import { Person, Thing } from 'types/machineLearning'; import { DateValue, Bbox } from 'types/search'; export type SelectedState = { @@ -19,6 +19,7 @@ export type Search = { location?: Bbox; fileIndex?: number; person?: Person; + thing?: Thing; }; export interface SearchStats { resultCount: number; diff --git a/src/types/search/index.ts b/src/types/search/index.ts index c0b2fce13..de5827316 100644 --- a/src/types/search/index.ts +++ b/src/types/search/index.ts @@ -1,4 +1,4 @@ -import { Person } from 'types/machineLearning'; +import { Person, Thing } from 'types/machineLearning'; import { IndexStatus } from 'types/machineLearning/ui'; export type Bbox = [number, number, number, number]; @@ -16,6 +16,7 @@ export enum SuggestionType { VIDEO, PERSON, INDEX_STATUS, + OBJECT, } export interface DateValue { @@ -27,6 +28,6 @@ export interface DateValue { export interface Suggestion { type: SuggestionType; label: string; - value: Bbox | DateValue | number | Person | IndexStatus; + value: Bbox | DateValue | number | Person | IndexStatus | Thing; hide?: boolean; }