Skip to content

Commit

Permalink
feat: multi account gracefully degrade
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jul 27, 2024
1 parent ea350b1 commit b8bdd7e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/__mocks__/notifications-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ export const mockAccountNotifications: AccountNotifications[] = [
{
account: mockGitHubCloudAccount,
notifications: mockGitHubNotifications,
error: null,
},
{
account: mockGitHubEnterpriseServerAccount,
notifications: mockEnterpriseNotifications,
error: null,
},
];

export const mockSingleAccountNotifications: AccountNotifications[] = [
{
account: mockGitHubCloudAccount,
notifications: [mockGitHubNotifications[0]],
error: null,
},
];
5 changes: 4 additions & 1 deletion src/components/AccountNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import {
} from '@primer/octicons-react';
import { type FC, type MouseEvent, useContext, useState } from 'react';
import { AppContext } from '../context/App';
import { type Account, Opacity, Size } from '../types';
import { type Account, type GitifyError, Opacity, Size } from '../types';
import type { Notification } from '../typesGitHub';
import { cn } from '../utils/cn';
import { openAccountProfile } from '../utils/links';
import { HoverGroup } from './HoverGroup';
import { NotificationRow } from './NotificationRow';
import { Oops } from './Oops';
import { RepositoryNotifications } from './RepositoryNotifications';
import { InteractionButton } from './buttons/InteractionButton';
import { PlatformIcon } from './icons/PlatformIcon';

interface IAccountNotifications {
account: Account;
notifications: Notification[];
error?: GitifyError;
showAccountHostname: boolean;
}

Expand Down Expand Up @@ -103,6 +105,7 @@ export const AccountNotifications: FC<IAccountNotifications> = (

{showAccountNotifications && (
<>
{props?.error && <Oops error={props.error} />}
{groupByRepository
? Object.values(groupedNotifications).map((repoNotifications) => {
const repoSlug = repoNotifications[0].repository.full_name;
Expand Down
20 changes: 12 additions & 8 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
markNotificationThreadAsRead,
markRepositoryNotificationsAsRead,
} from '../utils/api/client';
import { determineFailureType } from '../utils/api/errors';
import { getAccountUUID } from '../utils/auth/utils';
import {
getAllNotifications,
Expand Down Expand Up @@ -61,16 +60,21 @@ export const useNotifications = (): NotificationsState => {
async (state: GitifyState) => {
setStatus('loading');

try {
const fetchedNotifications = await getAllNotifications(state);
const fetchedNotifications = await getAllNotifications(state);

setNotifications(fetchedNotifications);
triggerNativeNotifications(notifications, fetchedNotifications, state);
setStatus('success');
} catch (err) {
if (
fetchedNotifications.every((account) => {
return account.error !== null;
})
) {
setStatus('error');
setErrorDetails(determineFailureType(err));
setErrorDetails(fetchedNotifications[0].error);
return;
}

setNotifications(fetchedNotifications);
triggerNativeNotifications(notifications, fetchedNotifications, state);
setStatus('success');
},
[notifications],
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const NotificationsRoute: FC = () => {
key={getAccountUUID(accountNotifications.account)}
account={accountNotifications.account}
notifications={accountNotifications.notifications}
error={accountNotifications.error}
showAccountHostname={
hasMultipleAccounts || settings.showAccountHostname
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type RadioGroupItem = {
export interface AccountNotifications {
account: Account;
notifications: Notification[];
error: GitifyError | null;
}

export interface GitifyUser {
Expand Down
30 changes: 20 additions & 10 deletions src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from '../types';
import { Notification } from '../typesGitHub';
import { listNotificationsForAuthenticatedUser } from './api/client';
import { determineFailureType } from './api/errors';
import { getAccountUUID } from './auth/utils';
import { hideWindow, showWindow, updateTrayIcon } from './comms';
import { openNotification } from './links';
Expand Down Expand Up @@ -129,21 +130,30 @@ export async function getAllNotifications(
responses
.filter((response) => !!response)
.map(async (accountNotifications) => {
let notifications = (await accountNotifications.notifications).data.map(
(notification: Notification) => ({
try {
let notifications = (
await accountNotifications.notifications
).data.map((notification: Notification) => ({
...notification,
account: accountNotifications.account,
}),
);
}));

notifications = await enrichNotifications(notifications, state);
notifications = await enrichNotifications(notifications, state);

notifications = filterNotifications(notifications, state.settings);
notifications = filterNotifications(notifications, state.settings);

return {
account: accountNotifications.account,
notifications: notifications,
};
return {
account: accountNotifications.account,
notifications: notifications,
error: null,
};
} catch (error) {
return {
account: accountNotifications.account,
notifications: [],
error: determineFailureType(error),
};
}
}),
);

Expand Down

0 comments on commit b8bdd7e

Please sign in to comment.