From 0d9137928137e997c8e8bf3b35108b34e43f6871 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Thu, 29 Nov 2018 16:22:18 +0800 Subject: [PATCH] fix: handle non-branch blob pages --- src/driver/core/SideBar.js | 17 ++++++++++------- src/utils/URLHelper.js | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/driver/core/SideBar.js b/src/driver/core/SideBar.js index 0f99b9e..39f17cc 100644 --- a/src/driver/core/SideBar.js +++ b/src/driver/core/SideBar.js @@ -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) diff --git a/src/utils/URLHelper.js b/src/utils/URLHelper.js index bdc8273..a5d7e8a 100644 --- a/src/utils/URLHelper.js +++ b/src/utils/URLHelper.js @@ -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, }