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.

11.2 KB
Raw Blame
import 'dart:async';

import 'package:flutter/material.dart';

import '../shared/theme/app_theme.dart' as grid;
import '../widgets/desktop_chrome.dart';
import 'decisions.dart';
import 'model.dart';
import 'private_project_lock.dart';
import 'section_heading.dart';
import '../widgets/laid_out_list.dart';

/// The reader's decisions in the Notifications pane: what is open first, one
/// batch per ask, then the last few answered.
class SuperTabDecisionList extends StatelessWidget {
  const SuperTabDecisionList({super.key, required this.decisions});
  final SuperTabDecisions decisions;

  /// How many answered decisions stay in view.
  static const answeredShown = 5;

  @override
  Widget build(BuildContext context) => ListenableBuilder(
    listenable: decisions,
    builder: (context, _) {
      grid.AppTheme.watch(context);
      final d = decisions;
      return Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          const Padding(
            padding: EdgeInsets.fromLTRB(12, 16, 12, 4),
            child: SuperTabSectionHeading('Decisions', endInset: 0),
          ),
          if (d.error case final error?) _note(error),
          if (d.open.isEmpty) _note('No decisions waiting for you.'),
          ..._batches(d),
          for (final answered in d.answered.take(answeredShown))
            SuperTabDecisionCard(decisions: d, decision: answered),
        ],
      );
    },
  );

  Iterable<Widget> _batches(SuperTabDecisions d) sync* {
    String? batch;
    for (final decision in d.open) {
      if (decision.batchId != batch) {
        batch = decision.batchId;
        final count = d.open.where((o) => o.batchId == batch).length;
        // An old Chat with PM an older server still sends opens the project's
        // chat, and is named so (as notifications.dart does).
        final c = d.controller;
        final hidden = c.snapshot?.conversation(decision.conversationId);
        final legacyChannel = hidden != null && c.hiddenByBuddies(hidden);
        final project = c.snapshot?.projects
            .where((p) => p.id == decision.projectId)
            .firstOrNull;
        final privateProject = legacyChannel && project?.isPrivate == true;
        yield _noteRich(
          TextSpan(
            children: [
              TextSpan(
                text:
                    '${decision.askerName} asked $count '
                    '${count == 1 ? 'question' : 'questions'} · '
                    '${decision.projectName} · ',
              ),
              if (privateProject)
                WidgetSpan(
                  alignment: PlaceholderAlignment.middle,
                  child: PrivateProjectLock(
                    size: 12,
                    color: DesktopChrome.muted,
                  ),
                ),
              TextSpan(
                text: legacyChannel
                    ? '${privateProject ? ' ' : '#'}${decision.projectName}'
                    : decision.conversationTitle,
              ),
            ],
          ),
        );
      }
      yield SuperTabDecisionCard(decisions: d, decision: decision);
    }
  }

  static Widget _note(String text) => _noteRich(TextSpan(text: text));

  static Widget _noteRich(TextSpan text) => Padding(
    padding: const EdgeInsets.fromLTRB(16, 6, 16, 4),
    child: Text.rich(text, style: DesktopChrome.metadata()),
  );
}

/// The open decisions asked in one project, above the reader's Buddy chat. Hide
/// and each card's Not now fold them into the one heading line until the
/// reader shows them again; a decision asked later shows.
class SuperTabDecisionsPanel extends StatelessWidget {
  const SuperTabDecisionsPanel({
    super.key,
    required this.decisions,
    required this.projectId,
    this.edge = false,
  });
  final SuperTabDecisions decisions;
  final String projectId;

  /// Ends with the chrome's rim while it shows, where the chat's list stops
  /// right under it; nothing is drawn while there are no decisions.
  final bool edge;

  @override
  Widget build(BuildContext context) => ListenableBuilder(
    listenable: decisions,
    builder: (context, _) {
      grid.AppTheme.watch(context);
      final open = decisions.openIn(projectId);
      if (open.isEmpty) return const SizedBox.shrink();
      final shown = decisions.shownIn(projectId);
      final folded = shown.isEmpty;
      final panel = ConstrainedBox(
        constraints: const BoxConstraints(maxHeight: 320),
        child: LaidOutListView(
          key: const ValueKey('super-pm-decisions'),
          shrinkWrap: true,
          padding: EdgeInsets.only(bottom: folded ? 0 : 8),
          children: [
            Padding(
              padding: EdgeInsets.fromLTRB(12, 12, 12, folded ? 0 : 4),
              child: SuperTabSectionHeading(
                'Decisions for you (${open.length})',
                endInset: 0,
                trailing: folded
                    ? quietDecisionAction(
                        key: const ValueKey('super-pm-decisions-show'),
                        label: 'Show',
                        onPressed: () => decisions.showAgain(projectId),
                      )
                    : quietDecisionAction(
                        key: const ValueKey('super-pm-decisions-hide'),
                        label: 'Hide',
                        onPressed: () => decisions.setAside(open),
                      ),
              ),
            ),
            for (final decision in shown)
              SuperTabDecisionCard(
                decisions: decisions,
                decision: decision,
                onNotNow: () => decisions.setAside([decision]),
              ),
          ],
        ),
      );
      return edge
          ? DecoratedBox(
              decoration: BoxDecoration(
                border: Border(bottom: BorderSide(color: DesktopChrome.rim)),
              ),
              child: panel,
            )
          : panel;
    },
  );
}

