Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use last_read_at timestamp when available #1103

Merged
merged 3 commits into from
May 13, 2024
Merged
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
17 changes: 17 additions & 0 deletions src/components/NotificationRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ describe('components/NotificationRow.tsx', () => {
expect(tree).toMatchSnapshot();
});

it('should render itself & its children when last_read_at is null', async () => {
jest
.spyOn(global.Date, 'now')
.mockImplementation(() => new Date('2024').valueOf());

const mockNotification = mockedSingleNotification;
mockNotification.last_read_at = null;

const props = {
notification: mockNotification,
hostname: 'github.com',
};

const tree = TestRenderer.create(<NotificationRow {...props} />);
expect(tree).toMatchSnapshot();
});

it('should render itself & its children without avatar', async () => {
jest
.spyOn(global.Date, 'now')
Expand Down
11 changes: 6 additions & 5 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
FeedPersonIcon,
ReadIcon,
} from '@primer/octicons-react';
import { formatDistanceToNow, parseISO } from 'date-fns';
import {
type FC,
type KeyboardEvent,
Expand All @@ -16,7 +15,11 @@ import {
import { AppContext } from '../context/App';
import type { Notification } from '../typesGitHub';
import { openExternalLink } from '../utils/comms';
import { formatForDisplay, openInBrowser } from '../utils/helpers';
import {
formatForDisplay,
formatNotificationUpdatedAt,
openInBrowser,
} from '../utils/helpers';
import {
getNotificationTypeIcon,
getNotificationTypeIconColor,
Expand Down Expand Up @@ -71,9 +74,7 @@ export const NotificationRow: FC<IProps> = ({ notification, hostname }) => {
const NotificationIcon = getNotificationTypeIcon(notification.subject);
const iconColor = getNotificationTypeIconColor(notification.subject);

const updatedAt = formatDistanceToNow(parseISO(notification.updated_at), {
addSuffix: true,
});
const updatedAt = formatNotificationUpdatedAt(notification);
const updatedLabel = notification.subject.user
? `${notification.subject.user.login} updated ${updatedAt}`
: `Updated ${updatedAt}`;
Expand Down
232 changes: 232 additions & 0 deletions src/components/__snapshots__/NotificationRow.test.tsx.snap

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

11 changes: 11 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { formatDistanceToNow, parseISO } from 'date-fns';
import type { AuthState } from '../types';
import type {
Discussion,
Expand Down Expand Up @@ -203,6 +204,16 @@ export function formatForDisplay(text: string[]): string {
});
}

export function formatNotificationUpdatedAt(
notification: Notification,
): string {
const date = notification.last_read_at ?? notification.updated_at;

return formatDistanceToNow(parseISO(date), {
addSuffix: true,
});
}

export async function openInBrowser(
notification: Notification,
accounts: AuthState,
Expand Down