Skip to content

Commit f13124d

Browse files
authored
refactor: replace axios.all and axios.spread (#1063)
* refactor: replace axios.all and axios.spread * refactor: replace axios.all and axios.spread
1 parent 846b24a commit f13124d

File tree

1 file changed

+85
-92
lines changed

1 file changed

+85
-92
lines changed

src/hooks/useNotifications.ts

+85-92
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import axios, { type AxiosError } from 'axios';
1+
import type { AxiosError } from 'axios';
22
import { useCallback, useState } from 'react';
33

44
import type {
@@ -102,105 +102,98 @@ export const useNotifications = (): NotificationsState => {
102102

103103
setStatus('loading');
104104

105-
return axios
106-
.all([getGitHubNotifications(), ...getEnterpriseNotifications()])
107-
.then(
108-
axios.spread((gitHubNotifications, ...entAccNotifications) => {
109-
const enterpriseNotifications = entAccNotifications.map(
110-
(accountNotifications) => {
111-
const { hostname } = new URL(accountNotifications.config.url);
112-
return {
113-
hostname,
114-
notifications: accountNotifications.data.map(
105+
return Promise.all([
106+
getGitHubNotifications(),
107+
...getEnterpriseNotifications(),
108+
])
109+
.then(([gitHubNotifications, ...entAccNotifications]) => {
110+
const enterpriseNotifications = entAccNotifications.map(
111+
(accountNotifications) => {
112+
const { hostname } = new URL(accountNotifications.config.url);
113+
return {
114+
hostname,
115+
notifications: accountNotifications.data.map(
116+
(notification: Notification) => {
117+
return {
118+
...notification,
119+
hostname: hostname,
120+
};
121+
},
122+
),
123+
};
124+
},
125+
);
126+
127+
const data = isGitHubLoggedIn(accounts)
128+
? [
129+
...enterpriseNotifications,
130+
{
131+
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
132+
notifications: gitHubNotifications.data.map(
115133
(notification: Notification) => {
116134
return {
117135
...notification,
118-
hostname: hostname,
136+
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
119137
};
120138
},
121139
),
122-
};
123-
},
124-
);
140+
},
141+
]
142+
: [...enterpriseNotifications];
143+
144+
Promise.all(
145+
data.map(async (accountNotifications) => {
146+
return {
147+
hostname: accountNotifications.hostname,
148+
notifications: await Promise.all<Notification>(
149+
accountNotifications.notifications.map(
150+
async (notification: Notification) => {
151+
if (!settings.detailedNotifications) {
152+
return notification;
153+
}
154+
155+
const token = getTokenForHost(
156+
notification.hostname,
157+
accounts,
158+
);
159+
160+
const additionalSubjectDetails =
161+
await getGitifySubjectDetails(notification, token);
125162

126-
const data = isGitHubLoggedIn(accounts)
127-
? [
128-
...enterpriseNotifications,
129-
{
130-
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
131-
notifications: gitHubNotifications.data.map(
132-
(notification: Notification) => {
133-
return {
134-
...notification,
135-
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
136-
};
137-
},
138-
),
139-
},
140-
]
141-
: [...enterpriseNotifications];
142-
143-
axios
144-
.all(
145-
data.map(async (accountNotifications) => {
146-
return {
147-
hostname: accountNotifications.hostname,
148-
notifications: await axios
149-
.all<Notification>(
150-
accountNotifications.notifications.map(
151-
async (notification: Notification) => {
152-
if (!settings.detailedNotifications) {
153-
return notification;
154-
}
155-
156-
const token = getTokenForHost(
157-
notification.hostname,
158-
accounts,
159-
);
160-
161-
const additionalSubjectDetails =
162-
await getGitifySubjectDetails(
163-
notification,
164-
token,
165-
);
166-
167-
return {
168-
...notification,
169-
subject: {
170-
...notification.subject,
171-
...additionalSubjectDetails,
172-
},
173-
};
174-
},
175-
),
176-
)
177-
.then((notifications) => {
178-
return notifications.filter((notification) => {
179-
if (
180-
!settings.showBots &&
181-
notification.subject?.user?.type === 'Bot'
182-
) {
183-
return false;
184-
}
185-
186-
return true;
187-
});
188-
}),
189-
};
163+
return {
164+
...notification,
165+
subject: {
166+
...notification.subject,
167+
...additionalSubjectDetails,
168+
},
169+
};
170+
},
171+
),
172+
).then((notifications) => {
173+
return notifications.filter((notification) => {
174+
if (
175+
!settings.showBots &&
176+
notification.subject?.user?.type === 'Bot'
177+
) {
178+
return false;
179+
}
180+
181+
return true;
182+
});
190183
}),
191-
)
192-
.then((parsedNotifications) => {
193-
setNotifications(parsedNotifications);
194-
triggerNativeNotifications(
195-
notifications,
196-
parsedNotifications,
197-
settings,
198-
accounts,
199-
);
200-
setStatus('success');
201-
});
202-
}),
203-
)
184+
};
185+
}),
186+
).then((parsedNotifications) => {
187+
setNotifications(parsedNotifications);
188+
triggerNativeNotifications(
189+
notifications,
190+
parsedNotifications,
191+
settings,
192+
accounts,
193+
);
194+
setStatus('success');
195+
});
196+
})
204197
.catch((err: AxiosError<GitHubRESTError>) => {
205198
setStatus('error');
206199
setErrorDetails(determineFailureType(err));

0 commit comments

Comments
 (0)