-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30044 from storybookjs/ui-fixes
Core: Fix `scrollIntoView` behavior and reimplement testing module time rendering
- Loading branch information
Showing
5 changed files
with
107 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { RelativeTime } from './RelativeTime'; | ||
|
||
const meta = { | ||
component: RelativeTime, | ||
} satisfies Meta<typeof RelativeTime>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const JustNow: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 10, | ||
}, | ||
}; | ||
|
||
export const AMinuteAgo: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 60, | ||
}, | ||
}; | ||
|
||
export const MinutesAgo: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 60 * 2, | ||
}, | ||
}; | ||
|
||
export const HoursAgo: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 60 * 60 * 3, | ||
}, | ||
}; | ||
|
||
export const Yesterday: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 60 * 60 * 24, | ||
}, | ||
}; | ||
|
||
export const DaysAgo: Story = { | ||
args: { | ||
timestamp: Date.now() - 1000 * 60 * 60 * 24 * 3, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,35 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
export function getRelativeTimeString(date: Date): string { | ||
const delta = Math.round((date.getTime() - Date.now()) / 1000); | ||
const cutoffs = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity]; | ||
const units: Intl.RelativeTimeFormatUnit[] = [ | ||
'second', | ||
'minute', | ||
'hour', | ||
'day', | ||
'week', | ||
'month', | ||
'year', | ||
]; | ||
|
||
const unitIndex = cutoffs.findIndex((cutoff) => cutoff > Math.abs(delta)); | ||
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1; | ||
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' }); | ||
return rtf.format(Math.floor(delta / divisor), units[unitIndex]); | ||
} | ||
|
||
export const RelativeTime = ({ timestamp, testCount }: { timestamp: Date; testCount: number }) => { | ||
const [relativeTimeString, setRelativeTimeString] = useState(null); | ||
export const RelativeTime = ({ timestamp }: { timestamp?: number }) => { | ||
const [timeAgo, setTimeAgo] = useState(null); | ||
|
||
useEffect(() => { | ||
if (timestamp) { | ||
setRelativeTimeString(getRelativeTimeString(timestamp).replace(/^now$/, 'just now')); | ||
|
||
const interval = setInterval(() => { | ||
setRelativeTimeString(getRelativeTimeString(timestamp).replace(/^now$/, 'just now')); | ||
}, 10000); | ||
|
||
setTimeAgo(Date.now() - timestamp); | ||
const interval = setInterval(() => setTimeAgo(Date.now() - timestamp), 10000); | ||
return () => clearInterval(interval); | ||
} | ||
}, [timestamp]); | ||
|
||
return ( | ||
relativeTimeString && | ||
`Ran ${testCount} ${testCount === 1 ? 'test' : 'tests'} ${relativeTimeString}` | ||
); | ||
if (timeAgo === null) { | ||
return null; | ||
} | ||
|
||
const seconds = Math.round(timeAgo / 1000); | ||
if (seconds < 60) { | ||
return `just now`; | ||
} | ||
|
||
const minutes = Math.floor(seconds / 60); | ||
if (minutes < 60) { | ||
return minutes === 1 ? `a minute ago` : `${minutes} minutes ago`; | ||
} | ||
|
||
const hours = Math.floor(minutes / 60); | ||
if (hours < 24) { | ||
return hours === 1 ? `an hour ago` : `${hours} hours ago`; | ||
} | ||
|
||
const days = Math.floor(hours / 24); | ||
return days === 1 ? `yesterday` : `${days} days ago`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters