Skip to content

Commit

Permalink
fix discussions-url
Browse files Browse the repository at this point in the history
  • Loading branch information
Araxeus committed Mar 5, 2022
1 parent f8a6ac2 commit 09020ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/components/NotificationRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -38,6 +38,16 @@ export const NotificationRow: React.FC<IProps> = ({
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]);

Expand Down
41 changes: 41 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { EnterpriseAccount } from '../types';
import { Notification } from '../typesGithub';
import { apiRequestAuth } from '../utils/api-requests';

import { Constants } from './constants';

Expand Down Expand Up @@ -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<string> {
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;
}

0 comments on commit 09020ae

Please sign in to comment.