Skip to content

Commit d219e6b

Browse files
committed
refactor: factorize the options.tokenFieldset helper
1 parent 0273d7d commit d219e6b

1 file changed

Lines changed: 47 additions & 52 deletions

File tree

‎options/options.js‎

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,55 @@ function showStatus(messageKey, elementId = "status") {
1010
document.getElementById(elementId).textContent = browser.i18n.getMessage(messageKey)
1111
}
1212

13+
// build a token-input fieldset shared by the static forges and the self-hosted
14+
// instances: a legend, a "token" label, an optional hint and a password input
15+
// whose data-storage-key ties it to the forge-agnostic load/save/clear. Returns
16+
// the fieldset so callers can append extras (e.g. a remove button).
17+
function tokenFieldset({ legendText, storageKey, hintKey }) {
18+
const fieldset = document.createElement("fieldset")
19+
fieldset.className = "options__forge"
20+
21+
const legend = document.createElement("legend")
22+
legend.textContent = legendText
23+
fieldset.appendChild(legend)
24+
25+
const label = document.createElement("label")
26+
label.className = "options__label"
27+
label.setAttribute("for", storageKey)
28+
label.textContent = browser.i18n.getMessage("optionsTokenLabel")
29+
fieldset.appendChild(label)
30+
31+
if (hintKey) {
32+
const hint = document.createElement("p")
33+
hint.className = "options__hint"
34+
hint.textContent = browser.i18n.getMessage(hintKey)
35+
fieldset.appendChild(hint)
36+
}
37+
38+
const input = document.createElement("input")
39+
input.type = "password"
40+
input.id = storageKey
41+
input.className = "options__input"
42+
input.autocomplete = "off"
43+
input.spellcheck = false
44+
input.dataset.storageKey = storageKey
45+
fieldset.appendChild(input)
46+
47+
return fieldset
48+
}
49+
1350
// render one token field per authenticated forge; each input is tagged with the
1451
// forge's storage key so load/save/clear can stay forge-agnostic
1552
function renderForges() {
1653
const container = document.getElementById("forges")
1754

1855
for (const forge of authForges()) {
19-
const fieldset = document.createElement("fieldset")
20-
fieldset.className = "options__forge"
21-
22-
// forge name (a proper noun, not localized)
23-
const legend = document.createElement("legend")
24-
legend.textContent = forge.label
25-
fieldset.appendChild(legend)
26-
27-
const label = document.createElement("label")
28-
label.className = "options__label"
29-
label.setAttribute("for", forge.tokenStorageKey)
30-
label.textContent = browser.i18n.getMessage("optionsTokenLabel")
31-
fieldset.appendChild(label)
32-
33-
const hint = document.createElement("p")
34-
hint.className = "options__hint"
35-
hint.textContent = browser.i18n.getMessage("optionsTokenHint")
36-
fieldset.appendChild(hint)
37-
38-
const input = document.createElement("input")
39-
input.type = "password"
40-
input.id = forge.tokenStorageKey
41-
input.className = "options__input"
42-
input.autocomplete = "off"
43-
input.spellcheck = false
44-
input.dataset.storageKey = forge.tokenStorageKey
45-
fieldset.appendChild(input)
46-
47-
container.appendChild(fieldset)
56+
// forge name is a proper noun, not localized
57+
container.appendChild(tokenFieldset({
58+
legendText: forge.label,
59+
storageKey: forge.tokenStorageKey,
60+
hintKey: "optionsTokenHint",
61+
}))
4862
}
4963
}
5064

@@ -143,29 +157,10 @@ async function renderInstances() {
143157
const typeLabels = Object.fromEntries(selfHostedTypes().map(t => [t.type, t.label]))
144158

145159
for (const instance of instances) {
146-
const fieldset = document.createElement("fieldset")
147-
fieldset.className = "options__forge"
148-
149-
const legend = document.createElement("legend")
150-
legend.textContent = `${instance.hostname} — ${typeLabels[instance.type] || instance.type}`
151-
fieldset.appendChild(legend)
152-
153-
const key = selfHostedTokenKey(instance.hostname)
154-
155-
const label = document.createElement("label")
156-
label.className = "options__label"
157-
label.setAttribute("for", key)
158-
label.textContent = browser.i18n.getMessage("optionsTokenLabel")
159-
fieldset.appendChild(label)
160-
161-
const input = document.createElement("input")
162-
input.type = "password"
163-
input.id = key
164-
input.className = "options__input"
165-
input.autocomplete = "off"
166-
input.spellcheck = false
167-
input.dataset.storageKey = key
168-
fieldset.appendChild(input)
160+
const fieldset = tokenFieldset({
161+
legendText: `${instance.hostname} — ${typeLabels[instance.type] || instance.type}`,
162+
storageKey: selfHostedTokenKey(instance.hostname),
163+
})
169164

170165
const remove = document.createElement("button")
171166
remove.type = "button"

0 commit comments

Comments
 (0)