lbry-desktop/ui/page/listBlocked/view.jsx

286 lines
9 KiB
React
Raw Normal View History

// @flow
import * as ICONS from 'constants/icons';
2021-06-16 04:27:58 +02:00
import { BLOCK_LEVEL } from 'constants/comment';
import React from 'react';
import classnames from 'classnames';
import moment from 'moment';
import humanizeDuration from 'humanize-duration';
import BlockList from 'component/blockList';
2021-06-16 04:27:58 +02:00
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';
2021-06-16 04:27:58 +02:00
const VIEW = {
BLOCKED: 'blocked',
ADMIN: 'admin',
MODERATOR: 'moderator',
MUTED: 'muted',
};
type Props = {
mutedUris: ?Array<string>,
2021-06-16 04:27:58 +02:00
personalBlockList: ?Array<string>,
adminBlockList: ?Array<string>,
moderatorBlockList: ?Array<string>,
personalTimeoutMap: { [uri: string]: { blockedAt: string, bannedFor: number, banRemaining: number } },
adminTimeoutMap: { [uri: string]: { blockedAt: string, bannedFor: number, banRemaining: number } },
moderatorTimeoutMap: { [uri: string]: { blockedAt: string, bannedFor: number, banRemaining: number } },
2021-06-16 04:27:58 +02:00
moderatorBlockListDelegatorsMap: { [string]: Array<string> },
fetchingModerationBlockList: boolean,
fetchModBlockedList: () => void,
2021-06-16 04:27:58 +02:00
fetchModAmIList: () => void,
delegatorsById: { [string]: { global: boolean, delegators: { name: string, claimId: string } } },
myChannelClaimIds: ?Array<string>,
};
function ListBlocked(props: Props) {
2021-06-16 04:27:58 +02:00
const {
mutedUris,
personalBlockList,
adminBlockList,
moderatorBlockList,
personalTimeoutMap,
adminTimeoutMap,
moderatorTimeoutMap,
moderatorBlockListDelegatorsMap: delegatorsMap,
2021-06-16 04:27:58 +02:00
fetchingModerationBlockList,
fetchModBlockedList,
fetchModAmIList,
delegatorsById,
myChannelClaimIds,
2021-06-16 04:27:58 +02:00
} = props;
const [viewMode, setViewMode] = usePersistedState('blocked-muted:display', VIEW.BLOCKED);
const [localDelegatorsMap, setLocalDelegatorsMap] = React.useState(undefined);
const stringifiedDelegatorsMap = JSON.stringify(delegatorsMap);
const stringifiedLocalDelegatorsMap = JSON.stringify(localDelegatorsMap);
2021-06-16 04:27:58 +02:00
const isAdmin = myChannelClaimIds && myChannelClaimIds.some((id) => delegatorsById[id] && delegatorsById[id].global);
2021-06-16 04:27:58 +02:00
const isModerator =
myChannelClaimIds &&
myChannelClaimIds.some((id) => delegatorsById[id] && Object.keys(delegatorsById[id].delegators).length > 0);
2021-06-16 04:27:58 +02:00
// **************************************************************************
2021-06-16 04:27:58 +02:00
function getList(view) {
2021-06-16 04:27:58 +02:00
switch (view) {
case VIEW.BLOCKED:
return personalBlockList;
2021-06-16 04:27:58 +02:00
case VIEW.ADMIN:
return adminBlockList;
2021-06-16 04:27:58 +02:00
case VIEW.MODERATOR:
return moderatorBlockList;
2021-06-16 04:27:58 +02:00
case VIEW.MUTED:
return mutedUris;
2021-06-16 04:27:58 +02:00
}
}
function getActionButtons(uri) {
const getDurationStr = (durationNs) => {
const NANO_TO_MS = 1000000;
return humanizeDuration(durationNs / NANO_TO_MS, { round: true });
};
const getBanInfoElem = (timeoutInfo) => {
return (
<div>
<div className="help">
<blockquote>
{moment(timeoutInfo.blockedAt).format('MMMM Do, YYYY @ HH:mm')}
<br />
{getDurationStr(timeoutInfo.bannedFor)}{' '}
{__('(Remaining: %duration%) --[timeout ban duration]--', {
duration: getDurationStr(timeoutInfo.banRemaining),
})}
</blockquote>
</div>
</div>
);
};
switch (viewMode) {
2021-06-16 04:27:58 +02:00
case VIEW.BLOCKED:
return (
<>
<ChannelBlockButton uri={uri} />
<ChannelMuteButton uri={uri} />
{personalTimeoutMap[uri] && getBanInfoElem(personalTimeoutMap[uri])}
2021-06-16 04:27:58 +02:00
</>
);
case VIEW.ADMIN:
return (
<>
<ChannelBlockButton uri={uri} blockLevel={BLOCK_LEVEL.ADMIN} />
{adminTimeoutMap[uri] && getBanInfoElem(adminTimeoutMap[uri])}
</>
);
2021-06-16 04:27:58 +02:00
case VIEW.MODERATOR:
const delegatorUrisForBlockedUri = localDelegatorsMap && localDelegatorsMap[uri];
2021-06-16 04:27:58 +02:00
if (!delegatorUrisForBlockedUri) return null;
return (
<>
{delegatorUrisForBlockedUri.map((delegatorUri) => {
return (
<div className="block-list--delegator" key={delegatorUri}>
<label>{__('Blocked on behalf of:')}</label>
<ul className="section">
<div>
<ClaimPreview uri={delegatorUri} hideMenu hideActions type="inline" properties={false} />
{moderatorTimeoutMap[uri] && getBanInfoElem(moderatorTimeoutMap[uri])}
</div>
<ChannelBlockButton uri={uri} blockLevel={BLOCK_LEVEL.MODERATOR} creatorUri={delegatorUri} />
2021-06-16 04:27:58 +02:00
</ul>
</div>
);
})}
</>
);
case VIEW.MUTED:
return (
<>
<ChannelMuteButton uri={uri} />
<ChannelBlockButton uri={uri} />
</>
);
}
}
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."
);
2021-06-16 04:27:58 +02:00
case VIEW.ADMIN:
return __('This is the global block list.');
2021-06-16 04:27:58 +02:00
case VIEW.MODERATOR:
return __('List of channels that you have blocked as a moderator, along with the list of delegators.');
2021-06-16 04:27:58 +02:00
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.'
);
2021-06-16 04:27:58 +02:00
}
}
function getEmptyListTitle(view) {
switch (view) {
case VIEW.BLOCKED:
return __('You do not have any blocked channels');
2021-06-16 04:27:58 +02:00
case VIEW.MUTED:
return __('You do not have any muted channels');
2021-06-16 04:27:58 +02:00
case VIEW.ADMIN:
return __('You do not have any globally-blocked channels');
2021-06-16 04:27:58 +02:00
case VIEW.MODERATOR:
return __('You do not have any blocked channels as a moderator');
2021-06-16 04:27:58 +02:00
}
}
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);
}
function getViewElem(view, label, icon) {
return (
<Button
icon={icon}
button="alt"
label={__(label)}
className={classnames(`button-toggle`, {
'button-toggle--active': viewMode === view,
})}
onClick={() => setViewMode(view)}
/>
);
}
function getRefreshElem() {
return (
myChannelClaimIds && (
<Button
icon={ICONS.REFRESH}
button="alt"
label={__('Refresh')}
onClick={() => {
fetchModBlockedList();
fetchModAmIList();
}}
/>
)
);
}
// **************************************************************************
2021-06-16 04:27:58 +02:00
React.useEffect(() => {
if (stringifiedDelegatorsMap && isSourceListLarger(stringifiedDelegatorsMap, stringifiedLocalDelegatorsMap)) {
setLocalDelegatorsMap(JSON.parse(stringifiedDelegatorsMap));
2021-06-16 04:27:58 +02:00
}
}, [stringifiedDelegatorsMap, stringifiedLocalDelegatorsMap]);
2021-06-16 04:27:58 +02:00
// **************************************************************************
return (
<Page
noFooter
noSideNavigation
settingsPage
backout={{ title: __('Blocked and muted channels'), backLabel: __('Back') }}
>
{fetchingModerationBlockList && (
<div className="main--empty">
<Spinner />
</div>
)}
{!fetchingModerationBlockList && (
<>
<div className="section__header--actions">
<div className="section__actions--inline">
{getViewElem(VIEW.BLOCKED, 'Blocked', ICONS.BLOCK)}
{isAdmin && getViewElem(VIEW.ADMIN, 'Global', ICONS.BLOCK)}
{isModerator && getViewElem(VIEW.MODERATOR, 'Moderator', ICONS.BLOCK)}
{getViewElem(VIEW.MUTED, 'Muted', ICONS.MUTE)}
</div>
<div className="section__actions--inline">{getRefreshElem()}</div>
</div>
<BlockList
key={viewMode}
uris={getList(viewMode)}
help={getHelpText(viewMode)}
titleEmptyList={getEmptyListTitle(viewMode)}
subtitle={getEmptyListSubtitle(viewMode)}
getActionButtons={getActionButtons}
className={viewMode === VIEW.MODERATOR ? 'block-list--moderator' : undefined}
/>
</>
)}
</Page>
);
}
export default ListBlocked;