From 09020ae25bb19bc858622abe0d3c4c1d8586e296 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Sat, 5 Mar 2022 16:07:36 +0200 Subject: [PATCH] fix discussions-url --- src/components/NotificationRow.tsx | 12 ++++++++- src/utils/helpers.ts | 41 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/components/NotificationRow.tsx b/src/components/NotificationRow.tsx index eb23e75d5..53f59de80 100644 --- a/src/components/NotificationRow.tsx +++ b/src/components/NotificationRow.tsx @@ -5,7 +5,7 @@ import { formatDistanceToNow, parseISO } from 'date-fns'; import { CheckIcon, MuteIcon } from '@primer/octicons-react'; import { formatReason, getNotificationTypeIcon } from '../utils/github-api'; -import { generateGitHubWebUrl } from '../utils/helpers'; +import { generateGitHubWebUrl, getDiscussionUrl } from '../utils/helpers'; import { Notification } from '../typesGithub'; import { AppContext } from '../context/App'; @@ -38,6 +38,16 @@ export const NotificationRow: React.FC = ({ accounts.user?.id ); shell.openExternal(url); + } else if (notification.subject.type === 'Discussion') { + getDiscussionUrl(notification, accounts.token).then(url => + shell.openExternal( + generateGitHubWebUrl( + url || `${notification.repository.url}/discussions`, + notification.id, + accounts.user?.id + ) + ) + ); } }, [notification]); diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index dbeb33a48..ba506b2a7 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -1,4 +1,6 @@ import { EnterpriseAccount } from '../types'; +import { Notification } from '../typesGithub'; +import { apiRequestAuth } from '../utils/api-requests'; import { Constants } from './constants'; @@ -59,3 +61,42 @@ export function generateGitHubWebUrl( return newUrl; } + +const addHours = (date: string, hours: number) => + new Date(new Date(date).getTime() + hours * 36e5).toISOString(); + +const queryString = (repo: string, title: string, lastUpdated: string) => + `${title} in:title repo:${repo} -updated:<${addHours(lastUpdated, -2)}`; + +export async function getDiscussionUrl( + notification: Notification, + token: string +): Promise { + const response = await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, { + query: `{ + search(query:"${queryString( + notification.repository.full_name, + notification.subject.title, + notification.updated_at + )}", type: DISCUSSION, first: 10) { + edges { + node { + ... on Discussion { + viewerSubscription + title + url + } + } + } + } + }`, + }); + let edges = response?.data?.data?.search?.edges?.filter( + edge => edge.node.title === notification.subject.title + ) || []; + if (edges.length > 1) + edges = edges.filter( + edge => edge.node.viewerSubscription === 'SUBSCRIBED' + ); + return edges[0]?.node.url; +}