Skip to content

Commit e41438a

Browse files
authored
refactor(discussions): use API to get latest comment and reply (#1094)
1 parent 2947c0a commit e41438a

File tree

3 files changed

+13
-76
lines changed

3 files changed

+13
-76
lines changed

src/__mocks__/mockedData.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -404,44 +404,6 @@ const mockDiscussionReplier: DiscussionAuthor = {
404404

405405
export const mockDiscussionComments: DiscussionComments = {
406406
nodes: [
407-
{
408-
databaseId: 2215656,
409-
createdAt: '2022-02-20T18:33:39Z',
410-
author: mockDiscussionAuthor,
411-
replies: {
412-
nodes: [],
413-
},
414-
},
415-
{
416-
databaseId: 2217789,
417-
createdAt: '2022-02-21T03:30:42Z',
418-
author: mockDiscussionAuthor,
419-
replies: {
420-
nodes: [],
421-
},
422-
},
423-
{
424-
databaseId: 2223243,
425-
createdAt: '2022-02-21T18:26:27Z',
426-
author: mockDiscussionAuthor,
427-
replies: {
428-
nodes: [
429-
{
430-
databaseId: 2232922,
431-
createdAt: '2022-02-23T00:57:58Z',
432-
author: mockDiscussionReplier,
433-
},
434-
],
435-
},
436-
},
437-
{
438-
databaseId: 2232921,
439-
createdAt: '2022-02-23T00:57:49Z',
440-
author: mockDiscussionAuthor,
441-
replies: {
442-
nodes: [],
443-
},
444-
},
445407
{
446408
databaseId: 2258799,
447409
createdAt: '2022-02-27T01:22:20Z',
@@ -456,35 +418,6 @@ export const mockDiscussionComments: DiscussionComments = {
456418
],
457419
},
458420
},
459-
{
460-
databaseId: 2297637,
461-
createdAt: '2022-03-04T20:39:44Z',
462-
author: mockDiscussionAuthor,
463-
464-
replies: {
465-
nodes: [
466-
{
467-
databaseId: 2300893,
468-
createdAt: '2022-03-05T17:41:04Z',
469-
author: mockDiscussionReplier,
470-
},
471-
],
472-
},
473-
},
474-
{
475-
databaseId: 2299763,
476-
createdAt: '2022-03-05T11:05:42Z',
477-
author: mockDiscussionAuthor,
478-
replies: {
479-
nodes: [
480-
{
481-
databaseId: 2300895,
482-
createdAt: '2022-03-05T17:41:44Z',
483-
author: mockDiscussionReplier,
484-
},
485-
],
486-
},
487-
},
488421
],
489422
};
490423

src/utils/api/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export async function searchDiscussions(
253253
notification.updated_at,
254254
),
255255
firstDiscussions: 10,
256-
lastComments: 100,
256+
lastComments: 1,
257257
lastReplies: 1,
258258
},
259259
});

src/utils/helpers.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ async function getDiscussionUrl(
117117

118118
const comments = discussion.comments.nodes;
119119

120-
const latestCommentId = getLatestDiscussionComment(comments)?.databaseId;
120+
const latestComment = getLatestDiscussionComment(comments);
121121

122-
if (latestCommentId) {
123-
url += `#discussioncomment-${latestCommentId}`;
122+
if (latestComment) {
123+
url += `#discussioncomment-${latestComment.databaseId}`;
124124
}
125125
}
126126

@@ -139,10 +139,11 @@ export async function fetchDiscussion(
139139
(discussion) => discussion.title === notification.subject.title,
140140
) || [];
141141

142-
if (discussions.length > 1)
142+
if (discussions.length > 1) {
143143
discussions = discussions.filter(
144144
(discussion) => discussion.viewerSubscription === 'SUBSCRIBED',
145145
);
146+
}
146147

147148
return discussions[0];
148149
} catch (err) {}
@@ -155,10 +156,13 @@ export function getLatestDiscussionComment(
155156
return null;
156157
}
157158

158-
return comments
159-
.flatMap((comment) => comment.replies.nodes)
160-
.concat([comments[comments.length - 1]])
161-
.reduce((a, b) => (a.createdAt > b.createdAt ? a : b));
159+
// Return latest reply if available
160+
if (comments[0].replies.nodes.length === 1) {
161+
return comments[0].replies.nodes[0];
162+
}
163+
164+
// Return latest comment if no replies
165+
return comments[0];
162166
}
163167

164168
export async function generateGitHubWebUrl(

0 commit comments

Comments
 (0)