/// A borderless text action, quieter than the options beside it (design
/// system: quiet inline controls).
Widget quietDecisionAction({
  required Key key,
  required String label,
  required VoidCallback? onPressed,
}) => TextButton(
  key: key,
  onPressed: onPressed,
  style: TextButton.styleFrom(
    foregroundColor: DesktopChrome.muted,
    backgroundColor: Colors.transparent,
    side: BorderSide.none,
    minimumSize: const Size(0, 28),
    padding: const EdgeInsets.symmetric(horizontal: 6),
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
    textStyle: DesktopChrome.metadata(),
  ),
  child: Text(label),
);

/// One question: what it unblocks, one button per option, the recommended
/// one marked, and the quiet `Let <coordinator> decide` (only with a
/// recommendation) and Not now ([onNotNow], in the Buddy chat). Answered, it
/// shows the choice, or that the coordinator decided, called what its project
/// calls it.
class SuperTabDecisionCard extends StatelessWidget {
  const SuperTabDecisionCard({
    super.key,
    required this.decisions,
    required this.decision,
    this.onNotNow,
  });
  final SuperTabDecisions decisions;
  final SuperTabDecision decision;
  final VoidCallback? onNotNow;

  String get _coordinator =>
      decisions.controller.snapshot?.coordinatorNameOf(decision.projectId) ??
      kDefaultCoordinatorName;

  @override
  Widget build(BuildContext context) {
    final choice = decision.choice;
    return Container(
      key: ValueKey('super-decision-${decision.id}'),
      margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      padding: const EdgeInsets.fromLTRB(12, 10, 12, 10),
      decoration: BoxDecoration(
        color: DesktopChrome.surface,
        border: Border.all(color: DesktopChrome.rim),
        borderRadius: BorderRadius.circular(DesktopChrome.rowRadius),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(decision.question, style: DesktopChrome.control(medium: true)),
          const SizedBox(height: 2),
          Text(
            'Unblocks: ${decision.unblocks}',
            style: DesktopChrome.metadata(),
          ),
          const SizedBox(height: 8),
          if (choice != null)
            Text(
              '${decision.delegated ? '$_coordinator decided' : 'You chose'}: '
              '${decision.options.elementAtOrNull(choice) ?? ''}',
              style: DesktopChrome.control(color: DesktopChrome.muted),
            )
          else ...[
            Wrap(
              spacing: 8,
              runSpacing: 6,
              children: [
                for (final (index, option) in decision.options.indexed)
                  _option(index, option),
              ],
            ),
            if (decisions.failedToSend(decision)) _failed(),
            if (_later() case final later?) ...[
              const SizedBox(height: 4),
              later,
            ],
          ],
        ],
      ),
    );
  }

  /// Its answer did not reach the backend: said on the card it was sent
  /// from, with Retry, wherever the card is shown.
  Widget _failed() => Padding(
    padding: EdgeInsets.only(top: grid.AppControl.menuGap),
    child: Wrap(
      crossAxisAlignment: WrapCrossAlignment.center,
      spacing: 4,
      children: [
        Text(SuperTabDecisions.sendFailed, style: DesktopChrome.metadata()),
        quietDecisionAction(
          key: ValueKey('super-decision-${decision.id}-retry'),
          label: 'Retry',
          onPressed: decisions.answering.contains(decision.id)
              ? null
              : () => unawaited(decisions.retry(decision)),
        ),
      ],
    ),
  );

  /// `Let <coordinator> decide` · Not now, whichever of them this card offers.
  Widget? _later() {
    final busy = decisions.answering.contains(decision.id);
    final actions = [
      if (decision.canDelegate)
        quietDecisionAction(
          key: ValueKey('super-decision-${decision.id}-delegate'),
          label: 'Let $_coordinator decide',
          onPressed: busy
              ? null
              : () => unawaited(decisions.delegate(decision)),
        ),
      // A send line's own Not now answers it: no second one that only folds it.
      if (onNotNow != null && !decision.send)
        quietDecisionAction(
          key: ValueKey('super-decision-${decision.id}-later'),
          label: 'Not now',
          onPressed: busy ? null : onNotNow,
        ),
    ];
    if (actions.isEmpty) return null;
    return Transform.translate(
      // The actions' text lines up with the question above.
      offset: const Offset(-6, 0),
      child: Wrap(
        crossAxisAlignment: WrapCrossAlignment.center,
        children: [
          for (final (index, action) in actions.indexed) ...[
            if (index > 0) Text('·', style: DesktopChrome.metadata()),
            action,
          ],
        ],
      ),
    );
  }

  Widget _option(int index, String option) {
    final recommended = index == decision.marked;
    final busy = decisions.answering.contains(decision.id);
    final onPressed = busy
        ? null
        : () => unawaited(decisions.answer(decision, index));
    final label = Text(recommended ? '$option · Recommended' : option);
    final key = ValueKey('super-decision-${decision.id}-$index');
    return recommended
        ? FilledButton.tonal(key: key, onPressed: onPressed, child: label)
        : OutlinedButton(key: key, onPressed: onPressed, child: label);
  }
}