Skip to content
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
2 changes: 2 additions & 0 deletions src/__mocks__/mockedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLSearch,
Discussion,
} from '../typesGithub';
import Constants from '../utils/constants';

export const mockedEnterpriseAccounts: EnterpriseAccount[] = [
{
Expand All @@ -21,6 +22,7 @@ export const mockedUser: GitifyUser = {

// prettier-ignore
export const mockedSingleNotification: Notification = {
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
id: '138661096',
unread: true,
reason: 'subscribed',
Expand Down
30 changes: 20 additions & 10 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getEnterpriseAccountToken,
generateGitHubAPIUrl,
isEnterpriseHost,
getTokenForHost,
} from '../utils/helpers';
import { removeNotification } from '../utils/remove-notification';
import {
Expand Down Expand Up @@ -97,7 +98,14 @@ export const useNotifications = (colors: boolean): NotificationsState => {
const { hostname } = new URL(accountNotifications.config.url);
return {
hostname,
notifications: accountNotifications.data,
notifications: accountNotifications.data.map(
(notification) => {
return {
...notification,
hostname: hostname,
};
},
),
};
},
);
Expand All @@ -106,7 +114,14 @@ export const useNotifications = (colors: boolean): NotificationsState => {
...enterpriseNotifications,
{
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
notifications: gitHubNotifications.data,
notifications: gitHubNotifications.data.map(
(notification) => {
return {
...notification,
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
};
},
),
},
]
: [...enterpriseNotifications];
Expand All @@ -131,15 +146,10 @@ export const useNotifications = (colors: boolean): NotificationsState => {
.all<Notification>(
accountNotifications.notifications.map(
async (notification: Notification) => {
const isEnterprise = isEnterpriseHost(
accountNotifications.hostname,
const token = getTokenForHost(
notification.hostname,
accounts,
);
const token = isEnterprise
? getEnterpriseAccountToken(
accountNotifications.hostname,
accounts.enterpriseAccounts,
)
: accounts.token;

const additionalSubjectDetails =
await getGitifySubjectDetails(
Expand Down
1 change: 1 addition & 0 deletions src/routes/__snapshots__/Notifications.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`routes/Notifications.tsx should render itself & its children (with noti
notifications={
[
{
"hostname": "github.com",
"id": "138661096",
"last_read_at": "2017-05-20T17:06:51Z",
"reason": "subscribed",
Expand Down
1 change: 1 addition & 0 deletions src/typesGithub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ 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
}

export type UserDetails = User & UserProfile;
Expand Down
19 changes: 13 additions & 6 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import { openExternalLink } from '../utils/comms';
import { Constants } from './constants';
import { getWorkflowRunAttributes, getCheckSuiteAttributes } from './subject';

export function getTokenForHost(hostname: string, accounts: AuthState): string {
const isEnterprise = isEnterpriseHost(hostname);
const token = isEnterprise
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
: accounts.token;

return token;
}

export function getEnterpriseAccountToken(
hostname: string,
accounts: EnterpriseAccount[],
Expand Down Expand Up @@ -244,22 +253,20 @@ export async function generateGitHubWebUrl(
accounts: AuthState,
): Promise<string> {
let url: string;
const token = getTokenForHost(notification.hostname, accounts);

if (notification.subject.latest_comment_url) {
url = await getHtmlUrl(
notification.subject.latest_comment_url,
accounts.token,
);
url = await getHtmlUrl(notification.subject.latest_comment_url, token);
} else if (notification.subject.url) {
url = await getHtmlUrl(notification.subject.url, accounts.token);
url = await getHtmlUrl(notification.subject.url, token);
} else {
// Perform any specific notification type handling (only required for a few special notification scenarios)
switch (notification.subject.type) {
case 'CheckSuite':
url = getCheckSuiteUrl(notification);
break;
case 'Discussion':
url = await getDiscussionUrl(notification, accounts.token);
url = await getDiscussionUrl(notification, token);
break;
case 'RepositoryInvitation':
url = `${notification.repository.html_url}/invitations`;
Expand Down