import { t } from '../i18n/index.js'; import { createTopBar } from '../components/topbar.js'; import { SHINE_CONNECTIONS_LOGO_SRC } from '../components/shine-logo.js'; import { renderUserAvatar } from '../components/avatar-image.js'; import { state } from '../state.js'; import { loadRelationsForPair, loadUserProfileCard } from '../services/user-connections.js'; import { createErrorState, createLoadingState } from '../components/feedback.js'; import { toUserMessage } from '../services/ui-error-texts.js'; import { makeProfileLinksRoute } from '../services/shine-routes.js'; import { navigateBack } from '../router.js'; import { profileCardHtml, profileTileHtml } from '../components/profile-card.js'; import { iconHtml } from '../components/ui-icon.js'; export const pageMeta = { id: 'user', get title() { return t('screens.user_profile_view.text_001'); } }; function escapeHtml(text) { return String(text || '') .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') .replaceAll("'", '''); } export function render({ navigate, route, chrome }) { const requestedLogin = String(route?.params?.login || '').trim(); const selfLogin = String(state.session.login || '').trim(); const screen = document.createElement('section'); screen.className = 'stack user-profile-screen'; const header = createTopBar({ title: t('screens.user_profile_view.text_002'), back: { label: '←', onClick: () => navigateBack() }, }); header.classList.add('user-profile-header'); chrome?.setTopbar(header); const status = document.createElement('div'); status.className = 'user-profile-status'; status.append(createLoadingState({ label: t('screens.user_profile_view.text_003') })); const body = document.createElement('div'); body.className = 'user-profile-body'; screen.append(status, body); let card = null; let disposed = false; function renderProfile() { if (!card || disposed) return; const isSelf = card.login.toLowerCase() === selfLogin.toLowerCase(); body.innerHTML = profileCardHtml({ card, login: requestedLogin, isSelf, tilesHtml: isSelf ? '' : [ profileTileHtml({ icon: 'user-plus', label: t('screens.user_profile_view.text_004'), attrs: 'data-profile-action="add"' }), profileTileHtml({ label: t('screens.user_profile_view.text_005'), attrs: 'data-profile-action="links"', iconMarkup: `` }), profileTileHtml({ icon: 'message', label: t('screens.user_profile_view.text_006'), attrs: 'data-profile-action="chat"' }), ].join(''), }); syncRelationTile(); const avatarSlot = body.querySelector('.user-profile-avatar-slot'); avatarSlot?.append(renderUserAvatar({ login: card.login, firstName: card.firstName, lastName: card.lastName, avatar: card.avatar, size: 'xl', className: 'user-profile-hero-avatar', official: card.accountRole === 'primary', })); status.replaceChildren(); } // Плитка показывает текущую связь («Друг ✓») или предлагает ответить, если человек // добавил вас первым; нажатие – как раньше, в управление связью. function syncRelationTile() { const tile = body.querySelector('[data-profile-action="add"]'); if (!tile || !relation) return; const r = relation; const theyFriend = r.inFriend || r.inCloseFriend; let icon = 'user-plus'; let label = t('remaining.profile.add_connection'); let hint = ''; if (r.outCloseFriend) { icon = 'check'; label = t('screens.user_profile_view.text_013'); } else if (r.outFriend) { icon = 'check'; label = t('screens.user_profile_view.text_014'); } else if (r.outContact) { icon = 'check'; label = t('screens.user_profile_view.text_015'); } else if (theyFriend) { label = t('screens.user_profile_view.text_016'); hint = t('screens.user_profile_view.text_017'); } else if (r.inContact) { label = t('screens.user_profile_view.text_018'); hint = t('screens.user_profile_view.text_019', { value1: card.gender === 'female' ? t('screens.user_profile_view.text_010') : card.gender === 'male' ? t('screens.user_profile_view.text_011') : t('screens.user_profile_view.text_012') }); } tile.innerHTML = `${iconHtml(icon)}`; tile.querySelector('span').textContent = label; tile.classList.toggle('is-related', icon === 'check'); tile.setAttribute('aria-label', icon === 'check' ? t('screens.user_profile_view.text_020', { value1: label }) : label); body.querySelector('.user-profile-relation-hint')?.remove(); if (hint) { const note = document.createElement('p'); note.className = 'user-profile-relation-hint'; note.textContent = `${[card.firstName, card.lastName].map((v) => String(v || '').trim()).filter(Boolean).join(' ') || card.login} ${hint}`; body.querySelector('.pf-tiles-wrap')?.before(note); } } body.addEventListener('click', (event) => { if (!card) return; const listButton = event.target.closest('[data-profile-list]'); if (listButton) { const kind = listButton.dataset.profileList; if (kind) navigate(`SHiNE/${encodeURIComponent(card.login)}/list/${encodeURIComponent(kind)}`); return; } const actionButton = event.target.closest('[data-profile-action]'); const action = actionButton?.dataset.profileAction; if (action === 'add') { navigate(`SHiNE/${encodeURIComponent(card.login)}/manage`); return; } if (action === 'links') { navigate(makeProfileLinksRoute(card.login)); return; } if (action === 'chat') { navigate(`chat/${encodeURIComponent(card.login)}`); } }); screen.cleanup = () => { if (disposed) return; disposed = true; }; async function refresh() { const loaded = await loadUserProfileCard(requestedLogin); if (disposed) return; card = loaded; renderProfile(); if (selfLogin && card.login.toLowerCase() !== selfLogin.toLowerCase()) { loadRelationsForPair({ currentLogin: selfLogin, targetLogin: card.login }) .then((flags) => { if (disposed) return; relation = flags; syncRelationTile(); }) .catch(() => { /* без сведений о связи плитка остаётся «Добавить связь» */ }); } } const showLoadError = (error) => { if (disposed) return; status.replaceChildren(createErrorState({ title: t('screens.user_profile_view.text_007'), text: toUserMessage(error, t('screens.user_profile_view.text_009')), onRetry: () => { status.replaceChildren(createLoadingState({ label: t('screens.user_profile_view.text_008'), delayMs: 0 })); refresh().catch(showLoadError); }, })); }; refresh().catch(showLoadError); return screen; }