forked from Nexus/nexus
53 lines
2 KiB
Dart
53 lines
2 KiB
Dart
import "package:flutter_hooks/flutter_hooks.dart";
|
|
import "package:hooks_riverpod/hooks_riverpod.dart";
|
|
import "package:material_adaptive_sheets/material_adaptive_sheets.dart";
|
|
import "package:material_ui/material_ui.dart";
|
|
import "package:nexus/controllers/subprofiles.dart";
|
|
import "package:nexus/controllers/settings.dart";
|
|
import "package:nexus/models/room_subprofiles.dart";
|
|
import "package:nexus/widgets/avatars/avatar_or_hash.dart";
|
|
import "package:nexus/widgets/composer/subprofile_picker.dart";
|
|
|
|
/// Chooses the subprofile messages are sent as in [roomId], if subprofiles are
|
|
/// on. It shows the one the text in [controller] would trigger, if any.
|
|
class const SubprofileButton(
|
|
final String roomId, {
|
|
required final TextEditingController controller,
|
|
super.key,
|
|
}) extends HookConsumerWidget {
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final enabled = ref.watch(
|
|
SettingsController.provider.select(
|
|
(settings) => settings.value?.subprofiles ?? false,
|
|
),
|
|
);
|
|
final subprofiles = ref.watch(SubprofilesController.provider(roomId));
|
|
final RoomSubprofiles(:profiles, :current) = subprofiles;
|
|
final triggered = useListenableSelector(
|
|
controller,
|
|
() => subprofiles.triggeredBy(controller.text),
|
|
);
|
|
if (!enabled || profiles.isEmpty) return const SizedBox.shrink();
|
|
final shown = triggered ?? current;
|
|
|
|
return IconButton(
|
|
tooltip: switch ((triggered, current)) {
|
|
(final triggered?, _) =>
|
|
"Sending this message as ${triggered.displayName ?? triggered.id}",
|
|
(null, final current?) =>
|
|
"Sending as ${current.displayName ?? current.id}",
|
|
(null, null) => "Choose subprofile",
|
|
},
|
|
icon: switch (shown) {
|
|
final shown? => AvatarOrHash(
|
|
shown.avatarUrl,
|
|
shown.displayName ?? shown.id,
|
|
),
|
|
null => Icon(Icons.person),
|
|
},
|
|
onPressed: () =>
|
|
context.showAdaptivePopover((_) => SubprofilePicker(roomId)),
|
|
);
|
|
}
|
|
}
|