Skip to content

fix: remove notification from state on open #789

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

Merged
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
5 changes: 3 additions & 2 deletions src/components/NotificationRow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('components/Notification.js', () => {
});

it('should open a notification in the browser', () => {
const markNotification = jest.fn();
const removeNotificationFromState = jest.fn();

const props = {
notification: mockedSingleNotification,
Expand All @@ -42,7 +42,7 @@ describe('components/Notification.js', () => {
<AppContext.Provider
value={{
settings: { ...mockSettings, markAsDoneOnOpen: false },
markNotification,
removeNotificationFromState,
accounts: mockAccounts,
}}
>
Expand All @@ -52,6 +52,7 @@ describe('components/Notification.js', () => {

fireEvent.click(getByRole('main'));
expect(shell.openExternal).toHaveBeenCalledTimes(1);
expect(removeNotificationFromState).toHaveBeenCalledTimes(1);
});

it('should open a notification in browser & mark it as done', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const NotificationRow: React.FC<IProps> = ({
const {
settings,
accounts,
removeNotificationFromState,
markNotification,
markNotificationDone,
unsubscribeNotification,
Expand All @@ -33,6 +34,9 @@ export const NotificationRow: React.FC<IProps> = ({

if (settings.markAsDoneOnOpen) {
markNotificationDone(notification.id, hostname);
} else {
// no need to mark as read, github does it by default when opening it
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

removeNotificationFromState(notification.id, hostname);
}
}, [settings]);

Expand Down
3 changes: 3 additions & 0 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface AppContextState {
notifications: AccountNotifications[];
isFetching: boolean;
requestFailed: boolean;
removeNotificationFromState: (id: string, hostname: string) => void;
fetchNotifications: () => Promise<void>;
markNotification: (id: string, hostname: string) => Promise<void>;
markNotificationDone: (id: string, hostname: string) => Promise<void>;
Expand All @@ -71,6 +72,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
notifications,
requestFailed,
isFetching,
removeNotificationFromState,
markNotification,
markNotificationDone,
unsubscribeNotification,
Expand Down Expand Up @@ -210,6 +212,7 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
notifications,
isFetching,
requestFailed,
removeNotificationFromState,
fetchNotifications: fetchNotificationsWithAccounts,
markNotification: markNotificationWithAccounts,
markNotificationDone: markNotificationDoneWithAccounts,
Expand Down
36 changes: 36 additions & 0 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,42 @@ describe('hooks/useNotifications.ts', () => {
});
});

describe('removeNotificationFromState', () => {
it('should remove a notification from state', async () => {
const notifications = [
{ id: 1, title: 'This is a notification.' },
{ id: 2, title: 'This is another one.' },
];

nock('https://api.github.com')
.get('/notifications?participating=false')
.reply(200, notifications);

nock('https://github.gitify.io/api/v3')
.get('/notifications?participating=false')
.reply(200, notifications);

const { result } = renderHook(() => useNotifications(false));

act(() => {
result.current.fetchNotifications(mockAccounts, mockSettings);
});

await waitFor(() => {
expect(result.current.isFetching).toBe(false);
});

act(() => {
result.current.removeNotificationFromState(
result.current.notifications[0].notifications[0].id,
result.current.notifications[0].hostname,
);
});

expect(result.current.notifications[0].notifications.length).toBe(1);
});
});

describe('markNotification', () => {
const id = 'notification-123';

Expand Down
16 changes: 16 additions & 0 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { removeNotifications } from '../utils/remove-notifications';

interface NotificationsState {
notifications: AccountNotifications[];
removeNotificationFromState: (id: string, hostname: string) => void;
fetchNotifications: (
accounts: AuthState,
settings: SettingsState,
Expand Down Expand Up @@ -312,11 +313,26 @@ export const useNotifications = (colors: boolean): NotificationsState => {
[notifications],
);

const removeNotificationFromState = useCallback(
(id, hostname) => {
const updatedNotifications = removeNotification(
id,
notifications,
hostname,
);

setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
},
[notifications],
);

return {
isFetching,
requestFailed,
notifications,

removeNotificationFromState,
fetchNotifications,
markNotification,
markNotificationDone,
Expand Down