Skip to content

Commit 36ad60d

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/performance-post-state
Signed-off-by: Adam Setch <adam.setch@outlook.com>
2 parents cd19159 + 3da45b9 commit 36ad60d

4 files changed

Lines changed: 40 additions & 17 deletions

File tree

src/renderer/utils/api/__mocks__/response-mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,13 @@ export const mockDiscussionComments: DiscussionComments = {
390390
nodes: [
391391
{
392392
databaseId: 2258799,
393-
createdAt: '2022-02-27T01:22:20Z',
393+
createdAt: '2017-02-20T17:51:57Z',
394394
author: mockDiscussionAuthor,
395395
replies: {
396396
nodes: [
397397
{
398398
databaseId: 2300902,
399-
createdAt: '2022-03-05T17:43:52Z',
399+
createdAt: '2017-05-20T17:51:57Z',
400400
author: mockDiscussionReplier,
401401
},
402402
],

src/renderer/utils/api/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ export async function searchDiscussions(
248248
notification.subject.title,
249249
),
250250
firstDiscussions: 1,
251-
lastComments: 1,
252-
lastReplies: 1,
251+
lastComments: 100,
252+
lastReplies: 100,
253253
firstLabels: 100,
254254
includeIsAnswered: isAnsweredDiscussionFeatureSupported(
255255
notification.account,

src/renderer/utils/helpers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { PlatformType } from './auth/types';
1212
import { Constants } from './constants';
1313
import {
1414
getCheckSuiteAttributes,
15-
getLatestDiscussionComment,
15+
getClosestDiscussionCommentOrReply,
1616
getWorkflowRunAttributes,
1717
} from './subject';
1818

@@ -93,10 +93,12 @@ async function getDiscussionUrl(notification: Notification): Promise<Link> {
9393
if (discussion) {
9494
url.href = discussion.url;
9595

96-
const latestComment = getLatestDiscussionComment(discussion.comments.nodes);
97-
98-
if (latestComment) {
99-
url.hash = `#discussioncomment-${latestComment.databaseId}`;
96+
const closestComment = getClosestDiscussionCommentOrReply(
97+
notification,
98+
discussion.comments.nodes,
99+
);
100+
if (closestComment) {
101+
url.hash = `#discussioncomment-${closestComment.databaseId}`;
100102
}
101103
}
102104

src/renderer/utils/subject.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { differenceInMilliseconds } from 'date-fns';
2+
13
import { logError } from '../../shared/logger';
24
import type { Link, SettingsState } from '../types';
35
import type {
@@ -181,7 +183,13 @@ async function getGitifySubjectForDiscussion(
181183
return null;
182184
}
183185

184-
const latestDiscussionComment = getLatestDiscussionComment(
186+
// Return early if this notification would be hidden by filters
187+
if (isStateFilteredOut(discussionState, settings)) {
188+
return null;
189+
}
190+
191+
const latestDiscussionComment = getClosestDiscussionCommentOrReply(
192+
notification,
185193
discussion.comments.nodes,
186194
);
187195

@@ -209,20 +217,33 @@ async function getGitifySubjectForDiscussion(
209217
};
210218
}
211219

212-
export function getLatestDiscussionComment(
220+
export function getClosestDiscussionCommentOrReply(
221+
notification: Notification,
213222
comments: DiscussionComment[],
214223
): DiscussionComment | null {
215224
if (!comments || comments.length === 0) {
216225
return null;
217226
}
218227

219-
// Return latest reply if available
220-
if (comments[0].replies.nodes.length === 1) {
221-
return comments[0].replies.nodes[0];
222-
}
228+
const targetTimestamp = notification.updated_at;
229+
230+
const allCommentsAndReplies = comments.flatMap((comment) => [
231+
comment,
232+
...comment.replies.nodes,
233+
]);
234+
235+
// Find the closest match using the target timestamp
236+
const closestComment = allCommentsAndReplies.reduce((prev, curr) => {
237+
const prevDiff = Math.abs(
238+
differenceInMilliseconds(prev.createdAt, targetTimestamp),
239+
);
240+
const currDiff = Math.abs(
241+
differenceInMilliseconds(curr.createdAt, targetTimestamp),
242+
);
243+
return currDiff < prevDiff ? curr : prev;
244+
}, allCommentsAndReplies[0]);
223245

224-
// Return latest comment if no replies
225-
return comments[0];
246+
return closestComment;
226247
}
227248

228249
async function getGitifySubjectForIssue(

0 commit comments

Comments
 (0)