Skip to content

Commit d630378

Browse files
committed
feat(filter): avoid further enrichment if state not visible
Signed-off-by: Adam Setch <[email protected]>
1 parent 72c771b commit d630378

File tree

2 files changed

+48
-61
lines changed

2 files changed

+48
-61
lines changed

src/renderer/utils/notifications/filters/filter.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SettingsState } from '../../../types';
2-
import type { Notification } from '../../../typesGitHub';
2+
import type { Notification, StateType } from '../../../typesGitHub';
33
import {
44
filterNotificationByHandle,
55
hasExcludeHandleFilters,
@@ -88,7 +88,7 @@ export function hasAnyFiltersSet(settings: SettingsState): boolean {
8888
);
8989
}
9090

91-
export function isNotificationStateVisible(
91+
function isNotificationStateVisible(
9292
notification: Notification,
9393
settings: SettingsState,
9494
): boolean {
@@ -100,3 +100,12 @@ export function isNotificationStateVisible(
100100

101101
return true;
102102
}
103+
104+
export function wouldStateBeHiddenByFilters(
105+
state: StateType,
106+
settings: SettingsState,
107+
): boolean {
108+
const notification = { subject: { state } } as Notification;
109+
110+
return !isNotificationStateVisible(notification, settings);
111+
}

src/renderer/utils/subject.ts

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
getPullRequestReviews,
2626
getRelease,
2727
} from './api/client';
28-
import { isNotificationStateVisible } from './notifications/filters/filter';
28+
import { wouldStateBeHiddenByFilters } from './notifications/filters/filter';
2929

3030
export async function getGitifySubjectDetails(
3131
notification: Notification,
@@ -165,22 +165,15 @@ async function getGitifySubjectForDiscussion(
165165
}
166166
}
167167

168-
let latestDiscussionComment: DiscussionComment;
169-
if (
170-
isNotificationStateVisible(
171-
{
172-
subject: {
173-
state: discussionState,
174-
},
175-
} as Notification,
176-
settings,
177-
)
178-
) {
179-
latestDiscussionComment = getLatestDiscussionComment(
180-
discussion.comments.nodes,
181-
);
168+
// Return early if this notification would be hidden by filters
169+
if (wouldStateBeHiddenByFilters(discussionState, settings)) {
170+
return null;
182171
}
183172

173+
const latestDiscussionComment = getLatestDiscussionComment(
174+
discussion.comments.nodes,
175+
);
176+
184177
let discussionUser: SubjectUser = {
185178
login: discussion.author.login,
186179
html_url: discussion.author.url,
@@ -231,27 +224,21 @@ async function getGitifySubjectForIssue(
231224

232225
const issueState = issue.state_reason ?? issue.state;
233226

227+
// Return early if this notification would be hidden by filters
228+
if (wouldStateBeHiddenByFilters(issueState, settings)) {
229+
return null;
230+
}
231+
234232
let issueCommentUser: User;
235233

236-
if (
237-
isNotificationStateVisible(
238-
{
239-
subject: {
240-
state: issueState,
241-
},
242-
} as Notification,
243-
settings,
244-
)
245-
) {
246-
if (notification.subject.latest_comment_url) {
247-
const issueComment = (
248-
await getIssueOrPullRequestComment(
249-
notification.subject.latest_comment_url,
250-
notification.account.token,
251-
)
252-
).data;
253-
issueCommentUser = issueComment.user;
254-
}
234+
if (notification.subject.latest_comment_url) {
235+
const issueComment = (
236+
await getIssueOrPullRequestComment(
237+
notification.subject.latest_comment_url,
238+
notification.account.token,
239+
)
240+
).data;
241+
issueCommentUser = issueComment.user;
255242
}
256243

257244
return {
@@ -280,36 +267,27 @@ async function getGitifySubjectForPullRequest(
280267
}
281268

282269
let prCommentUser: User;
283-
let reviews: GitifyPullRequestReview[];
284-
let linkedIssues: string[];
285270

271+
// Return early if this notification would be hidden by filters
272+
if (wouldStateBeHiddenByFilters(prState, settings)) {
273+
return null;
274+
}
286275
if (
287-
isNotificationStateVisible(
288-
{
289-
subject: {
290-
state: prState,
291-
},
292-
} as Notification,
293-
settings,
294-
)
276+
notification.subject.latest_comment_url &&
277+
notification.subject.latest_comment_url !== notification.subject.url
295278
) {
296-
if (
297-
notification.subject.latest_comment_url &&
298-
notification.subject.latest_comment_url !== notification.subject.url
299-
) {
300-
const prComment = (
301-
await getIssueOrPullRequestComment(
302-
notification.subject.latest_comment_url,
303-
notification.account.token,
304-
)
305-
).data;
306-
prCommentUser = prComment.user;
307-
}
308-
309-
reviews = await getLatestReviewForReviewers(notification);
310-
linkedIssues = parseLinkedIssuesFromPr(pr);
279+
const prComment = (
280+
await getIssueOrPullRequestComment(
281+
notification.subject.latest_comment_url,
282+
notification.account.token,
283+
)
284+
).data;
285+
prCommentUser = prComment.user;
311286
}
312287

288+
const reviews = await getLatestReviewForReviewers(notification);
289+
const linkedIssues = parseLinkedIssuesFromPr(pr);
290+
313291
return {
314292
number: pr.number,
315293
state: prState,

0 commit comments

Comments
 (0)