From d4a59983cbfba982b19519d20700a282d717be6a Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 23 Aug 2024 20:46:24 +0530 Subject: [PATCH] Fix holiday parsing --- web/apps/photos/src/services/searchService.ts | 32 ++----------------- .../new/photos/services/search/index.ts | 31 +++++++++++------- 2 files changed, 22 insertions(+), 41 deletions(-) diff --git a/web/apps/photos/src/services/searchService.ts b/web/apps/photos/src/services/searchService.ts index ce59404ad6..02a0062270 100644 --- a/web/apps/photos/src/services/searchService.ts +++ b/web/apps/photos/src/services/searchService.ts @@ -46,7 +46,6 @@ export const getAutoCompleteSuggestions = const suggestions: Suggestion[] = [ await getClipSuggestion(searchPhrase), ...getFileTypeSuggestion(searchPhrase), - ...getHolidaySuggestion(searchPhrase), ...getDateSuggestion(searchPhrase), ...getCollectionSuggestion(searchPhrase, collections), getFileNameSuggestion(searchPhrase, files), @@ -110,33 +109,6 @@ function getFileTypeSuggestion(searchPhrase: string): Suggestion[] { ); } -function getHolidaySuggestion(searchPhrase: string): Suggestion[] { - return [ - { - label: t("CHRISTMAS"), - value: { month: 12, date: 25 }, - type: SuggestionType.DATE, - }, - { - label: t("CHRISTMAS_EVE"), - value: { month: 12, date: 24 }, - type: SuggestionType.DATE, - }, - { - label: t("NEW_YEAR"), - value: { month: 1, date: 1 }, - type: SuggestionType.DATE, - }, - { - label: t("NEW_YEAR_EVE"), - value: { month: 12, date: 31 }, - type: SuggestionType.DATE, - }, - ].filter((suggestion) => - suggestion.label.toLowerCase().includes(searchPhrase), - ); -} - export async function getAllPeopleSuggestion(): Promise> { try { const people = await getAllPeople(200); @@ -187,10 +159,10 @@ export async function getMLStatusSuggestion(): Promise { } const getDateSuggestion = (searchPhrase: string): Suggestion[] => - parseDateComponents(searchPhrase).map(({ components, formattedDate }) => ({ + parseDateComponents(searchPhrase).map(({ components, label }) => ({ type: SuggestionType.DATE, value: components, - label: formattedDate, + label, })); function getCollectionSuggestion( diff --git a/web/packages/new/photos/services/search/index.ts b/web/packages/new/photos/services/search/index.ts index 26cce2f230..e421988633 100644 --- a/web/packages/new/photos/services/search/index.ts +++ b/web/packages/new/photos/services/search/index.ts @@ -1,11 +1,12 @@ import { nullToUndefined } from "@/utils/transform"; import type { Component } from "chrono-node"; import * as chrono from "chrono-node"; +import { t } from "i18next"; import type { SearchDateComponents } from "./types"; interface DateSearchResult { components: SearchDateComponents; - formattedDate: string; + label: string; } /** @@ -22,12 +23,10 @@ interface DateSearchResult { * 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): DateSearchResult[] => { - const result = parseChrono(s); - if (result.length) return result; - // chrono does not parse years like "2024", so do it manually. - return parseYearComponents(s); -}; +export const parseDateComponents = (s: string): DateSearchResult[] => + parseChrono(s) + .concat(parseYearComponents(s)) + .concat(parseHolidayComponents(s)); export const parseChrono = (s: string): DateSearchResult[] => chrono @@ -52,20 +51,30 @@ export const parseChrono = (s: string): DateSearchResult[] => if (weekday) format.weekday = "long"; const formatter = new Intl.DateTimeFormat(undefined, format); - const formattedDate = formatter.format(p.date()); - return { components, formattedDate }; + const label = formatter.format(p.date()); + return { components, label }; }) .filter((x) => x !== undefined); -/** Parse a string like "2024" into a date search result. */ +/** chrono does not parse years like "2024", so do it manually. */ const parseYearComponents = (s: string): DateSearchResult[] => { // s is already trimmed. if (s.length == 4) { const year = parseInt(s); if (year && year <= 9999) { const components = { year }; - return [{ components, formattedDate: s }]; + return [{ components, label: s }]; } } return []; }; + +const holidays: DateSearchResult[] = [ + { components: { month: 12, day: 25 }, label: t("CHRISTMAS") }, + { components: { month: 12, day: 24 }, label: t("CHRISTMAS_EVE") }, + { components: { month: 1, day: 1 }, label: t("NEW_YEAR") }, + { components: { month: 12, day: 31 }, label: t("NEW_YEAR_EVE") }, +]; + +const parseHolidayComponents = (s: string) => + holidays.filter(({ label }) => label.toLowerCase().includes(s));