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: 1 addition & 1 deletion src/context/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useContext } from 'react';
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
import { useNotifications } from '../hooks/useNotifications';
import type { AuthState, SettingsState } from '../types';
import * as apiRequests from '../utils/api-requests';
import * as apiRequests from '../utils/api/request';
import * as comms from '../utils/comms';
import Constants from '../utils/constants';
import * as notifications from '../utils/notifications';
Expand Down
10 changes: 3 additions & 7 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import {
type SettingsState,
Theme,
} from '../types';
import { apiRequestAuth } from '../utils/api-requests';
import { headNotifications } from '../utils/api/client';
import { addAccount, authGitHub, getToken, getUserData } from '../utils/auth';
import { setAutoLaunch, updateTrayTitle } from '../utils/comms';
import Constants from '../utils/constants';
import { generateGitHubAPIUrl } from '../utils/helpers';
import { getNotificationCount } from '../utils/notifications';
import { clearState, loadState, saveState } from '../utils/storage';
import { setTheme } from '../utils/theme';
Expand Down Expand Up @@ -166,11 +165,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {

const validateToken = useCallback(
async ({ token, hostname }: AuthTokenOptions) => {
await apiRequestAuth(
`${generateGitHubAPIUrl(hostname)}notifications`,
'HEAD',
token,
);
await headNotifications(hostname, token);

const user = await getUserData(token, hostname);
const updatedAccounts = addAccount(accounts, token, hostname, user);
setAccounts(updatedAccounts);
Expand Down
61 changes: 20 additions & 41 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { type AxiosError, type AxiosPromise } from 'axios';
import axios, { type AxiosError } from 'axios';
import { useCallback, useState } from 'react';

import type {
Expand All @@ -8,11 +8,16 @@ import type {
SettingsState,
} from '../types';
import type { GitHubRESTError, Notification } from '../typesGitHub';
import { apiRequestAuth } from '../utils/api-requests';
import {
ignoreNotificationThreadSubscription,
listNotificationsForAuthenticatedUser,
markNotificationThreadAsDone,
markNotificationThreadAsRead,
markRepositoryNotificationsAsRead,
} from '../utils/api/client';
import { determineFailureType } from '../utils/api/errors';
import Constants from '../utils/constants';
import {
generateGitHubAPIUrl,
getEnterpriseAccountToken,
getTokenForHost,
isEnterpriseHost,
Expand Down Expand Up @@ -74,28 +79,25 @@ export const useNotifications = (): NotificationsState => {

const fetchNotifications = useCallback(
async (accounts: AuthState, settings: SettingsState) => {
function getNotifications(
hostname: string,
token: string,
): AxiosPromise<Notification[]> {
const endpointSuffix = `notifications?participating=${settings.participating}`;
const url = `${generateGitHubAPIUrl(hostname)}${endpointSuffix}`;
return apiRequestAuth(url, 'GET', token);
}

function getGitHubNotifications() {
if (!isGitHubLoggedIn(accounts)) {
return;
}
return getNotifications(

return listNotificationsForAuthenticatedUser(
Constants.DEFAULT_AUTH_OPTIONS.hostname,
accounts.token,
settings,
);
}

function getEnterpriseNotifications() {
return accounts.enterpriseAccounts.map((account) => {
return getNotifications(account.hostname, account.token);
return listNotificationsForAuthenticatedUser(
account.hostname,
account.token,
settings,
);
});
}

Expand Down Expand Up @@ -220,12 +222,7 @@ export const useNotifications = (): NotificationsState => {
: accounts.token;

try {
await apiRequestAuth(
`${generateGitHubAPIUrl(hostname)}notifications/threads/${id}`,
'PATCH',
token,
{},
);
await markNotificationThreadAsRead(id, hostname, token);

const updatedNotifications = removeNotification(
id,
Expand Down Expand Up @@ -253,12 +250,7 @@ export const useNotifications = (): NotificationsState => {
: accounts.token;

try {
await apiRequestAuth(
`${generateGitHubAPIUrl(hostname)}notifications/threads/${id}`,
'DELETE',
token,
{},
);
await markNotificationThreadAsDone(id, hostname, token);

const updatedNotifications = removeNotification(
id,
Expand Down Expand Up @@ -286,14 +278,7 @@ export const useNotifications = (): NotificationsState => {
: accounts.token;

try {
await apiRequestAuth(
`${generateGitHubAPIUrl(
hostname,
)}notifications/threads/${id}/subscription`,
'PUT',
token,
{ ignored: true },
);
await ignoreNotificationThreadSubscription(id, hostname, token);
await markNotificationRead(accounts, id, hostname);
} catch (err) {
setIsFetching(false);
Expand All @@ -312,13 +297,7 @@ export const useNotifications = (): NotificationsState => {
: accounts.token;

try {
await apiRequestAuth(
`${generateGitHubAPIUrl(hostname)}repos/${repoSlug}/notifications`,
'PUT',
token,
{},
);

await markRepositoryNotificationsAsRead(repoSlug, hostname, token);
const updatedNotifications = removeNotifications(
repoSlug,
notifications,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/Settings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { AxiosResponse } from 'axios';
import { shell } from 'electron';
import { mockAccounts, mockSettings } from '../__mocks__/mock-state';
import { AppContext } from '../context/App';
import * as apiRequests from '../utils/api-requests';
import * as apiRequests from '../utils/api/request';
import Constants from '../utils/constants';
import { SettingsRoute } from './Settings';

Expand Down
8 changes: 3 additions & 5 deletions src/routes/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ import { Checkbox } from '../components/fields/Checkbox';
import { RadioGroup } from '../components/fields/RadioGroup';
import { AppContext } from '../context/App';
import { Theme } from '../types';
import { apiRequestAuth } from '../utils/api-requests';
import { getRootHypermediaLinks } from '../utils/api/client';
import {
openExternalLink,
updateTrayIcon,
updateTrayTitle,
} from '../utils/comms';
import Constants from '../utils/constants';
import { generateGitHubAPIUrl } from '../utils/helpers';
import { setTheme } from '../utils/theme';

export const SettingsRoute: FC = () => {
Expand Down Expand Up @@ -74,9 +73,8 @@ export const SettingsRoute: FC = () => {

useMemo(() => {
(async () => {
const response = await apiRequestAuth(
`${generateGitHubAPIUrl(Constants.DEFAULT_AUTH_OPTIONS.hostname)}`,
'GET',
const response = await getRootHypermediaLinks(
Constants.DEFAULT_AUTH_OPTIONS.hostname,
accounts.token,
);

Expand Down
60 changes: 54 additions & 6 deletions src/typesGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface UserProfile {
two_factor_authentication: boolean;
plan: Plan;
}

export interface Plan {
name: string;
space: number;
Expand Down Expand Up @@ -330,7 +331,8 @@ interface CommitFiles {
contents_url: string;
patch: string;
}
export interface CommitComments {

export interface CommitComment {
url: string;
html_url: string;
issue_url: string;
Expand Down Expand Up @@ -365,7 +367,7 @@ export interface Issue {
state_reason: IssueStateReasonType | null;
}

export interface IssueComments {
export interface IssueOrPullRequestComment {
url: string;
html_url: string;
issue_url: string;
Expand All @@ -377,20 +379,22 @@ export interface IssueComments {
body: string;
}

export interface ReleaseComments {
export interface Release {
url: string;
assets_url: string;
upload_url: string;
html_url: string;
id: number;
author: User;
node_id: string;
tag_name: string;
name: string;
target_commitish: string;
name: string | null;
body: string | null;
draft: boolean;
prerelease: boolean;
created_at: string;
published_at: string;
body: string;
published_at: string | null;
}

export interface GraphQLSearch<T> {
Expand Down Expand Up @@ -444,3 +448,47 @@ export interface GitHubRESTError {
message: string;
documentation_url: string;
}

export interface NotificationThreadSubscription {
subscribed: boolean;
ignored: boolean;
reason: string | null;
created_at: string;
url: string;
thread_url: string;
}

export interface RootHypermediaLinks {
current_user_url: string;
current_user_authorizations_html_url: string;
authorizations_url: string;
code_search_url: string;
commit_search_url: string;
emails_url: string;
emojis_url: string;
events_url: string;
feeds_url: string;
followers_url: string;
following_url: string;
gists_url: string;
hub_url: string;
issue_search_url: string;
issues_url: string;
keys_url: string;
notifications_url: string;
organization_url: string;
organization_repositories_url: string;
organization_teams_url: string;
public_gists_url: string;
rate_limit_url: string;
repository_url: string;
repository_search_url: string;
current_user_repositories_url: string;
starred_url: string;
starred_gists_url: string;
topic_search_url: string;
user_url: string;
user_organizations_url: string;
user_repositories_url: string;
user_search_url: string;
}
Loading