Skip to content

refactor: simplify useNotifications #1070

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 4 commits into from
Apr 30, 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
6 changes: 3 additions & 3 deletions src/__mocks__/mockedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const mockedCommenterUser: User = {
};

export const mockedSingleNotification: Notification = {
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
hostname: Constants.GITHUB_API_BASE_URL,
id: '138661096',
unread: true,
reason: 'subscribed',
Expand Down Expand Up @@ -249,7 +249,7 @@ export const mockedGitHubNotifications = [
url: 'https://api.github.com/notifications/threads/148827438',
subscription_url:
'https://api.github.com/notifications/threads/148827438/subscription',
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
hostname: Constants.GITHUB_API_BASE_URL,
} as Notification,
];

Expand Down Expand Up @@ -359,7 +359,7 @@ export const mockedEnterpriseNotifications = [
url: 'https://github.gitify.io/api/v3/notifications/threads/3',
subscription_url:
'https://github.gitify.io/api/v3/notifications/threads/3/subscription',
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
hostname: Constants.GITHUB_API_BASE_URL,
} as Notification,
];

Expand Down
21 changes: 13 additions & 8 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ describe('hooks/useNotifications.ts', () => {
expect(result.current.status).toBe('success');
});

expect(result.current.notifications[0].hostname).toBe(
expect(result.current.notifications[0].hostname).toBe('api.github.com');
expect(result.current.notifications[0].notifications.length).toBe(2);

expect(result.current.notifications[1].hostname).toBe(
'github.gitify.io',
);
expect(result.current.notifications[1].hostname).toBe('github.com');
expect(result.current.notifications[1].notifications.length).toBe(2);
});

it('should fetch notifications with failures - github.com & enterprise', async () => {
Expand Down Expand Up @@ -117,11 +120,12 @@ describe('hooks/useNotifications.ts', () => {
});

await waitFor(() => {
expect(result.current.notifications[0].hostname).toBe(
'github.gitify.io',
);
expect(result.current.status).toBe('success');
});

expect(result.current.notifications[0].hostname).toBe(
'github.gitify.io',
);
expect(result.current.notifications[0].notifications.length).toBe(2);
});

Expand Down Expand Up @@ -179,9 +183,10 @@ describe('hooks/useNotifications.ts', () => {
});

await waitFor(() => {
expect(result.current.notifications[0].hostname).toBe('github.com');
expect(result.current.status).toBe('success');
});

expect(result.current.notifications[0].hostname).toBe('api.github.com');
expect(result.current.notifications[0].notifications.length).toBe(2);
});

Expand Down Expand Up @@ -379,7 +384,7 @@ describe('hooks/useNotifications.ts', () => {
expect(result.current.status).toBe('success');
});

expect(result.current.notifications[0].hostname).toBe('github.com');
expect(result.current.notifications[0].hostname).toBe('api.github.com');
expect(result.current.notifications[0].notifications.length).toBe(6);
});
});
Expand Down Expand Up @@ -459,7 +464,7 @@ describe('hooks/useNotifications.ts', () => {
expect(result.current.status).toBe('success');
});

expect(result.current.notifications[0].hostname).toBe('github.com');
expect(result.current.notifications[0].hostname).toBe('api.github.com');
expect(result.current.notifications[0].notifications.length).toBe(1);
});
});
Expand Down
39 changes: 10 additions & 29 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,43 +106,24 @@ export const useNotifications = (): NotificationsState => {
getGitHubNotifications(),
...getEnterpriseNotifications(),
])
.then(([gitHubNotifications, ...entAccNotifications]) => {
const enterpriseNotifications = entAccNotifications.map(
(accountNotifications) => {
.then(([...responses]) => {
const accountsNotifications: AccountNotifications[] = responses
.filter((response) => !!response)
.map((accountNotifications) => {
const { hostname } = new URL(accountNotifications.config.url);
return {
hostname,
notifications: accountNotifications.data.map(
(notification: Notification) => {
return {
...notification,
hostname: hostname,
};
},
(notification: Notification) => ({
...notification,
hostname,
}),
),
};
},
);

const data = isGitHubLoggedIn(accounts)
? [
...enterpriseNotifications,
{
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
notifications: gitHubNotifications.data.map(
(notification: Notification) => {
return {
...notification,
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
};
},
),
},
]
: [...enterpriseNotifications];
});

Promise.all(
data.map(async (accountNotifications) => {
accountsNotifications.map(async (accountNotifications) => {
return {
hostname: accountNotifications.hostname,
notifications: await Promise.all<Notification>(
Expand Down
10 changes: 5 additions & 5 deletions src/routes/__snapshots__/Notifications.test.tsx.snap

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

3 changes: 2 additions & 1 deletion src/typesGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export interface Notification {
repository: Repository;
url: string;
subscription_url: string;
hostname: string; // This is not in the GitHub API, but we add it to the type to make it easier to work with
// TODO - rename this to apiBaseUrl
hostname: string; // This is not in the official GitHub API. We add this to make notification interactions easier.
}

export type UserDetails = User & UserProfile;
Expand Down