Skip to content

Commit 2ac288f

Browse files
wesbillmanCarl
andcommitted
Make message action dismissal idempotent
Route popover backdrops, action rows, and quick reactions through a single one-shot gate so repeated callbacks cannot pop an underlying route or submit duplicate reactions. Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz> Signed-off-by: Wes <[email protected]>
1 parent 8f7b30c commit 2ac288f

4 files changed

Lines changed: 135 additions & 9 deletions

File tree

‎mobile/lib/features/channels/message_actions/message_action_popover.dart‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,15 @@ class _MessageActionsPopover extends HookWidget {
522522
final mediaQuery = MediaQuery.of(context);
523523
final selectionStarted = useRef(false);
524524

525-
void selectAction(String actionId) {
525+
void select(Object? result, [VoidCallback? effect]) {
526526
if (selectionStarted.value) return;
527527
selectionStarted.value = true;
528-
Navigator.of(context).pop(actionId);
528+
Navigator.of(context).pop(result);
529+
effect?.call();
529530
}
530531

532+
void selectAction(String actionId) => select(actionId);
533+
531534
return LayoutBuilder(
532535
builder: (context, constraints) {
533536
final safeLeft = mediaQuery.padding.left + Grid.xxs;
@@ -656,8 +659,9 @@ class _MessageActionsPopover extends HookWidget {
656659
),
657660
Positioned.fill(
658661
child: GestureDetector(
662+
key: const ValueKey('message-actions-backdrop'),
659663
behavior: HitTestBehavior.opaque,
660-
onTap: () => Navigator.of(context).pop(),
664+
onTap: () => select(null),
661665
),
662666
),
663667
if (showPreview)
@@ -725,6 +729,7 @@ class _MessageActionsPopover extends HookWidget {
725729
pageContext: pageContext,
726730
pageRef: pageRef,
727731
popResult: _messageActionReactionSelection,
732+
onSelected: (result, effect) => select(result, effect),
728733
),
729734
),
730735
Positioned.fromRect(
@@ -812,6 +817,7 @@ class _MessageReactionTray extends StatelessWidget {
812817
final BuildContext pageContext;
813818
final WidgetRef pageRef;
814819
final Object popResult;
820+
final void Function(Object? result, VoidCallback effect) onSelected;
815821

816822
const _MessageReactionTray({
817823
required this.animation,
@@ -820,6 +826,7 @@ class _MessageReactionTray extends StatelessWidget {
820826
required this.pageContext,
821827
required this.pageRef,
822828
required this.popResult,
829+
required this.onSelected,
823830
});
824831

825832
@override
@@ -833,6 +840,7 @@ class _MessageReactionTray extends StatelessWidget {
833840
pageContext: pageContext,
834841
pageRef: pageRef,
835842
popResult: popResult,
843+
onSelected: onSelected,
836844
);
837845
}
838846
}

‎mobile/lib/features/channels/message_actions/quick_reaction_row.dart‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ class _QuickReactionRow extends ConsumerWidget {
2020

2121
final Object? popResult;
2222

23+
/// Selects exactly one popover result and side effect. Bottom sheets leave
24+
/// this null and retain their existing local dismissal behavior.
25+
final void Function(Object? result, VoidCallback effect)? onSelected;
26+
2327
const _QuickReactionRow({
2428
required this.message,
2529
required this.sheetContext,
2630
required this.pageContext,
2731
required this.pageRef,
2832
this.presentationAnimation,
2933
this.popResult,
34+
this.onSelected,
3035
});
3136

3237
@override
@@ -76,8 +81,15 @@ class _QuickReactionRow extends ConsumerWidget {
7681
child: _QuickReactionCircle(
7782
size: circleSize,
7883
onTap: () {
79-
Navigator.of(sheetContext).pop(popResult);
80-
react(emoji[index]);
84+
void effect() => react(emoji[index]);
85+
86+
final select = onSelected;
87+
if (select != null) {
88+
select(popResult, effect);
89+
} else {
90+
Navigator.of(sheetContext).pop(popResult);
91+
effect();
92+
}
8193
},
8294
child: _QuickReactionGlyph(
8395
value: emoji[index],
@@ -92,8 +104,16 @@ class _QuickReactionRow extends ConsumerWidget {
92104
child: _QuickReactionCircle(
93105
size: circleSize,
94106
onTap: () {
95-
Navigator.of(sheetContext).pop(popResult);
96-
showEmojiPicker(context: pageContext, onSelect: react);
107+
void effect() =>
108+
showEmojiPicker(context: pageContext, onSelect: react);
109+
110+
final select = onSelected;
111+
if (select != null) {
112+
select(popResult, effect);
113+
} else {
114+
Navigator.of(sheetContext).pop(popResult);
115+
effect();
116+
}
97117
},
98118
child: Icon(
99119
LucideIcons.plus,

‎mobile/lib/features/channels/message_actions/reaction_popover.dart‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void _showMessageReactionPopover({
4141
);
4242
}
4343

44-
class _MessageReactionPopover extends StatelessWidget {
44+
class _MessageReactionPopover extends HookWidget {
4545
final Rect anchorRect;
4646
final EdgeInsets spotlightPadding;
4747
final Animation<double> animation;
@@ -61,6 +61,14 @@ class _MessageReactionPopover extends StatelessWidget {
6161
@override
6262
Widget build(BuildContext context) {
6363
final mediaQuery = MediaQuery.of(context);
64+
final selectionStarted = useRef(false);
65+
66+
void select(Object? result, [VoidCallback? effect]) {
67+
if (selectionStarted.value) return;
68+
selectionStarted.value = true;
69+
Navigator.of(context).pop(result);
70+
effect?.call();
71+
}
6472

6573
return LayoutBuilder(
6674
builder: (context, constraints) {
@@ -125,7 +133,7 @@ class _MessageReactionPopover extends StatelessWidget {
125133
Positioned.fill(
126134
child: GestureDetector(
127135
behavior: HitTestBehavior.opaque,
128-
onTap: () => Navigator.of(context).pop(),
136+
onTap: () => select(null),
129137
),
130138
),
131139
Positioned(
@@ -141,6 +149,7 @@ class _MessageReactionPopover extends StatelessWidget {
141149
message: message,
142150
pageContext: pageContext,
143151
pageRef: pageRef,
152+
onSelected: (result, effect) => select(result, effect),
144153
),
145154
),
146155
],
@@ -159,6 +168,7 @@ class _AnimatedReactionTray extends StatelessWidget {
159168
final BuildContext pageContext;
160169
final WidgetRef pageRef;
161170
final Object? popResult;
171+
final void Function(Object? result, VoidCallback effect)? onSelected;
162172

163173
const _AnimatedReactionTray({
164174
required this.trayKey,
@@ -169,6 +179,7 @@ class _AnimatedReactionTray extends StatelessWidget {
169179
required this.pageContext,
170180
required this.pageRef,
171181
this.popResult,
182+
this.onSelected,
172183
});
173184

174185
@override
@@ -187,6 +198,7 @@ class _AnimatedReactionTray extends StatelessWidget {
187198
pageRef: pageRef,
188199
presentationAnimation: animation,
189200
popResult: popResult,
201+
onSelected: onSelected,
190202
),
191203
),
192204
),

‎mobile/test/features/channels/message_actions_test.dart‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:ui' as ui;
22

3+
import 'package:buzz/features/channels/channel_management_provider.dart';
34
import 'package:buzz/features/channels/message_actions.dart';
45
import 'package:buzz/features/channels/message_long_press_region.dart';
56
import 'package:buzz/shared/read_state/read_state_provider.dart';
@@ -219,6 +220,7 @@ Future<_MessageActionsPopoverHarness> _pumpMessageActionsPopover(
219220
FocusNode? composerFocusNode,
220221
bool composerInitiallyFocused = false,
221222
bool launcherOnNestedRoute = false,
223+
ChannelActions Function(Ref ref)? createChannelActions,
222224
Rect anchorRect = const Rect.fromLTWH(32, 260, 300, 72),
223225
}) async {
224226
final sourceHidden = ValueNotifier(false);
@@ -268,6 +270,8 @@ Future<_MessageActionsPopoverHarness> _pumpMessageActionsPopover(
268270
),
269271
),
270272
reminderServiceProvider.overrideWithValue(reminderService),
273+
if (createChannelActions != null)
274+
channelActionsProvider.overrideWith(createChannelActions),
271275
],
272276
child: MaterialApp(
273277
theme: AppTheme.light(),
@@ -324,6 +328,26 @@ Future<void> _dismissMessageActionsPopover(WidgetTester tester) async {
324328
await tester.pumpAndSettle();
325329
}
326330

331+
class _FakeChannelActions extends ChannelActions {
332+
final reactions = <({String eventId, String emoji})>[];
333+
334+
_FakeChannelActions(Ref ref)
335+
: super(
336+
ref: ref,
337+
session: ref.read(relaySessionProvider.notifier),
338+
signedEventRelay: SignedEventRelay(
339+
session: ref.read(relaySessionProvider.notifier),
340+
nsec: null,
341+
),
342+
currentPubkey: 'self',
343+
);
344+
345+
@override
346+
Future<void> addReaction(String eventId, String emoji) async {
347+
reactions.add((eventId: eventId, emoji: emoji));
348+
}
349+
}
350+
327351
void main() {
328352
testWidgets(
329353
'message long press keeps taps and scrolling while repeated holds win',
@@ -889,6 +913,68 @@ void main() {
889913
expect(tester.takeException(), isNull);
890914
});
891915

916+
testWidgets('ignores repeat backdrop taps once dismissal starts', (
917+
tester,
918+
) async {
919+
final prefs = await _mockPrefs();
920+
await _pumpMessageActionsPopover(
921+
tester,
922+
message: _message(),
923+
prefs: prefs,
924+
launcherOnNestedRoute: true,
925+
);
926+
final backdrop = tester.widget<GestureDetector>(
927+
find.byKey(const ValueKey('message-actions-backdrop')),
928+
);
929+
930+
backdrop.onTap!.call();
931+
backdrop.onTap!.call();
932+
await tester.pumpAndSettle();
933+
934+
expect(
935+
find.byKey(const ValueKey('message-actions-underlying-page')),
936+
findsOneWidget,
937+
);
938+
expect(
939+
find.byKey(const ValueKey('message-actions-root-page')),
940+
findsNothing,
941+
);
942+
expect(tester.takeException(), isNull);
943+
});
944+
945+
testWidgets('ignores repeat quick reactions once dismissal starts', (
946+
tester,
947+
) async {
948+
final prefs = await _mockPrefs();
949+
late _FakeChannelActions actions;
950+
await _pumpMessageActionsPopover(
951+
tester,
952+
message: _message(),
953+
prefs: prefs,
954+
launcherOnNestedRoute: true,
955+
createChannelActions: (ref) => actions = _FakeChannelActions(ref),
956+
);
957+
final reaction = find.byKey(const ValueKey('quick-reaction-\u{1F44D}'));
958+
final detector = tester.widget<GestureDetector>(
959+
find.descendant(of: reaction, matching: find.byType(GestureDetector)),
960+
);
961+
962+
detector.onTap!.call();
963+
detector.onTap!.call();
964+
await tester.pumpAndSettle();
965+
966+
expect(
967+
find.byKey(const ValueKey('message-actions-underlying-page')),
968+
findsOneWidget,
969+
);
970+
expect(
971+
find.byKey(const ValueKey('message-actions-root-page')),
972+
findsNothing,
973+
);
974+
expect(actions.reactions, [(eventId: 'msg-1', emoji: '\u{1F44D}')]);
975+
expect(tester.takeException(), isNull);
976+
});
977+
892978
testWidgets('fallback action rows grow with accessibility text', (
893979
tester,
894980
) async {

0 commit comments

Comments
 (0)