Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(timestamp): render nothing instead of crashing on an unparseable value (#3774)

Shahrier Islam Arhambd58c97bc928
diff --git a/.changeset/timestamp-invalid-value.md b/.changeset/timestamp-invalid-value.md
new file mode 100644
index 0000000000..03ad472ff1
--- /dev/null
+++ b/.changeset/timestamp-invalid-value.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Timestamp: render nothing instead of crashing on an unparseable value
+
+An unparseable `value` (a malformed date string, or a NaN timestamp from missing data) produced an Invalid Date whose formatting throws "Invalid time value", crashing the whole tree. Invalid values now render nothing and log a console warning instead.
+@arham766
diff --git a/packages/core/src/Timestamp/Timestamp.test.tsx b/packages/core/src/Timestamp/Timestamp.test.tsx
index 1b42051a86..8c65dd37e6 100644
--- a/packages/core/src/Timestamp/Timestamp.test.tsx
+++ b/packages/core/src/Timestamp/Timestamp.test.tsx
@@ -315,4 +315,31 @@ describe('Timestamp', () => {
const el = screen.getByRole('time');
expect(el.textContent).not.toContain('ago');
});
+
+ // --- Invalid values ---
+
+ it('renders nothing instead of crashing on an unparseable string value', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ const {container} = render(
+ <Timestamp value="not-a-date" data-testid="ts" />,
+ );
+ expect(container).toBeEmptyDOMElement();
+ expect(warn).toHaveBeenCalledWith(
+ expect.stringContaining('could not parse value'),
+ );
+ } finally {
+ warn.mockRestore();
+ }
+ });
+
+ it('renders nothing instead of crashing on a NaN value', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ try {
+ const {container} = render(<Timestamp value={NaN} data-testid="ts" />);
+ expect(container).toBeEmptyDOMElement();
+ } finally {
+ warn.mockRestore();
+ }
+ });
});
diff --git a/packages/core/src/Timestamp/Timestamp.tsx b/packages/core/src/Timestamp/Timestamp.tsx
index 2600b0fd89..66953f9a48 100644
--- a/packages/core/src/Timestamp/Timestamp.tsx
+++ b/packages/core/src/Timestamp/Timestamp.tsx
@@ -360,7 +360,12 @@ export function Timestamp({
const [now, setNow] = useState(() => new Date());
const date = parseValue(value);
- const isoString = date.toISOString();
+ // An unparseable value (a malformed date string, or a NaN timestamp from
+ // missing data) yields an Invalid Date, and formatting one throws "Invalid
+ // time value" — crashing the whole tree. Compute nothing from it here and
+ // bail out below (after the hooks) instead.
+ const isValidDate = !Number.isNaN(date.getTime());
+ const isoString = isValidDate ? date.toISOString() : '';
// Determine effective format
const diffSeconds = Math.round((now.getTime() - date.getTime()) / 1000);
@@ -372,19 +377,20 @@ export function Timestamp({
: format;
// Format the display text
- const displayText =
- effectiveFormat === 'relative'
+ const displayText = !isValidDate
+ ? ''
+ : effectiveFormat === 'relative'
? getRelativeTimeString(date, now)
: isAbsoluteFormat(effectiveFormat)
? formatTimestamp(date, effectiveFormat, isTimezoneShown)
: '';
// Full absolute text for tooltip and aria-label
- const fullAbsoluteText = getFullAbsoluteString(date);
+ const fullAbsoluteText = isValidDate ? getFullAbsoluteString(date) : '';
// Live updates
useEffect(() => {
- if (!isLive || effectiveFormat !== 'relative') {
+ if (!isLive || !isValidDate || effectiveFormat !== 'relative') {
return;
}
@@ -394,7 +400,15 @@ export function Timestamp({
}, interval);
return () => clearInterval(timer);
- }, [isLive, effectiveFormat, diffSeconds]);
+ }, [isLive, isValidDate, effectiveFormat, diffSeconds]);
+
+ // Placed after all hooks so the hook order stays stable across renders.
+ if (!isValidDate) {
+ console.warn(
+ `Timestamp: could not parse value ${JSON.stringify(value)} as a date. Rendering nothing.`,
+ );
+ return null;
+ }
const showTooltip = hasTooltip && effectiveFormat === 'relative';