1
- import axios , { type AxiosError , type AxiosPromise } from 'axios' ;
1
+ import axios , { type AxiosError } from 'axios' ;
2
2
import { useCallback , useState } from 'react' ;
3
3
4
4
import type {
@@ -8,11 +8,16 @@ import type {
8
8
SettingsState ,
9
9
} from '../types' ;
10
10
import type { GitHubRESTError , Notification } from '../typesGitHub' ;
11
- import { apiRequestAuth } from '../utils/api-requests' ;
11
+ import {
12
+ ignoreNotificationThreadSubscription ,
13
+ listNotificationsForAuthenticatedUser ,
14
+ markNotificationThreadAsDone ,
15
+ markNotificationThreadAsRead ,
16
+ markRepositoryNotificationsAsRead ,
17
+ } from '../utils/api/client' ;
12
18
import { determineFailureType } from '../utils/api/errors' ;
13
19
import Constants from '../utils/constants' ;
14
20
import {
15
- generateGitHubAPIUrl ,
16
21
getEnterpriseAccountToken ,
17
22
getTokenForHost ,
18
23
isEnterpriseHost ,
@@ -74,28 +79,25 @@ export const useNotifications = (): NotificationsState => {
74
79
75
80
const fetchNotifications = useCallback (
76
81
async ( accounts : AuthState , settings : SettingsState ) => {
77
- function getNotifications (
78
- hostname : string ,
79
- token : string ,
80
- ) : AxiosPromise < Notification [ ] > {
81
- const endpointSuffix = `notifications?participating=${ settings . participating } ` ;
82
- const url = `${ generateGitHubAPIUrl ( hostname ) } ${ endpointSuffix } ` ;
83
- return apiRequestAuth ( url , 'GET' , token ) ;
84
- }
85
-
86
82
function getGitHubNotifications ( ) {
87
83
if ( ! isGitHubLoggedIn ( accounts ) ) {
88
84
return ;
89
85
}
90
- return getNotifications (
86
+
87
+ return listNotificationsForAuthenticatedUser (
91
88
Constants . DEFAULT_AUTH_OPTIONS . hostname ,
92
89
accounts . token ,
90
+ settings ,
93
91
) ;
94
92
}
95
93
96
94
function getEnterpriseNotifications ( ) {
97
95
return accounts . enterpriseAccounts . map ( ( account ) => {
98
- return getNotifications ( account . hostname , account . token ) ;
96
+ return listNotificationsForAuthenticatedUser (
97
+ account . hostname ,
98
+ account . token ,
99
+ settings ,
100
+ ) ;
99
101
} ) ;
100
102
}
101
103
@@ -220,12 +222,7 @@ export const useNotifications = (): NotificationsState => {
220
222
: accounts . token ;
221
223
222
224
try {
223
- await apiRequestAuth (
224
- `${ generateGitHubAPIUrl ( hostname ) } notifications/threads/${ id } ` ,
225
- 'PATCH' ,
226
- token ,
227
- { } ,
228
- ) ;
225
+ await markNotificationThreadAsRead ( id , hostname , token ) ;
229
226
230
227
const updatedNotifications = removeNotification (
231
228
id ,
@@ -253,12 +250,7 @@ export const useNotifications = (): NotificationsState => {
253
250
: accounts . token ;
254
251
255
252
try {
256
- await apiRequestAuth (
257
- `${ generateGitHubAPIUrl ( hostname ) } notifications/threads/${ id } ` ,
258
- 'DELETE' ,
259
- token ,
260
- { } ,
261
- ) ;
253
+ await markNotificationThreadAsDone ( id , hostname , token ) ;
262
254
263
255
const updatedNotifications = removeNotification (
264
256
id ,
@@ -286,14 +278,7 @@ export const useNotifications = (): NotificationsState => {
286
278
: accounts . token ;
287
279
288
280
try {
289
- await apiRequestAuth (
290
- `${ generateGitHubAPIUrl (
291
- hostname ,
292
- ) } notifications/threads/${ id } /subscription`,
293
- 'PUT' ,
294
- token ,
295
- { ignored : true } ,
296
- ) ;
281
+ await ignoreNotificationThreadSubscription ( id , hostname , token ) ;
297
282
await markNotificationRead ( accounts , id , hostname ) ;
298
283
} catch ( err ) {
299
284
setIsFetching ( false ) ;
@@ -312,13 +297,7 @@ export const useNotifications = (): NotificationsState => {
312
297
: accounts . token ;
313
298
314
299
try {
315
- await apiRequestAuth (
316
- `${ generateGitHubAPIUrl ( hostname ) } repos/${ repoSlug } /notifications` ,
317
- 'PUT' ,
318
- token ,
319
- { } ,
320
- ) ;
321
-
300
+ await markRepositoryNotificationsAsRead ( repoSlug , hostname , token ) ;
322
301
const updatedNotifications = removeNotifications (
323
302
repoSlug ,
324
303
notifications ,
0 commit comments