Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/components/ReleaseTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Link } from '@remix-run/react';
import { formatDistance } from 'date-fns';
import { ElectronRelease } from '~/data/release-data';
import { humanFriendlyDaysSince, prettyReleaseDate } from '~/helpers/time';
import { prettyDateString, prettyReleaseDate } from '~/helpers/time';

type ReleaseTableProps = {
releases: (ElectronRelease | undefined)[];
Expand Down Expand Up @@ -91,10 +92,16 @@ export const ReleaseTable = ({
prefetch="intent"
>
<span className="flex items-center gap-2">
<span>{humanFriendlyDaysSince(release)}</span>
<span className="text-xs px-2 py-0.5 bg-gray-100 dark:bg-gray-700 rounded-full">
<span title={prettyDateString(release.fullDate, timeZone)}>
{prettyReleaseDate(release, timeZone)}
</span>
<span className="text-xs px-2 py-0.5 bg-gray-100 dark:bg-gray-700 rounded-full">
{release.fullDate
? formatDistance(release.fullDate, Date.now(), {
addSuffix: true,
})
: ''}
</span>
</span>
</Link>
</div>
Expand Down
70 changes: 1 addition & 69 deletions app/helpers/time.test.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,5 @@
import { describe, expect, test } from 'vitest';
import { humanFriendlyDaysSince, prettyDateString, prettyReleaseDate } from './time';

describe('humanFriendlyDaysSince', () => {
test('should return "Today" for today\'s date', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-02-02T00:00:00Z' },
new Date('2020-02-02T00:00:00Z'),
),
).toBe('Today');
});

test('should return "Yesterday" for yesterday\'s date', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-02-01T00:00:00Z' },
new Date('2020-02-02T00:00:00Z'),
),
).toBe('Yesterday');
});

test('should return "N days ago" for dates in the past', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-01-30T00:00:00Z' },
new Date('2020-02-02T00:00:00Z'),
),
).toBe('3 days ago');
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-01-31T00:00:00Z' },
new Date('2020-02-02T00:00:00Z'),
),
).toBe('2 days ago');
});

test('should return "N days ago" for dates in the past over month boundaries', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-01-30T00:00:00Z' },
new Date('2020-03-02T00:00:00Z'),
),
).toBe('32 days ago');
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-01-31T00:00:00Z' },
new Date('2020-03-02T00:00:00Z'),
),
).toBe('31 days ago');
});

test('should return "Tomorrow" for tomorrow', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-03-04T00:00:00Z' },
new Date('2020-03-03T00:00:00Z'),
),
).toBe('Tomorrow');
});

test('should return "N days in the future" for dates in the future', () => {
expect(
humanFriendlyDaysSince(
{ fullDate: '2020-03-04T00:00:00Z' },
new Date('2020-02-01T00:00:00Z'),
),
).toBe('32 days in the future');
});
});
import { prettyDateString, prettyReleaseDate } from './time';

describe('prettyReleaseDate', () => {
test('should format date correctly', () => {
Expand Down
20 changes: 0 additions & 20 deletions app/helpers/time.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import { ElectronRelease } from '~/data/release-data';

/**
* @param date A date string from an ElectronRelease, or any date string in the format
* YYYY-MM-DD
* @returns A human friendly string describing how many days ago the date was
*/
export const humanFriendlyDaysSince = (
release: Pick<ElectronRelease, 'fullDate'>,
currentDate = new Date(),
) => {
const releaseDate = new Date(release.fullDate);
const daysAgo = Math.floor(
(currentDate.getTime() - releaseDate.getTime()) / (1_000 * 60 * 60 * 24),
);
if (daysAgo === 0) return 'Today';
if (daysAgo === 1) return 'Yesterday';
if (daysAgo === -1) return 'Tomorrow';
if (daysAgo < 0) return `${-daysAgo} days in the future`;
return `${daysAgo} days ago`;
};

/**
* This function requires a timezone, do not call it from inside a memoized or cached
* data function. Return a date string from those functions and in the loader apply
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@remix-run/serve": "^2.16.5",
"cookie": "^1.0.2",
"cross-env": "^7.0.3",
"date-fns": "^4.1.0",
"dompurify": "^3.2.5",
"geoip-lite": "^1.4.10",
"isbot": "^5.1.26",
Expand Down