From 16aca00ff87053b6f528bcd2b20f305855f8f7b4 Mon Sep 17 00:00:00 2001 From: Scott Halver <76669202+scott-tp@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:53:03 -0500 Subject: [PATCH] Let users rename a device already on the dashboard. The friendly name was fixed when a device was added. Rename updates that existing column, and names stay within 60 characters so the card header does not overflow. --- README.md | 2 +- app/api.py | 72 +++++++++++++++++++ app/static/css/dashboard.css | 37 ++++++++++ app/static/js/components/device-card.js | 94 ++++++++++++++++++++++++- app/static/js/dashboard-app.js | 2 +- tests/test_api_connections.py | 85 ++++++++++++++++++++++ tests/test_remove_device_ui.py | 11 +++ 7 files changed, 300 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 04caea5..53a56df 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A self-hosted dashboard for monitoring and controlling multiple TinyPilot device ## What you get - One web UI that lists your TinyPilot devices, up to four per page, with a live preview of the connected system on each card (cached in the browser only). -- One-click device addition (friendly name + TinyPilot URL + Automation API key), plus Replace API key on an existing card. +- One-click device addition (friendly name + TinyPilot URL + Automation API key), plus rename and Replace API key on an existing card. - Per-device snapshot of TinyPilot status: connected display resolution, streaming mode (H.264 with bitrate, or MJPEG with framerate and quality), firmware version, network info, and more. - Encrypted at-rest storage for Automation API keys. diff --git a/app/api.py b/app/api.py index 2795776..22b6654 100644 --- a/app/api.py +++ b/app/api.py @@ -32,6 +32,8 @@ api_blueprint = Blueprint('api', __name__, url_prefix='/api') +_FRIENDLY_NAME_MAX = 60 + @api_blueprint.get('/version') def get_version(): @@ -125,6 +127,10 @@ def create_device(): return jsonify( {'error': 'friendly_name, base_url, and api_key are required'} ), 400 + if len(friendly_name) > _FRIENDLY_NAME_MAX: + return jsonify( + {'error': 'friendly_name must be 60 characters or fewer'} + ), 400 key_path = Path(current_app.config['SECRET_KEY_PATH']) db = get_db() @@ -174,6 +180,72 @@ def create_device(): ), 201 +@api_blueprint.put('/devices/') +def update_device_friendly_name(device_id: int): + payload = request.get_json(silent=True) or {} + raw_name = payload.get('friendly_name') if isinstance(payload, dict) else None + if not isinstance(raw_name, str) or not raw_name.strip(): + return jsonify({'error': 'friendly_name is required'}), 400 + friendly_name = raw_name.strip() + if len(friendly_name) > _FRIENDLY_NAME_MAX: + return jsonify( + {'error': 'friendly_name must be 60 characters or fewer'} + ), 400 + + db = get_db() + existing = db.execute( + """ + SELECT id + FROM devices + WHERE id = ? + """, + (device_id,), + ).fetchone() + if existing is None: + return jsonify({'error': 'device not found'}), 404 + + db.execute( + """ + UPDATE devices + SET friendly_name = ? + WHERE id = ? + """, + (friendly_name, device_id), + ) + db.commit() + row = db.execute( + """ + SELECT + devices.id, + devices.friendly_name, + devices.base_url, + device_runtime_state.screenshot_refresh_interval_minutes, + CASE + WHEN device_auth.encrypted_automation_token IS NOT NULL THEN 1 + ELSE 0 + END AS api_key_configured + FROM devices + LEFT JOIN device_runtime_state ON device_runtime_state.device_id = devices.id + LEFT JOIN device_auth ON device_auth.device_id = devices.id + WHERE devices.id = ? + """, + (device_id,), + ).fetchone() + return jsonify( + { + 'device': { + 'id': row['id'], + 'friendly_name': row['friendly_name'], + 'base_url': row['base_url'], + 'screenshot_refresh_interval_minutes': ( + row['screenshot_refresh_interval_minutes'] or 0 + ), + 'api_key_configured': bool(row['api_key_configured']), + } + } + ) + + @api_blueprint.delete('/devices/') def delete_device(device_id: int): db = get_db() diff --git a/app/static/css/dashboard.css b/app/static/css/dashboard.css index 30d822b..30b62fc 100644 --- a/app/static/css/dashboard.css +++ b/app/static/css/dashboard.css @@ -113,6 +113,43 @@ summary { .device-card-header .device-card-title { min-width: 0; + flex: 1; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.device-card-title-cluster { + display: flex; + align-items: center; + flex-wrap: wrap; + gap: var(--space-2); + min-width: 0; + flex: 1; +} + +.device-rename-form { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: var(--space-2); + min-width: 0; + flex: 1; +} + +.device-rename-form[hidden] { + display: none; +} + +.device-rename-form input { + min-width: 0; + flex: 1 1 8rem; +} + +.device-card-header .device-rename-error { + flex-basis: 100%; + margin: 0; + color: var(--color-nav-text); } .header-open-webui { diff --git a/app/static/js/components/device-card.js b/app/static/js/components/device-card.js index e7d1749..4dd2b8c 100644 --- a/app/static/js/components/device-card.js +++ b/app/static/js/components/device-card.js @@ -99,6 +99,8 @@ class DeviceCard extends HTMLElement { this._capturedAt = null; this._objectUrl = null; this._previewState = 'loading'; + this._renameSeq = 0; + this._renameSaving = false; } set device(value) { @@ -160,7 +162,16 @@ class DeviceCard extends HTMLElement { this.innerHTML = `
-

