diff --git a/packages/twenty-front/src/modules/localization/hooks/useDateTimeFormat.ts b/packages/twenty-front/src/modules/localization/hooks/useDateTimeFormat.ts index 59cf91488c..4f0cef9798 100644 --- a/packages/twenty-front/src/modules/localization/hooks/useDateTimeFormat.ts +++ b/packages/twenty-front/src/modules/localization/hooks/useDateTimeFormat.ts @@ -1,6 +1,8 @@ import { useRecoilValue } from 'recoil'; import { workspaceMemberFormatPreferencesState } from '@/localization/states/workspaceMemberFormatPreferencesState'; +import { resolveDateFormat } from '@/localization/utils/resolveDateFormat'; +import { resolveTimeFormat } from '@/localization/utils/resolveTimeFormat'; export const useDateTimeFormat = () => { const workspaceMemberFormatPreferences = useRecoilValue( @@ -9,8 +11,8 @@ export const useDateTimeFormat = () => { return { timeZone: workspaceMemberFormatPreferences.timeZone, - dateFormat: workspaceMemberFormatPreferences.dateFormat, - timeFormat: workspaceMemberFormatPreferences.timeFormat, + dateFormat: resolveDateFormat(workspaceMemberFormatPreferences.dateFormat), + timeFormat: resolveTimeFormat(workspaceMemberFormatPreferences.timeFormat), calendarStartDay: workspaceMemberFormatPreferences.calendarStartDay, }; }; diff --git a/packages/twenty-front/src/modules/localization/utils/resolveDateFormat.ts b/packages/twenty-front/src/modules/localization/utils/resolveDateFormat.ts new file mode 100644 index 0000000000..4f26806ecb --- /dev/null +++ b/packages/twenty-front/src/modules/localization/utils/resolveDateFormat.ts @@ -0,0 +1,11 @@ +import { DateFormat } from '@/localization/constants/DateFormat'; +import { detectDateFormat } from '@/localization/utils/detection/detectDateFormat'; + +export const resolveDateFormat = (dateFormat: DateFormat): DateFormat => { + if (dateFormat === DateFormat.SYSTEM) { + const detectedFormat = detectDateFormat(); + return DateFormat[detectedFormat]; + } + + return dateFormat; +}; diff --git a/packages/twenty-front/src/modules/localization/utils/resolveTimeFormat.ts b/packages/twenty-front/src/modules/localization/utils/resolveTimeFormat.ts new file mode 100644 index 0000000000..a597877c0e --- /dev/null +++ b/packages/twenty-front/src/modules/localization/utils/resolveTimeFormat.ts @@ -0,0 +1,11 @@ +import { TimeFormat } from '@/localization/constants/TimeFormat'; +import { detectTimeFormat } from '@/localization/utils/detection/detectTimeFormat'; + +export const resolveTimeFormat = (timeFormat: TimeFormat): TimeFormat => { + if (timeFormat === TimeFormat.SYSTEM) { + const detectedFormat = detectTimeFormat(); + return TimeFormat[detectedFormat]; + } + + return timeFormat; +}; diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimePickerInput.tsx b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimePickerInput.tsx index 3414a81f31..8ccf350fb8 100644 --- a/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimePickerInput.tsx +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/components/DateTimePickerInput.tsx @@ -2,10 +2,11 @@ import styled from '@emotion/styled'; import { useIMask } from 'react-imask'; import { useDateTimeFormat } from '@/localization/hooks/useDateTimeFormat'; -import { DATE_TIME_BLOCKS } from '@/ui/input/components/internal/date/constants/DateTimeBlocks'; +import { DATE_BLOCKS } from '@/ui/input/components/internal/date/constants/DateBlocks'; import { MAX_DATE } from '@/ui/input/components/internal/date/constants/MaxDate'; import { MIN_DATE } from '@/ui/input/components/internal/date/constants/MinDate'; import { getDateTimeMask } from '@/ui/input/components/internal/date/utils/getDateTimeMask'; +import { getTimeBlocks } from '@/ui/input/components/internal/date/utils/getTimeBlocks'; import { TimeZoneAbbreviation } from '@/ui/input/components/internal/date/components/TimeZoneAbbreviation'; import { useGetShiftedDateToCustomTimeZone } from '@/ui/input/components/internal/date/hooks/useGetShiftedDateToCustomTimeZone'; @@ -36,7 +37,7 @@ const StyledInput = styled.input<{ hasError?: boolean }>` padding-left: ${({ theme }) => theme.spacing(2)}; font-weight: 500; font-size: ${({ theme }) => theme.font.size.md}; - width: 105px; + width: 140px; `; type DateTimePickerInputProps = { @@ -58,7 +59,7 @@ export const DateTimePickerInput = ({ const { userTimezone } = useUserTimezone(); - const { dateFormat } = useDateTimeFormat(); + const { dateFormat, timeFormat } = useDateTimeFormat(); const { getShiftedDateToSystemTimeZone } = useGetShiftedDateToSystemTimeZone(); @@ -77,9 +78,9 @@ export const DateTimePickerInput = ({ return date; }; - const pattern = getDateTimeMask(dateFormat); + const pattern = getDateTimeMask({ dateFormat, timeFormat }); - const blocks = DATE_TIME_BLOCKS; + const blocks = { ...DATE_BLOCKS, ...getTimeBlocks(timeFormat) }; const defaultValueForIMask = isDefined(internalDate) ? new Date(internalDate?.toInstant().toString()) diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseDateTimeInputStringToJSDate.ts b/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseDateTimeInputStringToJSDate.ts index efcbb9702c..1d923c8373 100644 --- a/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseDateTimeInputStringToJSDate.ts +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseDateTimeInputStringToJSDate.ts @@ -3,11 +3,13 @@ import { isValid, parse } from 'date-fns'; import { getDateTimeFormatStringFoDatePickerInputMask } from '~/utils/date-utils'; export const useParseDateTimeInputStringToJSDate = () => { - const { dateFormat } = useDateTimeFormat(); + const { dateFormat, timeFormat } = useDateTimeFormat(); const parseDateTimeInputStringToJSDate = (dateAsString: string) => { - const parsingFormat = - getDateTimeFormatStringFoDatePickerInputMask(dateFormat); + const parsingFormat = getDateTimeFormatStringFoDatePickerInputMask({ + dateFormat, + timeFormat, + }); const referenceDate = new Date(); const parsedDate = parse(dateAsString, parsingFormat, referenceDate); diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseJSDateToIMaskDateTimeInputString.ts b/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseJSDateToIMaskDateTimeInputString.ts index a557c79b05..1247404f07 100644 --- a/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseJSDateToIMaskDateTimeInputString.ts +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/hooks/useParseJSDateToIMaskDateTimeInputString.ts @@ -1,13 +1,19 @@ import { useDateTimeFormat } from '@/localization/hooks/useDateTimeFormat'; -import { format } from 'date-fns'; +import { format, isValid } from 'date-fns'; import { getDateTimeFormatStringFoDatePickerInputMask } from '~/utils/date-utils'; export const useParseJSDateToIMaskDateTimeInputString = () => { - const { dateFormat } = useDateTimeFormat(); + const { dateFormat, timeFormat } = useDateTimeFormat(); const parseJSDateToDateTimeInputString = (date: Date) => { - const parsingFormat = - getDateTimeFormatStringFoDatePickerInputMask(dateFormat); + if (!date || !isValid(date)) { + return ''; + } + + const parsingFormat = getDateTimeFormatStringFoDatePickerInputMask({ + dateFormat, + timeFormat, + }); return format(date, parsingFormat); }; diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getDateTimeMask.ts b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getDateTimeMask.ts index 3c16f417e1..a870aab183 100644 --- a/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getDateTimeMask.ts +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getDateTimeMask.ts @@ -1,8 +1,14 @@ -import { TIME_MASK } from '@/ui/input/components/internal/date/constants/TimeMask'; - import { type DateFormat } from '@/localization/constants/DateFormat'; +import { type TimeFormat } from '@/localization/constants/TimeFormat'; import { getDateMask } from './getDateMask'; +import { getTimeMask } from './getTimeMask'; -export const getDateTimeMask = (dateFormat: DateFormat): string => { - return `${getDateMask(dateFormat)} ${TIME_MASK}`; +export const getDateTimeMask = ({ + dateFormat, + timeFormat, +}: { + dateFormat: DateFormat; + timeFormat: TimeFormat; +}): string => { + return `${getDateMask(dateFormat)} ${getTimeMask(timeFormat)}`; }; diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeBlocks.ts b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeBlocks.ts new file mode 100644 index 0000000000..f4b734992d --- /dev/null +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeBlocks.ts @@ -0,0 +1,24 @@ +import { TimeFormat } from '@/localization/constants/TimeFormat'; +import { IMask } from 'react-imask'; + +export const getTimeBlocks = (timeFormat: TimeFormat) => { + const isHour12 = timeFormat === TimeFormat.HOUR_12; + + return { + HH: { + mask: IMask.MaskedRange, + from: isHour12 ? 1 : 0, + to: isHour12 ? 12 : 23, + maxLength: 2, + }, + mm: { + mask: IMask.MaskedRange, + from: 0, + to: 59, + maxLength: 2, + }, + aa: { + mask: '**', + }, + }; +}; diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeMask.ts b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeMask.ts new file mode 100644 index 0000000000..b904da0823 --- /dev/null +++ b/packages/twenty-front/src/modules/ui/input/components/internal/date/utils/getTimeMask.ts @@ -0,0 +1,5 @@ +import { TimeFormat } from '@/localization/constants/TimeFormat'; + +export const getTimeMask = (timeFormat: TimeFormat): string => { + return timeFormat === TimeFormat.HOUR_12 ? 'HH`:mm` `aa' : 'HH`:mm`'; +}; diff --git a/packages/twenty-front/src/utils/date-utils.ts b/packages/twenty-front/src/utils/date-utils.ts index ebae560f72..18ed9da08a 100644 --- a/packages/twenty-front/src/utils/date-utils.ts +++ b/packages/twenty-front/src/utils/date-utils.ts @@ -13,6 +13,7 @@ import { } from 'date-fns'; import { DateFormat } from '@/localization/constants/DateFormat'; +import { TimeFormat } from '@/localization/constants/TimeFormat'; import { CustomError, isDefined } from 'twenty-shared/utils'; import { i18n } from '@lingui/core'; @@ -182,17 +183,26 @@ export const formatToHumanReadableDate = (date: Date | string) => { return i18n.date(parsedJSDate, { dateStyle: 'medium' }); }; -export const getDateTimeFormatStringFoDatePickerInputMask = ( - dateFormat: DateFormat, -): string => { +const getTimePattern = (timeFormat: TimeFormat) => { + return timeFormat === TimeFormat.HOUR_12 ? 'hh:mm a' : 'HH:mm'; +}; + +export const getDateTimeFormatStringFoDatePickerInputMask = ({ + dateFormat, + timeFormat, +}: { + dateFormat: DateFormat; + timeFormat: TimeFormat; +}): string => { + const timePattern = getTimePattern(timeFormat); switch (dateFormat) { case DateFormat.DAY_FIRST: - return `dd/MM/yyyy HH:mm`; + return `dd/MM/yyyy ${timePattern}`; case DateFormat.YEAR_FIRST: - return `yyyy-MM-dd HH:mm`; + return `yyyy-MM-dd ${timePattern}`; case DateFormat.MONTH_FIRST: default: - return `MM/dd/yyyy HH:mm`; + return `MM/dd/yyyy ${timePattern}`; } };