Skip to content

Commit 3da45b9

Browse files
authored
fix(discussions): find closest discussion comment or reply by timestamp (#2135)
* fix: find nearest discussion comment or reply Signed-off-by: Adam Setch <adam.setch@outlook.com> * update mock Signed-off-by: Adam Setch <adam.setch@outlook.com> * Merge remote-tracking branch 'origin/main' into fix/discussion-replies-nearest Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 27bb98a commit 3da45b9

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

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: 24 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 } from '../types';
35
import type {
@@ -162,7 +164,8 @@ async function getGitifySubjectForDiscussion(
162164
}
163165
}
164166

165-
const latestDiscussionComment = getLatestDiscussionComment(
167+
const latestDiscussionComment = getClosestDiscussionCommentOrReply(
168+
notification,
166169
discussion.comments.nodes,
167170
);
168171

@@ -190,20 +193,33 @@ async function getGitifySubjectForDiscussion(
190193
};
191194
}
192195

193-
export function getLatestDiscussionComment(
196+
export function getClosestDiscussionCommentOrReply(
197+
notification: Notification,
194198
comments: DiscussionComment[],
195199
): DiscussionComment | null {
196200
if (!comments || comments.length === 0) {
197201
return null;
198202
}
199203

200-
// Return latest reply if available
201-
if (comments[0].replies.nodes.length === 1) {
202-
return comments[0].replies.nodes[0];
203-
}
204+
const targetTimestamp = notification.updated_at;
205+
206+
const allCommentsAndReplies = comments.flatMap((comment) => [
207+
comment,
208+
...comment.replies.nodes,
209+
]);
210+
211+
// Find the closest match using the target timestamp
212+
const closestComment = allCommentsAndReplies.reduce((prev, curr) => {
213+
const prevDiff = Math.abs(
214+
differenceInMilliseconds(prev.createdAt, targetTimestamp),
215+
);
216+
const currDiff = Math.abs(
217+
differenceInMilliseconds(curr.createdAt, targetTimestamp),
218+
);
219+
return currDiff < prevDiff ? curr : prev;
220+
}, allCommentsAndReplies[0]);
204221

205-
// Return latest comment if no replies
206-
return comments[0];
222+
return closestComment;
207223
}
208224

209225
async function getGitifySubjectForIssue(

0 commit comments

Comments
 (0)