Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Ontology

Manav Rathi1317791fed1f
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 75a5df7017..9f44530ffa 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -11,7 +11,10 @@ import {
getLocalTrashedFiles,
} from "@/new/photos/services/files";
import { wipHasSwitchedOnceCmpAndSet } from "@/new/photos/services/ml";
-import { search, setSearchableData } from "@/new/photos/services/search";
+import {
+ filterSearchableFiles,
+ setSearchableData,
+} from "@/new/photos/services/search";
import {
SearchQuery,
SearchResultSummary,
@@ -533,7 +536,7 @@ export default function Gallery() {
let filteredFiles: EnteFile[] = [];
if (isInSearchMode) {
- filteredFiles = await search(searchQuery);
+ filteredFiles = await filterSearchableFiles(searchQuery);
} else {
filteredFiles = getUniqueFiles(
(isInHiddenSection ? hiddenFiles : files).filter((item) => {
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index b3b8e8896b..4614bfb06d 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -19,6 +19,7 @@ import type {
SearchOption,
SearchPerson,
SearchQuery,
+ SearchSuggestion,
Suggestion,
} from "./types";
import { SuggestionType } from "./types";
@@ -68,12 +69,12 @@ export const setSearchableData = (data: SearchableData) =>
void worker().then((w) => w.setSearchableData(data));
/**
- * Convert a search string into a reusable "search query" that can be passed on
- * to the {@link search} function.
+ * Convert a search string into a suggestions that can be shown in the search
+ * results, and can also be used filter the searchable files.
*
* @param searchString The string we want to search for.
*/
-export const createSearchQuery = async (searchString: string) => {
+export const suggestionsForString = async (searchString: string) => {
// Normalize it by trimming whitespace and converting to lowercase.
const s = searchString.trim().toLowerCase();
if (s.length == 0) return [];
@@ -83,7 +84,9 @@ export const createSearchQuery = async (searchString: string) => {
// the search worker, then combine the two.
const results = await Promise.all([
clipSuggestions(s, searchString).then((s) => s ?? []),
- worker().then((w) => w.createSearchQuery(s, localizedSearchData())),
+ worker().then((w) =>
+ w.suggestionsForString(s, searchString, localizedSearchData()),
+ ),
]);
return results.flat();
};
@@ -102,11 +105,11 @@ const clipSuggestions = async (s: string, searchString: string) => {
};
/**
- * Search for and return the list of {@link EnteFile}s that match the given
- * {@link search} query.
+ * Return the list of {@link EnteFile}s (from amongst the previously set
+ * {@link SearchableData}) that match the given search {@link suggestion}.
*/
-export const search = async (search: SearchQuery) =>
- worker().then((w) => w.search(search));
+export const filterSearchableFiles = async (suggestion: SearchSuggestion) =>
+ worker().then((w) => w.filterSearchableFiles(suggestion));
/**
* Cached value of {@link localizedSearchData}.
@@ -172,7 +175,7 @@ export const getAutoCompleteSuggestions =
]);
try {
const suggestions: Suggestion[] =
- await createSearchQuery(searchPhrase);
+ await suggestionsForString(searchPhrase);
return convertSuggestionsToOptions(suggestions);
} catch (e) {
log.error("getAutoCompleteSuggestions failed", e);
@@ -186,7 +189,7 @@ async function convertSuggestionsToOptions(
const previewImageAppendedOptions: SearchOption[] = [];
for (const suggestion of suggestions) {
const searchQuery = convertSuggestionToSearchQuery(suggestion);
- const resultFiles = await search(searchQuery);
+ const resultFiles = await filterSearchableFiles(searchQuery);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (searchQuery?.clip) {
resultFiles.sort((a, b) => {
diff --git a/web/packages/new/photos/services/search/worker.ts b/web/packages/new/photos/services/search/worker.ts
index 6bddf8be4a..0cd349dc9e 100644
--- a/web/packages/new/photos/services/search/worker.ts
+++ b/web/packages/new/photos/services/search/worker.ts
@@ -23,6 +23,7 @@ import type {
SearchableData,
SearchDateComponents,
SearchQuery,
+ SearchSuggestion,
Suggestion,
} from "./types";
import { SuggestionType } from "./types";
@@ -69,11 +70,16 @@ export class SearchWorker {
}
/**
- * Convert a search string into a reusable query.
+ * Convert a search string into a list of {@link SearchSuggestion}s.
*/
- createSearchQuery(s: string, localizedSearchData: LocalizedSearchData) {
- return createSearchQuery(
+ suggestionsForString(
+ s: string,
+ searchString: string,
+ localizedSearchData: LocalizedSearchData,
+ ) {
+ return suggestionsForString(
s,
+ searchString,
this.searchableData,
localizedSearchData,
this.locationTags,
@@ -82,19 +88,24 @@ export class SearchWorker {
}
/**
- * Return {@link EnteFile}s that satisfy the given {@link searchQuery}.
+ * Return {@link EnteFile}s that satisfy the given {@link suggestion}.
*/
- search(searchQuery: SearchQuery) {
+ filterSearchableFiles(suggestion: SearchSuggestion) {
return this.searchableData.files.filter((f) =>
- isMatchingFile(f, searchQuery),
+ isMatchingFile(f, suggestion),
);
}
}
expose(SearchWorker);
-const createSearchQuery = (
+/**
+ * @param s The normalized form of {@link searchString}.
+ * @param searchString The original search string.
+ */
+const suggestionsForString = (
s: string,
+ searchString: string,
{ collections, files }: SearchableData,
{ locale, holidays, labelledFileTypes }: LocalizedSearchData,
locationTags: Searchable<LocationTag>[],
@@ -105,8 +116,8 @@ const createSearchQuery = (
dateSuggestions(s, locale, holidays),
locationSuggestions(s, locationTags, cities),
collectionSuggestions(s, collections),
- fileNameSuggestions(s, files),
- fileCaptionSuggestions(s, files),
+ suggestionForFiles(fileNameMatches(s, files), searchString),
+ suggestionForFiles(fileCaptionMatches(s, files), searchString),
].flat();
const collectionSuggestions = (s: string, collections: Collection[]) =>
@@ -118,37 +129,31 @@ const collectionSuggestions = (s: string, collections: Collection[]) =>
label: name,
}));
-const fileNameSuggestions = (s: string, files: EnteFile[]) => {
+const fileNameMatches = (s: string, files: EnteFile[]) => {
// Convert the search string to a number. This allows searching a file by
// its exact (integral) ID.
const sn = Number(s) || undefined;
- const matchingFiles = files.filter(
+ return files.filter(
({ id, metadata }) =>
id === sn || metadata.title.toLowerCase().includes(s),
);
- return matchingFiles.length
+};
+
+const suggestionForFiles = (matchingFiles: EnteFile[], searchString: string) =>
+ matchingFiles.length
? {
type: SuggestionType.FILE_NAME,
value: matchingFiles.map((f) => f.id),
- label: s,
+ label: searchString,
}
: [];
-};
-const fileCaptionSuggestions = (s: string, files: EnteFile[]) => {
- const matchingFiles = files.filter((file) =>
+const fileCaptionMatches = (s: string, files: EnteFile[]) =>
+ files.filter((file) =>
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
file.pubMagicMetadata?.data?.caption?.toLowerCase().includes(s),
);
- return matchingFiles.length
- ? {
- type: SuggestionType.FILE_CAPTION,
- value: matchingFiles.map((f) => f.id),
- label: s,
- }
- : [];
-};
const dateSuggestions = (
s: string,