From 0fd22b917a8876ef4c71a3a71f62ef3d408223f3 Mon Sep 17 00:00:00 2001 From: Aemeath <35622087+tickcount@users.noreply.github.com> Date: Mon, 26 Oct 2026 06:41:54 +0300 Subject: [PATCH] Dawnline 0.16.1 - Checking one component for updates in Components no longer fails with "XHR request timed out": the check starts at once instead of keeping the page waiting twenty seconds - On a router without curl, updates and the installer download release files through GitHub's API: GitHub's own download links can make OpenWrt's built-in downloader hang - Adding a device in the admin offers the same four types as the personal page: PC, Mobile, Router and Other (phones and tablets are one choice) --- install.sh | 42 ++++++++++---- package/luci-app-dawnline/Makefile | 2 +- package/luci-app-dawnline/po/ru/dawnline.po | 4 ++ .../po/templates/dawnline.pot | 4 ++ .../root/usr/libexec/dawnline/updater | 2 +- .../root/usr/share/ucode/dawnline/github.uc | 56 ++++++++++++++----- .../usr/share/ucode/dawnline/rpc/updates.uc | 17 +++--- .../root/usr/share/ucode/dawnline/upd_app.uc | 2 +- .../root/usr/share/ucode/dawnline/util.uc | 2 +- tests/ucode/github.test.uc | 25 +++++++++ web/package-lock.json | 4 +- web/package.json | 2 +- web/src/admin/device.ts | 39 +++++++++---- web/src/admin/styles/overview.css | 2 +- web/src/admin/updater/operation.ts | 3 +- web/src/portal/flows/add.ts | 23 +++----- web/src/portal/i18n/portal.pot | 2 +- web/src/portal/i18n/ru.po | 2 +- web/src/shared/format.ts | 16 +++++- web/src/shared/icons.ts | 3 + 20 files changed, 179 insertions(+), 73 deletions(-) create mode 100644 tests/ucode/github.test.uc diff --git a/install.sh b/install.sh index 7432a66..5b28bf6 100755 --- a/install.sh +++ b/install.sh @@ -487,18 +487,19 @@ check_tools() { # ---------- downloads ---------- -# fetch_once URL FILE [SECONDS] +# fetch_once URL FILE [SECONDS] [ACCEPT] fetch_once() { _fu="$1" _fd="$2" _ft="${3:-$DOWNLOAD_TIMEOUT}" + _fa="${4:-}" rm -f "$_fd" case "$FETCHER" in curl) run_logged download curl -fsSL --connect-timeout "$CONNECT_TIMEOUT" --max-time "$_ft" \ - --speed-limit 1024 --speed-time 30 -o "$_fd" "$_fu" + --speed-limit 1024 --speed-time 30 ${_fa:+-H "Accept: $_fa"} -o "$_fd" "$_fu" ;; - *) run_logged download "$FETCHER" -q -T "$CONNECT_TIMEOUT" -O "$_fd" "$_fu" ;; + *) run_logged download "$FETCHER" -q -T "$CONNECT_TIMEOUT" ${_fa:+--header="Accept: $_fa"} -O "$_fd" "$_fu" ;; esac _frc=$? [ "$_frc" -eq 0 ] && [ -s "$_fd" ] && return 0 @@ -531,16 +532,29 @@ asset_digest() { jsonfilter -i "$1" -e "@.assets[@.name='$2'].digest" 2>/dev/null | head -n 1 | sed -n 's/^sha256:\([0-9a-f]\{64\}\)$/\1/p' } -# download URL FILE LABEL [SHA256]: direct, then (only when the sha256 is -# known) two mirrors, then direct again. A file whose sha256 doesn't match is -# thrown away and the next source is tried; without a sha256 only GitHub -# itself is used. +# A release file's URL in the GitHub API, from release JSON (empty if unknown). +# Asked for application/octet-stream, it answers with the file itself. +asset_api_url() { + [ -s "$1" ] || return 0 + jsonfilter -i "$1" -e "@.assets[@.name='$2'].url" 2>/dev/null | head -n 1 | + sed -n 's#^\(https://api\.github\.com/repos/[^/]*/[^/]*/releases/assets/[0-9][0-9]*\)$#\1#p' +} + +# download URL FILE LABEL [SHA256] [API_URL]: without curl the API's URL +# first (github.com's redirect carries a Content-Security-Policy header that +# can outgrow uclient-fetch's 4 KB line buffer, and it stalls there), then +# direct, then (only when the sha256 is known) two mirrors, then direct again. +# A file whose sha256 doesn't match is thrown away and the next source is +# tried; without a sha256 only GitHub itself is used. download() { _du="$1" _dd="$2" _dl="$3" _dw="${4:-}" + _da="${5:-}" + [ "$FETCHER" != curl ] || _da="" set -- "$_du" + [ -z "$_da" ] || set -- "$_da" "$_du" if [ -n "$_dw" ]; then for _m in $MIRRORS; do set -- "$@" "$_m$_du"; done fi @@ -555,7 +569,9 @@ download() { sleep 2 msg "Downloading $_dl via $(host_of "$_try") (attempt $_n/$_total)" fi - if fetch_once "$_try" "$_dd"; then + _acc="" + [ "$_try" != "$_da" ] || _acc="application/octet-stream" + if fetch_once "$_try" "$_dd" "" "$_acc"; then if [ -n "$_dw" ] && [ "$(sha256sum "$_dd" 2>/dev/null | awk '{ print $1 }')" != "$_dw" ]; then warn "$(host_of "$_try") sent a file that doesn't match GitHub's sha256: discarded" rm -f "$_dd" @@ -1118,10 +1134,11 @@ confirm() { download_release_file() { _name="$1" _sums="$TMP_DIR/sha256sums.txt" - # The checksum: GitHub's own (API), else the release's sha256sums.txt straight from github.com. + # The checksum: GitHub's own (API), else the release's sha256sums.txt straight from GitHub. _want="$(asset_digest "$TMP_DIR/release.json" "$_name")" if [ -z "$_want" ]; then - [ -s "$_sums" ] || download "$ASSET_BASE/sha256sums.txt" "$_sums" sha256sums.txt || { + [ -s "$_sums" ] || download "$ASSET_BASE/sha256sums.txt" "$_sums" sha256sums.txt "" \ + "$(asset_api_url "$TMP_DIR/release.json" sha256sums.txt)" || { err "Download failed: sha256sums.txt (does release $VERSION exist?)" return 1 } @@ -1131,7 +1148,7 @@ download_release_file() { return 1 } fi - download "$ASSET_BASE/$_name" "$TMP_DIR/$_name" "$_name" "$_want" || { + download "$ASSET_BASE/$_name" "$TMP_DIR/$_name" "$_name" "$_want" "$(asset_api_url "$TMP_DIR/release.json" "$_name")" || { err "Download failed: $_name" return 1 } @@ -1158,7 +1175,8 @@ download_awg() { _f="$TMP_DIR/$_p$_pf" # Verified when GitHub publishes the file's sha256 (then mirrors may serve it). _want="$(asset_digest "$TMP_DIR/awg-$_s.json" "$_p$_pf")" - if download "https://github.com/$_repo/releases/download/v$OWRT_RELEASE/$_p$_pf" "$_f" "$_p ($_repo)" "$_want" && + if download "https://github.com/$_repo/releases/download/v$OWRT_RELEASE/$_p$_pf" "$_f" "$_p ($_repo)" "$_want" \ + "$(asset_api_url "$TMP_DIR/awg-$_s.json" "$_p$_pf")" && verify_archive "$_f"; then _files="$_files $_f" else diff --git a/package/luci-app-dawnline/Makefile b/package/luci-app-dawnline/Makefile index cbc4cca..3ce3d34 100644 --- a/package/luci-app-dawnline/Makefile +++ b/package/luci-app-dawnline/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=luci-app-dawnline -PKG_VERSION:=0.16.0 +PKG_VERSION:=0.16.1 PKG_RELEASE:=1 PKG_LICENSE:=MIT # po//dawnline.po becomes luci-i18n-dawnline- (luci.mk), released diff --git a/package/luci-app-dawnline/po/ru/dawnline.po b/package/luci-app-dawnline/po/ru/dawnline.po index b58dd51..d80a34c 100644 --- a/package/luci-app-dawnline/po/ru/dawnline.po +++ b/package/luci-app-dawnline/po/ru/dawnline.po @@ -4099,6 +4099,10 @@ msgstr "Метод не поддерживается." msgid "Missing: %s." msgstr "Не хватает: %s." +#: web/src/shared/format.ts +msgid "Mobile" +msgstr "Мобильный" + #: web/src/admin/pages/person/limits.ts msgid "Monthly" msgstr "В месяц" diff --git a/package/luci-app-dawnline/po/templates/dawnline.pot b/package/luci-app-dawnline/po/templates/dawnline.pot index 739ea05..b8471ae 100644 --- a/package/luci-app-dawnline/po/templates/dawnline.pot +++ b/package/luci-app-dawnline/po/templates/dawnline.pot @@ -4043,6 +4043,10 @@ msgstr "" msgid "Missing: %s." msgstr "" +#: web/src/shared/format.ts +msgid "Mobile" +msgstr "" + #: web/src/admin/pages/person/limits.ts msgid "Monthly" msgstr "" diff --git a/package/luci-app-dawnline/root/usr/libexec/dawnline/updater b/package/luci-app-dawnline/root/usr/libexec/dawnline/updater index b44c862..b762a3a 100755 --- a/package/luci-app-dawnline/root/usr/libexec/dawnline/updater +++ b/package/luci-app-dawnline/root/usr/libexec/dawnline/updater @@ -360,7 +360,7 @@ let LOCK = null; for (let i = 0; i < 10 && !LOCK; i++) { LOCK = upd.try_lock(); if (!LOCK) - system('usleep 100000 2>/dev/null || sleep 1'); + sleep(100); } if (!LOCK) exit(0); diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/github.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/github.uc index b797219..52dc3e2 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/github.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/github.uc @@ -79,14 +79,17 @@ function cached(key, ttl, fn) { /* ---------- releases ---------- */ function slim(x) { - let digests = {}; - for (let a in x.assets ?? []) + let digests = {}, ids = {}; + for (let a in x.assets ?? []) { if (match(a.digest ?? '', /^sha256:[0-9a-f]{64}$/)) digests[a.name] = substr(a.digest, 7); + if (type(a.id) == 'int' && a.id > 0) + ids[a.name] = a.id; + } return { tag: x.tag_name, name: x.name ?? x.tag_name, published: x.published_at ?? null, prerelease: !!x.prerelease, url: x.html_url ?? null, body: substr(x.body ?? '', 0, 12000), - assets: map(x.assets ?? [], (a) => a.name), digests, + assets: map(x.assets ?? [], (a) => a.name), digests, ids, }; } @@ -194,6 +197,25 @@ export function sha256(path) { return match(run(`sha256sum ${q(path)}`).out, /^([0-9a-f]{64})/)?.[1]; }; +/** A release file's URL on github.com in parts: { repo, tag, name }, or null for any other URL. */ +export function release_file(url) { + let m = match(url ?? '', /^https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\/releases\/download\/([^\/?#]+)\/([^\/?#]+)$/); + return m ? { repo: m[1], tag: m[2], name: m[3] } : null; +}; + +/** The same file through the API (api.github.com), when its release is known; else null. */ +function asset_api_url(url) { + let f = release_file(url); + if (!f) + return null; + let id = null; + try { + id = release(f.repo, f.tag)?.ids?.[f.name]; + } + catch (e) {} + return id ? `https://api.github.com/repos/${f.repo}/releases/assets/${id}` : null; +} + /** * Download to a file. o: { sha256, mirrors, log }. Direct first; mirrors * only when the sha256 is known (from the API) and mirrors are allowed. A @@ -203,24 +225,32 @@ export function sha256(path) { export function download(url, dest, o) { o ??= {}; let want = o.sha256 ? lc(o.sha256) : null; - let tries = [ url ]; + let curl = have_curl(); + let tries = []; + // Without curl a release file comes through the API first: github.com's redirect to it carries a + // Content-Security-Policy header that can outgrow uclient-fetch's 4 KB line buffer, and it stalls there. + let api = curl ? null : asset_api_url(url); + if (api) + push(tries, { u: api, accept: 'application/octet-stream' }); + push(tries, { u: url }); + let direct = length(tries); if (want && o.mirrors && match(url, /^https:\/\/(github\.com|raw\.githubusercontent\.com)\//)) for (let m in MIRRORS) - push(tries, m + url); - for (let i, u in tries) { - let host = replace(u, /^https:\/\/([^\/]+)\/.*$/, '$1'); - if (i > 0) + push(tries, { u: m + url, mirror: true }); + for (let i, t in tries) { + let host = replace(t.u, /^https:\/\/([^\/]+)\/.*$/, '$1'); + if (t.mirror) o.log?.(sprintf(_('Trying mirror %s'), host)); - let r = have_curl() - ? run(`curl -fsSL --connect-timeout 15 --max-time 300 --retry 1 -o ${q(dest)} ${q(u)}`) - : run(`uclient-fetch -q -T 30 -O ${q(dest)} ${q(u)}`); + let r = curl + ? run(`curl -fsSL --connect-timeout 15 --max-time 300 --retry 1 -o ${q(dest)} ${q(t.u)}`) + : run(`uclient-fetch -q -T 30 ${t.accept ? `--header=${q('Accept: ' + t.accept)} ` : ''}-O ${q(dest)} ${q(t.u)}`); if (r.code == 0 && (stat(dest)?.size ?? 0) > 0) { if (!want || sha256(dest) == want) - return u; + return t.u; o.log?.(sprintf(_("Warning: %s sent a file that does not match GitHub's sha256, discarded"), host)); } system(`rm -f ${q(dest)}`); - if (i == 0 && length(tries) > 1) + if (i == direct - 1 && length(tries) > direct) o.log?.(_('GitHub did not answer, trying mirrors')); } return null; diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/rpc/updates.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/rpc/updates.uc index bcd3ba8..23ee438 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/rpc/updates.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/rpc/updates.uc @@ -80,7 +80,7 @@ function releases(a) { let page = +(a.page ?? 1); if (page != int(page) || page < 1 || page > 50) die(_('Unknown page.')); - return { releases: map(gh.releases(REPO, page), (x) => ({ ...x, body: null, digests: null })), page }; + return { releases: map(gh.releases(REPO, page), (x) => ({ ...x, body: null, digests: null, ids: null })), page }; } /** Release notes for What's new: Dawnline by tag, or an AmneziaWG build's release. */ @@ -94,7 +94,7 @@ function notes(a) { r = gh.release(rp, `v${tag}`); if (!r) die(sprintf(_('No release %s in %s.'), tag, rp)); - return { ...r, digests: null, repo: rp }; + return { ...r, digests: null, ids: null, repo: rp }; } /** What changed between the installed Dawnline and a release (points for the Components tab). */ @@ -111,8 +111,10 @@ function changes(a) { } /** - * Start an update job. Returns the new job once its worker has written it, - * else null (the caller follows updates_status); never an older job. + * Start an update job. Returns the new job once its worker has written it + * (a quick check may be over by then), else null (the caller follows + * updates_status); never an older job. Waits two seconds at most: LuCI gives + * up on a call after twenty. */ function run(a) { let what = a.what ?? 'check'; @@ -126,16 +128,17 @@ function run(a) { if (upd.busy()) die(_('Another component action is already running')); ensure_dir(RUN_DIR); - let t0 = time(); + let t0 = time(), prev = read_json(upd.JOB_FILE, null)?.id; let args = substr(what, 0, 5) == 'check' ? `check ${substr(what, 6)}` : `install ${what}`; // The job speaks the admin's language (use() let only a plain language code through). let lang = i18n.current(); system(`${lang ? `DAWNLINE_LANG=${lang} ` : ''}start-stop-daemon -S -b -x /usr/libexec/dawnline/updater -- ${args}`); for (let i = 0; i < 20; i++) { let j = read_json(upd.JOB_FILE, null); - if (j?.state == 'running' && j.started >= t0) + if (j && j.id != prev && j.started >= t0) return { started: true, job: j }; - system('usleep 100000 2>/dev/null || sleep 1'); + // ucode's own sleep: OpenWrt's BusyBox has no usleep. + sleep(100); } return { started: true, job: null }; } diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/upd_app.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/upd_app.uc index 95f5846..350d489 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/upd_app.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/upd_app.uc @@ -30,7 +30,7 @@ export function check(ctx) { /** * The sha256 a release publishes for a file: GitHub's own (API), else the - * release's sha256sums.txt fetched straight from github.com (never a mirror). + * release's sha256sums.txt fetched straight from GitHub (never a mirror). */ function want_sha(ctx, tag, asset, dir) { let want = gh.asset_sha256(REPO, tag, asset); diff --git a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc index f764ab8..15fd246 100644 --- a/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc +++ b/package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc @@ -5,7 +5,7 @@ import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs'; import { _ } from 'dawnline.i18n'; -export const VERSION = '0.16.0'; +export const VERSION = '0.16.1'; /** GitHub repository with Dawnline releases (updates and the installer). */ export const REPO = 'tickcount/luci-app-dawnline'; export const RUN_DIR = '/var/run/dawnline'; diff --git a/tests/ucode/github.test.uc b/tests/ucode/github.test.uc new file mode 100644 index 0000000..eacf87b --- /dev/null +++ b/tests/ucode/github.test.uc @@ -0,0 +1,25 @@ +// dawnline.github: which URLs are a release's files (those can come through +// the API instead of github.com's redirect) and the points of a commit message. +import * as gh from 'dawnline.github'; + +test('release_file', () => { + eq(gh.release_file('https://github.com/tickcount/luci-app-dawnline/releases/download/v0.16.0/sha256sums.txt'), + { repo: 'tickcount/luci-app-dawnline', tag: 'v0.16.0', name: 'sha256sums.txt' }, 'a Dawnline release file'); + eq(gh.release_file('https://github.com/Slava-Shchipunov/awg-openwrt/releases/download/v24.10.4/kmod-amneziawg_v24.10.4_aarch64_cortex-a53_mediatek_filogic.ipk'), + { repo: 'Slava-Shchipunov/awg-openwrt', tag: 'v24.10.4', name: 'kmod-amneziawg_v24.10.4_aarch64_cortex-a53_mediatek_filogic.ipk' }, + 'an AmneziaWG build'); + eq(gh.release_file('https://ghfast.top/https://github.com/tickcount/luci-app-dawnline/releases/download/v0.16.0/a.ipk'), null, 'a mirror'); + eq(gh.release_file('https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh'), null, 'not a release file'); + eq(gh.release_file('https://github.com/tickcount/luci-app-dawnline/releases/latest'), null, 'the latest redirect'); + eq(gh.release_file('https://github.com/a/b/releases/download/v1/x.ipk?raw=1'), null, 'a query'); + eq(gh.release_file('https://github.com/a/b/releases/download/v1/sub/x.ipk'), null, 'a deeper path'); + eq(gh.release_file(null), null, 'nothing'); +}); + +test('points', () => { + eq(gh.points('Dawnline 0.16.0\n\n- Check no longer times out\n- Downloads without curl\n go through the API'), + [ 'Check no longer times out', 'Downloads without curl go through the API' ], 'a release commit, a wrapped point'); + eq(gh.points('Fix the portal'), [ 'Fix the portal' ], 'another commit: its title'); + eq(gh.points('Dawnline 0.16.0'), [], 'a bare release title'); + eq(gh.points('Merge branch x\n\n- y'), [], 'a merge'); +}); diff --git a/web/package-lock.json b/web/package-lock.json index 582555b..b06c5b9 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -1,12 +1,12 @@ { "name": "dawnline-web", - "version": "0.16.0", + "version": "0.16.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "dawnline-web", - "version": "0.16.0", + "version": "0.16.1", "license": "MIT", "dependencies": { "qrcode-generator": "2.0.4" diff --git a/web/package.json b/web/package.json index d34f4db..bc2c67e 100644 --- a/web/package.json +++ b/web/package.json @@ -1,6 +1,6 @@ { "name": "dawnline-web", - "version": "0.16.0", + "version": "0.16.1", "private": true, "license": "MIT", "type": "module", diff --git a/web/src/admin/device.ts b/web/src/admin/device.ts index cd27b09..2f4ffa3 100644 --- a/web/src/admin/device.ts +++ b/web/src/admin/device.ts @@ -17,7 +17,20 @@ import { isOn, sw } from './ui/controls'; import { qrCode } from './ui/qr'; import { deviceNameHelp, type Help } from './netcheck'; import { deviceStatus } from './ui/badges'; -import { ago, bytes, defaultName, fitName, homeOf, isoDay, kindLabel, KINDS, netsLabel, todayOf } from '../shared/format'; +import { + ago, + bytes, + defaultName, + fitName, + homeOf, + isoDay, + kindLabel, + netsLabel, + pickLabel, + PICKS, + todayOf, + type KindPick, +} from '../shared/format'; import { guardDeviceName } from '../shared/name-guard'; import { fill, fmt, locale, num } from '../shared/i18n'; import { icon } from '../shared/icons'; @@ -610,44 +623,46 @@ export function addDevice(person: Person, o: DeviceOpts) { while (taken.some((t) => t.toLowerCase() === name.toLowerCase())) name = fitName(base, ` ${++n}`); return name; }; - let kind: Kind = 'phone'; + // Mobile from here is a phone: the admin is never on the device itself (see KindPick). + const kindOf = (p: KindPick): Kind => (p === 'mobile' ? 'phone' : p); + let pick: KindPick = 'mobile'; const nameInput = E('input', { type: 'text', class: 'cbi-input-text', - placeholder: suggest(kind), + placeholder: suggest(kindOf(pick)), }) as HTMLInputElement; const said = nameGuard(nameInput, () => nameRow.refresh()); const nameRow = liveField(_('Name'), nameInput, () => said( nameInput.value.trim() ? deviceNameHelp(nameInput.value, taken, _('Shown in the portal and in the app.')) - : { text: fmt(_('Named after its type: %s.'), suggest(kind)), bad: false }, + : { text: fmt(_('Named after its type: %s.'), suggest(kindOf(pick))), bad: false }, ), ); const kinds = E( 'div', { class: 'dl-kinds', role: 'radiogroup', 'aria-label': _('Type') }, - KINDS.map((k) => + PICKS.map((k) => E( 'button', { type: 'button', role: 'radio', - class: k === kind ? 'on' : '', - 'aria-checked': k === kind ? 'true' : 'false', + class: k === pick ? 'on' : '', + 'aria-checked': k === pick ? 'true' : 'false', 'data-kind': k, click: () => { - kind = k; + pick = k; kinds.querySelectorAll('button').forEach((c) => { const on = (c as HTMLElement).dataset.kind === k; c.classList.toggle('on', on); c.setAttribute('aria-checked', on ? 'true' : 'false'); }); - nameInput.placeholder = suggest(k); + nameInput.placeholder = suggest(kindOf(k)); nameRow.refresh(); }, }, - [icon(k), E('span', {}, kindLabel(k))], + [icon(k), E('span', {}, pickLabel(k))], ), ), ); @@ -704,8 +719,8 @@ export function addDevice(person: Person, o: DeviceOpts) { notify(h.text, 'warning'); return; } - const name = nameInput.value.trim() || suggest(kind); - const r = await attempt(() => api.deviceCreate(person.id, name, kind, isOn(bypass))); + const name = nameInput.value.trim() || suggest(kindOf(pick)); + const r = await attempt(() => api.deviceCreate(person.id, name, kindOf(pick), isOn(bypass))); if (!r) return; closeModal(); notify(fmt(_('%s added'), name), 'success', 3000); diff --git a/web/src/admin/styles/overview.css b/web/src/admin/styles/overview.css index cdb7f11..d6cbe75 100644 --- a/web/src/admin/styles/overview.css +++ b/web/src/admin/styles/overview.css @@ -180,7 +180,7 @@ .dl-fld { display: flex; flex-direction: column; gap: 8px; margin: 0 0 16px; } .dl-fld-label { font-size: 13px; font-weight: 500; color: var(--dl-fg); } .dl-fld .cbi-input-text { width: 100%; box-sizing: border-box; } -.dl-kinds { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; } +.dl-kinds { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; } .dl-kinds button { all: unset; box-sizing: border-box; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; height: 68px; border-radius: 10px; border: 1px solid var(--dl-line); color: var(--dl-fg-muted); font-size: 12px; font-weight: 500; text-align: center; min-width: 0; } .dl-kinds button .dl-icon { width: 20px; height: 20px; } diff --git a/web/src/admin/updater/operation.ts b/web/src/admin/updater/operation.ts index 6760018..7446f3e 100644 --- a/web/src/admin/updater/operation.ts +++ b/web/src/admin/updater/operation.ts @@ -93,7 +93,8 @@ export async function runOperation(what: string | null, o: OpOptions, follow?: U prevId = before?.job?.id; since = before?.now ?? 0; const r = await api.updatesRun(what!); - if (r.job && r.job.state === 'running' && r.job.id !== prevId) { + // A quick check may already be over: still this click's job (updates_run never returns an older one). + if (r.job && r.job.id !== prevId) { job = r.job; known(r.job); } diff --git a/web/src/portal/flows/add.ts b/web/src/portal/flows/add.ts index b901a2e..4272af2 100644 --- a/web/src/portal/flows/add.ts +++ b/web/src/portal/flows/add.ts @@ -7,7 +7,7 @@ // to add it there. import type { ClientConfig, Kind } from '../../shared/types'; -import { defaultName, fitName, kindLabel, netsLabel } from '../../shared/format'; +import { defaultName, fitName, netsLabel, pickLabel, pickOf, PICKS, type KindPick } from '../../shared/format'; import { guardDeviceName } from '../../shared/name-guard'; import { shake } from '../../shared/shake'; import { fmt, num, plural } from '../../shared/i18n'; @@ -77,15 +77,8 @@ function guessName(kind: Kind): string { return defaultName(kind); } -/* - * Phones and tablets are one choice. The device is still kept as a phone or a - * tablet (the lines say "the tablet"): on the device itself the browser tells - * which; picked from elsewhere it is a phone. - */ -type Pick = 'computer' | 'mobile' | 'router' | 'other'; -const pickLabel = (p: Pick): string => (p === 'mobile' ? _('Mobile') : kindLabel(p)); -const pickOf = (k: Kind): Pick => (k === 'phone' || k === 'tablet' ? 'mobile' : k); -const kindOf = (p: Pick): Kind => (p !== 'mobile' ? p : guessKind() === 'tablet' ? 'tablet' : 'phone'); +// Mobile is a tablet when this page is open on one (see KindPick). +const kindOf = (p: KindPick): Kind => (p !== 'mobile' ? p : guessKind() === 'tablet' ? 'tablet' : 'phone'); /** * What they chose so far, kept outside the view: Back from "Open on the device" @@ -93,7 +86,7 @@ const kindOf = (p: Pick): Kind => (p !== 'mobile' ? p : guessKind() === 'tablet' * typed (unset while the name is still the guess). */ interface Draft { - pick?: Pick; + pick?: KindPick; bypass: boolean; name?: string; } @@ -142,11 +135,11 @@ function vAdd(draft: Draft): View { const st = state()!; const p = st.person; // The kinds this router takes, as the four choices. - const picks = (['computer', 'mobile', 'router', 'other'] as Pick[]).filter((k) => st.kinds.some((x) => pickOf(x) === k)); + const picks = PICKS.filter((k) => st.kinds.some((x) => pickOf(x) === k)); const guess = picks.includes(pickOf(guessKind())) ? pickOf(guessKind()) : picks[0]; - let pick: Pick = draft.pick && picks.includes(draft.pick) ? draft.pick : guess; + let pick: KindPick = draft.pick && picks.includes(draft.pick) ? draft.pick : guess; const taken = new Set(p.devices.map((d) => d.name.toLowerCase())); - const suggest = (k: Pick) => { + const suggest = (k: KindPick) => { const n = k === pickOf(guessKind()) ? guessName(kindOf(k)) : defaultName(k === 'mobile' ? 'phone' : k); // Stripped of what the router refuses and cut to its limit, the number after it too ("Galaxy S24+ 2"). if (!taken.has(fitName(n).toLowerCase())) return fitName(n); @@ -230,7 +223,7 @@ function vAdd(draft: Draft): View { if (draft.name === undefined && pick === pickOf(guessKind())) input.value = suggest(pick); }); // The picked tile rings itself where it stands (the ring does not travel between tiles). - function choose(k: Pick) { + function choose(k: KindPick) { pick = draft.pick = k; tiles.forEach((t, i) => { const on = picks[i] === k; diff --git a/web/src/portal/i18n/portal.pot b/web/src/portal/i18n/portal.pot index e1688b0..c9cebc2 100644 --- a/web/src/portal/i18n/portal.pot +++ b/web/src/portal/i18n/portal.pot @@ -808,7 +808,7 @@ msgstr "" msgid "Method not allowed." msgstr "" -#: web/src/portal/flows/add.ts +#: web/src/shared/format.ts msgid "Mobile" msgstr "" diff --git a/web/src/portal/i18n/ru.po b/web/src/portal/i18n/ru.po index ceac703..dfb3b1b 100644 --- a/web/src/portal/i18n/ru.po +++ b/web/src/portal/i18n/ru.po @@ -813,7 +813,7 @@ msgstr "Мбит/с" msgid "Method not allowed." msgstr "Метод не поддерживается." -#: web/src/portal/flows/add.ts +#: web/src/shared/format.ts msgid "Mobile" msgstr "Мобильный" diff --git a/web/src/shared/format.ts b/web/src/shared/format.ts index 86ad808..925b7cf 100644 --- a/web/src/shared/format.ts +++ b/web/src/shared/format.ts @@ -222,13 +222,23 @@ export function defaultName(k: Kind): string { return k === 'other' ? _('Device') : kindLabel(k); } -/** In picker order. */ -export const KINDS: Kind[] = ['computer', 'phone', 'tablet', 'router', 'other']; - export function kindLabel(k: Kind): string { return { computer: _('PC'), phone: _('Phone'), tablet: _('Tablet'), router: _('Router'), other: _('Other') }[k]; } +/** + * Adding a device, phones and tablets are one choice. The device is still + * kept as a phone or a tablet (the lines say "the tablet"): on the device + * itself the browser tells which; picked from elsewhere it is a phone. + */ +export type KindPick = 'computer' | 'mobile' | 'router' | 'other'; + +/** In picker order. */ +export const PICKS: KindPick[] = ['computer', 'mobile', 'router', 'other']; + +export const pickLabel = (p: KindPick): string => (p === 'mobile' ? _('Mobile') : kindLabel(p)); +export const pickOf = (k: Kind): KindPick => (k === 'phone' || k === 'tablet' ? 'mobile' : k); + export function stateLabel(s: PersonState): string { return { active: _('Active'), diff --git a/web/src/shared/icons.ts b/web/src/shared/icons.ts index 91e9be2..0f0ebd4 100644 --- a/web/src/shared/icons.ts +++ b/web/src/shared/icons.ts @@ -4,6 +4,9 @@ const P: Record = { phone: '', computer: '', tablet: '', + // Phones and tablets as one choice (KindPick): a phone in front of a tablet, which stops short of it. + mobile: + '', router: '', server: '',