Skip to content

Commit b40e1ac

Browse files
authored
feat: use last_read_at timestamp when available (#1103)
1 parent b6c9ae1 commit b40e1ac

File tree

4 files changed

+266
-5
lines changed

4 files changed

+266
-5
lines changed

src/components/NotificationRow.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ describe('components/NotificationRow.tsx', () => {
3434
expect(tree).toMatchSnapshot();
3535
});
3636

37+
it('should render itself & its children when last_read_at is null', async () => {
38+
jest
39+
.spyOn(global.Date, 'now')
40+
.mockImplementation(() => new Date('2024').valueOf());
41+
42+
const mockNotification = mockedSingleNotification;
43+
mockNotification.last_read_at = null;
44+
45+
const props = {
46+
notification: mockNotification,
47+
hostname: 'github.com',
48+
};
49+
50+
const tree = TestRenderer.create(<NotificationRow {...props} />);
51+
expect(tree).toMatchSnapshot();
52+
});
53+
3754
it('should render itself & its children without avatar', async () => {
3855
jest
3956
.spyOn(global.Date, 'now')

src/components/NotificationRow.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
FeedPersonIcon,
66
ReadIcon,
77
} from '@primer/octicons-react';
8-
import { formatDistanceToNow, parseISO } from 'date-fns';
98
import {
109
type FC,
1110
type KeyboardEvent,
@@ -18,7 +17,11 @@ import { AppContext } from '../context/App';
1817
import { IconColor } from '../types';
1918
import type { Notification } from '../typesGitHub';
2019
import { openExternalLink } from '../utils/comms';
21-
import { formatForDisplay, openInBrowser } from '../utils/helpers';
20+
import {
21+
formatForDisplay,
22+
formatNotificationUpdatedAt,
23+
openInBrowser,
24+
} from '../utils/helpers';
2225
import {
2326
getNotificationTypeIcon,
2427
getNotificationTypeIconColor,
@@ -73,9 +76,7 @@ export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
7376
const NotificationIcon = getNotificationTypeIcon(notification.subject);
7477
const iconColor = getNotificationTypeIconColor(notification.subject);
7578

76-
const updatedAt = formatDistanceToNow(parseISO(notification.updated_at), {
77-
addSuffix: true,
78-
});
79+
const updatedAt = formatNotificationUpdatedAt(notification);
7980
const updatedLabel = notification.subject.user
8081
? `${notification.subject.user.login} updated ${updatedAt}`
8182
: `Updated ${updatedAt}`;

src/components/__snapshots__/NotificationRow.test.tsx.snap

Lines changed: 232 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/helpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { formatDistanceToNow, parseISO } from 'date-fns';
12
import type { AuthState } from '../types';
23
import type {
34
Discussion,
@@ -203,6 +204,16 @@ export function formatForDisplay(text: string[]): string {
203204
});
204205
}
205206

207+
export function formatNotificationUpdatedAt(
208+
notification: Notification,
209+
): string {
210+
const date = notification.last_read_at ?? notification.updated_at;
211+
212+
return formatDistanceToNow(parseISO(date), {
213+
addSuffix: true,
214+
});
215+
}
216+
206217
export async function openInBrowser(
207218
notification: Notification,
208219
accounts: AuthState,

0 commit comments

Comments
 (0)