refactor: extract _updateNotificationUI helper, use const, fix double query

- Extract shared _updateNotificationUI(statusEl, reqBtn, permission) helper
  to remove duplicated notification status UI update logic from openSettings
  and the requestPermission click handler
- Replace var with const for local bindings in new Notifications handlers
- Use this.checked in bell sound change handler instead of re-querying
  the DOM element that was already looked up for addEventListener
This commit is contained in:
Brian Krabach
2026-03-30 00:29:38 -07:00
parent 2f909eae89
commit e5827a8037
+30 -30
View File
@@ -1019,6 +1019,28 @@ function onDisplaySettingChange() {
applyDisplaySettings(ds); applyDisplaySettings(ds);
} }
/**
* Update notification UI controls to reflect the current permission state.
* @param {Element} statusEl - The status text element.
* @param {Element} reqBtn - The request-permission button.
* @param {string} permission - Notification.permission value, or 'unsupported'.
*/
function _updateNotificationUI(statusEl, reqBtn, permission) {
if (permission === 'granted') {
statusEl.textContent = 'Granted';
reqBtn.disabled = true;
} else if (permission === 'denied') {
statusEl.textContent = 'Denied (check browser settings)';
reqBtn.disabled = true;
} else if (permission === 'unsupported') {
statusEl.textContent = 'Not supported';
reqBtn.disabled = true;
} else {
statusEl.textContent = 'Not requested';
reqBtn.disabled = false;
}
}
/** /**
* Open the settings dialog. * Open the settings dialog.
* Sets _settingsOpen, calls dialog.showModal(), removes hidden from backdrop, * Sets _settingsOpen, calls dialog.showModal(), removes hidden from backdrop,
@@ -1046,19 +1068,8 @@ function openSettings() {
const statusEl = $('notification-status-text'); const statusEl = $('notification-status-text');
const reqBtn = $('notification-request-btn'); const reqBtn = $('notification-request-btn');
if (statusEl && reqBtn) { if (statusEl && reqBtn) {
if (typeof Notification === 'undefined') { const permission = typeof Notification === 'undefined' ? 'unsupported' : Notification.permission;
statusEl.textContent = 'Not supported'; _updateNotificationUI(statusEl, reqBtn, permission);
reqBtn.disabled = true;
} else if (Notification.permission === 'granted') {
statusEl.textContent = 'Granted';
reqBtn.disabled = true;
} else if (Notification.permission === 'denied') {
statusEl.textContent = 'Denied (check browser settings)';
reqBtn.disabled = true;
} else {
statusEl.textContent = 'Not requested';
reqBtn.disabled = false;
}
} }
// Populate Sessions tab from server settings // Populate Sessions tab from server settings
@@ -1355,10 +1366,8 @@ function bindStaticEventListeners() {
// Notifications settings — bell sound toggle persists to display settings localStorage // Notifications settings — bell sound toggle persists to display settings localStorage
on($('setting-bell-sound'), 'change', function() { on($('setting-bell-sound'), 'change', function() {
var el = $('setting-bell-sound'); const ds = loadDisplaySettings();
if (!el) return; ds.bellSound = this.checked;
var ds = loadDisplaySettings();
ds.bellSound = el.checked;
saveDisplaySettings(ds); saveDisplaySettings(ds);
}); });
@@ -1367,23 +1376,14 @@ function bindStaticEventListeners() {
if (typeof Notification === 'undefined') return; if (typeof Notification === 'undefined') return;
Notification.requestPermission().then(function(permission) { Notification.requestPermission().then(function(permission) {
_notificationPermission = permission; _notificationPermission = permission;
var ds = loadDisplaySettings(); const ds = loadDisplaySettings();
ds.notificationPermission = permission; ds.notificationPermission = permission;
saveDisplaySettings(ds); saveDisplaySettings(ds);
// Update UI state // Update UI state
var statusEl = $('notification-status-text'); const statusEl = $('notification-status-text');
var reqBtn = $('notification-request-btn'); const reqBtn = $('notification-request-btn');
if (statusEl && reqBtn) { if (statusEl && reqBtn) {
if (permission === 'granted') { _updateNotificationUI(statusEl, reqBtn, permission);
statusEl.textContent = 'Granted';
reqBtn.disabled = true;
} else if (permission === 'denied') {
statusEl.textContent = 'Denied (check browser settings)';
reqBtn.disabled = true;
} else {
statusEl.textContent = 'Not requested';
reqBtn.disabled = false;
}
} }
}); });
}); });