// @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'; import usePersistedState from 'effects/use-persisted-state'; import { Modal } from 'modal/modal'; 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, commentModBlock: (string, ?number) => void, commentModBlockAsAdmin: (string, string, ?number) => void, commentModBlockAsModerator: (string, string, string, ?number) => void, }; export default function ModalBlockChannel(props: Props) { const { commenterUri, activeChannelClaim, contentClaim, moderationDelegatorsById, closeModal, commentModBlock, commentModBlockAsAdmin, commentModBlockAsModerator, } = props; const contentChannelClaim = !contentClaim ? null : contentClaim.value_type === 'channel' ? contentClaim : contentClaim.signing_channel && contentClaim.is_channel_signature_valid ? contentClaim.signing_channel : null; 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); const [timeoutHrs, setTimeoutHrs] = usePersistedState('ModalBlockChannel:timeoutHrs', 1); const [timeoutHrsError, setTimeoutHrsError] = React.useState(''); const personalIsTheOnlyTab = !activeChannelIsModerator && !activeChannelIsAdmin; const blockButtonDisabled = blockType === BLOCK.TIMEOUT && (timeoutHrs === 0 || !Number.isInteger(timeoutHrs)); // ************************************************************************** // ************************************************************************** // Check 'tab' validity on mount. React.useEffect(() => { if ( personalIsTheOnlyTab || (tab === TAB.MODERATOR && !activeChannelIsModerator) || (tab === TAB.ADMIN && !activeChannelIsAdmin) ) { setTab(TAB.PERSONAL); } }, []); // eslint-disable-line react-hooks/exhaustive-deps // 'timeoutHrs' sanity check. React.useEffect(() => { if (Number.isInteger(timeoutHrs) && timeoutHrs > 0) { if (timeoutHrsError) { setTimeoutHrsError(''); } } else { if (!timeoutHrsError) { setTimeoutHrsError('Invalid duration.'); } } }, [timeoutHrs, timeoutHrsError]); // ************************************************************************** // ************************************************************************** function getTabElem(value, label) { return (