A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import Accessibility from '@app/features/accessibility/state/Accessibility';
import {showGenericErrorModal} from '@app/features/app/components/alerts/GenericErrorModalCommands';
import * as Modal from '@app/features/app/components/dialogs/Modal';
import {useAnimatedMediaVideoPlayback} from '@app/features/app/hooks/useAnimatedMediaPlayback';
import {useSaveData} from '@app/features/app/hooks/useSaveData';
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import * as GuildCommands from '@app/features/guild/commands/GuildCommands';
import {
CUSTOM_ELLIPSIS_DESCRIPTOR,
ONE_DAY_DURATION_DESCRIPTOR,
ONE_HOUR_DURATION_DESCRIPTOR,
ONE_MONTH_DURATION_DESCRIPTOR,
ONE_WEEK_DURATION_DESCRIPTOR,
SOMETHING_WENT_WRONG_DESCRIPTOR,
TWELVE_HOURS_DURATION_DESCRIPTOR,
} from '@app/features/i18n/utils/CommonMessageDescriptors';
import styles from '@app/features/moderation/components/modals/BanMemberModal.module.css';
import {BAN_DELETE_MESSAGE_OPTIONS} from '@app/features/moderation/constants/BanDeleteMessageOptions';
import {BANNED_MP4, BANNED_POSTER, BANNED_WEBM} from '@app/features/moderation/constants/BanModalMedia';
import {Logger} from '@app/features/platform/utils/AppLogger';
import {Button} from '@app/features/ui/button/Button';
import * as ModalCommands from '@app/features/ui/commands/ModalCommands';
import * as ToastCommands from '@app/features/ui/commands/ToastCommands';
import {Combobox as FormCombobox} from '@app/features/ui/components/form/FormCombobox';
import {Input} from '@app/features/ui/components/form/FormInput';
import {RadioGroup} from '@app/features/ui/radio_group/RadioGroup';
import type {User} from '@app/features/user/models/User';
import * as DisplayNameUtils from '@app/features/user/utils/NicknameUtils';
import {MAX_TEMP_BAN_DURATION_SECONDS, MIN_TEMP_BAN_DURATION_SECONDS} from '@fluxer/constants/src/LimitConstants';
import {msg} from '@lingui/core/macro';
import {Trans, useLingui} from '@lingui/react/macro';
import {observer} from 'mobx-react-lite';
import type React from 'react';
import {useCallback, useRef, useState} from 'react';
const PERMANENT_DESCRIPTOR = msg({
message: 'Permanent',
comment:
'Ban duration option in the destructive ban-member modal meaning the ban never expires. Short standalone label.',
});
const MESSAGE_3_DAYS_DESCRIPTOR = msg({
message: '3 days',
comment: 'Ban duration option in the destructive ban-member modal for a three-day temporary ban.',
});
const MESSAGE_5_DAYS_DESCRIPTOR = msg({
message: '5 days',
comment: 'Ban duration option in the destructive ban-member modal for a five-day temporary ban.',
});
const MESSAGE_2_WEEKS_DESCRIPTOR = msg({
message: '2 weeks',
comment: 'Ban duration option in the destructive ban-member modal for a two-week temporary ban.',
});
const BAN_DESCRIPTOR = msg({
message: 'Ban {tag}',
comment:
'Title of the destructive ban-member modal. {tag} is the target user tag (username#tag). Destructive moderation action; keep tone direct.',
});
const BAN_DURATION_DESCRIPTOR = msg({
message: 'Ban duration',
comment: 'Label above the ban duration dropdown in the destructive ban-member modal.',
});
const HOW_LONG_THIS_USER_SHOULD_BE_BANNED_FOR_DESCRIPTOR = msg({
message: 'How long they stay banned.',
comment: 'Helper text under the ban duration dropdown in the destructive ban-member modal.',
});
const CUSTOM_BAN_DURATION_SECONDS_DESCRIPTOR = msg({
message: 'Custom ban duration (seconds)',
comment:
'Label of the numeric input shown after choosing "Custom..." in the ban duration dropdown. The unit is seconds.',
});
const ANY_VALUE_FROM_TO_SECONDS_DESCRIPTOR = msg({
message: 'Any value from {minTempBanDurationSeconds} to {maxTempBanDurationSeconds} seconds.',
comment:
'Helper text under the custom ban duration input. Both placeholders are integer numbers of seconds bounding the allowed range.',
});
const DELETE_MESSAGE_HISTORY_DESCRIPTOR = msg({
message: 'Delete message history',
comment:
'Section heading and accessible label for the message-history-deletion radio group in the destructive ban-member modal. Destructive option group.',
});
const REASON_OPTIONAL_DESCRIPTOR = msg({
message: 'Reason (optional)',
comment:
'Label of the optional reason input in the destructive ban-member modal. Reason is recorded in the activity log.',
});
const ENTER_A_REASON_FOR_THE_BAN_DESCRIPTOR = msg({
message: 'Enter a reason for the ban',
comment: 'Placeholder in the optional reason input in the destructive ban-member modal.',
});
const logger = new Logger('BanMemberModal');
const BAN_DURATION_CUSTOM_SENTINEL = -1;
interface ComboboxOption {
value: number;
label: string;
}
export const BanMemberModal: React.FC<{guildId: string; targetUser: User}> = observer(({guildId, targetUser}) => {
const {i18n} = useLingui();
const videoRef = useRef<HTMLVideoElement>(null);
const dataSaverOn = useSaveData();
const showArtwork = !RuntimeConfig.isSelfHosted();
const motionArtworkAllowed = showArtwork && !Accessibility.useReducedMotion && !dataSaverOn;
const videoPlaybackAllowed = useAnimatedMediaVideoPlayback(videoRef, {enabled: motionArtworkAllowed});
const [reason, setReason] = useState('');
const [deleteMessageSeconds, setDeleteMessageSeconds] = useState<number>(60 * 60 * 24);
const [banDuration, setBanDuration] = useState<number>(0);
const [isBanDurationCustom, setIsBanDurationCustom] = useState(false);
const [isBanning, setIsBanning] = useState(false);
const targetUserTag = DisplayNameUtils.formatTagForStreamerMode(targetUser.tag);
const getBanDurationOptions = useCallback(
(): ReadonlyArray<ComboboxOption> => [
{value: 0, label: i18n._(PERMANENT_DESCRIPTOR)},
{value: 60 * 60, label: i18n._(ONE_HOUR_DURATION_DESCRIPTOR)},
{value: 60 * 60 * 12, label: i18n._(TWELVE_HOURS_DURATION_DESCRIPTOR)},
{value: 60 * 60 * 24, label: i18n._(ONE_DAY_DURATION_DESCRIPTOR)},
{value: 60 * 60 * 24 * 3, label: i18n._(MESSAGE_3_DAYS_DESCRIPTOR)},
{value: 60 * 60 * 24 * 5, label: i18n._(MESSAGE_5_DAYS_DESCRIPTOR)},
{value: 60 * 60 * 24 * 7, label: i18n._(ONE_WEEK_DURATION_DESCRIPTOR)},
{value: 60 * 60 * 24 * 14, label: i18n._(MESSAGE_2_WEEKS_DESCRIPTOR)},
{value: 60 * 60 * 24 * 30, label: i18n._(ONE_MONTH_DURATION_DESCRIPTOR)},
{value: BAN_DURATION_CUSTOM_SENTINEL, label: i18n._(CUSTOM_ELLIPSIS_DESCRIPTOR)},
],
[i18n],
);
const BAN_DURATION_OPTIONS = getBanDurationOptions();
const handleBan = async () => {
setIsBanning(true);
try {
await GuildCommands.banMember(guildId, targetUser.id, deleteMessageSeconds, reason || undefined, banDuration);
ToastCommands.createToast({
type: 'success',
children: <Trans>Banned {targetUserTag} from the community</Trans>,
});
ModalCommands.pop();
} catch (error) {
logger.error('Failed to ban member:', error);
showGenericErrorModal({
title: () => i18n._(SOMETHING_WENT_WRONG_DESCRIPTOR),
message: <Trans>Failed to ban member. Try again.</Trans>,
dataFlx: 'moderation.ban-member-modal.ban-error-modal',
defer: false,
});
} finally {
setIsBanning(false);
}
};
return (
<Modal.Root size="small" centered data-flx="moderation.ban-member-modal.modal-root">
<Modal.Header
title={i18n._(BAN_DESCRIPTOR, {tag: targetUserTag})}
data-flx="moderation.ban-member-modal.modal-header"
/>
<Modal.Content data-flx="moderation.ban-member-modal.modal-content">
<div className={styles.content} data-flx="moderation.ban-member-modal.content">
{motionArtworkAllowed ? (
<video
ref={videoRef}
autoPlay={videoPlaybackAllowed}
loop
muted
playsInline
poster={BANNED_POSTER}
width={480}
height={240}
className={styles.video}
data-flx="moderation.ban-member-modal.video"
>
<source src={BANNED_WEBM} type="video/webm" data-flx="moderation.ban-member-modal.source.video-webm" />
<source src={BANNED_MP4} type="video/mp4" data-flx="moderation.ban-member-modal.source.video-mp4" />
</video>
) : null}
{!motionArtworkAllowed && showArtwork ? (
<img
src={BANNED_POSTER}
alt=""
width={480}
height={240}
aria-hidden={true}
className={styles.video}
data-flx="moderation.ban-member-modal.video-still"
/>
) : null}
<div data-flx="moderation.ban-member-modal.div">
<FormCombobox<number>
label={i18n._(BAN_DURATION_DESCRIPTOR)}
description={i18n._(HOW_LONG_THIS_USER_SHOULD_BE_BANNED_FOR_DESCRIPTOR)}
value={isBanDurationCustom ? BAN_DURATION_CUSTOM_SENTINEL : banDuration}
onChange={(v) => {
if (v === BAN_DURATION_CUSTOM_SENTINEL) {
setIsBanDurationCustom(true);
if (banDuration === 0) {
setBanDuration(MIN_TEMP_BAN_DURATION_SECONDS);
}
return;
}
setIsBanDurationCustom(false);
setBanDuration(v);
}}
options={BAN_DURATION_OPTIONS}
disabled={isBanning}
data-flx="moderation.ban-member-modal.form-select.set-is-ban-duration-custom"
/>
{isBanDurationCustom && (
<Input
type="number"
label={i18n._(CUSTOM_BAN_DURATION_SECONDS_DESCRIPTOR)}
footer={i18n._(ANY_VALUE_FROM_TO_SECONDS_DESCRIPTOR, {
minTempBanDurationSeconds: MIN_TEMP_BAN_DURATION_SECONDS,
maxTempBanDurationSeconds: MAX_TEMP_BAN_DURATION_SECONDS,
})}
min={MIN_TEMP_BAN_DURATION_SECONDS}
max={MAX_TEMP_BAN_DURATION_SECONDS}
step={1}
value={String(banDuration)}
onChange={(event) => {
const raw = event.target.value;
if (raw === '') return;
const parsed = Number.parseInt(raw, 10);
if (Number.isNaN(parsed)) return;
const clamped = Math.max(
MIN_TEMP_BAN_DURATION_SECONDS,
Math.min(MAX_TEMP_BAN_DURATION_SECONDS, parsed),
);
setBanDuration(clamped);
}}
disabled={isBanning}
data-flx="moderation.ban-member-modal.input.set-ban-duration.number"
/>
)}
</div>
<div data-flx="moderation.ban-member-modal.div--2">
<div className={styles.sectionTitle} data-flx="moderation.ban-member-modal.section-title">
<Trans>Delete message history</Trans>
</div>
<RadioGroup
aria-label={i18n._(DELETE_MESSAGE_HISTORY_DESCRIPTOR)}
options={BAN_DELETE_MESSAGE_OPTIONS.map((option) => ({
value: option.seconds,
name: i18n._(option.label),
}))}
value={deleteMessageSeconds}
onChange={setDeleteMessageSeconds}
disabled={isBanning}
data-flx="moderation.ban-member-modal.radio-group.set-delete-message-seconds"
/>
</div>
<Input
type="text"
label={i18n._(REASON_OPTIONAL_DESCRIPTOR)}
value={reason}
onChange={(e) => setReason(e.target.value)}
placeholder={i18n._(ENTER_A_REASON_FOR_THE_BAN_DESCRIPTOR)}
maxLength={512}
disabled={isBanning}
data-flx="moderation.ban-member-modal.input.set-reason.text"
/>
</div>
</Modal.Content>
<Modal.Footer data-flx="moderation.ban-member-modal.modal-footer">
<Button
variant="secondary"
onClick={() => ModalCommands.pop()}
disabled={isBanning}
data-flx="moderation.ban-member-modal.button.pop"
>
<Trans>Cancel</Trans>
</Button>
<Button
variant="danger"
onClick={handleBan}
disabled={isBanning}
data-flx="moderation.ban-member-modal.button.ban"
>
<Trans>Ban member</Trans>
</Button>
</Modal.Footer>
</Modal.Root>
);
});