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 { loadUserProfileCard } from '../services/user-connections.js';
import { makeProfileLinksRoute } from '../services/shine-routes.js';
import { navigateBack } from '../router.js';
import { profileCardHtml, profileTileHtml } from '../components/profile-card.js';
import { createPersonNotePreview, openPersonNoteSheet } from '../components/person-note-sheet.js';
import { iconHtml } from '../components/ui-icon.js';
import { PEOPLE_NOTES_EVENT, getPersonNote } from '../services/people-notes.js';
export const pageMeta = { id: 'user', title: 'Профиль' };
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: 'Профиль',
back: { label: '←', onClick: () => navigateBack() },
});
header.classList.add('user-profile-header');
chrome?.setTopbar(header);
const status = document.createElement('div');
status.className = 'status-line user-profile-status';
status.textContent = 'Загрузка профиля...';
const body = document.createElement('div');
body.className = 'user-profile-body';
screen.append(status, body);
let card = null;
let disposed = false;
let noteSheet = null;
function openNote() {
noteSheet?.close();
const name = [card.firstName, card.lastName].map((v) => String(v || '').trim()).filter(Boolean).join(' ') || card.login;
noteSheet = openPersonNoteSheet({ login: card.login, name, onClose: () => { noteSheet = null; } });
}
function renderProfile() {
if (!card || disposed) return;
const isSelf = card.login.toLowerCase() === selfLogin.toLowerCase();
const note = isSelf ? null : getPersonNote(card.login);
body.innerHTML = profileCardHtml({
card,
login: requestedLogin,
isSelf,
tilesHtml: isSelf ? '' : [
profileTileHtml({ icon: 'user-plus', label: 'Добавить связь', attrs: 'data-profile-action="add"' }),
profileTileHtml({ label: 'Связи', attrs: 'data-profile-action="links"', iconMarkup: `
` }),
profileTileHtml({ icon: 'message', label: 'Написать', attrs: 'data-profile-action="chat"' }),
].join(''),
});
// Личная заметка – сразу под кнопками действий; видна только владельцу аккаунта.
let noteEl = createPersonNotePreview(note, { className: 'user-profile-note', onClick: openNote });
if (noteEl) {
noteEl.setAttribute('aria-label', `Ваша заметка: ${noteEl.textContent}. Изменить`);
} else if (!isSelf) {
noteEl = document.createElement('button');
noteEl.type = 'button';
noteEl.className = 'text-btn user-profile-note-add';
noteEl.innerHTML = `${iconHtml('edit')}Добавить личную заметку`;
noteEl.addEventListener('click', openNote);
}
if (noteEl) body.querySelector('.pf-tiles-wrap')?.after(noteEl);
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.textContent = '';
}
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)}`);
}
});
const onNotesChanged = () => renderProfile();
window.addEventListener(PEOPLE_NOTES_EVENT, onNotesChanged);
screen.cleanup = () => {
if (disposed) return;
disposed = true;
window.removeEventListener(PEOPLE_NOTES_EVENT, onNotesChanged);
noteSheet?.close();
};
async function refresh() {
const loaded = await loadUserProfileCard(requestedLogin);
if (disposed) return;
card = loaded;
renderProfile();
}
refresh().catch((error) => {
if (disposed) return;
status.className = 'status-line user-profile-status is-unavailable';
status.textContent = `Ошибка: ${error?.message || 'unknown'}`;
});
return screen;
}