Files
pipelines/app/lib/features/terminal/terminal_machines.dart
T
alan c264f865ff 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>
2026-12-02 15:58:28 +00:00

93 lines
2.4 KiB
Dart

import 'package:flutter/foundation.dart';
import '../../core/api_gateway.dart';
import '../../core/servers/chat/chat_server_pool.dart';
/// One connected machine terminals can be opened on.
@immutable
class TerminalMachine {
const TerminalMachine({
required this.id,
required this.name,
required this.gateway,
});
/// The saved machine's identity (the chat pool's server id).
final String id;
final String name;
/// That machine's own authenticated connection.
final ApiGateway gateway;
}
/// The machines connected now, in the saved order.
typedef TerminalMachines = ValueListenable<List<TerminalMachine>>;
/// The chat pool's connected machines: one terminal surface across every
/// saved machine, each terminal on its own machine's socket.
class PoolTerminalMachines extends ValueNotifier<List<TerminalMachine>> {
PoolTerminalMachines(this.pool) : super(const []) {
pool.addListener(_update);
_update();
}
final ChatServerPool pool;
void _update() {
final next = [
for (final machine in pool.machines)
if (machine.connected && machine.adapter != null)
TerminalMachine(
id: machine.server.id,
name: machine.server.name,
gateway: machine.adapter!,
),
];
if (!_same(next, value)) value = next;
}
@override
void dispose() {
pool.removeListener(_update);
super.dispose();
}
}
/// The app's one server, while its connection is up.
class SingleTerminalMachines extends ValueNotifier<List<TerminalMachine>> {
SingleTerminalMachines({
required this.connection,
required this.machine,
required this.connected,
}) : super(const []) {
connection.addListener(_update);
_update();
}
final Listenable connection;
/// The machine as named now; [connection] also notifies a rename.
final TerminalMachine Function() machine;
final bool Function() connected;
void _update() {
final next = connected() ? [machine()] : const <TerminalMachine>[];
if (!_same(next, value)) value = next;
}
@override
void dispose() {
connection.removeListener(_update);
super.dispose();
}
}
bool _same(List<TerminalMachine> a, List<TerminalMachine> b) =>
a.length == b.length &&
[
for (var i = 0; i < a.length; i++)
a[i].id == b[i].id &&
a[i].name == b[i].name &&
identical(a[i].gateway, b[i].gateway),
].every((same) => same);