chain-studio/lib/widgets/chain_sealed_identity_bar.dart
flemming-it afe782e826 fix(workspace): close the sealed-switch privacy race, restore the parked filter
Two findings from the workspace persona review, both rated high:

* Switch race: the hub client re-pointed at a sealed area's hub
  before the workspace announced the sealed context, so the 2 s
  page pollers (runs, audit) could fetch and render that hub's
  data without the sealed marking. Switches now run inside an
  explicit switching window: opened before anything touches the
  connection, announced optimistically in the identity bar
  ("switching…" + spinner, leave button hidden), pollers and the
  shell health tick pause inside it, and pages drop replies whose
  context epoch changed mid-flight. The sealed context is
  announced only after the new hub answered healthy.

* Filter loss: entering a sealed area cleared the shared-hub
  project filter and returning restored only the endpoint. The
  filter is now parked on entry and restored on return; prefs
  keep the parked value throughout, so live state and prefs agree
  after the round trip (and after a mid-session relaunch).

Guard: workspace_switch_race_test pins both invariants
state-matrix-style against scripted hub + sealed-area fakes —
reconnects may only happen inside an open switch window, pollers
must stay silent inside it, and the filter must survive the round
trip. SealedAreaService gained a debugSetInstance seam so the
suite never scans a real ~/.chain.

Signed-off-by: flemming-it <stefan.a.flemming@googlemail.com>
2026-08-27 23:48:03 +02:00

93 lines
3.5 KiB
Dart

// ChainSealedIdentityBar — the identity strip shown under the AppBar
// while Studio is connected to a sealed area.
//
// Painted in the area's accent colour so "which area am I in?" is
// answerable at a glance (one window, one truth). The area colour is
// marking, not theming — Studio's blue stays the app accent
// everywhere else. Offers a one-click way back to the shared hub.
// Renders nothing when Studio is on the shared hub.
import 'package:flutter/material.dart';
import '../data/workspace.dart';
import '../l10n/app_localizations.dart';
import '../theme/tokens.dart';
import 'chain_workspace_switcher.dart' show parseAreaColor;
class ChainSealedIdentityBar extends StatelessWidget {
const ChainSealedIdentityBar({super.key});
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: Workspace.instance,
builder: (context, _) {
final ws = Workspace.instance;
// While a switch is in flight the bar announces it BEFORE the
// client re-points (privacy race guard): entering shows the
// target area, leaving keeps the current area's marking —
// whatever is still on screen is that area's data.
final switching = ws.switching;
final area = switching
? (ws.switchTarget ?? ws.activeSealed)
: ws.activeSealed;
if (area == null) return const SizedBox.shrink();
final theme = Theme.of(context);
final l = AppLocalizations.of(context)!;
final accent = parseAreaColor(area.color) ?? theme.colorScheme.primary;
// A readable foreground on the tinted bar in both themes.
final onAccent =
accent.computeLuminance() > 0.5 ? Colors.black87 : Colors.white;
return Material(
color: accent,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: ChainSpace.lg,
vertical: 6,
),
child: Row(
children: [
Icon(Icons.lock_outline, size: 15, color: onAccent),
const SizedBox(width: ChainSpace.sm),
Text(
area.name,
style: theme.textTheme.labelLarge?.copyWith(
color: onAccent,
fontWeight: FontWeight.w600,
),
),
const SizedBox(width: ChainSpace.md),
Expanded(
child: Text(
switching ? l.sealedSwitchingBar : l.sealedIdentityBar,
style: theme.textTheme.bodySmall?.copyWith(
color: onAccent.withValues(alpha: 0.85),
),
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: ChainSpace.sm),
if (switching)
SizedBox(
width: 14,
height: 14,
child: CircularProgressIndicator(
strokeWidth: 2,
color: onAccent,
),
)
else
TextButton.icon(
onPressed: () => Workspace.instance.switchToShared(),
style: TextButton.styleFrom(foregroundColor: onAccent),
icon: const Icon(Icons.logout, size: 15),
label: Text(l.sealedLeave),
),
],
),
),
);
},
);
}
}