Skip to content

Commit 09020ae

Browse files
committed
fix discussions-url
1 parent f8a6ac2 commit 09020ae

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/components/NotificationRow.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { formatDistanceToNow, parseISO } from 'date-fns';
55
import { CheckIcon, MuteIcon } from '@primer/octicons-react';
66

77
import { formatReason, getNotificationTypeIcon } from '../utils/github-api';
8-
import { generateGitHubWebUrl } from '../utils/helpers';
8+
import { generateGitHubWebUrl, getDiscussionUrl } from '../utils/helpers';
99
import { Notification } from '../typesGithub';
1010
import { AppContext } from '../context/App';
1111

@@ -38,6 +38,16 @@ export const NotificationRow: React.FC<IProps> = ({
3838
accounts.user?.id
3939
);
4040
shell.openExternal(url);
41+
} else if (notification.subject.type === 'Discussion') {
42+
getDiscussionUrl(notification, accounts.token).then(url =>
43+
shell.openExternal(
44+
generateGitHubWebUrl(
45+
url || `${notification.repository.url}/discussions`,
46+
notification.id,
47+
accounts.user?.id
48+
)
49+
)
50+
);
4151
}
4252
}, [notification]);
4353

src/utils/helpers.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { EnterpriseAccount } from '../types';
2+
import { Notification } from '../typesGithub';
3+
import { apiRequestAuth } from '../utils/api-requests';
24

35
import { Constants } from './constants';
46

@@ -59,3 +61,42 @@ export function generateGitHubWebUrl(
5961

6062
return newUrl;
6163
}
64+
65+
const addHours = (date: string, hours: number) =>
66+
new Date(new Date(date).getTime() + hours * 36e5).toISOString();
67+
68+
const queryString = (repo: string, title: string, lastUpdated: string) =>
69+
`${title} in:title repo:${repo} -updated:<${addHours(lastUpdated, -2)}`;
70+
71+
export async function getDiscussionUrl(
72+
notification: Notification,
73+
token: string
74+
): Promise<string> {
75+
const response = await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, {
76+
query: `{
77+
search(query:"${queryString(
78+
notification.repository.full_name,
79+
notification.subject.title,
80+
notification.updated_at
81+
)}", type: DISCUSSION, first: 10) {
82+
edges {
83+
node {
84+
... on Discussion {
85+
viewerSubscription
86+
title
87+
url
88+
}
89+
}
90+
}
91+
}
92+
}`,
93+
});
94+
let edges = response?.data?.data?.search?.edges?.filter(
95+
edge => edge.node.title === notification.subject.title
96+
) || [];
97+
if (edges.length > 1)
98+
edges = edges.filter(
99+
edge => edge.node.viewerSubscription === 'SUBSCRIBED'
100+
);
101+
return edges[0]?.node.url;
102+
}

0 commit comments

Comments
 (0)