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