chain-studio/lib/widgets/fai_module_sheet.dart
flemming-it 73e394b39f feat(studio): module uninstall + Doctor daemon control (v0.17.0)
UI parity for Windows operators who never touch a shell:

- Module sheet gains an Uninstall button at the bottom.
  Two-step confirmation, then calls UninstallModule + closes
  the sheet + refreshes the Modules page.

- Doctor page picks up two new sections:
  * Daemon files — log / config / audit DB / modules /
    flows / pid, each with a one-click "Open" button that
    shells out to the OS handler (open / xdg-open /
    explorer).
  * Daemon control — Restart / Stop / Status buttons that
    spawn `fai daemon …`. Captured stdout/stderr renders
    inline so the operator sees what happened.

- Update banner gains an "Apply update" button that spawns
  `fai update apply --channel <c>`. The previous version
  showed the command as text — Windows users had no way to
  execute it.

- Event log panel gains a "Verify now" button that re-runs
  the chain check (via the existing doctor refresh).

New SystemActions helper resolves the `fai` binary via
$FAI_BIN → PATH → `~/.fai/bin/fai` (or the Windows
equivalent), so the buttons work whether the operator
restarted their shell after installing or not.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
2026-05-07 17:46:46 +02:00

313 lines
9.5 KiB
Dart

// FaiModuleSheet — modal bottom-sheet showing detailed module
// info: full capability list, declared permissions, on-disk
// directory. Opened by tapping a module card on the Modules
// page.
import 'package:flutter/material.dart';
import '../data/hub.dart';
import '../theme/theme.dart';
import '../theme/tokens.dart';
import 'fai_pill.dart';
class FaiModuleSheet extends StatefulWidget {
final String moduleName;
const FaiModuleSheet({super.key, required this.moduleName});
/// Convenience launcher used from the Modules list. Resolves
/// to `true` when the operator uninstalled the module — the
/// caller (Modules page) refreshes its list on that signal.
static Future<bool> show(BuildContext context, String name) async {
final r = await showModalBottomSheet<bool>(
context: context,
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
isScrollControlled: true,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(FaiRadius.md),
),
),
builder: (_) => FaiModuleSheet(moduleName: name),
);
return r ?? false;
}
@override
State<FaiModuleSheet> createState() => _FaiModuleSheetState();
}
class _FaiModuleSheetState extends State<FaiModuleSheet> {
late final Future<ModuleDetail> _future;
bool _uninstalling = false;
@override
void initState() {
super.initState();
_future = HubService.instance.moduleInfo(widget.moduleName);
}
Future<void> _confirmAndUninstall(ModuleDetail detail) async {
final ok = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Uninstall module?'),
content: Text(
'Removes ${detail.name} v${detail.version} from this hub. '
'Flows that reference it will fail at the next run. '
'A `module.uninstalled` audit event is recorded.',
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx, false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(ctx, true),
style: FilledButton.styleFrom(
backgroundColor: Theme.of(ctx).colorScheme.error,
foregroundColor: Theme.of(ctx).colorScheme.onError,
),
child: const Text('Uninstall'),
),
],
),
);
if (ok != true || !mounted) return;
setState(() => _uninstalling = true);
try {
final r = await HubService.instance.uninstallModule(detail.name);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Uninstalled ${r.name} v${r.version}.')),
);
Navigator.pop(context, true);
} catch (e) {
if (!mounted) return;
setState(() => _uninstalling = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Uninstall failed: $e')),
);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final maxHeight = MediaQuery.of(context).size.height * 0.7;
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: maxHeight),
child: FutureBuilder<ModuleDetail>(
future: _future,
builder: (context, snapshot) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
_Handle(),
if (snapshot.connectionState == ConnectionState.waiting)
const Padding(
padding: EdgeInsets.all(FaiSpace.xxxl),
child: CircularProgressIndicator(),
)
else if (snapshot.hasError)
Padding(
padding: const EdgeInsets.all(FaiSpace.xl),
child: Text(
'Failed to load: ${snapshot.error}',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.error,
),
),
)
else
Flexible(
child: _Body(
detail: snapshot.data!,
uninstalling: _uninstalling,
onUninstall: () => _confirmAndUninstall(snapshot.data!),
),
),
],
);
},
),
);
}
}
class _Handle extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.symmetric(vertical: FaiSpace.sm),
child: Container(
width: 40,
height: 4,
decoration: BoxDecoration(
color: theme.colorScheme.outlineVariant,
borderRadius: BorderRadius.circular(2),
),
),
);
}
}
class _Body extends StatelessWidget {
final ModuleDetail detail;
final bool uninstalling;
final VoidCallback onUninstall;
const _Body({
required this.detail,
required this.uninstalling,
required this.onUninstall,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ListView(
padding: const EdgeInsets.fromLTRB(
FaiSpace.xxl,
FaiSpace.md,
FaiSpace.xxl,
FaiSpace.xxl,
),
shrinkWrap: true,
children: [
Row(
children: [
Text(
detail.name,
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(width: FaiSpace.md),
FaiPill(
label: 'v${detail.version}',
tone: FaiPillTone.accent,
monospace: true,
),
],
),
const SizedBox(height: FaiSpace.xs),
SelectableText(
detail.directory,
style: FaiTheme.mono(
size: 11,
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: FaiSpace.xl),
_SectionHeader('Capabilities'),
const SizedBox(height: FaiSpace.sm),
Wrap(
spacing: FaiSpace.xs,
runSpacing: FaiSpace.xs,
children: detail.capabilities
.map(
(c) => FaiPill(
label: c,
tone: FaiPillTone.accent,
monospace: true,
),
)
.toList(),
),
const SizedBox(height: FaiSpace.xl),
_SectionHeader('Declared permissions'),
const SizedBox(height: FaiSpace.sm),
if (detail.permissions.isEmpty)
Text(
'(none — pure-computation module)',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
fontStyle: FontStyle.italic,
),
)
else
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: detail.permissions
.map(
(p) => Padding(
padding: const EdgeInsets.symmetric(vertical: 2),
child: Row(
children: [
Icon(
_iconFor(p),
size: 14,
color: theme.colorScheme.onSurfaceVariant,
),
const SizedBox(width: FaiSpace.sm),
SelectableText(
p,
style: FaiTheme.mono(
size: 12,
color: theme.colorScheme.onSurface,
),
),
],
),
),
)
.toList(),
),
const SizedBox(height: FaiSpace.xxl),
Row(
children: [
const Spacer(),
OutlinedButton.icon(
onPressed: uninstalling ? null : onUninstall,
icon: uninstalling
? const SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.delete_outline, size: 16),
label: Text(uninstalling ? 'Uninstalling…' : 'Uninstall'),
style: OutlinedButton.styleFrom(
foregroundColor: theme.colorScheme.error,
side: BorderSide(
color: theme.colorScheme.error.withValues(alpha: 0.5),
),
),
),
],
),
],
);
}
IconData _iconFor(String permission) {
if (permission.startsWith('net:')) return Icons.public;
if (permission.startsWith('fs.read:')) return Icons.folder_open;
if (permission.startsWith('fs.write:')) return Icons.edit_note;
if (permission.startsWith('env:')) return Icons.terminal;
if (permission.startsWith('hub:')) return Icons.shield_outlined;
return Icons.lock_outline;
}
}
class _SectionHeader extends StatelessWidget {
final String text;
const _SectionHeader(this.text);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Text(
text.toUpperCase(),
style: theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
letterSpacing: 0.6,
fontSize: 10,
),
);
}
}