Break UserInfo:RoomAdminToolsContainer into smaller components

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2019-10-17 19:08:40 +01:00
parent fa8ad70880
commit 2494af37c8

View File

@ -374,24 +374,7 @@ const useRoomPowerLevels = (room) => {
return powerLevels;
};
const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, member, startUpdating, stopUpdating}) => {
let kickButton;
let banButton;
let muteButton;
let redactButton;
const powerLevels = useRoomPowerLevels(room);
const editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
const me = room.getMember(cli.getUserId());
const isMe = me.userId === member.userId;
const canAffectUser = member.powerLevel < me.powerLevel || isMe;
const membership = member.membership;
if (canAffectUser && me.powerLevel >= powerLevels.kick) {
const RoomKickButton = withLegacyMatrixClient(({cli, member, startUpdating, stopUpdating}) => {
const onKick = async () => {
const ConfirmUserActionDialog = sdk.getComponent("dialogs.ConfirmUserActionDialog");
const {finished} = Modal.createTrackedDialog(
@ -400,9 +383,9 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
ConfirmUserActionDialog,
{
member,
action: membership === "invite" ? _t("Disinvite") : _t("Kick"),
title: membership === "invite" ? _t("Disinvite this user?") : _t("Kick this user?"),
askReason: membership === "join",
action: member.membership === "invite" ? _t("Disinvite") : _t("Kick"),
title: member.membership === "invite" ? _t("Disinvite this user?") : _t("Kick this user?"),
askReason: member.membership === "join",
danger: true,
},
);
@ -427,15 +410,13 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
});
};
const kickLabel = membership === "invite" ? _t("Disinvite") : _t("Kick");
kickButton = (
<AccessibleButton className="mx_UserInfo_field"
onClick={onKick}>
const kickLabel = member.membership === "invite" ? _t("Disinvite") : _t("Kick");
return <AccessibleButton className="mx_UserInfo_field" onClick={onKick}>
{ kickLabel }
</AccessibleButton>
);
}
if (me.powerLevel >= powerLevels.redact) {
</AccessibleButton>;
});
const RedactMessagesButton = withLegacyMatrixClient(({cli, member}) => {
const onRedactAllMessages = async () => {
const {roomId, userId} = member;
const room = cli.getRoom(roomId);
@ -503,13 +484,12 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
}
};
redactButton = (
<AccessibleButton className="mx_UserInfo_field" onClick={onRedactAllMessages}>
return <AccessibleButton className="mx_UserInfo_field" onClick={onRedactAllMessages}>
{ _t("Remove recent messages") }
</AccessibleButton>
);
}
if (canAffectUser && me.powerLevel >= powerLevels.ban) {
</AccessibleButton>;
});
const BanToggleButton = withLegacyMatrixClient(({cli, member, startUpdating, stopUpdating}) => {
const onBanOrUnban = async () => {
const ConfirmUserActionDialog = sdk.getComponent("dialogs.ConfirmUserActionDialog");
const {finished} = Modal.createTrackedDialog(
@ -518,10 +498,10 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
ConfirmUserActionDialog,
{
member,
action: membership === 'ban' ? _t("Unban") : _t("Ban"),
title: membership === 'ban' ? _t("Unban this user?") : _t("Ban this user?"),
askReason: membership !== 'ban',
danger: membership !== 'ban',
action: member.membership === 'ban' ? _t("Unban") : _t("Ban"),
title: member.membership === 'ban' ? _t("Unban this user?") : _t("Ban this user?"),
askReason: member.membership !== 'ban',
danger: member.membership !== 'ban',
},
);
@ -530,7 +510,7 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
startUpdating();
let promise;
if (membership === 'ban') {
if (member.membership === 'ban') {
promise = cli.unban(member.roomId, member.userId);
} else {
promise = cli.ban(member.roomId, member.userId, reason || undefined);
@ -552,16 +532,16 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
};
let label = _t("Ban");
if (membership === 'ban') {
if (member.membership === 'ban') {
label = _t("Unban");
}
banButton = (
<AccessibleButton className="mx_UserInfo_field" onClick={onBanOrUnban}>
return <AccessibleButton className="mx_UserInfo_field" onClick={onBanOrUnban}>
{ label }
</AccessibleButton>
);
}
if (canAffectUser && me.powerLevel >= editPowerLevel) {
</AccessibleButton>;
});
const MuteToggleButton = withLegacyMatrixClient(({cli, member, room, powerLevels, startUpdating, stopUpdating}) => {
const isMuted = _isMuted(member, powerLevels);
const onMuteToggle = async () => {
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
@ -613,11 +593,47 @@ const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, me
};
const muteLabel = isMuted ? _t("Unmute") : _t("Mute");
muteButton = (
<AccessibleButton className="mx_UserInfo_field"
onClick={onMuteToggle}>
return <AccessibleButton className="mx_UserInfo_field" onClick={onMuteToggle}>
{ muteLabel }
</AccessibleButton>
</AccessibleButton>;
});
const RoomAdminToolsContainer = withLegacyMatrixClient(({cli, room, children, member, startUpdating, stopUpdating}) => {
let kickButton;
let banButton;
let muteButton;
let redactButton;
const powerLevels = useRoomPowerLevels(room);
const editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
const me = room.getMember(cli.getUserId());
const isMe = me.userId === member.userId;
const canAffectUser = member.powerLevel < me.powerLevel || isMe;
if (canAffectUser && me.powerLevel >= powerLevels.kick) {
kickButton = <RoomKickButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (me.powerLevel >= powerLevels.redact) {
redactButton = (
<RedactMessagesButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />
);
}
if (canAffectUser && me.powerLevel >= powerLevels.ban) {
banButton = <BanToggleButton member={member} startUpdating={startUpdating} stopUpdating={stopUpdating} />;
}
if (canAffectUser && me.powerLevel >= editPowerLevel) {
muteButton = (
<MuteToggleButton
member={member}
room={room}
powerLevels={powerLevels}
startUpdating={startUpdating}
stopUpdating={stopUpdating}
/>
);
}