Files
SHiNE-server/shine-UI/js/components/profile-card.js
T

135 lines
7.5 KiB
JavaScript

import { t, th } from '../i18n/index.js';
// data-profile-list / data-self-profile-action / data-profile-action обрабатывают profile-view.js и user-profile-view.js.
import { iconHtml } from './ui-icon.js';
import { displayCity } from '../services/cities.js';
export function escapeProfileHtml(text) {
return String(text || '')
.replaceAll('&', '&')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#39;');
}
const esc = escapeProfileHtml;
// Карточка статуса у аватара: кто подтвердил этот аккаунт и кого подтвердил он сам.
function statusSideHtml({ kind, title, active, received, given, verb }) {
const isOfficial = kind === 'primary';
const receivedKind = isOfficial ? 'primary_received' : 'shine_received';
const givenKind = isOfficial ? 'primary_given' : 'shine_given';
return `
<section class="pf-sc pf-sc--${esc(kind)}${active ? ' is-active' : ' is-empty'}" aria-label="${esc(title)}">
<div class="pf-sc__title">${esc(title)}</div>
<button type="button" class="pf-sc__row" data-profile-list="${receivedKind}" aria-label="${esc(title)}${th('remaining.html_profile_card.confirmed_by')}${Number(received || 0)}">
<strong>${Number(received || 0)}</strong><span>${th('screens.profile_card.text_017')}</span>
</button>
<button type="button" class="pf-sc__row" data-profile-list="${givenKind}" aria-label="${esc(title)}: ${esc(verb)} ${Number(given || 0)}">
<strong>${Number(given || 0)}</strong><span>${esc(verb)}</span>
</button>
</section>`;
}
function statHtml({ kind, label, valueHtml, ariaLabel }) {
return `
<button type="button" class="pf-stat" data-profile-list="${esc(kind)}" aria-label="${esc(ariaLabel)}">
<span class="pf-stat-value">${valueHtml}</span>
<span class="pf-stat-label">${esc(label)}</span>
</button>`;
}
function contactRows(card) {
return [
[t('screens.profile_card.text_006'), card?.web],
[t('screens.profile_card.text_007'), card?.phone],
[t('screens.profile_card.text_008'), card?.address],
]
.filter(([, value]) => String(value || '').trim())
.map(([label, value]) => `
<div class="pf-row">
<span class="pf-row-label">${esc(label)}</span>
<span class="pf-row-value">${esc(value)}</span>
</div>`)
.join('');
}
/**
* @param {object} opts
* @param {object} opts.card карточка профиля (loadUserProfileCard)
* @param {string} opts.login логин на случай, если в карточке его нет
* @param {string} opts.tilesHtml плитки действий (свои/чужие)
* @param {string} [opts.tilesWrapClass] доп. класс обёртки плиток (для меню «Добавить»)
* @param {string} [opts.beforeTilesHtml] разметка внутри обёртки перед плитками
*/
export function profileCardHtml({ card, login = '', tilesHtml = '', tilesWrapClass = '', beforeTilesHtml = '', isSelf = true }) {
const stats = card?.stats || {};
const official = card?.accountRole === 'primary';
const shining = card?.shineStatus === 'shining';
const cardLogin = String(card?.login || login || '').trim().replace(/^@+/, '');
const city = displayCity(card?.city);
const fullName = [card?.firstName, card?.lastName].map((v) => String(v || '').trim()).filter(Boolean).join(' ');
const displayName = fullName || cardLogin || t('screens.profile_card.text_012');
const about = String(card?.about || '').trim();
const spiritualPath = String(card?.spiritualPath || '').trim();
const contacts = contactRows(card);
const aboutRow = (about || isSelf) ? `
<div class="pf-row pf-row--block">
<span class="pf-row-label">${th('screens.profile_card.text_018')}</span>
<p class="pf-row-text${about ? '' : ' is-empty'}">${esc(about || t('screens.profile_card.text_013'))}</p>
</div>` : '';
const pathRow = spiritualPath ? `
<div class="pf-row pf-row--block">
<span class="pf-row-label">${th('screens.profile_card.text_019')}</span>
<p class="pf-row-text">${esc(spiritualPath)}</p>
</div>` : '';
const listInner = aboutRow + contacts + pathRow;
const listHtml = listInner.trim() ? `<div class="pf-list">${listInner}</div>` : '';
const friends = Number(stats.friendsCount || 0);
const closeFriends = Number(stats.closeFriendsCount || 0);
const shineReceived = Number(stats.shineReceivedCount || 0);
const shineGiven = Number(stats.shineGivenCount || 0);
const primaryReceived = Number(stats.primaryReceivedCount || 0);
const primaryGiven = Number(stats.primaryGivenCount || 0);
// Статусы по бокам аватара: слева «Официальный», справа «Сияние». На своём профиле – всегда
// (пустая сторона бледнее), на чужом – если статус есть или уже есть подтверждения.
const verb = card?.gender === 'female' ? t('screens.profile_card.text_009') : card?.gender === 'male' ? t('screens.profile_card.text_010') : t('screens.profile_card.text_011');
const officialSide = (isSelf || official || primaryReceived || primaryGiven)
? statusSideHtml({ kind: 'primary', title: t('screens.profile_card.text_001'), active: official, received: primaryReceived, given: primaryGiven, verb })
: '';
const shineSide = (isSelf || shining || shineReceived || shineGiven)
? statusSideHtml({ kind: 'shine', title: t('screens.profile_card.text_002'), active: shining, received: shineReceived, given: shineGiven, verb })
: '';
return `
<div class="pf-hero" aria-label="${th('remaining.html_profile_card.profile')}${esc(cardLogin)}">
<div class="pf-identity">
<div class="pf-side pf-side--left">${officialSide}</div>
<div class="pf-avatar user-profile-avatar-slot${shining ? ' is-shining' : ''}"></div>
<div class="pf-side pf-side--right">${shineSide}</div>
</div>
<h2 class="pf-name">${esc(displayName)}</h2>
<div class="pf-login">${esc(cardLogin)}</div>
${city ? `<div class="pf-city">${iconHtml('place')}<span>${esc(city)}</span></div>` : ''}
</div>
<div class="pf-stats">
${statHtml({ kind: 'friends', label: t('screens.profile_card.text_003'), valueHtml: closeFriends ? `${friends}<small> · ${closeFriends}${th('remaining.html_profile_card.close')}</small>` : String(friends), ariaLabel: t('screens.profile_card.text_014', { value1: friends, value2: closeFriends }) })}
${statHtml({ kind: 'channels_owned', label: t('screens.profile_card.text_004'), valueHtml: String(Number(stats.ownedPublicChannelsCount || 0)), ariaLabel: t('screens.profile_card.text_015', { value1: Number(stats.ownedPublicChannelsCount || 0) }) })}
${statHtml({ kind: 'channels_following', label: t('screens.profile_card.text_005'), valueHtml: String(Number(stats.followingChannelsCount || 0)), ariaLabel: t('screens.profile_card.text_016', { value1: Number(stats.followingChannelsCount || 0) }) })}
</div>
${tilesHtml ? `
<div class="pf-tiles-wrap user-profile-actions-wrap ${esc(tilesWrapClass)}">
${beforeTilesHtml}
<div class="pf-tiles user-profile-actions">${tilesHtml}</div>
</div>` : ''}
${listHtml}`;
}
export function profileTileHtml({ icon, label, attrs = '', iconMarkup = '' }) {
return `<button type="button" class="pf-tile user-profile-action-btn" ${attrs}>${iconMarkup || iconHtml(icon)}<span>${esc(label)}</span></button>`;
}