Files
Febe/src/Febe.Web/wwwroot/js/pages/connection.js
T

185 lines
8.2 KiB
JavaScript

// Collegamento a Instagram (mockup 10, spec §4.1, §10.3). Docker: the large remote view of the server's browser with
// the steps «Come collegarti», the note that the password goes only to Instagram (keys pass through the server without
// being recorded), the risk note, Annulla and «Ho finito». Windows: the browser is the separate window «Browser di
// Febe» and the large area says so, with «Portalo in primo piano». When connected: the account, «Usa il browser» and
// «Scollega» with its confirmation. While Alberto uses the browser the engine is still.
import { api } from '../core/api.js';
import { caps } from '../core/context.js';
import { h, icon, fill } from '../core/dom.js';
import { refreshEngine } from '../core/engine.js';
import { navigate } from '../core/shell.js';
import { t } from '../core/text.js';
import { avatar, confirmDialog, label, showError, toast } from '../core/ui.js';
import { remoteView } from './remote-view.js';
const STATE_KIND = { notConnected: 'ambra', connected: 'verde', verificationRequired: 'rosso', browserUnavailable: '' };
/** Starts using the browser (the engine is still); with openLogin the real login page is loaded. */
export async function beginDriving(openLogin) {
try {
await api('/api/connection/begin', { method: 'POST', body: { openLogin } });
return true;
} catch (error) {
showError(error);
return false;
}
}
/** The area that replaces the remote view on Windows: the browser is its own window. */
export function windowPanel() {
return h('div.vista.vista-finestra',
h('div.vista-segnaposto',
icon('app-window', 'l'),
h('h2', t('impostazioni.collegamento.finestra')),
h('p', t('impostazioni.collegamento.finestraTesto')),
h('button.btn.primo', { type: 'button', onclick: () => beginDriving(false) }, icon('app-window', 's'), t('impostazioni.collegamento.primoPiano'))));
}
/** «Ho finito»: Febe checks the session and hides the browser. Returns the answer of the server. */
export async function finishDriving() {
try {
const result = await api('/api/connection/finish', { method: 'POST' });
refreshEngine();
return result;
} catch (error) {
showError(error);
return null;
}
}
export async function mountConnection(page) {
page.back(t('pagine.impostazioni'), '/impostazioni');
page.title(t('impostazioni.collegamento.titolo'));
const stateLabel = h('span');
page.head.insertBefore(stateLabel, page.head.children[1] || null);
const left = h('div.collega-sinistra');
const right = h('div.collega-destra');
fill(page.body, h('div.pagina-collegamento', left, right));
const windows = caps().browserWindow;
let status = await api('/api/connection', { signal: page.signal });
async function draw() {
fill(stateLabel, label(t(`impostazioni.collegamento.stati.${status.state}`), STATE_KIND[status.state] || ''));
if (status.state === 'connected') drawConnected();
else if (status.state === 'browserUnavailable') drawUnavailable();
else drawLogin();
}
// ------------------------------------------------------------------------------------------- login or verification
function drawLogin() {
const verification = status.state === 'verificationRequired';
beginDriving(!verification);
fill(left, windows ? windowPanel() : remoteView(page, { url: status.currentUrl, login: !verification }).element);
const steps = t(windows ? 'impostazioni.collegamento.passiWindows' : 'impostazioni.collegamento.passi');
const message = h('div', { 'aria-live': 'polite' });
fill(right,
verification
? h('div.avviso.rosso', icon('octagon-alert', 's'), h('span', h('b', t('impostazioni.collegamento.verificaTitolo')), ' ', t('impostazioni.collegamento.verificaTesto')))
: h('div',
h('h2.sez', t('impostazioni.collegamento.come')),
h('ol.passi-collegamento', (Array.isArray(steps) ? steps : []).map((s, i) => h('li', h('span.numero', String(i + 1)), h('div', h('b', s.titolo), h('p', s.testo)))))),
passwordNote(windows),
h('div.avviso.ambra', icon('triangle-alert', 's'), h('span', t('impostazioni.collegamento.rischio'))),
// Windows: one engine per account (spec §5) said before the login too; Docker keeps mockup 10 as it is.
windows ? h('div.avviso', icon('info', 's'), h('span', t('impostazioni.collegamento.unMotore'))) : null,
message,
h('div.molla'),
h('div.bottoni-collegamento',
h('button.btn.grande', { type: 'button', onclick: cancel }, t('impostazioni.collegamento.annulla')),
h('button.btn.primo.grande', { type: 'button', onclick: () => finish(message) }, t('impostazioni.collegamento.hoFinito'))));
}
async function cancel() {
try {
await api('/api/connection/cancel', { method: 'POST' });
} catch (error) {
showError(error);
}
navigate('/impostazioni');
}
async function finish(message) {
const result = await finishDriving();
if (!result) return;
if (result.connected) {
toast(result.message);
status = await api('/api/connection', { signal: page.signal });
draw();
} else {
fill(message, h('div.avviso.rosso', icon('octagon-alert', 's'), h('span', result.message)));
}
}
// ------------------------------------------------------------------------------------------- connected
function drawConnected() {
fill(left, h('div.pannello.collegato',
h('div.chi-collegato', avatar(status.username || '', 'g'),
h('div', h('h2', t('impostazioni.collegamento.collegatoCome', { nome: status.username || '' })), status.displayName ? h('span.muto', status.displayName) : null)),
h('p', t('impostazioni.collegamento.collegatoTesto')),
h('div.azioni-collegato',
h('button.btn', { type: 'button', onclick: useBrowser }, icon('app-window', 's'), t('impostazioni.collegamento.usaBrowser')),
h('button.btn.pericolo', { type: 'button', onclick: disconnect }, icon('log-out', 's'), t('impostazioni.collegamento.scollega'))),
h('p.aiuto.piccolo', t('impostazioni.collegamento.usaBrowserTesto'))));
fill(right,
passwordNote(windows),
h('div.avviso.ambra', icon('triangle-alert', 's'), h('span', t('impostazioni.collegamento.rischio'))),
h('div.avviso', icon('info', 's'), h('span', t('impostazioni.collegamento.unMotore'))));
}
async function useBrowser() {
if (windows) {
await beginDriving(false);
} else {
navigate('/browser');
}
}
async function disconnect() {
const choice = await confirmDialog({
title: t('impostazioni.collegamento.scollegaTitolo'),
text: t('impostazioni.collegamento.scollegaTesto'),
buttons: [
{ label: t('comune.annulla'), value: 'no', kind: 'nudo', autofocus: true },
{ label: t('impostazioni.collegamento.usaPrima'), value: 'browser', kind: '' },
{ label: t('impostazioni.collegamento.scollegaConferma'), value: 'yes', kind: 'pericolo' },
],
});
if (choice === 'browser') {
useBrowser();
return;
}
if (choice !== 'yes') return;
try {
await api('/api/connection/disconnect', { method: 'POST' });
toast(t('impostazioni.collegamento.scollegato'));
refreshEngine();
status = await api('/api/connection', { signal: page.signal });
draw();
} catch (error) {
showError(error);
}
}
// ------------------------------------------------------------------------------------------- unavailable
function drawUnavailable() {
fill(left, h('div.vista.vista-finestra', h('div.vista-segnaposto',
icon('octagon-alert', 'l'),
h('h2', t('impostazioni.collegamento.nonDisponibileTitolo')),
h('p', t('impostazioni.collegamento.nonDisponibileTesto')))));
fill(right, passwordNote(windows), h('div.avviso.ambra', icon('triangle-alert', 's'), h('span', t('impostazioni.collegamento.rischio'))));
}
await draw();
}
/** «La password va solo a Instagram», with the true wording of each shell (spec §10.3). */
export function passwordNote(windows) {
return h('div.avviso.nota-password', icon('shield-check', 's'),
h('span', h('b', t('impostazioni.collegamento.passwordTitolo')), ' ', t(windows ? 'impostazioni.collegamento.passwordWindows' : 'impostazioni.collegamento.passwordDocker')));
}
export default {
mount: (page) => mountConnection(page),
};