Files
SHiNE-server/shine-UI/js/pages/user-profile-list-view.js
T

207 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { t, th } from '../i18n/index.js';
import { createTopBar } from '../components/topbar.js';
import { renderUserAvatar } from '../components/avatar-image.js';
import { createErrorState, createLoadingState, createStateMessage } from '../components/feedback.js';
import { createPersonRow } from '../components/person-row.js';
import { iconHtml } from '../components/ui-icon.js';
import { authService } from '../state.js';
import { navigateBack } from '../router.js';
import { userDisplayName } from '../services/user-display.js';
import { toUserMessage } from '../services/ui-error-texts.js';
import { matchesPeopleQuery } from '../services/people-search.js';
import { avatarReferenceToRenderModel, parseAvatarReference } from '../services/avatar-reference.js';
export const pageMeta = { id: 'user-profile-list-view', get title() { return t('screens.user_profile_list_view.text_001'); } };
function parseAvatar(raw) {
return avatarReferenceToRenderModel(parseAvatarReference(raw));
}
const TITLES = {
get friends() { return t('screens.user_profile_list_view.text_017'); }, get close_friends() { return t('screens.user_profile_list_view.text_018'); }, get primary_received() { return t('screens.user_profile_list_view.text_019'); },
get primary_given() { return t('screens.user_profile_list_view.text_020'); }, get shine_received() { return t('screens.user_profile_list_view.text_021'); }, get shine_given() { return t('screens.user_profile_list_view.text_022'); },
get channels_owned() { return t('screens.user_profile_list_view.text_023'); }, get channels_following() { return t('screens.user_profile_list_view.text_024'); },
};
const EMPTY_TEXT = {
get friends() { return t('screens.user_profile_list_view.text_025'); },
get close_friends() { return t('screens.user_profile_list_view.text_026'); },
get primary_received() { return t('screens.user_profile_list_view.text_027'); },
get primary_given() { return t('screens.user_profile_list_view.text_028'); },
get shine_received() { return t('screens.user_profile_list_view.text_029'); },
get shine_given() { return t('screens.user_profile_list_view.text_030'); },
get channels_owned() { return t('screens.user_profile_list_view.text_031'); },
get channels_following() { return t('screens.user_profile_list_view.text_032'); },
};
const RELATION_TEXT = { get contact() { return t('screens.user_profile_list_view.text_033'); }, get friend() { return t('screens.user_profile_list_view.text_034'); }, get close_friend() { return t('screens.user_profile_list_view.text_035'); } };
const SEARCH_FROM = 12; // с какого размера списка появляется поиск по нему
function searchText(value) {
return String(value || '').toLowerCase().replaceAll('ё', 'е').trim();
}
export function render({ navigate, route, chrome }) {
const login = String(route?.params?.login || '').trim();
const initialKind = String(route?.params?.kind || '').trim();
let activeKind = initialKind;
const isFriendsScreen = initialKind === 'friends' || initialKind === 'close_friends';
const isChannelsScreen = initialKind === 'channels_owned' || initialKind === 'channels_following';
let includeDeleted = false;
let disposed = false;
let rows = [];
let query = '';
const screen = document.createElement('section');
screen.className = 'stack profile-list-screen';
const body = document.createElement('div');
body.className = 'profile-list-body';
chrome?.setTopbar(createTopBar({
title: TITLES[initialKind] || t('screens.user_profile_list_view.text_015'),
back: { onClick: () => navigateBack() },
}));
if (isFriendsScreen) {
const tabs = document.createElement('div');
tabs.className = 'tabs profile-list-kind';
tabs.setAttribute('role', 'radiogroup');
tabs.setAttribute('aria-label', t('screens.user_profile_list_view.text_039'));
for (const [kind, label] of [['friends', t('screens.user_profile_list_view.text_007')], ['close_friends', t('screens.user_profile_list_view.text_008')]]) {
const button = document.createElement('button');
button.type = 'button';
button.className = 'tab-btn';
button.dataset.kind = kind;
button.setAttribute('role', 'radio');
button.setAttribute('aria-checked', String(kind === activeKind));
button.textContent = label;
button.addEventListener('click', () => {
if (kind === activeKind) return;
activeKind = kind;
tabs.querySelectorAll('.tab-btn').forEach((el) => el.setAttribute('aria-checked', String(el.dataset.kind === activeKind)));
void load(activeKind);
});
tabs.append(button);
}
screen.append(tabs);
}
if (isChannelsScreen) {
const filter = document.createElement('div');
filter.className = 'nav-list';
filter.innerHTML = `
<label class="nav-row nav-row--switch">
<span class="nav-row__text"><span class="nav-row__label">${th('screens.user_profile_list_view.text_036')}</span></span>
<input class="switch" type="checkbox" />
</label>`;
filter.querySelector('input').addEventListener('change', (event) => {
includeDeleted = Boolean(event.currentTarget.checked);
void load(activeKind);
});
screen.append(filter);
}
// Поиск по списку – когда людей много (у «Друзей» их бывают сотни).
const searchWrap = document.createElement('label');
searchWrap.className = 'search-field profile-list-search';
searchWrap.hidden = true;
searchWrap.innerHTML = `<span class="search-field__icon" aria-hidden="true">${iconHtml('search')}</span><input class="search-field__input" type="search" placeholder="${th('screens.user_profile_list_view.text_037')}" autocomplete="off" enterkeyhint="search" aria-label="${th('screens.user_profile_list_view.text_038')}" />`;
const searchInput = searchWrap.querySelector('input');
searchInput.addEventListener('input', () => {
query = searchText(searchInput.value);
renderRows();
});
screen.append(searchWrap, body);
let loadGeneration = 0;
function relationRow(row) {
const marks = [
RELATION_TEXT[row.relationType] || '',
row.shineStatus === 'shining' ? t('screens.user_profile_list_view.text_009') : '',
row.accountRole === 'primary' ? t('screens.user_profile_list_view.text_010') : '',
row.primaryConfirmed ? t('screens.user_profile_list_view.text_011') : '',
row.shineConfirmed ? t('screens.user_profile_list_view.text_012') : '',
].filter(Boolean);
const name = userDisplayName(row);
const sub = [name !== row.login ? row.login : '', ...marks].filter(Boolean).join(' · ');
return createPersonRow({
login: row.login,
name,
firstName: row.firstName,
lastName: row.lastName,
avatar: (row.avatarAr || row.avatarPr) ? { ar: String(row.avatarAr || '').trim(), pr: String(row.avatarPr || '').trim() } : null,
official: row.accountRole === 'primary',
ring: row.shineStatus === 'shining' ? 'shining' : (row.relationType === 'close_friend' ? 'close' : ''),
sub,
onClick: () => navigate(`SHiNE/${encodeURIComponent(row.login)}`),
actions: [{ icon: 'message', label: t('screens.user_profile_list_view.text_002'), onClick: () => navigate(`chat/${encodeURIComponent(row.login)}`) }],
});
}
function channelRow(row) {
const el = document.createElement('button');
el.type = 'button';
el.className = 'ui-button card row profile-list-row';
el.append(renderUserAvatar({ login: row.ownerLogin, firstName: row.displayName, lastName: '', avatar: (row.avatarAr || row.avatarPr) ? { ar: String(row.avatarAr || ''), pr: String(row.avatarPr || '') } : null, size: 'md' }));
const text = document.createElement('div');
text.className = 'profile-list-row-text';
const title = document.createElement('b');
title.textContent = `${String(row.displayName || row.slug || '')}${row.deleted ? t('screens.user_profile_list_view.text_013') : ''}`;
const sub = document.createElement('small');
sub.textContent = `${String(row.ownerLogin || '')} / ${String(row.slug || '')}`;
text.append(title, sub);
el.append(text);
el.addEventListener('click', () => navigate(`channel/${encodeURIComponent(row.ownerBlockchainName)}/${Number(row.rootBlockNumber || 0)}/${encodeURIComponent(row.rootBlockHashHex || '')}/about`));
return el;
}
function renderRows() {
body.replaceChildren();
const matches = (row) => (isChannelsScreen
? !query || searchText(`${row.displayName} ${row.slug} ${row.ownerLogin}`).includes(query)
: matchesPeopleQuery(`${userDisplayName(row)} ${row.login}`, query));
const visible = rows.filter(matches);
if (!rows.length) {
body.append(createStateMessage({ icon: isChannelsScreen ? 'channels' : 'network', title: EMPTY_TEXT[activeKind] || t('screens.user_profile_list_view.text_016') }));
return;
}
if (!visible.length) {
body.append(createStateMessage({ icon: 'search', title: t('screens.user_profile_list_view.text_003'), text: t('screens.user_profile_list_view.text_014') }));
return;
}
const frag = document.createDocumentFragment();
visible.forEach((row) => frag.append(isChannelsScreen ? channelRow(row) : relationRow(row)));
body.append(frag);
}
async function load(kind) {
const generation = ++loadGeneration;
body.replaceChildren(createLoadingState({ label: t('screens.user_profile_list_view.text_004') }));
try {
const payload = isChannelsScreen
? await authService.listUserProfileChannels(login, kind === 'channels_owned' ? 'owned' : 'following', 200, 0, includeDeleted)
: await authService.listUserProfileRelations(login, kind, 200, 0);
if (disposed || generation !== loadGeneration) return;
rows = Array.isArray(isChannelsScreen ? payload?.channels : payload?.users) ? (isChannelsScreen ? payload.channels : payload.users) : [];
searchWrap.hidden = rows.length < SEARCH_FROM;
renderRows();
} catch (error) {
if (disposed || generation !== loadGeneration) return;
body.replaceChildren(createErrorState({
title: t('screens.user_profile_list_view.text_005'),
text: toUserMessage(error, t('screens.user_profile_list_view.text_006')),
onRetry: () => { void load(activeKind); },
}));
}
}
screen.cleanup = () => {
disposed = true;
loadGeneration += 1;
};
void load(activeKind);
return screen;
}