${escapeHtml(device.friendly_name)}

+
+

${escapeHtml(device.friendly_name)}

+ + +
${renderHeaderOpenWebUi(device)}
@@ -235,10 +246,91 @@ class DeviceCard extends HTMLElement {
`; this._cacheElements(); + this._bindRename(); this._showLoadingPlaceholder(); void this._primeFromCache(); } + _bindRename() { + const header = this.querySelector('.device-card-header'); + const title = header.querySelector('.device-card-title'); + const start = header.querySelector('[data-rename="start"]'); + const form = header.querySelector('.device-rename-form'); + const input = form.querySelector('input[name="friendly_name"]'); + const error = form.querySelector('.device-rename-error'); + const showError = (text) => { + error.hidden = !text; + error.textContent = text || ''; + }; + const closeEditor = () => { + // Bump the seq so an in-flight save cannot apply after Cancel. + this._renameSeq += 1; + this._renameSaving = false; + form.hidden = true; + title.hidden = false; + start.hidden = false; + showError(''); + }; + start.addEventListener('click', () => { + input.value = this._device.friendly_name; + showError(''); + title.hidden = true; + start.hidden = true; + form.hidden = false; + input.focus(); + }); + form.querySelector('[data-rename="cancel"]').addEventListener('click', closeEditor); + input.addEventListener('keydown', (event) => { + if (event.key === 'Escape') { + event.preventDefault(); + closeEditor(); + } + }); + form.addEventListener('submit', async (event) => { + event.preventDefault(); + if (this._renameSaving) { + return; + } + const typed = input.value.trim(); + if (!typed) { + showError('Enter a name.'); + return; + } + this._renameSaving = true; + const seq = this._renameSeq; + const result = await window.dashboardApi.putJson( + `/api/devices/${this._device.id}`, + { friendly_name: typed }, + ); + if (seq !== this._renameSeq) { + return; + } + this._renameSaving = false; + if (result.error) { + showError(result.error); + return; + } + const saved = result.device && result.device.friendly_name; + if (typeof saved !== 'string' || !saved.trim()) { + showError('Could not rename this device.'); + return; + } + // ponytail: write the name in place. Assigning this.device calls + // _render() and drops the preview. The page list holds this object. + this._device.friendly_name = saved; + title.textContent = saved; + const link = this.querySelector(`#connected-screenshot-link-${this._device.id}`); + const image = this.querySelector(`#connected-screenshot-${this._device.id}`); + if (link) { + link.setAttribute('aria-label', `Control ${saved}`); + } + if (image) { + image.alt = `Latest screenshot for ${saved}`; + } + closeEditor(); + }); + } + _showLoadingPlaceholder() { const { screenshot, placeholder, frame } = this._elements; if (screenshot) { diff --git a/app/static/js/dashboard-app.js b/app/static/js/dashboard-app.js index 352669b..4d0519b 100644 --- a/app/static/js/dashboard-app.js +++ b/app/static/js/dashboard-app.js @@ -55,7 +55,7 @@ class DashboardApp extends HTMLElement {