// @flow import * as ICONS from 'constants/icons'; import { BLOCK_LEVEL } from 'constants/comment'; import React from 'react'; import classnames from 'classnames'; import ClaimList from 'component/claimList'; import ClaimPreview from 'component/claimPreview'; import Page from 'component/page'; import Spinner from 'component/spinner'; import Button from 'component/button'; import usePersistedState from 'effects/use-persisted-state'; import ChannelBlockButton from 'component/channelBlockButton'; import ChannelMuteButton from 'component/channelMuteButton'; import Yrbl from 'component/yrbl'; const VIEW = { BLOCKED: 'blocked', ADMIN: 'admin', MODERATOR: 'moderator', MUTED: 'muted', }; type Props = { mutedUris: ?Array, personalBlockList: ?Array, adminBlockList: ?Array, moderatorBlockList: ?Array, moderatorBlockListDelegatorsMap: { [string]: Array }, fetchingModerationBlockList: boolean, fetchModBlockedList: () => void, fetchModAmIList: () => void, delegatorsById: { [string]: { global: boolean, delegators: { name: string, claimId: string } } }, myChannelClaims: ?Array, }; function ListBlocked(props: Props) { const { mutedUris, personalBlockList, adminBlockList, moderatorBlockList, moderatorBlockListDelegatorsMap, fetchingModerationBlockList, fetchModBlockedList, fetchModAmIList, delegatorsById, myChannelClaims, } = props; const [viewMode, setViewMode] = usePersistedState('blocked-muted:display', VIEW.BLOCKED); // Keep a local list to allow for undoing actions in this component const [localPersonalList, setLocalPersonalList] = React.useState(undefined); const [localAdminList, setLocalAdminList] = React.useState(undefined); const [localModeratorList, setLocalModeratorList] = React.useState(undefined); const [localModeratorListDelegatorsMap, setLocalModeratorListDelegatorsMap] = React.useState(undefined); const [localMutedList, setLocalMutedList] = React.useState(undefined); const hasLocalMuteList = localMutedList && localMutedList.length > 0; const hasLocalPersonalList = localPersonalList && localPersonalList.length > 0; const stringifiedMutedList = JSON.stringify(mutedUris); const stringifiedPersonalList = JSON.stringify(personalBlockList); const stringifiedAdminList = JSON.stringify(adminBlockList); const stringifiedModeratorList = JSON.stringify(moderatorBlockList); const stringifiedModeratorListDelegatorsMap = JSON.stringify(moderatorBlockListDelegatorsMap); const stringifiedLocalAdminList = JSON.stringify(localAdminList); const stringifiedLocalModeratorList = JSON.stringify(localModeratorList); const stringifiedLocalModeratorListDelegatorsMap = JSON.stringify(localModeratorListDelegatorsMap); const justMuted = localMutedList && mutedUris && localMutedList.length < mutedUris.length; const justPersonalBlocked = localPersonalList && personalBlockList && localPersonalList.length < personalBlockList.length; const isAdmin = myChannelClaims && myChannelClaims.some((c) => delegatorsById[c.claim_id] && delegatorsById[c.claim_id].global); const isModerator = myChannelClaims && myChannelClaims.some( (c) => delegatorsById[c.claim_id] && Object.keys(delegatorsById[c.claim_id].delegators).length > 0 ); const listForView = getLocalList(viewMode); const showUris = listForView && listForView.length > 0; function getLocalList(view) { switch (view) { case VIEW.BLOCKED: return localPersonalList; case VIEW.ADMIN: return localAdminList; case VIEW.MODERATOR: return localModeratorList; case VIEW.MUTED: return localMutedList; } } function getButtons(view, uri) { switch (view) { case VIEW.BLOCKED: return ( <> ); case VIEW.ADMIN: return ; case VIEW.MODERATOR: const delegatorUrisForBlockedUri = localModeratorListDelegatorsMap && localModeratorListDelegatorsMap[uri]; if (!delegatorUrisForBlockedUri) return null; return ( <> {delegatorUrisForBlockedUri.map((delegatorUri) => { return (
); })} ); case VIEW.MUTED: return ( <> ); } } function getHelpText(view) { switch (view) { case VIEW.BLOCKED: return "Blocked channels will be invisible to you in the app. They will not be able to comment on your content, nor reply to your comments left on other channels' content."; case VIEW.ADMIN: return 'This is the global block list.'; case VIEW.MODERATOR: return 'List of channels that you have blocked as a moderator, along with the list of delegators.'; case VIEW.MUTED: return 'Muted channels will be invisible to you in the app. They will not know they are muted and can still interact with you and your content.'; } } function getEmptyListTitle(view) { switch (view) { case VIEW.BLOCKED: return 'You do not have any blocked channels'; case VIEW.MUTED: return 'You do not have any muted channels'; case VIEW.ADMIN: return 'You do not have any globally-blocked channels'; case VIEW.MODERATOR: return 'You do not have any blocked channels as a moderator'; } } function getEmptyListSubtitle(view) { switch (view) { case VIEW.BLOCKED: case VIEW.MUTED: return getHelpText(view); case VIEW.ADMIN: case VIEW.MODERATOR: return null; } } function isSourceListLarger(source, local) { // Comparing the length of stringified is not perfect, but what are the // chances of having different lists with the exact same length? return source && (!local || local.length < source.length); } React.useEffect(() => { const jsonMutedChannels = stringifiedMutedList && JSON.parse(stringifiedMutedList); if (!hasLocalMuteList && jsonMutedChannels && jsonMutedChannels.length > 0) { setLocalMutedList(jsonMutedChannels); } }, [stringifiedMutedList, hasLocalMuteList]); React.useEffect(() => { const jsonBlockedChannels = stringifiedPersonalList && JSON.parse(stringifiedPersonalList); if (!hasLocalPersonalList && jsonBlockedChannels && jsonBlockedChannels.length > 0) { setLocalPersonalList(jsonBlockedChannels); } }, [stringifiedPersonalList, hasLocalPersonalList]); React.useEffect(() => { if (stringifiedAdminList && isSourceListLarger(stringifiedAdminList, stringifiedLocalAdminList)) { setLocalAdminList(JSON.parse(stringifiedAdminList)); } }, [stringifiedAdminList, stringifiedLocalAdminList]); React.useEffect(() => { if (stringifiedModeratorList && isSourceListLarger(stringifiedModeratorList, stringifiedLocalModeratorList)) { setLocalModeratorList(JSON.parse(stringifiedModeratorList)); } }, [stringifiedModeratorList, stringifiedLocalModeratorList]); React.useEffect(() => { if ( stringifiedModeratorListDelegatorsMap && isSourceListLarger(stringifiedModeratorListDelegatorsMap, stringifiedLocalModeratorListDelegatorsMap) ) { setLocalModeratorListDelegatorsMap(JSON.parse(stringifiedModeratorListDelegatorsMap)); } }, [stringifiedModeratorListDelegatorsMap, stringifiedLocalModeratorListDelegatorsMap]); React.useEffect(() => { if (justMuted && stringifiedMutedList) { setLocalMutedList(JSON.parse(stringifiedMutedList)); } }, [stringifiedMutedList, justMuted, setLocalMutedList]); React.useEffect(() => { if (justPersonalBlocked && stringifiedPersonalList) { setLocalPersonalList(JSON.parse(stringifiedPersonalList)); } }, [stringifiedPersonalList, justPersonalBlocked, setLocalPersonalList]); return ( {fetchingModerationBlockList && (
)} {!fetchingModerationBlockList && ( <>
{showUris &&
{getHelpText(viewMode)}
} {showUris ? (
{ return
{getButtons(viewMode, claim.permanent_url)}
; }} />
) : (
} /> )} )}
); } export default ListBlocked;