feat: live gRPC pages — drop mock data, talk to the real hub

Studio's three MVP pages now pull live data from a running hub
via fai_dart_sdk. Mock fixture removed.

- data/hub.dart: HubService singleton wraps fai_dart_sdk's
  HubClient and exposes UI-friendly types (ModuleSummary,
  AuditEvent, PendingApproval) so pages don't import protobuf.
- pages/modules.dart: FutureBuilder against listModules,
  groups CapabilityEntry rows by module, retry-on-error UI.
- pages/audit.dart: 2s polling Timer, status bar shows
  "live (polling 2s)" or "disconnected — <error>".
- pages/approvals.dart: live listApprovals, Approve sends
  DecideApproval(APPROVE), Reject opens a reason dialog and
  sends DecideApproval(REJECT). Both refresh after success.
- Connection-error states across all pages: cloud_off icon +
  hint to start `fai serve` + Retry button.

When no hub is running the pages render their disconnected
state instead of crashing. When a hub is running, the data is
real — no more mock fixture.

Bumps fai_studio 0.1.0 -> 0.2.0.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
This commit is contained in:
flemming-it 2026-05-05 16:27:54 +02:00
parent 7c11598846
commit 127c497f73
6 changed files with 602 additions and 354 deletions

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import '../data/mock.dart';
import '../data/hub.dart';
class ApprovalsPage extends StatefulWidget {
const ApprovalsPage({super.key});
@ -10,20 +10,121 @@ class ApprovalsPage extends StatefulWidget {
}
class _ApprovalsPageState extends State<ApprovalsPage> {
final _decided = <String>{};
late Future<List<PendingApproval>> _future;
// Hard-coded reviewer for the MVP; Phase 1+ wires this to an
// authenticated session.
final String _reviewer = 'studio-mvp';
@override
void initState() {
super.initState();
_future = HubService.instance.pendingApprovals();
}
void _refresh() => setState(() {
_future = HubService.instance.pendingApprovals();
});
Future<void> _approve(PendingApproval a) async {
try {
await HubService.instance.approve(a.id, _reviewer);
_refresh();
} catch (e) {
_showError('approve failed: $e');
}
}
Future<void> _reject(PendingApproval a) async {
final reason = await _promptReason(context);
if (reason == null || reason.isEmpty) return;
try {
await HubService.instance.reject(a.id, _reviewer, reason);
_refresh();
} catch (e) {
_showError('reject failed: $e');
}
}
void _showError(String msg) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
Future<String?> _promptReason(BuildContext context) async {
final controller = TextEditingController();
return showDialog<String>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Reject approval'),
content: TextField(
controller: controller,
autofocus: true,
decoration: const InputDecoration(
labelText: 'Reason (recorded in audit log)',
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, null),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(ctx, controller.text),
child: const Text('Reject'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final pending =
mockApprovals.where((a) => !_decided.contains(a.id)).toList();
return Scaffold(
appBar: AppBar(
title: const Text('Pending Approvals'),
centerTitle: false,
actions: [
IconButton(
icon: const Icon(Icons.refresh),
tooltip: 'Reload',
onPressed: _refresh,
),
],
),
body: pending.isEmpty
? Center(
body: FutureBuilder<List<PendingApproval>>(
future: _future,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.cloud_off_outlined,
size: 48,
color: theme.colorScheme.error,
),
const SizedBox(height: 16),
Text('Hub unreachable: ${snapshot.error}'),
const SizedBox(height: 16),
FilledButton.tonal(
onPressed: _refresh,
child: const Text('Retry'),
),
],
),
),
);
}
final pending = snapshot.data ?? [];
if (pending.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(32),
child: Column(
@ -32,7 +133,8 @@ class _ApprovalsPageState extends State<ApprovalsPage> {
Icon(
Icons.check_circle_outline,
size: 64,
color: theme.colorScheme.primary.withValues(alpha: 0.5),
color: theme.colorScheme.primary
.withValues(alpha: 0.5),
),
const SizedBox(height: 16),
Text(
@ -44,22 +146,23 @@ class _ApprovalsPageState extends State<ApprovalsPage> {
],
),
),
)
: ListView.separated(
padding: const EdgeInsets.all(24),
itemCount: pending.length,
separatorBuilder: (_, _) => const SizedBox(height: 16),
itemBuilder: (context, i) {
final a = pending[i];
return _ApprovalCard(
approval: a,
onApprove: () =>
setState(() => _decided.add(a.id)),
onReject: () =>
setState(() => _decided.add(a.id)),
);
},
),
);
}
return ListView.separated(
padding: const EdgeInsets.all(24),
itemCount: pending.length,
separatorBuilder: (_, _) => const SizedBox(height: 16),
itemBuilder: (context, i) {
final a = pending[i];
return _ApprovalCard(
approval: a,
onApprove: () => _approve(a),
onReject: () => _reject(a),
);
},
);
},
),
);
}
}