fix: handle non-branch blob pages

This commit is contained in:
EnixCoda 2018-11-29 16:22:18 +08:00
parent bbe26aed67
commit 0d91379281
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
2 changed files with 22 additions and 12 deletions

View file

@ -16,7 +16,8 @@ const init = dispatch => async () => {
let detectedBranchName
const metaData = URLHelper.parse()
if (DOMHelper.isInCodePage()) {
detectedBranchName = DOMHelper.getCurrentBranch()
detectedBranchName = DOMHelper.getCurrentBranch() // not working well with non-branch blob
|| URLHelper.parseBlobSHA() // cannot handle '/' split branch name, should not use when possibly on branch page
}
metaData.branchName = detectedBranchName || 'master'
dispatch(setMetaData, metaData)
@ -67,12 +68,14 @@ const init = dispatch => async () => {
}
})
}
getTreeData.then(treeData => {
if (treeData) {
// in an unknown rare case this NOT happen
dispatch({ treeData })
}
}).catch(err => dispatch(handleError, err))
getTreeData
.then(treeData => {
if (treeData) {
// in an unknown rare case this NOT happen
dispatch({ treeData })
}
})
.catch(err => dispatch(handleError, err))
Object.assign(metaData, { api: metaDataFromAPI })
dispatch(setMetaData, metaData)
const shouldShow = URLHelper.isInCodePage(metaData)

View file

@ -1,9 +1,10 @@
import { raiseError } from "analytics";
import { raiseError } from 'analytics'
function parse() {
const { pathname } = window.location
let [
, // ignore content before the first '/'
,
// ignore content before the first '/'
userName,
repoName,
type,
@ -17,6 +18,11 @@ function parse() {
}
}
function parseBlobSHA() {
const { type, path } = parse()
return type === 'blob' ? path[0] : false
}
function isInRepoPage() {
const repoHeaderSelector = '.repohead'
return Boolean(document.querySelector(repoHeaderSelector))
@ -36,9 +42,9 @@ function isInCodePage(metaData = {}) {
const { type, branchName } = mergedRepo
return Boolean(
isInRepoPage(mergedRepo) &&
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName))
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName))
)
}
@ -75,4 +81,5 @@ export default {
isInRepoPage,
isInCodePage,
parse,
parseBlobSHA,
}