2021-08-12 09:10:44 +02:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
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-09-07 09:50:00 +02:00
|
|
|
import FormFieldDuration from 'component/formFieldDuration';
|
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,
|
2021-11-09 15:43:02 +01:00
|
|
|
offendingCommentId?: string,
|
|
|
|
// --- redux ---
|
2021-08-12 09:10:44 +02:00
|
|
|
activeChannelClaim: ?ChannelClaim,
|
|
|
|
contentClaim: ?Claim,
|
2021-11-24 15:33:34 +01:00
|
|
|
contentClaimIsMine: ?boolean,
|
2021-08-12 09:10:44 +02:00
|
|
|
moderationDelegatorsById: { [string]: { global: boolean, delegators: { name: string, claimId: string } } },
|
2021-11-09 15:43:02 +01:00
|
|
|
doHideModal: () => void,
|
|
|
|
doCommentModBlock: (commenterUri: string, offendingCommentId: ?string, timeoutSec: ?number) => void,
|
|
|
|
doCommentModBlockAsAdmin: (
|
2021-09-11 15:03:57 +02:00
|
|
|
commenterUri: string,
|
2021-11-09 15:43:02 +01:00
|
|
|
offendingCommentId: ?string,
|
|
|
|
blockerId: ?string,
|
|
|
|
timeoutSec: ?number
|
|
|
|
) => void,
|
|
|
|
doCommentModBlockAsModerator: (
|
|
|
|
commenterUri: string,
|
|
|
|
offendingCommentId: ?string,
|
2021-09-11 15:03:57 +02:00
|
|
|
creatorUri: string,
|
2021-11-09 15:43:02 +01:00
|
|
|
blockerId: ?string,
|
2021-09-11 15:03:57 +02:00
|
|
|
timeoutSec: ?number
|
|
|
|
) => void,
|
2021-08-12 09:10:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function ModalBlockChannel(props: Props) {
|
|
|
|
const {
|
|
|
|
commenterUri,
|
2021-11-09 15:43:02 +01:00
|
|
|
offendingCommentId,
|
2021-08-12 09:10:44 +02:00
|
|
|
activeChannelClaim,
|
|
|
|
contentClaim,
|
2021-11-24 15:33:34 +01:00
|
|
|
contentClaimIsMine,
|
2021-08-12 09:10:44 +02:00
|
|
|
moderationDelegatorsById,
|
2021-11-09 15:43:02 +01:00
|
|
|
doHideModal,
|
|
|
|
doCommentModBlock,
|
|
|
|
doCommentModBlockAsAdmin,
|
|
|
|
doCommentModBlockAsModerator,
|
2021-08-12 09:10:44 +02:00
|
|
|
} = 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 [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-11-24 15:33:34 +01:00
|
|
|
const isTimeoutAvail = contentClaimIsMine || 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
|
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
|
|
|
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">
|
2021-10-01 02:09:50 +02:00
|
|
|
{contentChannelClaim
|
|
|
|
? __('Block this channel on behalf of %creator%.', { creator: contentChannelClaim.name })
|
|
|
|
: __('Block this channel on behalf of the creator.')}
|
2021-08-12 09:10:44 +02:00
|
|
|
</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() {
|
|
|
|
return (
|
2021-09-07 09:50:00 +02:00
|
|
|
<FormFieldDuration
|
2021-08-20 03:04:48 +02:00
|
|
|
name="time_out"
|
|
|
|
value={timeoutInput}
|
|
|
|
onChange={(e) => setTimeoutInput(e.target.value)}
|
2021-09-07 09:50:00 +02:00
|
|
|
onResolve={(valueInSeconds) => setTimeoutSec(valueInSeconds)}
|
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:
|
2021-11-09 15:43:02 +01:00
|
|
|
doCommentModBlock(commenterUri, offendingCommentId, duration);
|
2021-08-12 09:10:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TAB.MODERATOR:
|
|
|
|
if (activeChannelClaim && contentChannelClaim) {
|
2021-11-09 15:43:02 +01:00
|
|
|
doCommentModBlockAsModerator(
|
2021-09-11 15:03:57 +02:00
|
|
|
commenterUri,
|
2021-11-09 15:43:02 +01:00
|
|
|
offendingCommentId,
|
2021-09-11 15:03:57 +02:00
|
|
|
contentChannelClaim.permanent_url,
|
|
|
|
activeChannelClaim.claim_id,
|
|
|
|
duration
|
|
|
|
);
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAB.ADMIN:
|
|
|
|
if (activeChannelClaim) {
|
2021-11-09 15:43:02 +01:00
|
|
|
doCommentModBlockAsAdmin(commenterUri, offendingCommentId, activeChannelClaim.claim_id, duration);
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-09 15:43:02 +01:00
|
|
|
doHideModal();
|
2021-08-12 09:10:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// **************************************************************************
|
|
|
|
|
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.
|
2021-11-09 15:43:02 +01:00
|
|
|
doCommentModBlock(commenterUri, offendingCommentId);
|
|
|
|
doHideModal();
|
2021-09-03 10:47:06 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:10:44 +02:00
|
|
|
return (
|
2021-11-09 15:43:02 +01:00
|
|
|
<Modal isOpen type="card" onAborted={doHideModal}>
|
2021-08-12 09:10:44 +02:00
|
|
|
<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">
|
2022-02-26 13:35:04 +01:00
|
|
|
<label>{__('Duration --[period e.g. ban duration]--')}</label>
|
2021-08-12 09:10:44 +02:00
|
|
|
<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} />
|
2021-11-09 15:43:02 +01:00
|
|
|
<Button button="link" label={__('Cancel')} onClick={doHideModal} />
|
2021-08-12 09:10:44 +02:00
|
|
|
{getActiveChannelElem()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|