Files
SHiNE-server/shine-UI/js/components/like-action-sheet.js
T

63 lines
3.1 KiB
JavaScript

import { openSheet } from './sheet.js';
function safeCount(raw, maximum = Infinity) {
const count = Number(raw);
return Number.isFinite(count) ? Math.max(0, Math.min(maximum, Math.trunc(count))) : 0;
}
// Значения счётчиков получены от сервера, это не криптографическая аттестация личности.
export function confirmLikeAction({ liked = false, counts = {}, onViewList = null } = {}) {
return new Promise((resolve) => {
let accepted = false;
const all = safeCount(counts.all ?? counts.likesCount);
const official = safeCount(counts.official ?? counts.primaryLikesCount, all);
const shining = safeCount(counts.shining ?? counts.shiningLikesCount, official);
const body = document.createElement('div');
body.className = 'stack like-consent';
const intro = document.createElement('p');
intro.textContent = 'Лайки этого сообщения (по данным сервера):';
const grid = document.createElement('div');
grid.className = 'like-consent-counters';
const categories = [
{ label: 'Официальные сияющие', count: shining, type: 'shining' },
{ label: 'Все официальные', count: official, type: 'official' },
{ label: 'Все аккаунты', count: all, type: 'all' },
];
categories.forEach(({ label, count, type }) => {
const item = document.createElement(onViewList ? 'button' : 'div');
item.className = 'like-consent-count';
if (onViewList) {
item.type = 'button';
item.addEventListener('click', () => {
sheet.close();
onViewList(type);
});
}
const number = document.createElement('strong');
number.textContent = String(count);
const name = document.createElement('span');
name.textContent = label;
item.append(number, name);
grid.append(item);
});
const warning = document.createElement('p');
warning.className = 'meta-muted';
warning.textContent = 'Добавление и отмена лайка создают записи в публичной истории блокчейна. Даже отменённый лайк может остаться в истории.';
const actions = document.createElement('div');
actions.className = 'form-actions-grid';
const cancel = document.createElement('button');
cancel.type = 'button';
cancel.className = 'secondary-btn';
cancel.textContent = 'Не менять';
const confirm = document.createElement('button');
confirm.type = 'button';
confirm.className = 'primary-btn';
confirm.textContent = liked ? 'Убрать мой лайк' : 'Добавить мой лайк';
actions.append(cancel, confirm);
body.append(intro, grid, warning, actions);
const sheet = openSheet({ title: 'Лайки', className: 'like-consent-sheet', content: body, onClose: () => resolve(accepted) });
cancel.addEventListener('click', () => sheet.close());
confirm.addEventListener('click', () => { accepted = true; sheet.close(); });
});
}