From 29dc1f1e7c5b5a23cc62d7221bb2e9eefc1530dc Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Wed, 13 Aug 2025 17:58:01 -0400 Subject: [PATCH 1/3] fix: find nearest discussion comment or reply Signed-off-by: Adam Setch --- src/renderer/utils/api/client.ts | 4 ++-- src/renderer/utils/helpers.ts | 12 +++++++----- src/renderer/utils/subject.ts | 32 ++++++++++++++++++++++++-------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/renderer/utils/api/client.ts b/src/renderer/utils/api/client.ts index 18b396128..2dfb466e9 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, 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..9336c8a73 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; + }); - // Return latest comment if no replies - return comments[0]; + return closestComment; } async function getGitifySubjectForIssue( From 6f688c8d270ff978bd572ed8bd31121aa9c82e37 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sun, 17 Aug 2025 10:33:27 -0400 Subject: [PATCH 2/3] update mock Signed-off-by: Adam Setch --- src/renderer/utils/api/__mocks__/response-mocks.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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, }, ], From dc102bcfdb8b0c0550b3cb551f32ba78c2510cb1 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sun, 17 Aug 2025 14:38:01 -0400 Subject: [PATCH 3/3] Merge remote-tracking branch 'origin/main' into fix/discussion-replies-nearest Signed-off-by: Adam Setch --- src/renderer/utils/subject.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/utils/subject.ts b/src/renderer/utils/subject.ts index 9336c8a73..def607af1 100644 --- a/src/renderer/utils/subject.ts +++ b/src/renderer/utils/subject.ts @@ -217,7 +217,7 @@ export function getClosestDiscussionCommentOrReply( differenceInMilliseconds(curr.createdAt, targetTimestamp), ); return currDiff < prevDiff ? curr : prev; - }); + }, allCommentsAndReplies[0]); return closestComment; }