This commit is contained in:
Manav Rathi
2024-08-23 17:27:38 +05:30
parent aaadffa613
commit a5ffb0f4bd
5 changed files with 44 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ import {
} from "@/new/photos/services/ml";
import { parsePotentialDate } from "@/new/photos/services/search";
import type {
DateValue,
SearchDateComponents,
SearchPerson,
} from "@/new/photos/services/search/types";
import { EnteFile } from "@/new/photos/types/file";
@@ -217,13 +217,13 @@ function getDateSuggestion(searchPhrase: string): Suggestion[] {
}));
}
export function getFormattedDate(date: DateValue) {
export function getFormattedDate(date: SearchDateComponents) {
const options = {};
date.date && (options["day"] = "numeric");
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.date ?? 1),
new Date(date.year ?? 1, date.month ?? 1, date.day ?? 1),
);
}
@@ -369,7 +369,7 @@ function convertSuggestionToSearchQuery(option: Suggestion): Search {
switch (option.type) {
case SuggestionType.DATE:
return {
date: option.value as DateValue,
date: option.value as SearchDateComponents,
};
case SuggestionType.LOCATION:

View File

@@ -1,7 +1,7 @@
import { FileType } from "@/media/file-type";
import type { MLStatus } from "@/new/photos/services/ml";
import type {
DateValue,
SearchDateComponents,
SearchPerson,
} from "@/new/photos/services/search/types";
import { EnteFile } from "@/new/photos/types/file";
@@ -25,7 +25,7 @@ export interface Suggestion {
type: SuggestionType;
label: string;
value:
| DateValue
| SearchDateComponents
| number[]
| SearchPerson
| MLStatus
@@ -37,7 +37,7 @@ export interface Suggestion {
}
export type Search = {
date?: DateValue;
date?: SearchDateComponents;
location?: LocationTagData;
city?: City;
collection?: number;

View File

@@ -1,4 +1,4 @@
import type { DateValue } from "@/new/photos/services/search/types";
import type { SearchDateComponents } from "@/new/photos/services/search/types";
import { EnteFile } from "@/new/photos/types/file";
import * as Comlink from "comlink";
import {
@@ -66,18 +66,19 @@ function isSearchedFile(file: EnteFile, search: Search) {
return false;
}
const isSameDayAnyYear = (baseDate: DateValue) => (compareDate: Date) => {
let same = true;
const isSameDayAnyYear =
(baseDate: SearchDateComponents) => (compareDate: Date) => {
let same = true;
if (baseDate.month || baseDate.month === 0) {
same = baseDate.month === compareDate.getMonth();
}
if (same && baseDate.date) {
same = baseDate.date === compareDate.getDate();
}
if (same && baseDate.year) {
same = baseDate.year === compareDate.getFullYear();
}
if (baseDate.month || baseDate.month === 0) {
same = baseDate.month === compareDate.getMonth();
}
if (same && baseDate.day) {
same = baseDate.day === compareDate.getDate();
}
if (same && baseDate.year) {
same = baseDate.year === compareDate.getFullYear();
}
return same;
};
return same;
};

View File

@@ -1,9 +1,11 @@
import * as chrono from "chrono-node";
import type { DateValue } from "./types";
import type { SearchDateComponents } from "./types";
const DIGITS = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
export const parsePotentialDate = (humanDate: string): DateValue[] => {
export const parsePotentialDate = (
humanDate: string,
): SearchDateComponents[] => {
const date = chrono.parseDate(humanDate);
const date1 = chrono.parseDate(`${humanDate} 1`);
if (date !== null) {

View File

@@ -5,10 +5,23 @@
import type { EnteFile } from "../../types/file";
export interface DateValue {
date?: number;
month?: number;
year?: number;
/**
* A parsed version of a potential natural language date time string.
*
* The components which were parsed will be set. The type doesn't enforce this,
* but at least one component will be present.
*
* e.g. "December 2022" will be parsed into a (year 2022, month 12, day
* undefined), while "22 December 2022" will be parsed into (year 2022, month
* 12, day 22).
*/
export interface SearchDateComponents {
/** The year, if the search string specified one. */
year: number | undefined;
/** The month, if the search string specified one. */
month: number | undefined;
/** The day of the month, if the search string specified one. */
day: number | undefined;
}
/**