SHA256
CORE/CONTENT test5593 и адаптация UI
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
import { t, th } from '../i18n/index.js';
|
||||
import { createTopBar } from '../components/topbar.js';
|
||||
import { authService, state } from '../state.js';
|
||||
import { toUserMessage } from '../services/ui-error-texts.js';
|
||||
import { normalizeChannelDescription } from '../services/channel-name-rules.js';
|
||||
|
||||
export const pageMeta = { id: 'add-personal-public-chat-view', get title() { return t('screens.add_personal_public_chat_view.text_001'); } };
|
||||
|
||||
const CREATE_CHANNEL_FLASH_KEY = 'shine-channels-create-success';
|
||||
const CHANNEL_TYPE_PERSONAL = 100;
|
||||
|
||||
function persistCreateSuccessFlash(message) {
|
||||
try {
|
||||
sessionStorage.setItem(CREATE_CHANNEL_FLASH_KEY, String(message || '').trim());
|
||||
} catch {
|
||||
// ignore storage errors
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeLoginInput(value) {
|
||||
return String(value || '').trim().replace(/^@+/, '');
|
||||
}
|
||||
|
||||
function isValidLogin(value) {
|
||||
const clean = normalizeLoginInput(value);
|
||||
if (!clean) return false;
|
||||
if (clean.length < 1 || clean.length > 20) return false;
|
||||
return /^[A-Za-z0-9_]+$/.test(clean);
|
||||
}
|
||||
|
||||
function validateDescription(value) {
|
||||
const normalized = normalizeChannelDescription(value);
|
||||
const bytes = new TextEncoder().encode(normalized).length;
|
||||
if (bytes > 200) {
|
||||
return { ok: false, normalized, bytes, error: t('screens.add_personal_public_chat_view.text_005') };
|
||||
}
|
||||
return { ok: true, normalized, bytes, error: '' };
|
||||
}
|
||||
|
||||
function createDebounced(fn, delayMs = 240) {
|
||||
let timer = null;
|
||||
return (...args) => {
|
||||
if (timer) clearTimeout(timer);
|
||||
timer = setTimeout(() => {
|
||||
timer = null;
|
||||
fn(...args);
|
||||
}, delayMs);
|
||||
};
|
||||
}
|
||||
|
||||
export function render({navigate, chrome}) {
|
||||
const screen = document.createElement('section');
|
||||
screen.className = 'stack channels-screen channels-screen--add';
|
||||
|
||||
chrome?.setTopbar(createTopBar({
|
||||
title: t('screens.add_personal_public_chat_view.text_002'),
|
||||
back: { label: '<', onClick: () => navigate('channels-list/dialogs') },
|
||||
}));
|
||||
|
||||
const form = document.createElement('form');
|
||||
form.className = 'card stack';
|
||||
form.innerHTML = `
|
||||
<strong class="channel-head-title">${th('screens.add_personal_public_chat_view.text_013')}</strong>
|
||||
<p class="channel-head-meta">${th('screens.add_personal_public_chat_view.text_014')}</p>
|
||||
|
||||
<label for="chat-login">${th('screens.add_personal_public_chat_view.text_015')}</label>
|
||||
<input id="chat-login" class="input" maxlength="20" placeholder="${th('screens.add_personal_public_chat_view.text_016')}" autocomplete="off" required />
|
||||
<div id="chat-login-suggest" class="channels-search-suggest" style="display:none"></div>
|
||||
<div id="chat-login-error" class="meta-muted inline-error"></div>
|
||||
|
||||
<label for="chat-description">${th('screens.add_personal_public_chat_view.text_017')}</label>
|
||||
<textarea id="chat-description" class="input" rows="4" maxlength="400" placeholder="${th('screens.add_personal_public_chat_view.text_018')}"></textarea>
|
||||
<div class="meta-muted" id="chat-description-counter">${th('screens.add_personal_public_chat_view.text_019')}</div>
|
||||
<div id="chat-description-error" class="meta-muted inline-error"></div>
|
||||
|
||||
<div class="card meta-muted">
|
||||
${th('screens.add_personal_public_chat_view.text_020')}
|
||||
</div>
|
||||
|
||||
<div id="chat-create-error" class="meta-muted inline-error"></div>
|
||||
<div class="form-actions-grid">
|
||||
<button type="button" class="secondary-btn" id="cancel-create-chat">${th('screens.add_personal_public_chat_view.text_021')}</button>
|
||||
<button type="submit" class="primary-btn" id="submit-create-chat">${th('screens.add_personal_public_chat_view.text_022')}</button>
|
||||
</div>
|
||||
`;
|
||||
|
||||
const loginEl = form.querySelector('#chat-login');
|
||||
const suggestEl = form.querySelector('#chat-login-suggest');
|
||||
const loginErrorEl = form.querySelector('#chat-login-error');
|
||||
const descriptionEl = form.querySelector('#chat-description');
|
||||
const descriptionErrorEl = form.querySelector('#chat-description-error');
|
||||
const descriptionCounterEl = form.querySelector('#chat-description-counter');
|
||||
const errorEl = form.querySelector('#chat-create-error');
|
||||
const submitEl = form.querySelector('#submit-create-chat');
|
||||
const cancelEl = form.querySelector('#cancel-create-chat');
|
||||
|
||||
let submitInFlight = false;
|
||||
let selectedCanonicalLogin = '';
|
||||
|
||||
const setBusy = (busy) => {
|
||||
submitInFlight = !!busy;
|
||||
submitEl.disabled = submitInFlight;
|
||||
cancelEl.disabled = submitInFlight;
|
||||
loginEl.disabled = submitInFlight;
|
||||
descriptionEl.disabled = submitInFlight;
|
||||
submitEl.textContent = submitInFlight ? t('screens.add_personal_public_chat_view.text_006') : t('screens.add_personal_public_chat_view.text_007');
|
||||
};
|
||||
|
||||
const renderLoginSuggestions = (logins) => {
|
||||
suggestEl.innerHTML = '';
|
||||
const rows = Array.isArray(logins) ? logins.filter(Boolean) : [];
|
||||
if (!rows.length) {
|
||||
suggestEl.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
suggestEl.style.display = '';
|
||||
rows.slice(0, 8).forEach((login) => {
|
||||
const btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'ui-button channel-search-item';
|
||||
btn.textContent = String(login);
|
||||
btn.addEventListener('click', () => {
|
||||
selectedCanonicalLogin = String(login);
|
||||
loginEl.value = selectedCanonicalLogin;
|
||||
suggestEl.style.display = 'none';
|
||||
});
|
||||
suggestEl.append(btn);
|
||||
});
|
||||
};
|
||||
|
||||
const updateValidation = () => {
|
||||
const loginRaw = String(loginEl.value || '').trim();
|
||||
const loginOk = isValidLogin(loginRaw);
|
||||
const descriptionCheck = validateDescription(descriptionEl.value);
|
||||
|
||||
loginErrorEl.textContent = loginOk ? '' : t('screens.add_personal_public_chat_view.text_008');
|
||||
descriptionErrorEl.textContent = descriptionCheck.error;
|
||||
descriptionCounterEl.textContent = t('screens.add_personal_public_chat_view.text_011', { value1: Number(descriptionCheck.bytes || 0) });
|
||||
|
||||
const ok = loginOk && descriptionCheck.ok;
|
||||
submitEl.disabled = submitInFlight || !ok;
|
||||
return { ok, description: descriptionCheck.normalized };
|
||||
};
|
||||
|
||||
const refreshSuggestions = createDebounced(async () => {
|
||||
if (submitInFlight) return;
|
||||
const loginRaw = normalizeLoginInput(loginEl.value);
|
||||
if (loginRaw.length < 1) {
|
||||
suggestEl.style.display = 'none';
|
||||
suggestEl.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const logins = await authService.searchUsers(loginRaw);
|
||||
renderLoginSuggestions(logins);
|
||||
} catch {
|
||||
suggestEl.style.display = 'none';
|
||||
suggestEl.innerHTML = '';
|
||||
}
|
||||
}, 220);
|
||||
|
||||
loginEl.addEventListener('input', () => {
|
||||
selectedCanonicalLogin = '';
|
||||
errorEl.textContent = '';
|
||||
updateValidation();
|
||||
refreshSuggestions();
|
||||
});
|
||||
descriptionEl.addEventListener('input', updateValidation);
|
||||
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
if (submitInFlight) return;
|
||||
|
||||
const login = state.session.login;
|
||||
const storagePwd = state.session.storagePwdInMemory;
|
||||
if (!login || !storagePwd) {
|
||||
errorEl.textContent = t('screens.add_personal_public_chat_view.text_003');
|
||||
return;
|
||||
}
|
||||
|
||||
const validation = updateValidation();
|
||||
if (!validation.ok) return;
|
||||
|
||||
setBusy(true);
|
||||
errorEl.textContent = '';
|
||||
loginErrorEl.textContent = '';
|
||||
|
||||
try {
|
||||
const inputLogin = normalizeLoginInput(loginEl.value);
|
||||
const foundUser = await authService.getUser(inputLogin);
|
||||
if (!foundUser?.exists) {
|
||||
throw new Error(t('screens.add_personal_public_chat_view.text_009'));
|
||||
}
|
||||
const canonicalLogin = String(foundUser?.login || inputLogin).trim();
|
||||
if (!canonicalLogin) throw new Error(t('screens.add_personal_public_chat_view.text_010'));
|
||||
|
||||
await authService.addBlockCreateChannel({
|
||||
login,
|
||||
storagePwd,
|
||||
channelName: canonicalLogin,
|
||||
channelDescription: validation.description,
|
||||
channelType: CHANNEL_TYPE_PERSONAL,
|
||||
channelTypeVersion: 1,
|
||||
});
|
||||
|
||||
persistCreateSuccessFlash(t('screens.add_personal_public_chat_view.text_012', { value1: canonicalLogin }));
|
||||
navigate('channels-list/dialogs');
|
||||
} catch (error) {
|
||||
errorEl.textContent = toUserMessage(error, t('screens.add_personal_public_chat_view.text_004'));
|
||||
setBusy(false);
|
||||
updateValidation();
|
||||
}
|
||||
});
|
||||
|
||||
cancelEl.addEventListener('click', () => navigate('channels-list/dialogs'));
|
||||
|
||||
screen.append(form);
|
||||
loginEl.focus();
|
||||
updateValidation();
|
||||
return screen;
|
||||
}
|
||||
Reference in New Issue
Block a user