-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the admin page somewhat simpler by removing the complex table de…
…pendency and removing the date time formatting dependency
- Loading branch information
Showing
6 changed files
with
208 additions
and
232 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,36 @@ | ||
import React from "react"; | ||
|
||
function formatRelativeTime(date) { | ||
const now = new Date(); | ||
const seconds = Math.floor((now - date) / 1000); | ||
|
||
const units = [ | ||
{ max: 60, value: 1, name: "second" }, | ||
{ max: 3600, value: 60, name: "minute" }, | ||
{ max: 86400, value: 3600, name: "hour" }, | ||
{ max: 2592000, value: 86400, name: "day" }, | ||
{ max: 31536000, value: 2592000, name: "month" }, | ||
{ max: Infinity, value: 31536000, name: "year" }, | ||
]; | ||
|
||
const { value, name } = units.find((unit) => Math.abs(seconds) < unit.max); | ||
const relativeTime = Math.floor(seconds / value); | ||
|
||
return new Intl.RelativeTimeFormat("en", { numeric: "auto" }).format( | ||
-relativeTime, | ||
name | ||
); | ||
} | ||
|
||
function formatAbsoluteTime(date) { | ||
return new Intl.DateTimeFormat("en", { | ||
dateStyle: "long", | ||
timeStyle: "short", | ||
}).format(date); | ||
} | ||
|
||
export function ReadableTimestamp({ date }) { | ||
return ( | ||
<span title={formatAbsoluteTime(date)}>{formatRelativeTime(date)}</span> | ||
); | ||
} |
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,39 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import { ReadableTimestamp } from "./ReadableTimestamp"; | ||
import { describe, test, expect } from "vitest"; | ||
|
||
describe(`${ReadableTimestamp.name} Component`, () => { | ||
test.each([ | ||
{ | ||
date: new Date(Date.now() - 10 * 1000), | ||
expectedRegex: /10 seconds ago/i, | ||
}, | ||
{ | ||
date: new Date(Date.now() - 5 * 60 * 1000), | ||
expectedRegex: /5 minutes ago/i, | ||
}, | ||
{ | ||
date: new Date(Date.now() - 2 * 60 * 60 * 1000), | ||
expectedRegex: /2 hours ago/i, | ||
}, | ||
{ | ||
date: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), | ||
expectedRegex: /3 days ago/i, | ||
}, | ||
{ | ||
date: new Date(Date.now() - 1 * 30 * 24 * 60 * 60 * 1000), | ||
expectedRegex: /last month/i, | ||
}, | ||
{ | ||
date: new Date(Date.now() - 2 * 365 * 24 * 60 * 60 * 1000), | ||
expectedRegex: /2 years ago/i, | ||
}, | ||
])("renders correct relative time for date", ({ date, expectedRegex }) => { | ||
render(<ReadableTimestamp date={date} />); | ||
|
||
const spanElement = screen.getByText(expectedRegex); | ||
expect(spanElement).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.