Skip to content

armgit / ente

Public Imported from GitHub
0

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

Parse

Manav Rathi8379fb726da5
diff --git a/web/apps/photos/src/services/searchService.ts b/web/apps/photos/src/services/searchService.ts
index 27bbe574bc..26b07cf937 100644
--- a/web/apps/photos/src/services/searchService.ts
+++ b/web/apps/photos/src/services/searchService.ts
@@ -7,7 +7,7 @@ import {
isMLSupported,
mlStatusSnapshot,
} from "@/new/photos/services/ml";
-import { parsePotentialDate } from "@/new/photos/services/search";
+import { parseDateComponents } from "@/new/photos/services/search";
import type {
SearchDateComponents,
SearchPerson,
@@ -207,25 +207,12 @@ export async function getMLStatusSuggestion(): Promise<Suggestion> {
};
}
-function getDateSuggestion(searchPhrase: string): Suggestion[] {
- const searchedDates = parsePotentialDate(searchPhrase);
-
- return searchedDates.map((searchedDate) => ({
+const getDateSuggestion = (searchPhrase: string): Suggestion[] =>
+ parseDateComponents(searchPhrase).map(({ components, formattedDate }) => ({
type: SuggestionType.DATE,
- value: searchedDate,
- label: getFormattedDate(searchedDate),
+ value: components,
+ label: formattedDate,
}));
-}
-
-export function getFormattedDate(date: SearchDateComponents) {
- const options = {};
- date.day && (options["day"] = "numeric");
- (date.month || date.month === 0) && (options["month"] = "long");
- date.year && (options["year"] = "numeric");
- return new Intl.DateTimeFormat("en-IN", options).format(
- new Date(date.year ?? 1, date.month ?? 1, date.day ?? 1),
- );
-}
function getCollectionSuggestion(
searchPhrase: string,
diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts
index a60b02b14f..5aac8dcceb 100644
--- a/web/packages/new/photos/services/search/index.ts
+++ b/web/packages/new/photos/services/search/index.ts
@@ -13,15 +13,28 @@ import type { SearchDateComponents } from "./types";
* while "22 December 2022" will be parsed into
*
* [(year 2022, month 12, day 22)]
+ *
+ * In addition, also return a formatted representation of the "best" guess at
+ * the date that was intended by the search string.
*/
-export const parseDateComponents = (s: string): SearchDateComponents[] =>
+export const parseDateComponents = (
+ s: string,
+): { components: SearchDateComponents; formattedDate: string }[] =>
chrono.parse(s).map((result) => {
const p = result.start;
const component = (s: Component) =>
p.isCertain(s) ? nullToUndefined(p.get(s)) : undefined;
+
const year = component("year");
const month = component("month");
const day = component("day");
- const date = p.date();
- return { year, month, day, date };
+
+ const format: Intl.DateTimeFormatOptions = {};
+ if (year) format.year = "numeric";
+ if (month !== undefined) format.month = "long";
+ if (day) format.day = "numeric";
+
+ const formatter = new Intl.DateTimeFormat(undefined, format);
+ const formattedDate = formatter.format(p.date());
+ return { components: { year, month, day }, formattedDate };
});
diff --git a/web/packages/new/photos/services/search/types.ts b/web/packages/new/photos/services/search/types.ts
index 5cc4ea7976..2cce11126a 100644
--- a/web/packages/new/photos/services/search/types.ts
+++ b/web/packages/new/photos/services/search/types.ts
@@ -15,20 +15,15 @@ export interface SearchDateComponents {
/**
* The year, if the search string specified one.
*/
- year: number | undefined;
+ year?: number;
/**
* The month, if the search string specified one.
*/
- month: number | undefined;
+ month?: number;
/**
* The day of the month, if the search string specified one.
*/
- day: number | undefined;
- /**
- * The "best" guess at the exact JavaScript {@link Date} that was intended
- * by the search string.
- */
- date: Date;
+ day?: number;
}
/**