fix: guard against invalid date and undefined links field value (#16039)

closes https://github.com/twentyhq/twenty/issues/15854

- Add `isValid()` check in `formatDateISOStringToDateTime` before
calling `formatInTimeZone`
- Add `isDefined()` guard in `getFieldLinkDefinedLinks` (mirrors
`phonesUtils` pattern)

before:


https://github.com/user-attachments/assets/1eb89fa4-70b6-4794-8860-0a42522598b5



https://github.com/user-attachments/assets/781c7d37-c435-4832-98d4-8e6925b51e10



after:


https://github.com/user-attachments/assets/b1c22d25-4e8e-45c3-af98-be1684a5962e



https://github.com/user-attachments/assets/ce084a9d-03b8-4f88-8522-a32cb1b7cf2f

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
nitin
2025-12-29 22:33:24 +05:30
committed by GitHub
parent b56dcd8c22
commit e5e5ae8e1d
5 changed files with 35 additions and 10 deletions
@@ -1,5 +1,6 @@
import { type DateFormat } from '@/localization/constants/DateFormat';
import { type TimeFormat } from '@/localization/constants/TimeFormat';
import { isValid } from 'date-fns';
import { formatInTimeZone } from 'date-fns-tz';
export const formatDateISOStringToDateTime = ({
@@ -15,14 +16,15 @@ export const formatDateISOStringToDateTime = ({
timeFormat: TimeFormat;
localeCatalog: Locale;
}) => {
const parsedDate = new Date(date);
if (!isValid(parsedDate)) {
return '';
}
// TODO: replace this with shiftPointInTimeToFromTimezoneDifference to remove date-fns-tz, which formatInTimeZone is doig under the hood :
// https://github.com/marnusw/date-fns-tz/blob/4f3383b26a5907a73b14512a2701f3dfd8cf1579/src/toZonedTime/index.ts#L36C9-L36C27
return formatInTimeZone(
new Date(date),
timeZone,
`${dateFormat} ${timeFormat}`,
{
locale: localeCatalog,
},
);
return formatInTimeZone(parsedDate, timeZone, `${dateFormat} ${timeFormat}`, {
locale: localeCatalog,
});
};