Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

8.3 KB
Raw Blame
import 'dart:async';
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/harness_file_store.dart';
import '../../core/local_key_value_store.dart';
import '../../state/app_state.dart';
import 'web_your_computers.dart';

/// One of the account's computers as the connect page last listed it: its
/// name and id, nothing about how to reach it.
@immutable
class SavedComputer {
  const SavedComputer({required this.id, required this.name});

  final String id;
  final String name;

  Map<String, String> toJson() => {'id': id, 'name': name};

  static SavedComputer? fromJson(Object? raw) =>
      raw is Map && raw['id'] is String && raw['name'] is String
      ? SavedComputer(id: raw['id'] as String, name: raw['name'] as String)
      : null;

  @override
  bool operator ==(Object other) =>
      other is SavedComputer && other.id == id && other.name == name;

  @override
  int get hashCode => Object.hash(id, name);

  @override
  String toString() => 'SavedComputer($id, $name)';
}

/// The account's computers kept in this browser, so the connect page can list
/// them before the machine list answers. Kept like a saved Super Tab view
/// (`super_tabs/saved_view.dart`): for the one account using this browser, and
/// forgotten when another signs in or this one signs out.
class SavedComputers {
  SavedComputers(this.storage);

  final LocalKeyValueStore storage;

  String _key(String account) => 'web.computers.$account';

  /// The account whose computers this browser keeps.
  static const _owner = 'web.computers.saved_by';

  Future<void> _tail = Future.value();

  /// One operation at a time here, and across this browser's tabs under its lock.
  Future<T> _serial<T>(Future<T> Function() action) {
    final run = _tail.then(
      (_) => storage.synchronized('web.computers', action),
    );
    _tail = run.then((_) {}, onError: (Object _) {});
    return run;
  }

  /// [account]'s computers, or null: none kept, unreadable, or a storage that
  /// refuses (full, private mode). It never throws.
  Future<List<SavedComputer>?> read(String account) => _serial(() async {
    try {
      if (await storage.read(_owner) != account) return null;
      final raw = await storage.read(_key(account));
      if (raw == null) return null;
      final list = jsonDecode(raw);
      if (list is! List) return null;
      return [for (final row in list) ?SavedComputer.fromJson(row)];
    } catch (_) {
      return null;
    }
  });

  /// Keeps [computers] for [account]. Only for the account using this
  /// browser: a late write after a sign-out or another account's claim keeps
  /// nothing.
  Future<void> write(String account, List<SavedComputer> computers) =>
      _serial(() async {
        try {
          if (await storage.read(_owner) != account) return;
          await storage.write(_key(account), jsonEncode(computers));
        } catch (_) {}
      });

  /// Clear now: [account]'s computers go; the browser stays [account]'s.
  Future<void> clear(String account) => _serial(() async {
    if (await storage.read(_owner) == account) {
      await storage.delete(_key(account));
    }
  });

  /// [account] (null when signed out) is the one using this browser now:
  /// any other's computers, kept before a reload too, are forgotten.
  Future<void> claim(String? account) => _serial(() async {
    try {
      final owner = await storage.read(_owner);
      if (owner == account) return;
      if (owner != null) await storage.delete(_key(owner));
      if (account == null) {
        await storage.delete(_owner);
      } else {
        await storage.write(_owner, account);
      }
    } catch (_) {}
  });
}

/// Keeps the computers the connect page lists in [store] whenever the machine
/// list answers, and offers the kept ones ([shown]) until it does: a
/// returning browser sees its computers at once rather than "Finding your
/// computers…" for as long as the list takes.
class SavedComputersWatch extends ChangeNotifier {
  SavedComputersWatch(this.app, this.store) {
    app.addListener(_changed);
    _changed();
  }

  final AppNotifier app;
  final SavedComputers store;

  String? _account;

