diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a949abf377..f395719974 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -785,9 +785,6 @@ importers: cmdk: specifier: ^1.0.0 version: 1.1.1(@types/react-dom@18.3.7(@types/react@18.3.31))(@types/react@18.3.31)(react-dom@18.3.1(react@18.3.1))(react@18.3.1) - date-fns: - specifier: ^4.1.0 - version: 4.4.0 diff: specifier: ^5.2.0 version: 5.2.2 @@ -4674,9 +4671,6 @@ packages: resolution: {integrity: sha512-BS8PfmtDGnrgYdOonGZQdLZslWIeCGFP9tpan0hi1Co2Zr2NKADsvGYA8XxuG/4UWgJ6Cjtv+YJnB6MM69QGlQ==} engines: {node: '>= 0.4'} - date-fns@4.4.0: - resolution: {integrity: sha512-+1UMbeh68lH1SegH83CGWwpb6OHHbpSgr3+s5Eww5M4CAgswBpoWS0AjTOfEJ33HiYKz1hdj/KTFprzXHmq/6w==} - dayjs@1.11.23: resolution: {integrity: sha512-QDTCU0M0MxR3hQfnlDJfwekQiaanm1ubOD231u73WBckQ/fsamwRLiE2GBz6D3a/xF1NgfiDLJjXBa1hYOYTtQ==} @@ -13170,8 +13164,6 @@ snapshots: es-errors: 1.3.0 is-data-view: 1.0.2 - date-fns@4.4.0: {} - dayjs@1.11.23: {} debug@4.4.3(supports-color@8.1.1): diff --git a/webview-ui/package.json b/webview-ui/package.json index 8c3ca2d73a..8ef2c15545 100644 --- a/webview-ui/package.json +++ b/webview-ui/package.json @@ -41,7 +41,8 @@ "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "cmdk": "^1.0.0", - "date-fns": "^4.1.0", + "react-icons": "^5.5.0", + "diff": "^5.2.0", "diff": "^5.2.0", "fast-deep-equal": "^3.1.3", "fzf": "^0.5.2", @@ -57,7 +58,6 @@ "react-compiler-runtime": "^1.0.0", "react-dom": "^18.3.1", "react-i18next": "^15.4.1", - "react-icons": "^5.5.0", "react-markdown": "^9.0.3", "react-remark": "^2.1.0", "react-textarea-autosize": "^8.5.3", diff --git a/webview-ui/src/components/chat/Announcement.tsx b/webview-ui/src/components/chat/Announcement.tsx index bf88306ab4..e6f5a65af1 100644 --- a/webview-ui/src/components/chat/Announcement.tsx +++ b/webview-ui/src/components/chat/Announcement.tsx @@ -1,11 +1,11 @@ import { memo, type MouseEvent, type ReactNode, useState } from "react" import { Trans } from "react-i18next" -import { SiDiscord, SiReddit, SiX } from "react-icons/si" import { Package } from "@roo/package" import { useAppTranslation } from "@src/i18n/TranslationContext" import { vscode } from "@src/utils/vscode" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@src/components/ui" +import { SiDiscord, SiReddit, SiX } from "react-icons/si" import { EXTERNAL_LINKS } from "@src/constants/externalLinks" interface AnnouncementProps { diff --git a/webview-ui/src/components/mcp/McpErrorRow.tsx b/webview-ui/src/components/mcp/McpErrorRow.tsx index 9de2ded184..3a7a07cd4c 100644 --- a/webview-ui/src/components/mcp/McpErrorRow.tsx +++ b/webview-ui/src/components/mcp/McpErrorRow.tsx @@ -1,8 +1,115 @@ import { useMemo } from "react" -import { formatRelative } from "date-fns" import type { McpErrorEntry } from "@roo-code/types" +// `Intl.DateTimeFormat` instances are immutable and locale-fixed, so they are +// built once per timezone and cached instead of per render. The cache key is +// the resolved timezone string: an instance built without an explicit timeZone +// option resolves the zone at construction time, so reusing one instance +// across a runtime timezone change (tests, or a moved webview host) would +// format in the stale zone. +const formatterCache = new Map() + +const getFormatter = (options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat => { + const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone + const cacheKey = `${timeZone}:${JSON.stringify(options)}` + let formatter = formatterCache.get(cacheKey) + + if (!formatter) { + formatter = new Intl.DateTimeFormat("en-US", { ...options, timeZone }) + formatterCache.set(cacheKey, formatter) + } + + return formatter +} + +const timeFormat = () => getFormatter({ hour: "numeric", minute: "2-digit" }) +const weekdayFormat = () => getFormatter({ weekday: "long" }) +const shortDateFormat = () => getFormatter({ year: "numeric", month: "2-digit", day: "2-digit" }) + +const MILLISECONDS_IN_DAY = 86_400_000 +const MILLISECONDS_IN_MINUTE = 60_000 + +/** + * Whole calendar days from `baseDate` to `date`, in local time. + * + * Calendar days, not elapsed milliseconds: 23:59 and 00:01 on consecutive days + * are one day apart even though only two minutes elapsed. + * + * Each local midnight is normalized by its own UTC offset before dividing, the + * same arithmetic `date-fns`' `differenceInCalendarDays` uses. Raw millisecond + * subtraction alone breaks when the two midnights have different offsets: + * Pacific/Apia skipped 2011-12-30 entirely (UTC-11 -> UTC+13), so the + * 2011-12-29 and 2011-12-31 midnights are only 24h apart but are two calendar + * days apart, and a 23h/25h DST day would otherwise round to the wrong side. + * + * The inputs are copied and zeroed with `setHours` rather than rebuilt through + * `new Date(year, month, day)`, because that constructor maps years 0-99 onto + * 1900-1999: a date in year 99 would be silently read as 1999 and produce a + * delta of centuries instead of a day. + */ +const differenceInCalendarDays = (date: Date, baseDate: Date): number => { + const dateMidnight = new Date(date) + dateMidnight.setHours(0, 0, 0, 0) + + const baseMidnight = new Date(baseDate) + baseMidnight.setHours(0, 0, 0, 0) + + const dateOffsetMs = dateMidnight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE + const baseOffsetMs = baseMidnight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE + + return Math.round( + (dateMidnight.getTime() - dateOffsetMs - (baseMidnight.getTime() - baseOffsetMs)) / MILLISECONDS_IN_DAY, + ) +} + +/** + * Render a timestamp the way `date-fns`' `formatRelative` did for the `en-US` + * locale: a calendar phrase for the surrounding two weeks, a short date beyond. + * + * | Distance to `baseDate` | Result | + * | ---------------------- | ----------------------- | + * | Same day | `today at 9:00 AM` | + * | Previous day | `yesterday at 9:00 AM` | + * | Next day | `tomorrow at 9:00 AM` | + * | Previous 2-6 days | `last Tuesday at 9:00 AM` | + * | Next 2-6 days | `Saturday at 9:00 AM` | + * | Anything else | `10/22/2026` | + * + * The output is deliberately fixed to `en-US` because that is what the previous + * implementation produced: `formatRelative` was called without a `locale` + * option, so it used date-fns' `enUS` default regardless of the UI language. + * + * @throws {RangeError} When `date` is an invalid date. + */ +export const formatRelativeTimestamp = (date: Date, baseDate: Date): string => { + const dayDelta = differenceInCalendarDays(date, baseDate) + + if (Number.isNaN(dayDelta)) { + throw new RangeError("Invalid time value") + } + + const time = timeFormat().format(date) + + if (dayDelta < -6 || dayDelta >= 7) { + return shortDateFormat().format(date) + } + if (dayDelta < -1) { + return `last ${weekdayFormat().format(date)} at ${time}` + } + if (dayDelta < 0) { + return `yesterday at ${time}` + } + if (dayDelta < 1) { + return `today at ${time}` + } + if (dayDelta < 2) { + return `tomorrow at ${time}` + } + + return `${weekdayFormat().format(date)} at ${time}` +} + type McpErrorRowProps = { error: McpErrorEntry } @@ -25,7 +132,7 @@ export const McpErrorRow = ({ error }: McpErrorRowProps) => { {error.message}
- {formatRelative(error.timestamp, new Date())} + {formatRelativeTimestamp(new Date(error.timestamp), new Date())}
) diff --git a/webview-ui/src/components/mcp/__tests__/McpErrorRow.spec.tsx b/webview-ui/src/components/mcp/__tests__/McpErrorRow.spec.tsx new file mode 100644 index 0000000000..f4fbb4c0c7 --- /dev/null +++ b/webview-ui/src/components/mcp/__tests__/McpErrorRow.spec.tsx @@ -0,0 +1,164 @@ +import React from "react" +import { render, screen } from "@/utils/test-utils" + +import { McpErrorRow, formatRelativeTimestamp } from "../McpErrorRow" + +// Thursday 29 October 2026, 12:00 local. Fixed so every expectation below is +// independent of the machine clock and timezone. +const BASE = new Date(2026, 9, 29, 12, 0, 0) + +describe("formatRelativeTimestamp", () => { + // Expected strings captured from date-fns `formatRelative` (en-US) before the + // swap, so this table pins behaviour equivalence rather than new behaviour. + it.each([ + ["same day, earlier", new Date(2026, 9, 29, 9, 0), "today at 9:00 AM"], + ["same day, later", new Date(2026, 9, 29, 21, 5), "today at 9:05 PM"], + ["same day, midnight", new Date(2026, 9, 29, 0, 0), "today at 12:00 AM"], + ["previous day", new Date(2026, 9, 28, 23, 59), "yesterday at 11:59 PM"], + ["two days back", new Date(2026, 9, 27, 9, 0), "last Tuesday at 9:00 AM"], + ["six days back", new Date(2026, 9, 23, 9, 0), "last Friday at 9:00 AM"], + ["seven days back", new Date(2026, 9, 22, 9, 0), "10/22/2026"], + ["thirty days back", new Date(2026, 8, 29, 9, 0), "09/29/2026"], + ["four hundred days back", new Date(2025, 8, 24, 9, 0), "09/24/2025"], + ["next day", new Date(2026, 9, 30, 9, 0), "tomorrow at 9:00 AM"], + ["two days ahead", new Date(2026, 9, 31, 9, 0), "Saturday at 9:00 AM"], + ["six days ahead", new Date(2026, 10, 4, 9, 0), "Wednesday at 9:00 AM"], + ["seven days ahead", new Date(2026, 10, 5, 9, 0), "11/05/2026"], + ])("formats %s", (_label, date, expected) => { + expect(formatRelativeTimestamp(date, BASE)).toBe(expected) + }) + + it("counts calendar days, not elapsed time", () => { + // Two minutes apart, but on different calendar days. Expected strings + // verified against date-fns `formatRelative` for the same inputs. + expect(formatRelativeTimestamp(new Date(2026, 9, 29, 0, 1), new Date(2026, 9, 28, 23, 59))).toBe( + "tomorrow at 12:01 AM", + ) + expect(formatRelativeTimestamp(new Date(2026, 9, 28, 23, 59), new Date(2026, 9, 29, 0, 1))).toBe( + "yesterday at 11:59 PM", + ) + }) + + it("throws on an invalid date", () => { + expect(() => formatRelativeTimestamp(new Date(Number.NaN), BASE)).toThrow(RangeError) + }) + + // The maintainer asked for the DST behaviour to be pinned against the + // replaced helper. Every expectation below was captured by running the real + // date-fns 4.1.0 `formatRelative` on the same inputs before these tests were + // written, so they pin parity rather than new behaviour. + describe("DST and skipped local dates (date-fns 4.1.0 pinned)", () => { + const withTimeZone = (tz: string, run: () => void) => { + const hadPreviousTz = "TZ" in process.env + const previousTz = process.env.TZ + process.env.TZ = tz + + try { + run() + } finally { + // Restoring with `undefined` would set the literal string + // "undefined", which Node resolves as UTC; delete instead when the + // variable was absent so the machine's local zone comes back. + if (hadPreviousTz) { + process.env.TZ = previousTz + } else { + delete process.env.TZ + } + } + } + + it("counts the skipped Pacific/Apia 2011-12-30 as a full day", () => { + // Samoa skipped 2011-12-30 entirely when it jumped from UTC-11 to + // UTC+13. The 12-29 and 12-31 local midnights are only 24h apart, but + // date-fns reports two calendar days: "Saturday at 12:00 PM". + withTimeZone("Pacific/Apia", () => { + const dec29 = new Date(2011, 11, 29, 12, 0) + const dec31 = new Date(2011, 11, 31, 12, 0) + + expect(dec29.getFullYear()).toBe(2011) + expect(dec31.getFullYear()).toBe(2011) + expect(formatRelativeTimestamp(dec31, dec29)).toBe("Saturday at 12:00 PM") + expect(formatRelativeTimestamp(dec29, dec31)).toBe("last Thursday at 12:00 PM") + expect(formatRelativeTimestamp(dec29, dec29)).toBe("today at 12:00 PM") + }) + }) + + it("rounds a 23-hour spring-forward day to one day", () => { + // America/New_York, 2026-03-08: 02:00 EST jumps to 03:00 EDT. + withTimeZone("America/New_York", () => { + const mar8 = new Date(2026, 2, 8, 12, 0) + const mar9 = new Date(2026, 2, 9, 12, 0) + + expect(formatRelativeTimestamp(mar9, mar8)).toBe("tomorrow at 12:00 PM") + expect(formatRelativeTimestamp(mar8, mar9)).toBe("yesterday at 12:00 PM") + }) + }) + + it("rounds a 25-hour fall-back day to one day", () => { + // America/New_York, 2026-11-01: 02:00 EDT repeats as 02:00 EST. + withTimeZone("America/New_York", () => { + const nov1 = new Date(2026, 10, 1, 12, 0) + const nov2 = new Date(2026, 10, 2, 12, 0) + + expect(formatRelativeTimestamp(nov2, nov1)).toBe("tomorrow at 12:00 PM") + expect(formatRelativeTimestamp(nov1, nov2)).toBe("yesterday at 12:00 PM") + }) + }) + }) + + it("keeps two-digit years instead of remapping them to 1900s", () => { + // `new Date(99, 11, 31)` would mean 1999. These are real year-99 dates, and + // the day delta across the year-99/100 boundary must stay one day. + const year99 = new Date("0099-12-31T12:00:00") + const year100 = new Date("0100-01-01T12:00:00") + + expect(year99.getFullYear()).toBe(99) + expect(formatRelativeTimestamp(year99, year100)).toBe("yesterday at 12:00 PM") + expect(formatRelativeTimestamp(year100, year99)).toBe("tomorrow at 12:00 PM") + expect(formatRelativeTimestamp(year100, year100)).toBe("today at 12:00 PM") + }) +}) + +describe("McpErrorRow", () => { + // Freeze the clock at a stable midday instant. Without this the component + // formats against the live `new Date()` while the fixture is built from the + // live `Date.now()`; in the first five minutes after local midnight the two + // land on different calendar days and the "today at" assertion fails. + beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(new Date(2026, 9, 29, 12, 0, 0)) + }) + + afterEach(() => { + vi.useRealTimers() + }) + + it("renders the error message and a relative timestamp", () => { + const timestamp = Date.now() - 5 * 60 * 1000 + + render() + + expect(screen.getByText("Connection refused")).toBeInTheDocument() + expect(screen.getByText("today at 11:55 AM")).toBeInTheDocument() + }) + + it("still renders a same-day timestamp just after local midnight", () => { + // The exact window the flake appeared in: 00:02 local, five minutes ago + // lands on the previous calendar day, so the phrasing must be "yesterday". + vi.setSystemTime(new Date(2026, 9, 29, 0, 2, 0)) + + render() + + expect(screen.getByText("yesterday at 11:57 PM")).toBeInTheDocument() + }) + + it.each([ + ["error", "var(--vscode-testing-iconFailed)"], + ["warn", "var(--vscode-charts-yellow)"], + ["info", "var(--vscode-testing-iconPassed)"], + ] as const)("colors the %s level", (level, expectedColor) => { + render() + + expect(screen.getByText("msg")).toHaveStyle({ color: expectedColor }) + }) +})