Skip to content

Commit 0afe267

Browse files
committed
feat: works with the "repo refresh" redesign
1 parent c35eb48 commit 0afe267

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/index.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const GITHUB_API = 'https://api.github.com/graphql'
33
const GITHUB_API_V3 = 'https://api.github.com/repos/'
44
const REPO_STATS_CLASS = 'numbers-summary'
5+
const REPO_REFRESH_STATS_QUERY = '.repository-content .Box .Details ul'
56
const REPO_SIZE_ID = 'addon-repo-size'
67
const SIZE_KILO = 1024
78
const UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
@@ -119,13 +120,23 @@ const injectRepoSize = async () => {
119120
const repoInfo = getRepoInfo(window.location.pathname.substring(1))
120121

121122
if (repoInfo != null) {
123+
let statsElt, isRefresh = false
122124
const statsCol = document.getElementsByClassName(REPO_STATS_CLASS)
123125

124-
if (statsCol.length !== 1) {
125-
return
126+
if (statsCol.length === 0) {
127+
// maybe we're on the design refresh
128+
const statsRow = document.querySelector(REPO_REFRESH_STATS_QUERY)
129+
if (statsRow == null) {
130+
// can't find any element to add our stats element, we stop here
131+
return
132+
}
133+
134+
isRefresh = true
135+
statsElt = statsRow
136+
} else {
137+
statsElt = statsCol[0]
126138
}
127139

128-
const statsElt = statsCol[0]
129140
const repoSizeElt = document.getElementById(REPO_SIZE_ID)
130141

131142
// nothing to do if we already have the size displayed
@@ -156,8 +167,8 @@ const injectRepoSize = async () => {
156167
}
157168

158169
const humanSize = getHumanFileSize(repoSize * 1024)
159-
160-
createSizeWrapperElement(statsElt, createSizeElements(humanSize))
170+
const sizeElt = isRefresh ? createRepoRefreshSizeElements(humanSize) : createSizeElements(humanSize)
171+
createSizeWrapperElement(statsElt, createSizeElements(humanSize), isRefresh)
161172
}
162173
}
163174

@@ -180,7 +191,19 @@ const createSizeElements = repoSizeHuman => {
180191
return [size, whiteSpace, unitText]
181192
}
182193

183-
const createSizeWrapperElement = async (parent, children) => {
194+
const createRepoRefreshSizeElements = repoSizeHuman => {
195+
const size = document.createElement('strong')
196+
const sizeText = document.createTextNode(repoSizeHuman.size)
197+
size.appendChild(sizeText)
198+
199+
const whiteSpace = document.createTextNode(' ')
200+
201+
const unitText = document.createTextNode(repoSizeHuman.unit)
202+
203+
return [size, whiteSpace, unitText]
204+
}
205+
206+
const createSizeWrapperElement = async (parent, children, isRefresh = false) => {
184207
const storedToken = await getStoredSetting(TOKEN_KEY)
185208
let tokenInfo = '', tokenPlaceholder = ''
186209
if (storedToken) {
@@ -201,7 +224,7 @@ const createSizeWrapperElement = async (parent, children) => {
201224
li.innerHTML = `
202225
<details id="${MODAL_ID}-size-stat-wrapper" class="details-reset details-overlay details-overlay-dark">
203226
<summary>
204-
<a href="#" id="${MODAL_ID}-size-stat-content">
227+
<a id="${MODAL_ID}-size-stat-content" ${isRefresh ? 'class="link-gray-dark no-underline d-inline-block"' : ''}>
205228
<svg class="octicon octicon-database" height="16" width="14" viewBox="0 0 14 16" aria-hidden="true" version="1.1"><path d="M6,15 C2.69,15 0,14.1 0,13 L0,11 C0,10.83 0.09,10.66 0.21,10.5 C0.88,11.36 3.21,12 6,12 C8.79,12 11.12,11.36 11.79,10.5 C11.92,10.66 12,10.83 12,11 L12,13 C12,14.1 9.31,15 6,15 L6,15 Z M6,11 C2.69,11 0,10.1 0,9 L0,7 C0,6.89 0.04,6.79 0.09,6.69 L0.09,6.69 C0.12,6.63 0.16,6.56 0.21,6.5 C0.88,7.36 3.21,8 6,8 C8.79,8 11.12,7.36 11.79,6.5 C11.84,6.56 11.88,6.63 11.91,6.69 L11.91,6.69 C11.96,6.79 12,6.9 12,7 L12,9 C12,10.1 9.31,11 6,11 L6,11 Z M6,7 C2.69,7 0,6.1 0,5 L0,4 L0,3 C0,1.9 2.69,1 6,1 C9.31,1 12,1.9 12,3 L12,4 L12,5 C12,6.1 9.31,7 6,7 L6,7 Z M6,2 C3.79,2 2,2.45 2,3 C2,3.55 3.79,4 6,4 C8.21,4 10,3.55 10,3 C10,2.45 8.21,2 6,2 L6,2 Z" fill-rule="evenodd"></path></svg>
206229
</a>
207230
</summary>
@@ -239,7 +262,6 @@ const createSizeWrapperElement = async (parent, children) => {
239262
parent.appendChild(li)
240263

241264
const elt = document.getElementById(`${MODAL_ID}-size-stat-content`)
242-
elt.setAttribute('href', '#')
243265
elt.addEventListener('click', askForToken)
244266
elt.appendChild(document.createTextNode(' '))
245267

@@ -248,6 +270,10 @@ const createSizeWrapperElement = async (parent, children) => {
248270

249271
const form = document.getElementById(`${MODAL_ID}-form`)
250272
form.addEventListener('submit', saveToken)
273+
274+
if (isRefresh) {
275+
li.className = 'ml-3 d-none d-md-block'
276+
}
251277

252278
children.forEach(c => elt.appendChild(c))
253279
}

0 commit comments

Comments
 (0)