@@ -37,6 +37,10 @@ import 'bestie_agent_action.dart';
3737
3838Route <Channel >? _openAgentsRoute;
3939
40+ /// The open Agents stack's nested navigator, so a link can tell whether
41+ /// anything is open above its list.
42+ GlobalKey <NavigatorState >? _openAgentsNavigator;
43+
4044/// Opens the active community's full-page Agents navigation stack.
4145///
4246/// A [draft] from a `buzz://agent/new` link opens [CreateAgentPage] pre-filled
@@ -47,23 +51,39 @@ Future<void> showAgents(
4751}) async {
4852 final container = ProviderScope .containerOf (context);
4953 final controller = container.read (remoteAgentControllerProvider);
50- unawaited (controller.run (controller.refresh));
5154 final navigator = Navigator .of (context);
5255 // A link replaces an Agents stack that is already open instead of stacking
53- // a second one on top of it.
56+ // a second one on top of it, but only while it shows just its list: a form,
57+ // profile or dialog above it may hold work the user hasn't finished.
5458 final open = _openAgentsRoute;
5559 if (draft != null &&
5660 open != null &&
5761 open.isActive &&
5862 open.navigator == navigator) {
63+ if (! open.isCurrent ||
64+ (_openAgentsNavigator? .currentState? .canPop () ?? false )) {
65+ ScaffoldMessenger .maybeOf (context)? .showFeedbackSnackBar (
66+ const SnackBar (
67+ content: Text (
68+ 'Finish or close what’s open in Agents, then reopen the link.' ,
69+ ),
70+ ),
71+ kind: ToastKind .error,
72+ );
73+ return ;
74+ }
5975 navigator.removeRoute (open);
6076 }
77+ unawaited (controller.run (controller.refresh));
6178 final route = MaterialPageRoute <Channel >(
6279 builder: (_) => _AgentsNavigation (controller: controller, draft: draft),
6380 );
6481 _openAgentsRoute = route;
6582 final channel = await navigator.push (route);
66- if (identical (_openAgentsRoute, route)) _openAgentsRoute = null ;
83+ if (identical (_openAgentsRoute, route)) {
84+ _openAgentsRoute = null ;
85+ _openAgentsNavigator = null ;
86+ }
6787 if (channel != null && context.mounted) {
6888 await Navigator .of (context).push <void >(
6989 MaterialPageRoute (builder: (_) => ChannelDetailPage (channel: channel)),
@@ -84,6 +104,9 @@ class _AgentsNavigation extends HookConsumerWidget {
84104 final navigator = useMemoized (() => GlobalKey <NavigatorState >(), [
85105 communityId,
86106 ]);
107+ if (identical (ModalRoute .of (context), _openAgentsRoute)) {
108+ _openAgentsNavigator = navigator;
109+ }
87110 return NavigatorPopHandler <Channel >(
88111 onPopWithResult: (result) {
89112 if (navigator.currentState? .canPop () ?? false ) {
@@ -288,6 +311,7 @@ class AgentsPage extends HookConsumerWidget {
288311 // Open a link's draft once: sign in first if needed, then wait for the
289312 // registry so an unfinished setup is known before anything is offered.
290313 final draftStep = useRef (draft == null ? _DraftStep .done : _DraftStep .wait);
314+ final draftRefreshFor = useRef <String ?>(null );
291315 // Looked up during build: effects cannot read inherited widgets, and
292316 // snack bars cannot show while a frame is building.
293317 final messenger = ScaffoldMessenger .maybeOf (context);
@@ -333,6 +357,18 @@ class AgentsPage extends HookConsumerWidget {
333357 }
334358 return null ;
335359 }
360+ // The setup check below needs this account's registry, which a
361+ // sign-in started by the link doesn't reload by itself.
362+ final subject = controller.account.account! .subject;
363+ if (! controller.loadedForCurrentAccount) {
364+ if (draftRefreshFor.value != subject) {
365+ draftRefreshFor.value = subject;
366+ unawaited (controller.run (controller.refresh));
367+ } else {
368+ drop ('Couldn’t load your agents. Reopen the link to retry.' );
369+ }
370+ return null ;
371+ }
336372 if (controller.listingFailed) {
337373 drop ('Couldn’t load your agents. Reopen the link to retry.' );
338374 return null ;
@@ -345,7 +381,6 @@ class AgentsPage extends HookConsumerWidget {
345381 }
346382 draftStep.value = _DraftStep .done;
347383 // The checks above hold for this account; the fetch can take minutes.
348- final subject = controller.account.account? .subject;
349384 final client = ref.read (agentCardClientProvider)();
350385 unawaited (() async {
351386 // Hold Create (+) off while the card loads, like an open form does.
@@ -393,6 +428,7 @@ class AgentsPage extends HookConsumerWidget {
393428 flow.stage,
394429 community? .id,
395430 controller.account.account? .subject,
431+ controller.loadedForCurrentAccount,
396432 ],
397433 );
398434
0 commit comments