Servers: name a machine after its hostname once it connects

A saved server still named by the app ('My server', 'This server',
'Original legacy server' or its address host/IP) takes the hostname the
machine reports in get_status the first time the app connects; typed
names are kept. New servers (legacy migration, Settings with a blank
name, onboarding) start with the address host. A browser on its page
origin with no saved profile shows the reported hostname too. No server
change: get_status already reports the hostname.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 15:58:28 +00:00
parent 3e817a189b
commit c264f865ff
16 changed files with 654 additions and 24 deletions
@@ -1,8 +1,11 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../core/servers/machine_access.dart';
import '../../core/servers/server_catalog.dart';
import '../../core/servers/server_catalog_store.dart';
import '../../core/servers/server_naming.dart';
/// Saved configurations are committed before switching the app's connection.
/// A failed save leaves the old selection and its live socket unchanged.
@@ -79,6 +82,47 @@ class ServerConnections extends ChangeNotifier {
bool get busy => _busy;
String? get error => _error;
String? _originHostname;
/// The hostname of the page-origin server a browser uses with no saved
/// profile for it; it names that server, which has no catalog entry.
String? get originHostname => _originHostname;
final _learned = <String, String>{};
/// Records the [hostname] the machine [id] reported. A saved machine still
/// named by the app (see [hasPlaceholderName]) is renamed and saved; a name
/// the operator typed is kept. A null [id] is the unsaved page origin.
/// A change already in progress defers the rename until it finishes.
Future<bool> learnHostname(String? id, String hostname) async {
final name = hostname.trim();
if (_disposed || name.isEmpty) return false;
if (id == null) {
if (_originHostname == name) return false;
_originHostname = name;
_notify();
return true;
}
_learned[id] = name;
return _applyLearnedNames();
}
Future<bool> _applyLearnedNames() async {
if (_busy || _disposed || _learned.isEmpty) return false;
final learned = Map.of(_learned);
_learned.clear();
var next = _catalog;
for (final server in _catalog.servers) {
final hostname = learned[server.id];
final renamed = hostname == null
? null
: renamedToHostname(server, hostname);
if (renamed != null) next = next.upsert(renamed);
}
if (identical(next, _catalog)) return false;
return _change(() => next, activate: false);
}
/// Browsers always connect to their page origin, even after Back or a failed
/// cross-origin navigation. Do not label another saved profile as active.
void alignBrowserOrigin(Uri endpoint) {
@@ -181,6 +225,7 @@ class ServerConnections extends ChangeNotifier {
} finally {
_busy = false;
_notify();
if (_learned.isNotEmpty) scheduleMicrotask(_applyLearnedNames);
}
}