Files
dawnline/web/src/portal/flows/setup-parts.ts
T
Aemeath fb73ac3a9f
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
Dawnline 0.12.0
- The portal's traffic charts show what came down and what went up apart.
  A device's last 7 days and "Data this month" draw each day both ways:
  downloads rise above a thin line, uploads hang below it, on one scale.
  Beside them "↓ 3.2 GB ↑ 557 MB" instead of one total; hover a day for
  its own, its date where "left" stands
- Charts answer the pointer between their bars too, in the portal and in
  the admin's daily charts (a device's week, a device card's 30 days, the
  last 30 days): the figures no longer blink back to the total
- The calendar under "Access until" plays its days in only when the row is
  opened, not again each time the page refreshes with it open
- On a phone the page comes in from the top down: "Your access" first,
  then the devices
- "Show key" on a router's set-up morphs to "Hide key" with its icon, and
  brings the keys themselves into focus (it blurred the field names)
2026-10-20 20:58:36 +03:00

429 lines
16 KiB
TypeScript
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.
// The pieces the set-up sheet is made of: store badges, the step that adds the
// connection (a link, the share sheet, a key or a file, as the app and the
// device allow), the panel that sends the connection to another device, the
// .conf for a router, the Download / Copy keys beside × and the line saying
// where an app scans a code. The rules behind them are setup.ts.
import type { ClientConfig, Device } from '../../shared/types';
import { fill, fmt } from '../../shared/i18n';
import { h, ic, type Kid } from '../dom';
import { EASE_OUT, POP, SMOOTH, play } from '../motion';
import {
APP_NAME,
importPath,
importSteps,
keyFileName,
opensKeyLinks,
platformLabel,
routerStore,
scanPath,
vpnContinue,
storeFor,
type AppId,
type Platform,
type Store,
} from '../setup';
import { accordion } from '../ui/accordion';
import { copy, download, sendFile, shareFile, swapIcon } from '../ui/feedback';
import { glass } from '../ui/glass';
import { menuLine, morphText, setText } from '../ui/morph';
import { hint } from '../ui/sheet';
export const APP_ICON: Record<AppId, string> = { vpn: 'amnezia', awg: 'amneziawg', wg: 'wireguard' };
/** The app a device was set up with last time, so the sheet reopens on it. */
const memo = (d: Device) => `dl.app.${d.id}`;
export function recallApp(d: Device): AppId | null {
try {
const v = localStorage.getItem(memo(d));
return v === 'vpn' || v === 'awg' || v === 'wg' ? v : null;
} catch {
return null;
}
}
export function rememberApp(d: Device, app: AppId) {
try {
localStorage.setItem(memo(d), app);
} catch {
/* private mode */
}
}
/** What the steps work with: the device, its configuration, the key, and the choices made. */
export interface SetupCtx {
d: Device;
cfg: ClientConfig;
key: string;
/** The platform this page is open on. */
pf: Platform;
/** "the phone": the device in a sentence. */
there: string;
app(): AppId;
/** The page is open on the device being set up. */
same(): boolean;
}
/**
* A badge: the mark in a tile, two lines, a chevron (an action) or the open
* mark (a store page, in a new tab). `primary`: the one thing to tap in step 2.
*/
export function badge(
st: { href?: string; top: string; main: string; mark: string; onclick?: (ev: Event) => void },
primary = false,
): HTMLElement {
const inner = [
h('span', { class: 'b-ic' }, ic(st.mark)),
h('span', { class: 'b-t' }, h('span', { class: 'b-top' }, st.top), h('span', { class: 'b-main' }, st.main)),
ic(st.onclick || st.href?.startsWith('vpn:') ? 'chevron' : 'open'),
];
const cls = `badge${primary ? ' primary' : ''}`;
if (st.onclick) return h('button', { type: 'button', class: cls, onclick: st.onclick }, inner);
// A vpn:// key opens the app in place: no new tab.
const app = st.href?.startsWith('vpn:');
return h('a', { class: cls, href: st.href, target: app ? null : '_blank', rel: 'noopener noreferrer' }, inner);
}
/** Where the app comes from: the store (and, on Android, the APK beside it), or a line to go and get it. */
export function storeBlock(app: AppId, os: Platform, router: boolean): Kid[] {
const st: Store | null = router ? routerStore(app) : storeFor(app, os);
if (!st) return [hint(fmt(_('Get %s on the %s and come back here.'), APP_NAME[app], platformLabel(os)))];
// Android: the store can fail (no Google Play, or it hides the app), so the APK sits beside it.
if (st.alt) return [h('div', { class: 'badges2' }, badge(st), badge(st.alt)), hint(st.alt.why)];
return [badge(st)];
}
const howto = (steps: [string, string][]) =>
h(
'ol',
{ class: 'howto' },
steps.map(([text, bold]) => h('li', {}, text.replace(/ → $/, ' ›'), bold ? [' ', h('b', {}, bold)] : null)),
);
const linkBtn = (label: string, run: (ev: Event) => void) => h('button', { type: 'button', onclick: run }, label);
/** Step 2: a link for AmneziaVPN, the file for the others, in the words of where they go. */
export function connectKids(c: SetupCtx): Kid[] {
const app = c.app();
const name = APP_NAME[app];
const hereHandheld = c.pf === 'ios' || c.pf === 'android';
// On the phone being set up, a link opens AmneziaVPN only on Android; an
// iPhone takes the key as a .vpn file from the share sheet.
const inHand = c.same() && hereHandheld;
const copyKey = (ev: Event) => copy(c.key, _('Key copied'), ev.currentTarget);
const send = app === 'vpn' && inHand && !opensKeyLinks(c.pf) ? shareFile(keyFileName(c.cfg.filename), c.key) : null;
if (app === 'vpn' && inHand && (opensKeyLinks(c.pf) || send))
return [
send
? badge({ top: _('Then come back here and tap'), main: _('Send to AmneziaVPN'), mark: 'amnezia', onclick: () => void send() }, true)
: badge({ href: c.key, top: _('Then come back here and tap'), main: _('Open in AmneziaVPN'), mark: 'amnezia' }, true),
hint(
...fill(
send
? _('Pick AmneziaVPN in the share sheet. Not there? %s and add it in the app.')
: _('Nothing opened? %s and add it in the app.'),
linkBtn(_('Copy the key', 'in a sentence'), copyKey),
),
),
];
if (app === 'vpn') {
const path = importPath('vpn');
return [
badge({ top: _('The connection as one key'), main: _('Copy the key'), mark: 'copy', onclick: copyKey }, true),
howto([
[fmt(_('Open %s', 'step'), name), ''],
[path.replace(/ → [^→]*$/, ' → '), path.replace(/^.* → /, '')],
[vpnContinue(), ''],
]),
];
}
const share = hereHandheld ? shareFile(c.cfg.filename, c.cfg.conf) : null;
return [
share
? badge(
{
top: c.same() ? fmt(_('Straight into %s'), name) : fmt(_('Send it to the %s'), c.there),
main: _('Share the file'),
mark: 'share',
onclick: () => void share(),
},
true,
)
: badge(
{
top: _('Then import it in the app'),
main: _('Download the file'),
mark: 'download',
onclick: () => download(c.cfg.filename, c.cfg.conf),
},
true,
),
// In the words of the app where the file goes: the phone in hand, or the computer's system.
howto(importSteps(app, c.pf, c.cfg.filename, !!share && c.same())),
];
}
/** A computer set up from elsewhere: the connection to send there, as the key or the file. */
export function sendKids(c: SetupCtx): Kid[] {
const app = c.app();
const btn = (icon: string, label: string, run: (ev: Event) => void) =>
h('button', { type: 'button', class: 'btn soft sm', onclick: run }, ic(icon), h('span', { class: 'lbl' }, label));
if (app === 'vpn') {
const file = keyFileName(c.cfg.filename);
const share = !!shareFile(file, c.key);
return [
h(
'p',
{ class: 'desc' },
fmt(_('Send it to the %s in a message or a note, then in AmneziaVPN: %s.'), c.there, importPath('vpn').replace(/ → /g, ' › ')),
),
h(
'div',
{ class: 'btnrow2' },
btn('copy', _('Copy the key'), (ev) => copy(c.key, _('Key copied'), ev.currentTarget)),
btn(share ? 'share' : 'download', share ? _('Share as a file') : _('Download the key'), () => sendFile(file, c.key)),
),
];
}
const share = !!shareFile(c.cfg.filename, c.cfg.conf);
return [
h(
'p',
{ class: 'desc' },
...fill<Node | string>(_('Send it to the %s, then import %s in %s.'), c.there, h('b', {}, c.cfg.filename), APP_NAME[app]),
),
h(
'div',
{ class: 'btnrow2' },
btn(share ? 'share' : 'download', share ? _('Share the file') : _('Download the file'), () => sendFile(c.cfg.filename, c.cfg.conf)),
btn('copy', _('Copy the configuration'), (ev) => copy(c.cfg.conf, _('Configuration copied'), ev.currentTarget)),
),
];
}
/** One configuration line, highlighted; private and preshared keys masked unless shown. */
function confLine(line: string, reveal: boolean): Node {
if (/^\s*\[.+\]\s*$/.test(line)) return h('span', { class: 'sec' }, line);
const m = /^(\s*[A-Za-z0-9]+\s*=)\s*(.*)$/.exec(line);
if (!m) return document.createTextNode(line);
const secret = /^(\s*)(PrivateKey|PresharedKey)\b/.test(line);
const value = !secret ? m[2] : reveal ? h('span', { class: 'secret' }, m[2]) : h('span', { class: 'mask' }, '••••••••••••••••');
return h('span', {}, h('span', { class: 'k' }, m[1]), ' ', value);
}
/** The .conf for a router: a file card to download (or share), and the configuration folded, its keys hidden. */
export function filePane(cfg: ClientConfig): HTMLElement {
let reveal = false;
const pre = h('pre', { class: 'conf' });
const paint = () =>
pre.replaceChildren(...cfg.conf.split('\n').flatMap((l, i) => (i ? ['\n', confLine(l, reveal)] : [confLine(l, reveal)])));
paint();
const share = !!shareFile(cfg.filename, cfg.conf);
let icon = ic('key');
const label = h('span', {}, _('Show key'));
const eye = h(
'button',
{
type: 'button',
class: 'btn soft sm',
onclick: () => {
reveal = !reveal;
icon = swapIcon(icon, reveal ? 'x' : 'key');
morphText(label, reveal ? _('Hide key') : _('Show key'));
paint();
// A key shown comes into focus out of a blur; hidden again, the mask fades back.
pre.querySelectorAll('.mask, .secret').forEach((x) =>
play(
x,
[
{ filter: 'blur(4px)', opacity: 0.4 },
{ filter: 'none', opacity: 1 },
],
{ duration: 320, easing: EASE_OUT },
),
);
},
},
icon,
label,
);
const cp = h(
'button',
{ type: 'button', class: 'btn soft sm', onclick: (ev: Event) => copy(cfg.conf, _('Configuration copied'), ev.currentTarget) },
ic('copy'),
h('span', { class: 'lbl' }, _('Copy')),
);
const card = h(
'div',
{ class: 'filecard' },
h(
'div',
{ class: 'fc-row' },
h('span', { class: 'filecard-ic' }, '.conf'),
h('span', { class: 't' }, h('span', { class: 'mono' }, cfg.filename), h('span', {}, _('For computers and routers'))),
),
h(
'button',
{ type: 'button', class: 'btn accent sm block', onclick: () => sendFile(cfg.filename, cfg.conf) },
ic(share ? 'open' : 'download'),
h('span', {}, share ? _('Share file') : _('Download')),
),
);
const conf = accordion([{ icon: glass('file'), title: _('Configuration'), body: [h('div', { class: 'confbar' }, eye, cp), pre] }], {
inset: true,
});
return h('div', { class: 'step-box' }, card, conf.el);
}
export interface ScanLine {
el: HTMLElement;
/** Another app picked: its name and its path morph in place. */
set(app: AppId): void;
}
/** "In AmneziaWG: + › Create from QR code", with both phone systems' wording where they differ. */
export function scanLine(app: AppId): ScanLine {
const name = h('span', { class: 'osm-p' }, APP_NAME[app]);
const path = menuLine(scanPath(app, 'ios'), scanPath(app, 'android'));
return {
el: h('span', { class: 'scanline' }, h('span', {}, ...fill(_('In %s:'), name)), path.el),
set(a) {
morphText(name, APP_NAME[a], undefined, { whole: true });
path.set(scanPath(a, 'ios'), scanPath(a, 'android'));
},
};
}
export interface ConnPill {
el: HTMLElement;
/** The labels follow the app picked. */
sync(): void;
}
/**
* Beside ×: the connection itself, to download or copy, where the sheet offers
* neither (a phone or a tablet set up from elsewhere). One tooltip for both
* keys, under them: the first waits a beat; once one is up the next comes at
* once and the tooltip glides across. Copied: a check, and the tooltip says so.
*/
export function connPill(get: () => { text: string; msg: string; file: string; app: string }): ConnPill {
const tt = h('span', { class: 'tt' });
const tm = h('span', { class: 'tt-m' });
const tip = h('span', { class: 'hp-tip', 'aria-hidden': 'true' }, tt, tm);
const dl = h('button', { type: 'button', class: 'hp-b', onclick: () => sendFile(get().file, get().text) }, ic('download'));
const cp = h('button', { type: 'button', class: 'hp-b' }, ic('copy'));
const el = h('div', { class: 'hpill', role: 'group', 'aria-label': _('The connection'), 'data-key': 'conn' }, dl, cp, tip);
let tipFor: HTMLElement | null = null;
let tipT = 0;
let warmUntil = 0;
const words = (b: HTMLElement) => (b === cp && cp.classList.contains('done') ? _('Copied') : (b.getAttribute('aria-label') ?? ''));
// Centred under its key, but never past the sheet's edge (× and its margin to the right).
const centre = (b: HTMLElement, t: string) => {
tm.textContent = t;
const w = tm.offsetWidth + 20;
return Math.min(b.offsetLeft + b.offsetWidth / 2, el.offsetWidth + 46 - w / 2);
};
const at = (x: number) => `translateX(${x}px) translateX(-50%)`;
const glideTo = (x: number) => {
const from = getComputedStyle(tip).transform;
tip.getAnimations().forEach((a) => a.cancel());
tip.style.transform = at(x);
play(tip, [{ transform: from }, { transform: at(x) }], { duration: SMOOTH.duration, easing: SMOOTH.easing });
};
// Another key's words, or "Copied": what the two share slides, the rest morphs; the pill glides with it.
const reword = (t: string) => morphText(tt, t);
const show = (b: HTMLElement) => {
clearTimeout(tipT);
if (tipFor === b && tip.classList.contains('on')) return;
tipFor = b;
if (tip.classList.contains('on')) {
const t = words(b);
glideTo(centre(b, t));
return reword(t);
}
const up = () => {
const t = words(b);
setText(tt, t);
const x = centre(b, t);
tip.getAnimations().forEach((a) => a.cancel());
tip.style.transform = at(x);
tip.classList.add('on');
play(
tip,
[
{ opacity: 0, transform: `${at(x)} translateY(-3px) scale(.96)`, filter: 'blur(3px)' },
{ opacity: 1, transform: at(x), filter: 'blur(0px)' },
],
{
duration: 220,
easing: EASE_OUT,
},
);
};
if (performance.now() < warmUntil) up();
else tipT = window.setTimeout(up, 420);
};
const hide = () => {
clearTimeout(tipT);
tipFor = null;
if (!tip.classList.contains('on')) return;
warmUntil = performance.now() + 500;
tip.classList.remove('on');
play(tip, [{ opacity: 1 }, { opacity: 0 }], { duration: 120, easing: 'ease-out' });
};
for (const b of [dl, cp]) {
b.addEventListener('pointerenter', (e) => {
if (e.pointerType === 'mouse') show(b);
});
b.addEventListener('focus', () => {
if (b.matches(':focus-visible')) show(b);
});
b.addEventListener('blur', hide);
}
el.addEventListener('pointerleave', hide);
const icon = (name: string) => {
const n = ic(name);
cp.querySelector('svg')!.replaceWith(n);
play(
n,
[
{ opacity: 0, transform: 'scale(.6)', filter: 'blur(3px)' },
{ opacity: 1, transform: 'none', filter: 'none' },
],
{ duration: POP.duration, easing: POP.easing },
);
};
// Copied: the key shows a check and the tooltip says so, then both go back.
const sayCopy = () => {
if (tipFor !== cp || !tip.classList.contains('on')) return;
const t = words(cp);
glideTo(centre(cp, t));
reword(t);
};
let back = 0;
cp.addEventListener('click', () => {
const c = get();
void copy(c.text, c.msg);
clearTimeout(back);
if (!cp.classList.contains('done')) {
cp.classList.add('done');
icon('check');
sayCopy();
}
back = window.setTimeout(() => {
cp.classList.remove('done');
icon('copy');
sayCopy();
}, 1600);
});
return {
el,
sync() {
const app = get().app;
dl.setAttribute('aria-label', fmt(_('Download %s config'), app));
cp.setAttribute('aria-label', fmt(_('Copy %s config'), app));
},
};
}