Skip to content

Replace moment.js with Intl.DateTimeFormat - batch#6406

Open
lkostrowski wants to merge 4 commits intomainfrom
lkostrowski/replace-moment-intl
Open

Replace moment.js with Intl.DateTimeFormat - batch#6406
lkostrowski wants to merge 4 commits intomainfrom
lkostrowski/replace-moment-intl

Conversation

@lkostrowski
Copy link
Member

Scope of the change

Replace moment/moment-timezone with native Intl.DateTimeFormat API across date formatting utilities. Removes external date library dependency and uses modern browser APIs.

Changes:

  • Rewrote useDateLocalize hook with format mapping for ll, lll, and llll patterns

  • Extracted formatDateTime helpers in OrderDraftListDatagrid and VoucherListDatagrid modules

  • Extracted formatMonthYear helper in CustomerDetails component

  • Added 11 new unit tests for date formatting functions across all affected modules

  • Updated existing test assertions to match Intl.DateTimeFormat output format (includes comma before time)

  • I confirm I added ripples for changes (see src/ripples) or my feature doesn't contain any user-facing changes

  • I used analytics "trackEvent" for important events

Remove moment/moment-timezone imports from useDateLocalize, OrderDraftListDatagrid,
VoucherListDatagrid, and CustomerDetails. Use native Intl.DateTimeFormat API instead.

- Rewrite useDateLocalize hook with format mapping (ll, lll, llll)
- Extract formatDateTime helper in both datagrid modules
- Extract formatMonthYear helper in CustomerDetails
- Add dedicated unit tests for all extracted functions
- Update existing test assertions to match Intl output format

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 10, 2026 09:59
@lkostrowski lkostrowski requested a review from a team as a code owner March 10, 2026 09:59
@lkostrowski lkostrowski requested a review from witoszekdev March 10, 2026 09:59
@changeset-bot
Copy link

changeset-bot bot commented Mar 10, 2026

🦋 Changeset detected

Latest commit: 68cf87a

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lkostrowski lkostrowski changed the title Replace moment.js with Intl.DateTimeFormat Replace moment.js with Intl.DateTimeFormat - batch Mar 10, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Replace Moment/Moment-Timezone usage with native Intl.DateTimeFormat for date/time formatting across UI utilities and corresponding unit tests.

Changes:

  • Added Intl.DateTimeFormat-based formatDateTime helpers in order draft + voucher datagrids
  • Rewrote useDateLocalize to map Moment-like tokens (ll, lll, llll) to Intl options
  • Added/updated unit tests to validate new formatting output (including comma before time)

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
src/orders/components/OrderDraftListDatagrid/datagrid.ts Replaces Moment formatting with Intl helper for order draft “created” column
src/orders/components/OrderDraftListDatagrid/datagrid.test.ts Updates assertions + adds formatDateTime unit tests
src/hooks/useDateLocalize.ts Replaces Moment hook with Intl + format-option mapping
src/hooks/useDateLocalize.test.tsx Updates test output to match Intl formatting
src/discounts/components/VoucherListDatagrid/datagrid.ts Replaces Moment formatting with Intl helper for voucher start/end dates
src/discounts/components/VoucherListDatagrid/datagrid.test.ts Adds formatDateTime tests + updates assertions
src/customers/components/CustomerDetails/CustomerDetails.tsx Replaces Moment “MMM YYYY” formatting with Intl helper
src/customers/components/CustomerDetails/CustomerDetails.test.ts Adds tests for formatMonthYear

@codecov
Copy link

codecov bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.16%. Comparing base (e640f8c) to head (de02dc8).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6406      +/-   ##
==========================================
+ Coverage   43.13%   43.16%   +0.03%     
==========================================
  Files        2524     2524              
  Lines       44009    44029      +20     
  Branches    10011    10398     +387     
==========================================
+ Hits        18983    19007      +24     
+ Misses      24985    23700    -1285     
- Partials       41     1322    +1281     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

witoszekdev
witoszekdev previously approved these changes Mar 10, 2026
Copilot AI review requested due to automatic review settings March 11, 2026 08:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

describe("formatMonthYear", () => {
it("formats date as abbreviated month and year", () => {
// Arrange & Act
const result = formatMonthYear("2024-01-15T14:30:00Z");
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

formatMonthYear is exported as a curried helper (locale) => (date) => string, but the tests call it with a date string as the first argument, so result is a function and these assertions will fail at runtime. Update tests to pass a locale first (e.g. formatMonthYear(Locale.EN)(date)) or adjust the helper signature to match the intended usage.

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +38
(date: string): string =>
new Intl.DateTimeFormat(locale, {
month: "short",
year: "numeric",
}).format(new Date(date));
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

new Intl.DateTimeFormat(...).format(new Date(date)) will throw a RangeError for invalid dates (e.g. empty/undefined dateJoined), which would crash this render path. Consider validating the parsed date (isNaN(parsed.getTime())) and returning a safe placeholder (or "Invalid date" for parity with moment) instead of throwing; also prefer typing locale as Locale (from @dashboard/components/Locale) rather than string to avoid invalid tags.

Suggested change
(date: string): string =>
new Intl.DateTimeFormat(locale, {
month: "short",
year: "numeric",
}).format(new Date(date));
(date: string): string => {
const parsed = new Date(date);
if (isNaN(parsed.getTime())) {
// Fallback for invalid dates, matching Moment's "Invalid date" behavior
return "Invalid date";
}
return new Intl.DateTimeFormat(locale, {
month: "short",
year: "numeric",
}).format(parsed);
};

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +16
export function formatDateTime(date: string, locale: Locale): string {
return new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeStyle: "short" }).format(
new Date(date),
);
}
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

formatDateTime will throw on invalid input (new Date(date) -> Invalid Date), and then every cell render relies on try/catch. Prefer handling invalid dates inside formatDateTime (e.g., return "-" when isNaN(parsed.getTime())) so callers don’t need exception control flow on hot paths.

Copilot uses AI. Check for mistakes.
"saleor-dashboard": patch
---

Replaced a few instanced of moment.js usage with native Intl API
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

Typo: “instanced” → “instances”.

Suggested change
Replaced a few instanced of moment.js usage with native Intl API
Replaced a few instances of moment.js usage with native Intl API

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants