2021-08-12 09:10:44 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
2021-08-20 03:04:48 +02:00
|
|
|
import parseDuration from 'parse-duration';
|
2021-08-12 09:10:44 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
|
|
|
import ClaimPreview from 'component/claimPreview';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import { FormField } from 'component/common/form';
|
2021-08-20 03:04:48 +02:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2021-08-12 09:10:44 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
|
|
|
import { Modal } from 'modal/modal';
|
2021-09-08 18:31:45 +02:00
|
|
|
import { getChannelFromClaim } from 'util/claim';
|
2021-08-12 09:10:44 +02:00
|
|
|
|
|
|
|
const TAB = {
|
|
|
|
PERSONAL: 'personal',
|
|
|
|
MODERATOR: 'moderator',
|
|
|
|
ADMIN: 'admin',
|
|
|
|
};
|
|
|
|
|
|
|
|
const BLOCK = {
|
|
|
|
PERMANENT: 'permanent',
|
|
|
|
TIMEOUT: 'timeout',
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
contentUri: string,
|
|
|
|
commenterUri: string,
|
|
|
|
// --- select ---
|
|
|
|
activeChannelClaim: ?ChannelClaim,
|
|
|
|
contentClaim: ?Claim,
|
|
|
|
moderationDelegatorsById: { [string]: { global: boolean, delegators: { name: string, claimId: string } } },
|
|
|
|
// --- perform ---
|
|
|
|
closeModal: () => void,
|
2021-09-11 15:03:57 +02:00
|
|
|
commentModBlock: (commenterUri: string, timeoutSec: ?number) => void,
|
|
|
|
commentModBlockAsAdmin: (commenterUri: string, blockerId: string, timeoutSec: ?number) => void,
|
|
|
|
commentModBlockAsModerator: (
|
|
|
|
commenterUri: string,
|
|
|
|
creatorUri: string,
|
|
|
|
blockerId: string,
|
|
|
|
timeoutSec: ?number
|
|
|
|
) => void,
|
2021-08-12 09:10:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function ModalBlockChannel(props: Props) {
|
|
|
|
const {
|
|
|
|
commenterUri,
|
|
|
|
activeChannelClaim,
|
|
|
|
contentClaim,
|
|
|
|
moderationDelegatorsById,
|
|
|
|
closeModal,
|
|
|
|
commentModBlock,
|
|
|
|
commentModBlockAsAdmin,
|
|
|
|
commentModBlockAsModerator,
|
|
|
|
} = props;
|
|
|
|
|
2021-09-08 18:31:45 +02:00
|
|
|
const contentChannelClaim = getChannelFromClaim(contentClaim);
|
2021-08-12 09:10:44 +02:00
|
|
|
const activeModeratorInfo = activeChannelClaim && moderationDelegatorsById[activeChannelClaim.claim_id];
|
|
|
|
const activeChannelIsAdmin = activeChannelClaim && activeModeratorInfo && activeModeratorInfo.global;
|
|
|
|
const activeChannelIsModerator =
|
|
|
|
activeChannelClaim &&
|
|
|
|
contentChannelClaim &&
|
|
|
|
activeModeratorInfo &&
|
|
|
|
Object.values(activeModeratorInfo.delegators).includes(contentChannelClaim.claim_id);
|
|
|
|
|
|
|
|
const [tab, setTab] = usePersistedState('ModalBlockChannel:tab', TAB.PERSONAL);
|
|
|
|
const [blockType, setBlockType] = usePersistedState('ModalBlockChannel:blockType', BLOCK.PERMANENT);
|
2021-08-20 03:04:48 +02:00
|
|
|
const [timeoutInput, setTimeoutInput] = usePersistedState('ModalBlockChannel:timeoutInput', '10m');
|
|
|
|
const [timeoutInputErr, setTimeoutInputErr] = React.useState('');
|
|
|
|
const [timeoutSec, setTimeoutSec] = React.useState(-1);
|
2021-08-12 09:10:44 +02:00
|
|
|
|
2021-09-03 10:47:06 +02:00
|
|
|
const isPersonalTheOnlyTab = !activeChannelIsModerator && !activeChannelIsAdmin;
|
2021-09-03 17:33:04 +02:00
|
|
|
const isTimeoutAvail = (contentClaim && contentClaim.is_my_output) || activeChannelIsModerator;
|
2021-08-20 03:04:48 +02:00
|
|
|
const blockButtonDisabled = blockType === BLOCK.TIMEOUT && timeoutSec < 1;
|
2021-08-12 09:10:44 +02:00
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
2021-09-03 10:47:06 +02:00
|
|
|
// Check settings validity on mount.
|
2021-08-12 09:10:44 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (
|
2021-09-03 10:47:06 +02:00
|
|
|
isPersonalTheOnlyTab ||
|
2021-08-12 09:10:44 +02:00
|
|
|
(tab === TAB.MODERATOR && !activeChannelIsModerator) ||
|
|
|
|
(tab === TAB.ADMIN && !activeChannelIsAdmin)
|
|
|
|
) {
|
|
|
|
setTab(TAB.PERSONAL);
|
|
|
|
}
|
2021-09-03 10:47:06 +02:00
|
|
|
|
|
|
|
if (!isTimeoutAvail && blockType === BLOCK.TIMEOUT) {
|
|
|
|
setBlockType(BLOCK.PERMANENT);
|
|
|
|
}
|
2021-08-12 09:10:44 +02:00
|
|
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
|
|
|
2021-08-20 03:04:48 +02:00
|
|
|
// 'timeoutInput' to 'timeoutSec' conversion.
|
2021-08-12 09:10:44 +02:00
|
|
|
React.useEffect(() => {
|
2021-09-07 10:03:05 +02:00
|
|
|
const handleInvalidInput = (errMsg: string) => {
|
2021-08-20 03:04:48 +02:00
|
|
|
if (timeoutSec !== -1) {
|
|
|
|
setTimeoutSec(-1);
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
2021-09-07 10:03:05 +02:00
|
|
|
if (timeoutInputErr !== errMsg) {
|
2021-08-20 03:04:48 +02:00
|
|
|
setTimeoutInputErr(errMsg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 10:03:05 +02:00
|
|
|
const handleValidInput = (seconds) => {
|
2021-08-20 03:04:48 +02:00
|
|
|
if (seconds !== timeoutSec) {
|
|
|
|
setTimeoutSec(seconds);
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
2021-08-20 03:04:48 +02:00
|
|
|
if (timeoutInputErr) {
|
|
|
|
setTimeoutInputErr('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 10:03:05 +02:00
|
|
|
if (!timeoutInput) {
|
|
|
|
handleValidInput(-1); // Reset
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-20 03:04:48 +02:00
|
|
|
const ONE_HUNDRED_YEARS_IN_SECONDS = 3154000000;
|
|
|
|
const seconds = parseDuration(timeoutInput, 's');
|
|
|
|
|
|
|
|
if (Number.isInteger(seconds) && seconds > 0) {
|
|
|
|
if (seconds > ONE_HUNDRED_YEARS_IN_SECONDS) {
|
2021-09-07 10:03:05 +02:00
|
|
|
handleInvalidInput(__('Wow, banned for more than 100 years?'));
|
2021-08-20 03:04:48 +02:00
|
|
|
} else {
|
2021-09-07 10:03:05 +02:00
|
|
|
handleValidInput(seconds);
|
2021-08-20 03:04:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-07 10:03:05 +02:00
|
|
|
handleInvalidInput(__('Invalid duration.'));
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
2021-08-20 03:04:48 +02:00
|
|
|
}, [timeoutInput, timeoutInputErr, timeoutSec]);
|
2021-08-12 09:10:44 +02:00
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
|
|
|
function getTabElem(value, label) {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={value}
|
|
|
|
label={__(label)}
|
|
|
|
button="alt"
|
|
|
|
onClick={() => setTab(value)}
|
|
|
|
className={classnames('button-toggle', { 'button-toggle--active': tab === value })}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTabHelperElem(tab) {
|
|
|
|
switch (tab) {
|
|
|
|
case TAB.PERSONAL:
|
|
|
|
return null;
|
|
|
|
case TAB.MODERATOR:
|
|
|
|
return (
|
|
|
|
<p className="help">
|
|
|
|
{__('Block this channel on behalf of %creator%', {
|
|
|
|
creator: contentChannelClaim ? contentChannelClaim.name : __('creator'),
|
|
|
|
})}
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
case TAB.ADMIN:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:47:06 +02:00
|
|
|
function getBlockTypeElem(value, label, disabled = false, disabledLabel = '') {
|
2021-08-12 09:10:44 +02:00
|
|
|
return (
|
|
|
|
<FormField
|
|
|
|
type="radio"
|
|
|
|
name={value}
|
|
|
|
key={value}
|
2021-09-03 10:47:06 +02:00
|
|
|
label={disabled && disabledLabel ? __(disabledLabel) : __(label)}
|
|
|
|
disabled={disabled}
|
2021-08-12 09:10:44 +02:00
|
|
|
checked={blockType === value}
|
|
|
|
onChange={() => setBlockType(value)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTimeoutDurationElem() {
|
2021-08-20 03:04:48 +02:00
|
|
|
const examples = '\n- 30s\n- 10m\n- 1h\n- 2d\n- 3mo\n- 1y';
|
2021-08-12 09:10:44 +02:00
|
|
|
return (
|
|
|
|
<FormField
|
2021-08-20 03:04:48 +02:00
|
|
|
name="time_out"
|
|
|
|
label={
|
|
|
|
<>
|
|
|
|
{__('Duration')}
|
|
|
|
<Icon
|
|
|
|
customTooltipText={__('Enter the timeout duration. Examples: %examples%', { examples })}
|
|
|
|
className="icon--help"
|
|
|
|
icon={ICONS.HELP}
|
|
|
|
tooltip
|
|
|
|
size={16}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
type="text"
|
|
|
|
placeholder="30s, 10m, 1h, 2d, 3mo, 1y"
|
|
|
|
value={timeoutInput}
|
|
|
|
onChange={(e) => setTimeoutInput(e.target.value)}
|
|
|
|
error={timeoutInputErr}
|
2021-08-12 09:10:44 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCommenterPreview(uri) {
|
|
|
|
return (
|
|
|
|
<div className="content__non-clickable">
|
|
|
|
<ClaimPreview uri={uri} hideMenu hideActions type="small" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActiveChannelElem() {
|
|
|
|
return activeChannelClaim ? (
|
|
|
|
<div className="block-modal--active-channel">
|
|
|
|
<ChannelThumbnail xsmall noLazyLoad uri={activeChannelClaim.permanent_url} />
|
|
|
|
<div className="block-modal--active-channel-label">
|
|
|
|
{__('Interacting as %channelName%', { channelName: activeChannelClaim.name })}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleBlock() {
|
2021-08-20 03:04:48 +02:00
|
|
|
const duration = blockType === BLOCK.TIMEOUT && timeoutSec > 0 ? timeoutSec : undefined;
|
2021-08-12 09:10:44 +02:00
|
|
|
|
|
|
|
switch (tab) {
|
|
|
|
case TAB.PERSONAL:
|
|
|
|
commentModBlock(commenterUri, duration);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAB.MODERATOR:
|
|
|
|
if (activeChannelClaim && contentChannelClaim) {
|
2021-09-11 15:03:57 +02:00
|
|
|
commentModBlockAsModerator(
|
|
|
|
commenterUri,
|
|
|
|
contentChannelClaim.permanent_url,
|
|
|
|
activeChannelClaim.claim_id,
|
|
|
|
duration
|
|
|
|
);
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAB.ADMIN:
|
|
|
|
if (activeChannelClaim) {
|
|
|
|
commentModBlockAsAdmin(commenterUri, activeChannelClaim.claim_id, duration);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
closeModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
2021-09-03 10:47:06 +02:00
|
|
|
if (isPersonalTheOnlyTab && !isTimeoutAvail) {
|
|
|
|
// There's only 1 option. Just execute it and don't show the modal.
|
|
|
|
commentModBlock(commenterUri);
|
|
|
|
closeModal();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:10:44 +02:00
|
|
|
return (
|
|
|
|
<Modal isOpen type="card" onAborted={closeModal}>
|
|
|
|
<Card
|
|
|
|
title={__('Block Channel')}
|
|
|
|
subtitle={getCommenterPreview(commenterUri)}
|
|
|
|
actions={
|
|
|
|
<>
|
2021-09-03 10:47:06 +02:00
|
|
|
{!isPersonalTheOnlyTab && (
|
2021-08-12 09:10:44 +02:00
|
|
|
<div className="section__actions">
|
|
|
|
<div className="section">
|
|
|
|
<label>{__('Block list')}</label>
|
|
|
|
<div className="block-modal--values">
|
|
|
|
{getTabElem(TAB.PERSONAL, 'Personal')}
|
|
|
|
{activeChannelIsModerator && getTabElem(TAB.MODERATOR, 'Moderator')}
|
|
|
|
{activeChannelIsAdmin && getTabElem(TAB.ADMIN, 'Global Admin')}
|
|
|
|
{getTabHelperElem(tab)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="section section--vertical-compact">
|
|
|
|
<label>{__('Duration')}</label>
|
|
|
|
<div className="block-modal--values">
|
|
|
|
<fieldset>
|
|
|
|
{getBlockTypeElem(BLOCK.PERMANENT, 'Permanent')}
|
2021-09-03 10:47:06 +02:00
|
|
|
{getBlockTypeElem(
|
|
|
|
BLOCK.TIMEOUT,
|
|
|
|
'Timeout --[time-based ban instead of permanent]--',
|
|
|
|
!isTimeoutAvail,
|
|
|
|
'Timeout (only available on content that you own)'
|
|
|
|
)}
|
2021-08-12 09:10:44 +02:00
|
|
|
</fieldset>
|
|
|
|
{blockType === BLOCK.TIMEOUT && getTimeoutDurationElem()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="block-modal--finalize">
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button button="primary" label={__('Block')} onClick={handleBlock} disabled={blockButtonDisabled} />
|
|
|
|
<Button button="link" label={__('Cancel')} onClick={closeModal} />
|
|
|
|
{getActiveChannelElem()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|