Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RootState, store, useAppSelector } from "@/store/store";

import { enUS, ar } from "date-fns/locale";
import i18n from "@/18n";
import { format } from "date-fns";
import { format, isValid } from "date-fns";

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The toDate() function's global dash replacement corrupts ISO 8601 timestamps, causing new Date() to fail and downstream date formatting to return an empty string.
Severity: HIGH

Suggested Fix

Modify toDate() to avoid corrupting ISO 8601 timestamps. Instead of a global replacement, consider splitting the string by 'T', replacing dashes only in the date part, and then rejoining. Alternatively, use a more robust date parsing library or method that correctly handles ISO strings without manual manipulation.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/utils/common.ts#L6

Potential issue: The `toDate()` function globally replaces all dashes with slashes. When
called with an ISO 8601 string containing a 'T' separator (e.g., from
`Date.toISOString()`), it produces an invalid date format like
"2024/01/01T09:30:00.000Z". This string cannot be parsed by `new Date()`, leading to an
`Invalid Date` object. Consequently, `dateTimeFormat()` returns an empty string, causing
UI components like date pickers in `form-datetime.tsx` and `date-dropdown.tsx` to
display blank values.

Also affects:

  • form-datetime.tsx:59
  • date-dropdown.tsx:35

Did we get this right? 👍 / 👎 to inform future reviews.


// A custom hook that builds on useLocation to parse
// the query string for you.
Expand All @@ -16,7 +16,9 @@ export function useQuery() {
}

export function dateTimeFormat(value: string, dateFormat: string): string {
return format(value, dateFormat, { locale: i18n.language == 'en' ? enUS : ar })
const converted = toDate(value);
if (!converted || !isValid(converted)) return '';
return format(converted as Date, dateFormat, { locale: i18n.language == 'en' ? enUS : ar })
}

export function formatNumber(value: number) {
Expand Down