Skip to content

armgit / ente

Public Imported from GitHub
0

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

Gallery fix 2

Manav Rathi5aa967103778
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 2b02c1e5f2..2fc5e447a4 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -206,7 +206,6 @@ export default function Gallery() {
const [collectionNamerAttributes, setCollectionNamerAttributes] =
useState<CollectionNamerAttributes>(null);
const [collectionNamerView, setCollectionNamerView] = useState(false);
- const [searchQuery, setSearchQuery] = useState<SearchQuery>(null);
const [shouldDisableDropzone, setShouldDisableDropzone] = useState(false);
const [isPhotoSwipeOpen, setIsPhotoSwipeOpen] = useState(false);
// TODO(MR): This is never true currently, this is the WIP ability to show
@@ -249,7 +248,9 @@ export default function Gallery() {
accept: ".zip",
});
+ // If we're in "search mode". See: [Note: "search mode"].
const [isInSearchMode, setIsInSearchMode] = useState(false);
+ // The option selected by the user selected from the search bar dropdown.
const [selectedSearchOption, setSelectedSearchOption] = useState<
SearchOption | undefined
>();
@@ -536,8 +537,10 @@ export default function Gallery() {
}
let filteredFiles: EnteFile[] = [];
- if (isInSearchMode) {
- filteredFiles = await filterSearchableFiles(searchQuery);
+ if (selectedSearchOption) {
+ filteredFiles = await filterSearchableFiles(
+ selectedSearchOption.suggestion,
+ );
} else {
filteredFiles = getUniqueFiles(
(isInHiddenSection ? hiddenFiles : files).filter((item) => {
@@ -597,11 +600,6 @@ export default function Gallery() {
}),
);
}
- if (searchQuery?.clip) {
- return filteredFiles.sort((a, b) => {
- return searchQuery.clip.get(b.id) - searchQuery.clip.get(a.id);
- });
- }
const sortAsc = activeCollection?.pubMagicMetadata?.data?.asc ?? false;
if (sortAsc) {
return sortFiles(filteredFiles, true);
@@ -615,7 +613,8 @@ export default function Gallery() {
tempDeletedFileIds,
tempHiddenFileIds,
hiddenFileIds,
- searchQuery,
+ isInSearchMode,
+ selectedSearchOption,
activeCollectionID,
archivedCollections,
]);
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index d8448a971b..c87c19127d 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -2,8 +2,6 @@ import log from "@/base/log";
import { masterKeyFromSession } from "@/base/session-store";
import { ComlinkWorker } from "@/base/worker/comlink-worker";
import { FileType } from "@/media/file-type";
-import type { EnteFile } from "@/new/photos/types/file";
-import { ensure } from "@/utils/ensure";
import i18n, { t } from "i18next";
import { clipMatches, isMLEnabled, isMLSupported } from "../ml";
import type {
@@ -108,26 +106,14 @@ const suggestionsToOptions = async (suggestions: SearchSuggestion[]) =>
Promise.all(
suggestions.map(async (suggestion) => {
const matchingFiles = await filterSearchableFiles(suggestion);
- const files = sortMatchesIfNeeded(matchingFiles, suggestion);
return {
suggestion,
- fileCount: files.length,
- previewFiles: files.slice(0, 3),
+ fileCount: matchingFiles.length,
+ previewFiles: matchingFiles.slice(0, 3),
};
}),
);
-const sortMatchesIfNeeded = (
- files: EnteFile[],
- suggestion: SearchSuggestion,
-) => {
- if (suggestion.type != "clip") return files;
- // Sort CLIP matches by their corresponding scores.
- const score = ({ id }: EnteFile) =>
- ensure(suggestion.clipScoreForFileID.get(id));
- return files.sort((a, b) => score(b) - score(a));
-};
-
/**
* Return the list of {@link EnteFile}s (from amongst the previously set
* {@link SearchableData}) that match the given search {@link suggestion}.
diff --git a/web/packages/new/photos/services/search/worker.ts b/web/packages/new/photos/services/search/worker.ts
index 354327e21c..b32bf46439 100644
--- a/web/packages/new/photos/services/search/worker.ts
+++ b/web/packages/new/photos/services/search/worker.ts
@@ -3,6 +3,7 @@ import type { Location } from "@/base/types";
import type { Collection } from "@/media/collection";
import { fileCreationPhotoDate, fileLocation } from "@/media/file-metadata";
import type { EnteFile } from "@/new/photos/types/file";
+import { ensure } from "@/utils/ensure";
import { nullToUndefined } from "@/utils/transform";
import { getPublicMagicMetadataSync } from "@ente/shared/file-metadata";
import type { Component } from "chrono-node";
@@ -88,8 +89,11 @@ export class SearchWorker {
* Return {@link EnteFile}s that satisfy the given {@link suggestion}.
*/
filterSearchableFiles(suggestion: SearchSuggestion) {
- return this.searchableData.files.filter((f) =>
- isMatchingFile(f, suggestion),
+ return sortMatchesIfNeeded(
+ this.searchableData.files.filter((f) =>
+ isMatchingFile(f, suggestion),
+ ),
+ suggestion,
);
}
}
@@ -428,3 +432,21 @@ const isWithinRadius = (
* major axis (a) has to be scaled by the secant of the latitude.
*/
const radiusScaleFactor = (lat: number) => 1 / Math.cos(lat * (Math.PI / 180));
+
+/**
+ * Sort the files if necessary.
+ *
+ * Currently, only the CLIP results are sorted (by their score), in the other
+ * cases the files are displayed chronologically (when displaying them in search
+ * results) or arbitrarily (when showing them in the search option preview).
+ */
+const sortMatchesIfNeeded = (
+ files: EnteFile[],
+ suggestion: SearchSuggestion,
+) => {
+ if (suggestion.type != "clip") return files;
+ // Sort CLIP matches by their corresponding scores.
+ const score = ({ id }: EnteFile) =>
+ ensure(suggestion.clipScoreForFileID.get(id));
+ return files.sort((a, b) => score(b) - score(a));
+};