Skip to content

Commit

Permalink
fix: remove account notifications on logout (#1457)
Browse files Browse the repository at this point in the history
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
  • Loading branch information
setchy and afonsojramos authored Aug 12, 2024
1 parent 30cb5f5 commit e7b843b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const Sidebar: FC = () => {

<SidebarButton
title={`${notificationsCount} Unread Notifications`}
metric={notificationsCount}
metric={isLoggedIn ? notificationsCount : null}
icon={BellIcon}
onClick={() => openGitHubNotifications(primaryAccountHostname)}
/>
Expand Down
12 changes: 4 additions & 8 deletions src/components/__snapshots__/Sidebar.test.tsx.snap

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

8 changes: 7 additions & 1 deletion src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ interface AppContextState {
notifications: AccountNotifications[];
status: Status;
globalError: GitifyError;
removeAccountNotifications: (account: Account) => Promise<void>;
fetchNotifications: () => Promise<void>;
markNotificationRead: (notification: Notification) => Promise<void>;
markNotificationDone: (notification: Notification) => Promise<void>;
Expand All @@ -120,6 +121,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const [auth, setAuth] = useState<AuthState>(defaultAuth);
const [settings, setSettings] = useState<SettingsState>(defaultSettings);
const {
removeAccountNotifications,
fetchNotifications,
notifications,
globalError,
Expand All @@ -130,7 +132,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
markRepoNotificationsRead,
markRepoNotificationsDone,
} = useNotifications();

getNotificationCount;
useEffect(() => {
restoreSettings();
}, []);
Expand Down Expand Up @@ -239,6 +241,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

const logoutFromAccount = useCallback(
async (account: Account) => {
// Remove notifications for account
removeAccountNotifications(account);

// Remove from auth state
const updatedAuth = removeAccount(auth, account);
setAuth(updatedAuth);
saveState({ auth: updatedAuth, settings });
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import log from 'electron-log';
import { useCallback, useState } from 'react';
import type {
Account,
AccountNotifications,
GitifyError,
GitifyState,
Expand All @@ -25,6 +26,7 @@ import { removeNotifications } from '../utils/remove-notifications';

interface NotificationsState {
notifications: AccountNotifications[];
removeAccountNotifications: (account: Account) => Promise<void>;
fetchNotifications: (state: GitifyState) => Promise<void>;
markNotificationRead: (
state: GitifyState,
Expand Down Expand Up @@ -58,6 +60,21 @@ export const useNotifications = (): NotificationsState => {
[],
);

const removeAccountNotifications = useCallback(
async (account: Account) => {
setStatus('loading');

const updatedNotifications = notifications.filter(
(notification) => notification.account !== account,
);

setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
setStatus('success');
},
[notifications],
);

const fetchNotifications = useCallback(
async (state: GitifyState) => {
setStatus('loading');
Expand Down Expand Up @@ -258,6 +275,7 @@ export const useNotifications = (): NotificationsState => {
globalError,
notifications,

removeAccountNotifications,
fetchNotifications,
markNotificationRead,
markNotificationDone,
Expand Down

0 comments on commit e7b843b

Please sign in to comment.