SHA256
85 lines
4.4 KiB
JavaScript
85 lines
4.4 KiB
JavaScript
import { t } from '../i18n/index.js';
|
|
import { getShowPublicHistoryWarning, setShowPublicHistoryWarning, getPublicHistoryRememberLabel } from '../services/public-history-consent.js';
|
|
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(options = {}) {
|
|
if (!getShowPublicHistoryWarning()) return Promise.resolve(true);
|
|
return openLikeSheet(options);
|
|
}
|
|
|
|
// Просмотр счётчиков не является действием в блокчейне и не спрашивает согласие.
|
|
export function showLikeStatistics({ counts = {}, onViewList = null } = {}) {
|
|
return openLikeSheet({ counts, onViewList, readOnly: true });
|
|
}
|
|
|
|
function openLikeSheet({ liked = false, counts = {}, onViewList = null, readOnly = false } = {}) {
|
|
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 = t('dialogs.modals.like_action_sheet.label_001');
|
|
const grid = document.createElement('div');
|
|
grid.className = 'like-consent-counters';
|
|
const categories = [
|
|
{ label: t('dialogs.remaining.components.like_action_sheet.openlikesheet.ofitsialnye_siyayuschie'), count: shining, type: 'shining' },
|
|
{ label: t('dialogs.remaining.components.like_action_sheet.openlikesheet.vse_ofitsialnye'), count: official, type: 'official' },
|
|
{ label: t('dialogs.remaining.components.like_action_sheet.openlikesheet.vse_akkaunty'), 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 = t('dialogs.modals.like_action_sheet.label_002');
|
|
const rememberLabel = document.createElement('label');
|
|
rememberLabel.className = 'confirm-dialog__remember';
|
|
const remember = document.createElement('input');
|
|
remember.type = 'checkbox';
|
|
const rememberText = document.createElement('span');
|
|
rememberText.textContent = getPublicHistoryRememberLabel();
|
|
rememberLabel.append(remember, rememberText);
|
|
const actions = document.createElement('div');
|
|
actions.className = 'form-actions-grid';
|
|
const cancel = document.createElement('button');
|
|
cancel.type = 'button';
|
|
cancel.className = 'secondary-btn';
|
|
cancel.textContent = readOnly ? t('dialogs.modals.like_action_sheet.label_003') : t('dialogs.remaining.components.like_action_sheet.openlikesheet.ne_menyat');
|
|
const confirm = document.createElement('button');
|
|
confirm.type = 'button';
|
|
confirm.className = 'primary-btn';
|
|
confirm.textContent = liked ? t('dialogs.modals.like_action_sheet.label_004') : t('dialogs.remaining.components.like_action_sheet.openlikesheet.dobavit_moy_layk');
|
|
if (readOnly) actions.append(cancel);
|
|
else actions.append(cancel, confirm);
|
|
body.append(intro, grid);
|
|
if (!readOnly) body.append(warning, rememberLabel);
|
|
body.append(actions);
|
|
const sheet = openSheet({ title: t('dialogs.modals.like_action_sheet.label_005'), className: 'like-consent-sheet', content: body, onClose: () => resolve(accepted) });
|
|
cancel.addEventListener('click', () => sheet.close());
|
|
confirm.addEventListener('click', () => { if (remember.checked) setShowPublicHistoryWarning(false); accepted = true; sheet.close(); });
|
|
});
|
|
}
|