mirror of
https://github.com/zhigang1992/devhub.git
synced 2026-05-29 00:21:04 +08:00
Fix comment url
This commit is contained in:
@@ -123,10 +123,11 @@ export default class NotificationCard extends PureComponent<IProps> {
|
||||
|
||||
{Boolean(commit) && (
|
||||
<CommitRow
|
||||
key={`commit-row-${commit!.latest_comment_url}`}
|
||||
key={`commit-row-${commit!.url}`}
|
||||
isRead={isRead}
|
||||
latestCommentUrl={commit!.latest_comment_url}
|
||||
message={commit!.title}
|
||||
url={commit!.latest_comment_url || commit!.url}
|
||||
url={commit!.url}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
||||
@@ -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<IProps> = ({
|
||||
authorName,
|
||||
authorUsername: _authorUsername,
|
||||
isRead,
|
||||
latestCommentUrl,
|
||||
message: _message,
|
||||
url,
|
||||
}) => {
|
||||
@@ -65,7 +68,10 @@ const CommitRow: SFC<IProps> = ({
|
||||
|
||||
<View style={cardStyles.rightColumn}>
|
||||
<TouchableOpacity
|
||||
onPress={getGithubURLPressHandler(url)}
|
||||
onPress={getGithubURLPressHandler(url, {
|
||||
commentId:
|
||||
latestCommentUrl && getCommentIdFromUrl(latestCommentUrl),
|
||||
})}
|
||||
style={rowStyles.mainContentContainer}
|
||||
>
|
||||
<Text
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
import R from 'ramda'
|
||||
import SafariView from 'react-native-safari-view'
|
||||
|
||||
import { fixURL } from '../../../../utils/helpers/github/url'
|
||||
import { fixURL, IURLOptions } from '../../../../utils/helpers/github/url'
|
||||
|
||||
const baseURL = 'https://github.com'
|
||||
|
||||
export const getGithubURLPressHandler = R.memoize(
|
||||
(
|
||||
url?: string,
|
||||
{ issueOrPullRequestNumber }: { issueOrPullRequestNumber?: number } = {},
|
||||
) => {
|
||||
const fixedURL = fixURL(url, { issueOrPullRequestNumber })
|
||||
(url?: string, { commentId, issueOrPullRequestNumber }: IURLOptions = {}) => {
|
||||
const fixedURL = fixURL(url, { commentId, issueOrPullRequestNumber })
|
||||
return fixedURL ? () => SafariView.show({ url: fixedURL }) : undefined
|
||||
},
|
||||
) as (
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user