From 864540e31cf1b3ea6941a33cfe21bb9bc19999a3 Mon Sep 17 00:00:00 2001 From: Nathan Date: Thu, 30 Oct 2025 20:47:18 +0800 Subject: [PATCH] fix: date time undefined get --- .../database-yjs/fields/date/utils.ts | 43 ++++++++++++++----- src/application/database-yjs/selector.ts | 3 +- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/application/database-yjs/fields/date/utils.ts b/src/application/database-yjs/fields/date/utils.ts index c6c5e455..4d92d4bb 100644 --- a/src/application/database-yjs/fields/date/utils.ts +++ b/src/application/database-yjs/fields/date/utils.ts @@ -36,7 +36,8 @@ export function getRowTimeString(field: YDatabaseField, timeStamp: string, curre const typeOption = getTypeOptions(field); const typeOptionValue = getFieldDateTimeFormats(typeOption, currentUser); - const includeTime = typeOption.get(YjsDatabaseKey.include_time); + const includeTimeRaw = typeOption?.get(YjsDatabaseKey.include_time); + const includeTime = typeof includeTimeRaw === 'boolean' ? includeTimeRaw : Boolean(includeTimeRaw); return getDateTimeStr({ timeStamp, @@ -45,16 +46,36 @@ export function getRowTimeString(field: YDatabaseField, timeStamp: string, curre }); } -export function getFieldDateTimeFormats(typeOption: YMapFieldTypeOption, currentUser?: User) { - const typeOptionTimeFormat = typeOption.get(YjsDatabaseKey.time_format); - const typeOptionDateFormat = typeOption.get(YjsDatabaseKey.date_format); +export function getFieldDateTimeFormats(typeOption: YMapFieldTypeOption | undefined, currentUser?: User) { + /** + * Resolve the preferred date/time formats for a field. + * 1. Read any persisted overrides from the Yjs type option map. + * 2. Fall back to the signed-in user's preference metadata. + * 3. Default to AppFlowy's locale-aware formats as a final safety net. + */ + const typeOptionTimeFormat = typeOption?.get(YjsDatabaseKey.time_format); + const typeOptionDateFormat = typeOption?.get(YjsDatabaseKey.date_format); - const dateFormat = typeOptionDateFormat === undefined - ? currentUser?.metadata?.[MetadataKey.DateFormat] as DateFormat ?? DateFormat.Local - : parseInt(typeOptionDateFormat) as DateFormat; - const timeFormat = typeOptionTimeFormat === undefined - ? currentUser?.metadata?.[MetadataKey.TimeFormat] as TimeFormat ?? TimeFormat.TwelveHour - : parseInt(typeOptionTimeFormat) as TimeFormat; + const fallbackDateFormat = currentUser?.metadata?.[MetadataKey.DateFormat] as DateFormat | undefined; + const fallbackTimeFormat = currentUser?.metadata?.[MetadataKey.TimeFormat] as TimeFormat | undefined; + + const parsedDateFormat = + typeOptionDateFormat === undefined || typeOptionDateFormat === null + ? undefined + : Number.parseInt(String(typeOptionDateFormat), 10); + const parsedTimeFormat = + typeOptionTimeFormat === undefined || typeOptionTimeFormat === null + ? undefined + : Number.parseInt(String(typeOptionTimeFormat), 10); + + const dateFormat = + parsedDateFormat === undefined || Number.isNaN(parsedDateFormat) + ? fallbackDateFormat ?? DateFormat.Local + : parsedDateFormat as DateFormat; + const timeFormat = + parsedTimeFormat === undefined || Number.isNaN(parsedTimeFormat) + ? fallbackTimeFormat ?? TimeFormat.TwelveHour + : parsedTimeFormat as TimeFormat; return { dateFormat, @@ -64,7 +85,7 @@ export function getFieldDateTimeFormats(typeOption: YMapFieldTypeOption, current export function getDateCellStr({ cell, field, currentUser }: { cell: DateTimeCell; field: YDatabaseField, currentUser?: User }) { const typeOptionMap = field.get(YjsDatabaseKey.type_option); - const typeOption = typeOptionMap.get(String(cell.fieldType)); + const typeOption = typeOptionMap?.get(String(cell.fieldType)); const typeOptionValue = getFieldDateTimeFormats(typeOption, currentUser); diff --git a/src/application/database-yjs/selector.ts b/src/application/database-yjs/selector.ts index 026741c8..59f86108 100644 --- a/src/application/database-yjs/selector.ts +++ b/src/application/database-yjs/selector.ts @@ -1235,11 +1235,12 @@ export const useRowTimeString = (rowId: string, fieldId: string, attrName: strin const typeOption = getTypeOptions(field); const { dateFormat, timeFormat } = getFieldDateTimeFormats(typeOption, currentUser); + const includeTimeRaw = typeOption?.get(YjsDatabaseKey.include_time); return { dateFormat, timeFormat, - includeTime: typeOption.get(YjsDatabaseKey.include_time), + includeTime: typeof includeTimeRaw === 'boolean' ? includeTimeRaw : Boolean(includeTimeRaw), }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [field, clock, currentUser?.metadata]);