-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
116 lines (96 loc) · 3.87 KB
/
popup.js
File metadata and controls
116 lines (96 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const DEFAULT_BRANCHES = ['main', 'master', 'production', 'development', 'dev', 'staging', 'nightly'];
const DEFAULT_APPROVALS = 1;
const DEFAULT_ACTIONS = ['Reviewer', 'Channels', 'DevOps'];
// ── Tag widget ───────────────────────────────────────────────────────────────
function createTagWidget(boxId, inputId, items) {
const box = document.getElementById(boxId);
const input = document.getElementById(inputId);
let tags = [...items];
function render() {
// Remove existing tag elements (keep the input)
box.querySelectorAll('.tag').forEach(el => el.remove());
tags.forEach((tag, index) => {
const tagEl = document.createElement('span');
tagEl.className = 'tag';
tagEl.textContent = tag;
const removeBtn = document.createElement('button');
removeBtn.type = 'button';
removeBtn.setAttribute('aria-label', `Remove ${tag}`);
removeBtn.textContent = '×';
removeBtn.addEventListener('click', () => {
tags.splice(index, 1);
render();
});
tagEl.appendChild(removeBtn);
box.insertBefore(tagEl, input);
});
}
function addTag(value) {
const trimmed = value.trim().replace(/,+$/, '');
if (trimmed && !tags.includes(trimmed)) {
tags.push(trimmed);
render();
}
input.value = '';
}
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ',') {
e.preventDefault();
addTag(input.value);
} else if (e.key === 'Backspace' && input.value === '' && tags.length > 0) {
tags.pop();
render();
}
});
input.addEventListener('blur', () => {
if (input.value.trim()) addTag(input.value);
});
// Clicking anywhere in the box focuses the input
box.addEventListener('click', () => input.focus());
render();
return { getTags: () => [...tags] };
}
// ── Main ─────────────────────────────────────────────────────────────────────
let branchWidget, actionWidget;
document.addEventListener('DOMContentLoaded', () => {
const approvalsInput = document.getElementById('required-approvals');
const approvalsLabel = document.getElementById('approvals-label');
function updateApprovalsLabel(value) {
const n = parseInt(value, 10);
approvalsLabel.textContent = (n === 1 ? 'approval' : 'approvals') + ' before merge';
}
approvalsInput.addEventListener('input', () => updateApprovalsLabel(approvalsInput.value));
chrome.storage.sync.get(
{
criticalBranches: DEFAULT_BRANCHES,
requiredApprovals: DEFAULT_APPROVALS,
requiredActions: DEFAULT_ACTIONS,
},
(settings) => {
approvalsInput.value = settings.requiredApprovals;
updateApprovalsLabel(settings.requiredApprovals);
branchWidget = createTagWidget('branches-box', 'branch-input', settings.criticalBranches);
actionWidget = createTagWidget('actions-box', 'action-input', settings.requiredActions);
}
);
document.getElementById('save-btn').addEventListener('click', () => {
const approvals = parseInt(document.getElementById('required-approvals').value, 10);
chrome.storage.sync.set(
{
criticalBranches: branchWidget.getTags(),
requiredApprovals: isNaN(approvals) || approvals < 1 ? DEFAULT_APPROVALS : approvals,
requiredActions: actionWidget.getTags(),
},
() => {
const toast = document.getElementById('toast');
const saveBtn = document.getElementById('save-btn');
toast.style.display = 'block';
saveBtn.style.display = 'none';
setTimeout(() => {
toast.style.display = 'none';
saveBtn.style.display = '';
}, 2000);
}
);
});
});