From 9602c5444ea600641bfb9c86e31d798600c63798 Mon Sep 17 00:00:00 2001 From: Bruno Lemos Date: Sun, 25 Mar 2018 17:01:57 -0300 Subject: [PATCH] Fix comment url --- src/components/cards/NotificationCard.tsx | 5 +- .../cards/partials/rows/CommitRow.tsx | 8 ++- src/components/cards/partials/rows/helpers.ts | 9 +-- src/utils/helpers/github/url.ts | 61 +++++++++++++------ 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/components/cards/NotificationCard.tsx b/src/components/cards/NotificationCard.tsx index 444b1399..c0eebdaa 100644 --- a/src/components/cards/NotificationCard.tsx +++ b/src/components/cards/NotificationCard.tsx @@ -123,10 +123,11 @@ export default class NotificationCard extends PureComponent { {Boolean(commit) && ( )} diff --git a/src/components/cards/partials/rows/CommitRow.tsx b/src/components/cards/partials/rows/CommitRow.tsx index dd782589..00395ccf 100644 --- a/src/components/cards/partials/rows/CommitRow.tsx +++ b/src/components/cards/partials/rows/CommitRow.tsx @@ -4,6 +4,7 @@ import Icon from 'react-native-vector-icons/Octicons' import { tryGetUsernameFromGitHubEmail } from '../../../../utils/helpers/github/shared' import { + getCommentIdFromUrl, getGitHubSearchURL, getGitHubURLForUser, } from '../../../../utils/helpers/github/url' @@ -18,6 +19,7 @@ export interface IProps { authorName?: string authorUsername?: string isRead: boolean + latestCommentUrl?: string message: string url: string } @@ -29,6 +31,7 @@ const CommitRow: SFC = ({ authorName, authorUsername: _authorUsername, isRead, + latestCommentUrl, message: _message, url, }) => { @@ -65,7 +68,10 @@ const CommitRow: SFC = ({ { - const fixedURL = fixURL(url, { issueOrPullRequestNumber }) + (url?: string, { commentId, issueOrPullRequestNumber }: IURLOptions = {}) => { + const fixedURL = fixURL(url, { commentId, issueOrPullRequestNumber }) return fixedURL ? () => SafariView.show({ url: fixedURL }) : undefined }, ) as ( diff --git a/src/utils/helpers/github/url.ts b/src/utils/helpers/github/url.ts index 0705dad4..9107e103 100644 --- a/src/utils/helpers/github/url.ts +++ b/src/utils/helpers/github/url.ts @@ -1,5 +1,11 @@ +import Platform from '../../../libs/platform' import { IGitHubRepo } from '../../../types' +export interface IURLOptions { + commentId?: number + issueOrPullRequestNumber?: number +} + export const baseURL = 'https://github.com' export function getCommentIdFromUrl(url: string) { @@ -66,7 +72,7 @@ export const getGitHubURLForBranch = (repoFullName: string, branch: string) => export function githubHTMLUrlFromAPIUrl( apiURL: string, - { issueOrPullRequestNumber }: { issueOrPullRequestNumber?: number } = {}, + { commentId, issueOrPullRequestNumber }: IURLOptions = {}, ): string { if (!apiURL) return '' @@ -83,27 +89,47 @@ export function githubHTMLUrlFromAPIUrl( if (restOfURL2[0]) { switch (type2) { - case 'commits': + case 'commits': { + if (commentId) { + const elementId = Platform.selectUsingRealOS({ + default: `comment-${commentId}`, + web: `commitcomment-${commentId}`, + }) + + return `${baseURL}/${repoFullName}/commit/${ + restOfURL2[0] + }#${elementId}` + } + return `${baseURL}/${repoFullName}/commit/${restOfURL2.join('/')}` + } case 'issues': - if (restOfURL2[0] === 'comments' && restOfURL2[1]) { - return issueOrPullRequestNumber - ? `${baseURL}/${repoFullName}/pull/${issueOrPullRequestNumber}/comments#issuecomment-${ - restOfURL2[1] - }` - : '' + if ( + issueOrPullRequestNumber && + (commentId || (restOfURL2[0] === 'comments' && restOfURL2[1])) + ) { + const elementId = Platform.selectUsingRealOS({ + default: `comment-${commentId || restOfURL2[1]}`, + web: `issuecomment-${commentId || restOfURL2[1]}`, + }) + + return `${baseURL}/${repoFullName}/issues/${issueOrPullRequestNumber}#${elementId}` } return `${baseURL}/${repoFullName}/issues/${restOfURL2.join('/')}` case 'pulls': - if (restOfURL2[0] === 'comments' && restOfURL2[1]) { - return issueOrPullRequestNumber - ? `${baseURL}/${repoFullName}/pull/${issueOrPullRequestNumber}/comments#discussion_r${ - restOfURL2[1] - }` - : '' + if ( + issueOrPullRequestNumber && + (commentId || (restOfURL2[0] === 'comments' && restOfURL2[1])) + ) { + const elementId = Platform.selectUsingRealOS({ + default: `comment-${commentId || restOfURL2[1]}`, + web: `discussion_r${commentId || restOfURL2[1]}`, + }) + + return `${baseURL}/${repoFullName}/pull/${issueOrPullRequestNumber}#${elementId}` } return `${baseURL}/${repoFullName}/pull/${restOfURL2.join('/')}` @@ -122,10 +148,7 @@ export function githubHTMLUrlFromAPIUrl( return `${baseURL}/${restOfURL}` } -export function fixURL( - url?: string, - { issueOrPullRequestNumber }: { issueOrPullRequestNumber?: number } = {}, -) { +export function fixURL(url?: string, options: IURLOptions = {}) { if (!url) return // sometimes the url come like this: '/facebook/react', so we add https://github.com @@ -133,7 +156,7 @@ export function fixURL( url[0] === '/' && url.indexOf('github.com') < 0 ? `${baseURL}${url}` : url uri = uri.indexOf('api.github.com') >= 0 - ? githubHTMLUrlFromAPIUrl(uri, { issueOrPullRequestNumber }) + ? githubHTMLUrlFromAPIUrl(uri, options) : uri return uri