import { t, th } from '../i18n/index.js'; // «Уведомления» в настройках: звук сообщений, разрешение системных уведомлений и чаты // «без звука». Всё – настройки этого устройства, сервер про них не знает. import { createTopBar } from '../components/topbar.js'; import { createPersonRow } from '../components/person-row.js'; import { authService, getRememberedDmPeer, normalizeDmChatId, rememberDmPeer } from '../state.js'; import { DM_CHAT_PREFS_EVENT, formatDmMuteUntil, getMutedDmChats, setDmChatMuted, } from '../services/dm-chat-prefs.js'; import { DEVICE_PREFS_EVENT, getDevicePref, setDevicePref } from '../services/device-prefs.js'; import { showToast } from '../services/channels-ux.js'; export const pageMeta = { id: 'notification-settings-view', get title() { return t('screens.notification_settings_view.text_001'); } }; function permissionState() { if (typeof Notification === 'undefined') return 'unsupported'; return Notification.permission; // granted | denied | default } export function render({ navigate, chrome } = {}) { const screen = document.createElement('section'); screen.className = 'stack notification-settings-screen'; let disposed = false; chrome?.setTopbar(createTopBar({ title: t('screens.notification_settings_view.text_002'), back: { onClick: () => navigate('settings-view') }, })); const soundTitle = document.createElement('p'); soundTitle.className = 'nav-list__title'; soundTitle.textContent = t('screens.notification_settings_view.text_003'); const soundList = document.createElement('div'); soundList.className = 'nav-list'; const soundRow = document.createElement('label'); soundRow.className = 'nav-row nav-row--switch'; soundRow.innerHTML = ` ${th('screens.notification_settings_view.text_020')} ${th('screens.notification_settings_view.text_021')} `; const soundSwitch = soundRow.querySelector('input'); soundSwitch.checked = getDevicePref('dmSound', true) !== false; soundSwitch.addEventListener('change', () => setDevicePref('dmSound', soundSwitch.checked)); soundList.append(soundRow); // Системные уведомления – когда приложение свёрнуто или закрыто. const systemList = document.createElement('div'); systemList.className = 'nav-list'; const systemRow = document.createElement('div'); systemRow.className = 'nav-row nav-row--action'; systemList.append(systemRow); function renderSystemRow() { const permission = permissionState(); const texts = { granted: [t('screens.notification_settings_view.text_009'), t('screens.notification_settings_view.text_010')], denied: [t('screens.notification_settings_view.text_011'), t('screens.notification_settings_view.text_012')], default: [t('screens.notification_settings_view.text_013'), t('screens.notification_settings_view.text_014')], unsupported: [t('screens.notification_settings_view.text_015'), t('screens.notification_settings_view.text_016')], }; const [label, hint] = texts[permission] || texts.unsupported; systemRow.innerHTML = ` `; systemRow.querySelector('.nav-row__label').textContent = label; systemRow.querySelector('.nav-row__hint').textContent = hint; if (permission === 'default') { const allow = document.createElement('button'); allow.type = 'button'; allow.className = 'primary-btn nav-row__button'; allow.textContent = t('screens.notification_settings_view.text_004'); allow.addEventListener('click', async () => { allow.disabled = true; try { const result = await Notification.requestPermission(); if (result === 'granted') showToast(t('screens.notification_settings_view.text_007')); } catch { showToast(t('screens.notification_settings_view.text_008'), { kind: 'error' }); } if (!disposed) renderSystemRow(); }); systemRow.append(allow); } } renderSystemRow(); // Чаты «без звука»: кто и до какого времени; звук возвращается кнопкой в строке. const mutedTitle = document.createElement('p'); mutedTitle.className = 'nav-list__title'; const mutedList = document.createElement('div'); mutedList.className = 'notification-settings-muted'; const mutedEmpty = document.createElement('p'); mutedEmpty.className = 'meta-muted appearance-hint'; mutedEmpty.textContent = t('screens.notification_settings_view.text_005'); let peersRequested = false; function mutedRow({ chatId, until }) { const peer = getRememberedDmPeer(chatId) || {}; const fullName = [peer.firstName, peer.lastName].map((part) => String(part || '').trim()).filter(Boolean).join(' '); const login = peer.login || chatId; return createPersonRow({ login, name: fullName || login, firstName: peer.firstName, lastName: peer.lastName, avatar: peer.avatar || null, sub: [fullName ? login : '', until ? formatDmMuteUntil(until) : t('screens.notification_settings_view.text_017')].filter(Boolean).join(' · '), onClick: () => navigate(`chat/${encodeURIComponent(normalizeDmChatId(chatId))}`), actions: [{ icon: 'bell', label: t('screens.notification_settings_view.text_006'), onClick: () => setDmChatMuted(chatId, false) }], }); } // Имена и аватары – из списка чатов; если его в этот раз не открывали, спрашиваем сервер один раз. async function loadPeers() { peersRequested = true; try { const payload = await authService.listContacts(); if (disposed) return; (Array.isArray(payload?.dialogs) ? payload.dialogs : []).forEach((dialog) => rememberDmPeer({ login: dialog?.peerLogin, firstName: dialog?.firstName, lastName: dialog?.lastName, avatar: dialog?.avatar, })); renderMuted(); } catch { // Без имён строки остаются с логинами – этого достаточно. } } function renderMuted() { const muted = getMutedDmChats(); mutedTitle.textContent = muted.length ? t('screens.notification_settings_view.text_019', { value1: muted.length }) : t('screens.notification_settings_view.text_018'); mutedList.hidden = !muted.length; mutedEmpty.hidden = Boolean(muted.length); mutedList.replaceChildren(...muted.map(mutedRow)); if (!peersRequested && muted.some(({ chatId }) => !getRememberedDmPeer(chatId))) void loadPeers(); } renderMuted(); const onPrefs = () => { if (!disposed) renderMuted(); }; const onDevicePrefs = () => { if (!disposed) soundSwitch.checked = getDevicePref('dmSound', true) !== false; }; window.addEventListener(DM_CHAT_PREFS_EVENT, onPrefs); window.addEventListener(DEVICE_PREFS_EVENT, onDevicePrefs); screen.append(soundTitle, soundList, systemList, mutedTitle, mutedList, mutedEmpty); screen.cleanup = () => { if (disposed) return; disposed = true; window.removeEventListener(DM_CHAT_PREFS_EVENT, onPrefs); window.removeEventListener(DEVICE_PREFS_EVENT, onDevicePrefs); }; return screen; }