59f7582463
Fixes: #16872 ## Summary Fixes the DateTimePicker to respect user's 12H/24H time format preference. Previously, the time display at the top of the DateTimePicker always showed 24-hour format (e.g., "14:30") regardless of the user's time format setting. ## Screenshots _After:_ Time respects user preference, showing 12H with AM/PM (e.g., "03:28 PM") or 24H format <img width="357" height="491" alt="image" src="https://github.com/user-attachments/assets/4a03f195-a52b-4f74-84ba-c5eb2fc6c8c1" /> ## Implementation Details ### Changes Made: 1. **TimeMask.ts** - Added `getTimeMask()` to return appropriate mask pattern based on time format 2. **TimeBlocks.ts** - Restored as constant file per linting requirements 3. **getTimeBlocks.ts** (new) - Dynamic block generator supporting 12H (1-12 + AM/PM) and 24H (0-23) 4. **DateTimeBlocks.ts** - Simplified to static constant 5. **getDateTimeMask.ts** - Updated to accept and use `timeFormat` parameter 6. **DateTimePickerInput.tsx** - Dynamically generates mask and blocks based on user's time format preference, increased input width to accommodate AM/PM 7. **useParseJSDateToIMaskDateTimeInputString.ts** - Formats dates with correct time pattern 8. **useParseDateTimeInputStringToJSDate.ts** - Parses dates with correct time pattern 9. **date-utils.ts** - Added `getTimePattern()` helper (returns 'hh:mm a' for 12H, 'HH:mm' for 24H) 10. **parseDateTimeToString.ts** - Added optional `timeFormat` parameter for consistency ### Technical Approach: - Leverages existing `useDateTimeFormat()` hook to get user's `timeFormat` preference - Supports `TimeFormat.SYSTEM`, `TimeFormat.HOUR_12`, and `TimeFormat.HOUR_24` - Uses IMask blocks with appropriate ranges: 1-12 for 12H, 0-23 for 24H - Adds 'aa' (AM/PM) block for 12-hour format with enum validation --------- Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
222 lines
5.6 KiB
TypeScript
222 lines
5.6 KiB
TypeScript
import { isDate, isNumber, isString } from '@sniptt/guards';
|
|
import {
|
|
differenceInCalendarDays,
|
|
differenceInDays,
|
|
differenceInYears,
|
|
format,
|
|
formatDistance,
|
|
formatDistanceToNow,
|
|
isToday,
|
|
isValid,
|
|
parseISO,
|
|
type Locale,
|
|
} 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';
|
|
import { plural, t } from '@lingui/core/macro';
|
|
import { logError } from './logError';
|
|
|
|
// TODO: review all of this with Temporal
|
|
// - also break down this into small files
|
|
export const parseDate = (dateToParse: Date | string | number): Date => {
|
|
if (dateToParse === 'now') return new Date();
|
|
|
|
let formattedDate: Date | null = null;
|
|
|
|
if (!dateToParse) {
|
|
throw new CustomError(
|
|
`Invalid date passed to formatPastDate: "${dateToParse}"`,
|
|
'INVALID_DATE_FORMAT',
|
|
);
|
|
} else if (isString(dateToParse)) {
|
|
formattedDate = parseISO(dateToParse);
|
|
} else if (isDate(dateToParse)) {
|
|
formattedDate = dateToParse;
|
|
} else if (isNumber(dateToParse)) {
|
|
formattedDate = new Date(dateToParse);
|
|
}
|
|
|
|
if (!formattedDate) {
|
|
throw new CustomError(
|
|
`Invalid date passed to formatPastDate: "${dateToParse}"`,
|
|
'INVALID_DATE_FORMAT',
|
|
);
|
|
}
|
|
|
|
if (!isValid(formattedDate)) {
|
|
throw new CustomError(
|
|
`Invalid date passed to formatPastDate: "${dateToParse}"`,
|
|
'INVALID_DATE_FORMAT',
|
|
);
|
|
}
|
|
|
|
return formattedDate;
|
|
};
|
|
|
|
export const formatDate = (
|
|
dateToFormat: Date | string | number,
|
|
formatString: string,
|
|
) => {
|
|
try {
|
|
const parsedDate = parseDate(dateToFormat);
|
|
return format(parsedDate, formatString);
|
|
} catch (error) {
|
|
logError(error);
|
|
return '';
|
|
}
|
|
};
|
|
|
|
export const beautifyExactDateTime = (
|
|
dateToBeautify: Date | string | number,
|
|
) => {
|
|
const parsedDate = parseDate(dateToBeautify);
|
|
const isTodayDate = isToday(parsedDate);
|
|
const dateFormat = isTodayDate ? 'HH:mm' : 'MMM d, yyyy · HH:mm';
|
|
return formatDate(dateToBeautify, dateFormat);
|
|
};
|
|
|
|
export const beautifyExactDate = (dateToBeautify: Date | string | number) => {
|
|
const parsedDate = parseDate(dateToBeautify);
|
|
const isTodayDate = isToday(parsedDate);
|
|
if (isTodayDate) {
|
|
return t`Today`;
|
|
}
|
|
return formatDate(dateToBeautify, 'MMM d, yyyy');
|
|
};
|
|
|
|
export const beautifyPastDateRelativeToNow = (
|
|
pastDate: Date | string | number,
|
|
locale?: Locale,
|
|
) => {
|
|
try {
|
|
const parsedDate = parseDate(pastDate);
|
|
const now = new Date();
|
|
const diffInSeconds = Math.abs(
|
|
(now.getTime() - parsedDate.getTime()) / 1000,
|
|
);
|
|
|
|
// For very recent times (less than 30 seconds), show "now"
|
|
if (diffInSeconds < 30) {
|
|
return t`now`;
|
|
}
|
|
|
|
return formatDistanceToNow(parsedDate, {
|
|
addSuffix: true,
|
|
locale,
|
|
includeSeconds: true,
|
|
});
|
|
} catch (error) {
|
|
logError(error);
|
|
return '';
|
|
}
|
|
};
|
|
|
|
export const hasDatePassed = (date: Date | string | number) => {
|
|
try {
|
|
const parsedDate = parseDate(date);
|
|
|
|
return differenceInCalendarDays(new Date(), parsedDate) >= 1;
|
|
} catch (error) {
|
|
logError(error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const beautifyDateDiff = (
|
|
date: string,
|
|
dateToCompareWith?: string,
|
|
short = false,
|
|
locale?: Locale,
|
|
) => {
|
|
// For simple cases, use date-fns which has excellent locale support
|
|
if (!short && isDefined(locale)) {
|
|
const fromDate = new Date(date);
|
|
const toDate = dateToCompareWith ? new Date(dateToCompareWith) : new Date();
|
|
return formatDistance(fromDate, toDate, { locale });
|
|
}
|
|
|
|
// Manual implementation for complex cases or when locale is not available
|
|
const fromDate = parseISO(date);
|
|
const toDate = dateToCompareWith ? parseISO(dateToCompareWith) : new Date();
|
|
|
|
const years = differenceInYears(fromDate, toDate);
|
|
// Calculate remaining days after accounting for full years
|
|
const startDateForDayCalculation = new Date(toDate);
|
|
startDateForDayCalculation.setFullYear(
|
|
startDateForDayCalculation.getFullYear() + years,
|
|
);
|
|
const days = differenceInDays(fromDate, startDateForDayCalculation);
|
|
|
|
let result = '';
|
|
|
|
if (years !== 0) {
|
|
result = plural(Math.abs(years), {
|
|
one: `${years} year`,
|
|
other: `${years} years`,
|
|
});
|
|
|
|
if (short) return result;
|
|
}
|
|
|
|
if (years !== 0 && days !== 0) {
|
|
result += ` ${t`and`} `;
|
|
}
|
|
|
|
if (days !== 0) {
|
|
const daysPart = plural(Math.abs(days), {
|
|
one: `${days} day`,
|
|
other: `${days} days`,
|
|
});
|
|
result += daysPart;
|
|
}
|
|
|
|
return result;
|
|
};
|
|
|
|
export const formatToHumanReadableDate = (date: Date | string) => {
|
|
const parsedJSDate = parseDate(date);
|
|
|
|
return i18n.date(parsedJSDate, { dateStyle: 'medium' });
|
|
};
|
|
|
|
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 ${timePattern}`;
|
|
case DateFormat.YEAR_FIRST:
|
|
return `yyyy-MM-dd ${timePattern}`;
|
|
case DateFormat.MONTH_FIRST:
|
|
default:
|
|
return `MM/dd/yyyy ${timePattern}`;
|
|
}
|
|
};
|
|
|
|
export const getDateFormatStringForDatePickerInputMask = (
|
|
dateFormat: DateFormat,
|
|
): string => {
|
|
switch (dateFormat) {
|
|
case DateFormat.DAY_FIRST:
|
|
return `dd/MM/yyyy`;
|
|
case DateFormat.YEAR_FIRST:
|
|
return `yyyy-MM-dd`;
|
|
case DateFormat.MONTH_FIRST:
|
|
default:
|
|
return `MM/dd/yyyy`;
|
|
}
|
|
};
|