|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_hooks/flutter_hooks.dart'; |
| 3 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 4 | +import 'package:lucide_icons_flutter/lucide_icons.dart'; |
| 5 | + |
| 6 | +import '../../shared/theme/theme.dart'; |
| 7 | +import '../../shared/widgets/app_list.dart'; |
| 8 | +import '../../shared/widgets/app_list_card.dart'; |
| 9 | +import '../../shared/widgets/modal_presentation.dart'; |
| 10 | +import 'channel.dart'; |
| 11 | +import 'channel_identity_names_provider.dart'; |
| 12 | +import 'channel_management_provider.dart'; |
| 13 | +import 'channels_provider.dart'; |
| 14 | + |
| 15 | +/// Channel-scoped management rows embedded below a member's profile actions. |
| 16 | +class ChannelMemberProfileActions extends HookConsumerWidget { |
| 17 | + const ChannelMemberProfileActions({ |
| 18 | + super.key, |
| 19 | + required this.channel, |
| 20 | + required this.pubkey, |
| 21 | + }); |
| 22 | + |
| 23 | + final Channel channel; |
| 24 | + final String pubkey; |
| 25 | + |
| 26 | + @override |
| 27 | + Widget build(BuildContext context, WidgetRef ref) { |
| 28 | + final busy = useState(false); |
| 29 | + final error = useState<String?>(null); |
| 30 | + final actor = ref.watch(currentPubkeyProvider)?.toLowerCase(); |
| 31 | + final roster = ref.watch(channelMembersProvider(channel.id)); |
| 32 | + final channels = ref.watch(channelsProvider).asData?.value; |
| 33 | + final currentChannel = |
| 34 | + channels |
| 35 | + ?.where((candidate) => candidate.id == channel.id) |
| 36 | + .firstOrNull ?? |
| 37 | + channel; |
| 38 | + final target = pubkey.toLowerCase(); |
| 39 | + |
| 40 | + ChannelMember? manageableMember( |
| 41 | + AsyncValue<List<ChannelMember>> snapshot, |
| 42 | + String? currentActor, |
| 43 | + Channel current, |
| 44 | + ) { |
| 45 | + if (current.isDm || |
| 46 | + current.isArchived || |
| 47 | + snapshot.isLoading || |
| 48 | + snapshot.hasError || |
| 49 | + currentActor == null || |
| 50 | + target == currentActor) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + final members = snapshot.asData?.value ?? const <ChannelMember>[]; |
| 54 | + final viewer = members |
| 55 | + .where((m) => m.pubkey.toLowerCase() == currentActor) |
| 56 | + .firstOrNull; |
| 57 | + final member = members |
| 58 | + .where((m) => m.pubkey.toLowerCase() == target) |
| 59 | + .firstOrNull; |
| 60 | + if (viewer?.isElevated != true || member == null || member.isOwner) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + return member; |
| 64 | + } |
| 65 | + |
| 66 | + final member = manageableMember(roster, actor, currentChannel); |
| 67 | + if (member == null) return const SizedBox.shrink(); |
| 68 | + |
| 69 | + bool stillAllowed() { |
| 70 | + final latest = |
| 71 | + ref |
| 72 | + .read(channelsProvider) |
| 73 | + .asData |
| 74 | + ?.value |
| 75 | + .where((candidate) => candidate.id == channel.id) |
| 76 | + .firstOrNull ?? |
| 77 | + currentChannel; |
| 78 | + return ref.read(currentPubkeyProvider)?.toLowerCase() == actor && |
| 79 | + manageableMember( |
| 80 | + ref.read(channelMembersProvider(channel.id)), |
| 81 | + actor, |
| 82 | + latest, |
| 83 | + )?.role == |
| 84 | + member.role; |
| 85 | + } |
| 86 | + |
| 87 | + Future<void> perform({String? role}) async { |
| 88 | + if (busy.value || !stillAllowed()) return; |
| 89 | + busy.value = true; |
| 90 | + error.value = null; |
| 91 | + try { |
| 92 | + if (role == null) { |
| 93 | + final label = ref |
| 94 | + .read(channelIdentityNamesProvider(channel.id)) |
| 95 | + .labelFor(target); |
| 96 | + final confirmed = await showBuzzDialog<bool>( |
| 97 | + context: context, |
| 98 | + builder: (dialogContext) => AlertDialog( |
| 99 | + title: const Text('Remove from channel?'), |
| 100 | + content: Text('Remove $label from ${currentChannel.name}?'), |
| 101 | + actions: [ |
| 102 | + TextButton( |
| 103 | + onPressed: () => Navigator.of(dialogContext).pop(false), |
| 104 | + child: const Text('Cancel'), |
| 105 | + ), |
| 106 | + TextButton( |
| 107 | + onPressed: () => Navigator.of(dialogContext).pop(true), |
| 108 | + child: const Text('Remove'), |
| 109 | + ), |
| 110 | + ], |
| 111 | + ), |
| 112 | + ); |
| 113 | + if (!context.mounted || confirmed != true || !stillAllowed()) return; |
| 114 | + await ref |
| 115 | + .read(channelActionsProvider) |
| 116 | + .removeMember(channelId: channel.id, pubkey: target); |
| 117 | + if (context.mounted) Navigator.of(context).pop(); |
| 118 | + } else { |
| 119 | + await ref |
| 120 | + .read(channelActionsProvider) |
| 121 | + .changeMemberRole( |
| 122 | + channelId: channel.id, |
| 123 | + pubkey: target, |
| 124 | + role: role, |
| 125 | + ); |
| 126 | + } |
| 127 | + } catch (_) { |
| 128 | + if (context.mounted) { |
| 129 | + error.value = role == null |
| 130 | + ? 'Could not remove this person. Please try again.' |
| 131 | + : 'Could not change this role. Please try again.'; |
| 132 | + } |
| 133 | + } finally { |
| 134 | + if (context.mounted) busy.value = false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return Column( |
| 139 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 140 | + children: [ |
| 141 | + AppListCard( |
| 142 | + horizontalPadding: 0, |
| 143 | + children: [ |
| 144 | + if (!member.isBot) ...[ |
| 145 | + if (member.role != 'admin') |
| 146 | + AppListRow( |
| 147 | + icon: LucideIcons.shieldCheck, |
| 148 | + title: 'Make channel admin', |
| 149 | + onTap: busy.value ? null : () => perform(role: 'admin'), |
| 150 | + ), |
| 151 | + if (member.role == 'admin' || member.role == 'guest') |
| 152 | + AppListRow( |
| 153 | + icon: LucideIcons.user, |
| 154 | + title: member.role == 'guest' |
| 155 | + ? 'Make member' |
| 156 | + : 'Change to member', |
| 157 | + onTap: busy.value ? null : () => perform(role: 'member'), |
| 158 | + ), |
| 159 | + ], |
| 160 | + AppListRow( |
| 161 | + icon: LucideIcons.userMinus, |
| 162 | + title: 'Remove from channel', |
| 163 | + titleColor: context.colors.error, |
| 164 | + onTap: busy.value ? null : () => perform(), |
| 165 | + ), |
| 166 | + ], |
| 167 | + ), |
| 168 | + if (busy.value) |
| 169 | + Semantics( |
| 170 | + liveRegion: true, |
| 171 | + label: 'Updating channel member', |
| 172 | + child: const ExcludeSemantics( |
| 173 | + child: Center(child: Text('Updating…')), |
| 174 | + ), |
| 175 | + ), |
| 176 | + if (error.value != null) |
| 177 | + Semantics( |
| 178 | + liveRegion: true, |
| 179 | + label: error.value!, |
| 180 | + child: ExcludeSemantics( |
| 181 | + child: Text( |
| 182 | + error.value!, |
| 183 | + style: context.textTheme.bodyMedium?.copyWith( |
| 184 | + color: context.colors.error, |
| 185 | + ), |
| 186 | + ), |
| 187 | + ), |
| 188 | + ), |
| 189 | + ], |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
0 commit comments