  /// Whether the browser was found signed out and the last account's
  /// computers forgotten: a boot into an expired session never had an
  /// account to leave.
  bool _forgotten = false;
  List<SavedComputer>? _kept;
  List<SavedComputer>? _written;
  List<SavedComputer>? _wasShown;

  /// The kept computers while the machine list has not answered; null once
  /// it has, or with none kept.
  List<SavedComputer>? get shown => app.machineInventoryLoaded ? null : _kept;

  void _changed() {
    final signedOut = app.status == AppStatus.unauthenticated;
    // Booting, nobody is known yet and nothing is forgotten; signed out (by
    // hand, or a session that expired, at boot too), the last account's go.
    final account = signedOut ? null : app.currentUser?.id;
    if (account != _account || (signedOut && !_forgotten)) {
      _bind(account);
      // The Super Tab views too, as Clear now: a boot signed out never binds
      // the account they were kept for.
      if (signedOut) {
        unawaited(app.superTabs.clearSavedViews().catchError((Object _) {}));
      }
    }
    // The list answering takes the kept computers off the page.
    if (!identical(shown, _wasShown)) {
      _wasShown = shown;
      notifyListeners();
    }
    if (account == null || !app.machineInventoryLoaded) return;
    final listed = [
      for (final state in WebYourComputers.listed(app))
        SavedComputer(
          id: state.machine.machineId,
          name: state.machine.displayName,
        ),
    ];
    if (listEquals(listed, _written)) return;
    _written = listed;
    unawaited(store.write(account, listed));
  }

  /// Settings ▸ Account's Clear now: the kept computers go. The list on
  /// screen is not kept again until it changes.
  Future<void> clear() async {
    final account = _account;
    _kept = null;
    _changed();
    if (account != null) await store.clear(account);
  }

  void _bind(String? account) {
    _account = account;
    _forgotten = account == null;
    _kept = null;
    _written = null;
    final claimed = store.claim(account);
    if (account == null) return;
    unawaited(
      claimed.then((_) => store.read(account)).then((kept) {
        if (_account != account || kept == null || kept.isEmpty) return;
        _kept = kept;
        _changed();
      }),
    );
  }

  @override
  void dispose() {
    app.removeListener(_changed);
    super.dispose();
  }
}

/// The one [SavedComputersWatch] of [app], kept in this browser's storage and
/// cleared by Clear now ([AppNotifier.browserCopies]). The web workspace asks
/// for it when it is first drawn, so the list is kept whenever it answers, not
/// only once the connect page shows.
SavedComputersWatch savedComputersOf(AppNotifier app) =>
    _watches[app] ??= _watch(app);

SavedComputersWatch _watch(AppNotifier app) {
  final watch = SavedComputersWatch(
    app,
    SavedComputers(app.localStore ?? HarnessFileStore.shared),
  );
  app.browserCopies.add(watch.clear);
  return watch;
}

final _watches = Expando<SavedComputersWatch>('savedComputersOf');

/// Above every screen of the browser build (`web_entry.dart`'s frame): the
/// app's [SavedComputersWatch] from its start, so a browser that opens signed
/// out, or into an expired session, forgets the last account's computers
/// even though no workspace is ever drawn.
class KeptComputers extends StatefulWidget {
  const KeptComputers({super.key, required this.child});

  final Widget child;

  @override
  State<KeptComputers> createState() => _KeptComputersState();
}

class _KeptComputersState extends State<KeptComputers> {
  bool _started = false;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (_started) return;
    _started = true;
    // The app comes from the ProviderScope around HarnessApp; a frame drawn
    // without one (a widget test of the frame alone) keeps nothing.
    final scope = context
        .getElementForInheritedWidgetOfExactType<UncontrolledProviderScope>();
    if (scope == null) return;
    savedComputersOf(
      ProviderScope.containerOf(context, listen: false).read(appStateProvider),
    );
  }

  @override
  Widget build(BuildContext context) => widget.child;
}