diff --git a/src/js/lib.js b/src/js/lib.js index f0df779..23196b7 100644 --- a/src/js/lib.js +++ b/src/js/lib.js @@ -48,8 +48,12 @@ const getDiffElement = (fileId) => { return el } - const gitHubEnterpriseEl = document.querySelector(`[data-anchor="${fileId}"]`).parentElement - return gitHubEnterpriseEl + const gitHubEnterpriseEl = document.querySelector(`[data-anchor="${fileId}"]`) + if (gitHubEnterpriseEl) { + return gitHubEnterpriseEl.parentElement + } + + return null } const countCommentsForFileId = (fileId) => { @@ -115,6 +119,7 @@ export const createFileTree = (filter = EMPTY_FILTER) => { title = getCurrentFileLocation(title) return { title, href, parts: title.split('/') } }) + const count = fileInfo.filter(({ href }) => href && href.includes('#diff')).length const tree = { nodeLabel: '/', list: [], @@ -129,20 +134,22 @@ export const createFileTree = (filter = EMPTY_FILTER) => { if (!node) { const hrefSplit = href.split('#') const fileId = hrefSplit[hrefSplit.length - 1] - const hasComments = (countCommentsForFileId(fileId) > 0) - const isDeleted = isDeletedForFileId(fileId) const diffElement = getDiffElement(fileId) - tree.diffElements.push(diffElement) - node = { - nodeLabel: part, - list: [], - href: (index === parts.length - 1) ? href : null, - hasComments, - isDeleted, - diffElement, - diffStats: getDiffStatsForDiffElement(diffElement) + if (diffElement) { + const hasComments = (countCommentsForFileId(fileId) > 0) + const isDeleted = isDeletedForFileId(fileId) + tree.diffElements.push(diffElement) + node = { + nodeLabel: part, + list: [], + href: (index === parts.length - 1) ? href : null, + hasComments, + isDeleted, + diffElement, + diffStats: getDiffStatsForDiffElement(diffElement) + } + location.list.push(node) } - location.list.push(node) } location.list = location.list.sort(sorter) location = node @@ -151,7 +158,7 @@ export const createFileTree = (filter = EMPTY_FILTER) => { }) return { tree: folderConcat(tree), - count: fileInfo.length + count } } @@ -177,9 +184,9 @@ const EMPTY_FILTER = '' export const getBrowserApi = () => { let browserApi = window.chrome - if (typeof browser !== 'undefined') { - browserApi = browser - } + if (typeof browser !== 'undefined') { + browserApi = browser + } return browserApi }