import { createTopBar } from '../components/topbar.js'; import { authService, state } from '../state.js'; import { loadRelationsForPair, loadUserProfileCard } from '../services/user-connections.js'; import { navigateBack } from '../router.js'; import { escapeProfileHtml } from '../components/profile-card.js'; export const pageMeta = { id: 'user-relation-manage-view', title: 'Добавить связь' }; const SOCIAL_LABELS = { none: 'Нет связи', contact: 'Контакт', friend: 'Друг', close_friend: 'Близкий друг', }; const MUTUAL_LABELS = { contact: 'контакты', friend: 'друзья', close_friend: 'близкие друзья', }; function effectiveSocial(flags = {}, direction = 'out') { const prefix = direction === 'in' ? 'in' : 'out'; if (flags[`${prefix}CloseFriend`]) return 'close_friend'; if (flags[`${prefix}Friend`]) return 'friend'; if (flags[`${prefix}Contact`]) return 'contact'; return 'none'; } function relationSummaryHtml(flags = {}) { const outgoing = effectiveSocial(flags, 'out'); const incoming = effectiveSocial(flags, 'in'); if (outgoing !== 'none' && outgoing === incoming) { return `
Взаимно: ${escapeProfileHtml(MUTUAL_LABELS[outgoing])}
`; } return `
Вы к пользователю ${escapeProfileHtml(SOCIAL_LABELS[outgoing])}
${incoming !== 'none' ? `
Пользователь к вам ${escapeProfileHtml(SOCIAL_LABELS[incoming])}
` : ''}
`; } function socialButtonsHtml(flags = {}) { const current = effectiveSocial(flags, 'out'); const kinds = ['none', 'contact', 'friend', 'close_friend']; return kinds.map((kind) => ` `).join(''); } function officialBlockHtml(flags = {}) { return `
Основной аккаунт Этот аккаунт — основной
${flags.outOfficialAccount ? '
✓ Вы подтвердили его основной аккаунт
' : ''} ${flags.inOfficialAccount ? '
✓ Этот пользователь подтвердил ваш основной аккаунт
' : ''}
`; } function shineBlockHtml(flags = {}) { return `
Сияющий Этот аккаунт — сияющий
${flags.outShineConfirmed ? '
✓ Вы подтверждаете его сияющим
' : ''} ${flags.inShineConfirmed ? '
✓ Этот пользователь подтверждает вас сияющим
' : ''}
`; } export function render({ route, chrome }) { const targetLogin = String(route?.params?.login || '').trim(); const selfLogin = String(state.session.login || '').trim(); const screen = document.createElement('section'); screen.className = 'stack relation-manage-screen'; const body = document.createElement('div'); body.className = 'stack relation-manage-body'; const status = document.createElement('div'); status.className = 'status-line relation-manage-status'; chrome?.setTopbar(createTopBar({ title: 'Добавить связь', back: { label: '←', onClick: () => navigateBack() }, })); screen.append(status, body); let flags = {}; let selfCard = null; let targetCard = null; let busy = false; async function setKind(kind, enabled) { await authService.setUserRelation({ login: selfLogin, toLogin: targetLogin, kind, enabled, storagePwd: state.session.storagePwdInMemory, }); } async function changeSocial(next) { const current = effectiveSocial(flags, 'out'); if (current === next) return; if (next === 'none') { if (flags.outCloseFriend) await setKind('close_friend', false); if (flags.outFriend) await setKind('friend', false); if (flags.outContact) await setKind('contact', false); } if (next === 'contact') { if (flags.outCloseFriend) await setKind('close_friend', false); if (flags.outFriend) await setKind('friend', false); if (!flags.outContact) await setKind('contact', true); } if (next === 'friend') { if (flags.outCloseFriend) await setKind('close_friend', false); if (!flags.outFriend) await setKind('friend', true); } if (next === 'close_friend' && !flags.outCloseFriend) { await setKind('close_friend', true); } } function renderBody() { const canPrimary = selfCard?.accountRole === 'primary' && targetCard?.accountRole === 'primary'; const canShine = selfCard?.shineStatus === 'shining' && targetCard?.shineStatus === 'shining'; body.innerHTML = `
Связь @${escapeProfileHtml(targetCard?.login || targetLogin)}
${relationSummaryHtml(flags)}
${socialButtonsHtml(flags)}
${canPrimary ? officialBlockHtml(flags) : ''} ${canShine ? shineBlockHtml(flags) : ''} `; } async function refresh({ loading = true } = {}) { if (loading) { status.className = 'status-line relation-manage-status'; status.textContent = 'Загрузка...'; } [selfCard, targetCard, flags] = await Promise.all([ loadUserProfileCard(selfLogin), loadUserProfileCard(targetLogin), loadRelationsForPair({ currentLogin: selfLogin, targetLogin }), ]); renderBody(); status.className = 'status-line relation-manage-status'; status.textContent = ''; } body.addEventListener('click', async (event) => { const button = event.target.closest('button'); if (!button || busy) return; const social = button.dataset.socialKind; const confirm = button.dataset.confirm; if (!social && !confirm) return; busy = true; body.classList.add('is-busy'); status.className = 'status-line relation-manage-status'; status.textContent = 'Сохранение...'; try { if (social) { await changeSocial(social); } else if (confirm === 'official_account') { await setKind('official_account', !flags.outOfficialAccount); } else if (confirm === 'shine_confirmed') { await setKind('shine_confirmed', !flags.outShineConfirmed); } await refresh({ loading: false }); } catch (error) { status.className = 'status-line relation-manage-status is-unavailable'; status.textContent = `Ошибка: ${error?.message || 'Не удалось изменить связь'}`; } finally { busy = false; body.classList.remove('is-busy'); } }); if (!selfLogin || !targetLogin) { status.className = 'status-line relation-manage-status is-unavailable'; status.textContent = 'Не удалось определить пользователя.'; return screen; } refresh().catch((error) => { status.className = 'status-line relation-manage-status is-unavailable'; status.textContent = `Ошибка: ${error?.message || 'Не удалось загрузить связи'}`; }); return screen; }