diff --git a/src/renderer/utils/api/__mocks__/response-mocks.ts b/src/renderer/utils/api/__mocks__/response-mocks.ts index 714635ab5..6aa43e4a9 100644 --- a/src/renderer/utils/api/__mocks__/response-mocks.ts +++ b/src/renderer/utils/api/__mocks__/response-mocks.ts @@ -390,13 +390,13 @@ export const mockDiscussionComments: DiscussionComments = { nodes: [ { databaseId: 2258799, - createdAt: '2022-02-27T01:22:20Z', + createdAt: '2017-02-20T17:51:57Z', author: mockDiscussionAuthor, replies: { nodes: [ { databaseId: 2300902, - createdAt: '2022-03-05T17:43:52Z', + createdAt: '2017-05-20T17:51:57Z', author: mockDiscussionReplier, }, ], diff --git a/src/renderer/utils/api/client.ts b/src/renderer/utils/api/client.ts index f6e9d8fc3..70f835f9c 100644 --- a/src/renderer/utils/api/client.ts +++ b/src/renderer/utils/api/client.ts @@ -248,8 +248,8 @@ export async function searchDiscussions( notification.subject.title, ), firstDiscussions: 1, - lastComments: 1, - lastReplies: 1, + lastComments: 100, + lastReplies: 100, firstLabels: 100, includeIsAnswered: isAnsweredDiscussionFeatureSupported( notification.account, diff --git a/src/renderer/utils/helpers.ts b/src/renderer/utils/helpers.ts index be4b47566..c207f3e2e 100644 --- a/src/renderer/utils/helpers.ts +++ b/src/renderer/utils/helpers.ts @@ -12,7 +12,7 @@ import type { PlatformType } from './auth/types'; import { Constants } from './constants'; import { getCheckSuiteAttributes, - getLatestDiscussionComment, + getClosestDiscussionCommentOrReply, getWorkflowRunAttributes, } from './subject'; @@ -93,10 +93,12 @@ async function getDiscussionUrl(notification: Notification): Promise { if (discussion) { url.href = discussion.url; - const latestComment = getLatestDiscussionComment(discussion.comments.nodes); - - if (latestComment) { - url.hash = `#discussioncomment-${latestComment.databaseId}`; + const closestComment = getClosestDiscussionCommentOrReply( + notification, + discussion.comments.nodes, + ); + if (closestComment) { + url.hash = `#discussioncomment-${closestComment.databaseId}`; } } diff --git a/src/renderer/utils/subject.ts b/src/renderer/utils/subject.ts index c0d4198ac..def607af1 100644 --- a/src/renderer/utils/subject.ts +++ b/src/renderer/utils/subject.ts @@ -1,3 +1,5 @@ +import { differenceInMilliseconds } from 'date-fns'; + import { logError } from '../../shared/logger'; import type { Link } from '../types'; import type { @@ -162,7 +164,8 @@ async function getGitifySubjectForDiscussion( } } - const latestDiscussionComment = getLatestDiscussionComment( + const latestDiscussionComment = getClosestDiscussionCommentOrReply( + notification, discussion.comments.nodes, ); @@ -190,20 +193,33 @@ async function getGitifySubjectForDiscussion( }; } -export function getLatestDiscussionComment( +export function getClosestDiscussionCommentOrReply( + notification: Notification, comments: DiscussionComment[], ): DiscussionComment | null { if (!comments || comments.length === 0) { return null; } - // Return latest reply if available - if (comments[0].replies.nodes.length === 1) { - return comments[0].replies.nodes[0]; - } + const targetTimestamp = notification.updated_at; + + const allCommentsAndReplies = comments.flatMap((comment) => [ + comment, + ...comment.replies.nodes, + ]); + + // Find the closest match using the target timestamp + const closestComment = allCommentsAndReplies.reduce((prev, curr) => { + const prevDiff = Math.abs( + differenceInMilliseconds(prev.createdAt, targetTimestamp), + ); + const currDiff = Math.abs( + differenceInMilliseconds(curr.createdAt, targetTimestamp), + ); + return currDiff < prevDiff ? curr : prev; + }, allCommentsAndReplies[0]); - // Return latest comment if no replies - return comments[0]; + return closestComment; } async function getGitifySubjectForIssue(