2019-07-08 22:54:58 +02:00
// @flow
2021-03-03 19:50:16 +01:00
import * as ICONS from 'constants/icons' ;
2019-07-08 22:54:58 +02:00
import React from 'react' ;
2021-03-03 19:50:16 +01:00
import classnames from 'classnames' ;
2019-07-08 22:54:58 +02:00
import ClaimList from 'component/claimList' ;
import Page from 'component/page' ;
2021-03-03 19:50:16 +01:00
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-03-04 07:03:58 +01:00
import Yrbl from 'component/yrbl' ;
2019-07-08 22:54:58 +02:00
type Props = {
2021-03-03 19:50:16 +01:00
mutedUris : ? Array < string > ,
blockedUris : ? Array < string > ,
fetchingModerationBlockList : boolean ,
2019-07-08 22:54:58 +02:00
} ;
2021-03-03 19:50:16 +01:00
const VIEW _BLOCKED = 'blocked' ;
const VIEW _MUTED = 'muted' ;
2019-07-08 22:54:58 +02:00
function ListBlocked ( props : Props ) {
2021-03-03 19:50:16 +01:00
const { mutedUris , blockedUris , fetchingModerationBlockList } = props ;
const [ viewMode , setViewMode ] = usePersistedState ( 'blocked-muted:display' , VIEW _BLOCKED ) ;
// Keep a local list to allow for undoing actions in this component
const [ localBlockedList , setLocalBlockedList ] = React . useState ( undefined ) ;
const [ localMutedList , setLocalMutedList ] = React . useState ( undefined ) ;
const hasLocalMuteList = localMutedList && localMutedList . length > 0 ;
const hasLocalBlockList = localBlockedList && localBlockedList . length > 0 ;
const stringifiedMutedChannels = JSON . stringify ( mutedUris ) ;
const justMuted = localMutedList && mutedUris && localMutedList . length < mutedUris . length ;
const justBlocked = localBlockedList && blockedUris && localBlockedList . length < blockedUris . length ;
const stringifiedBlockedChannels = JSON . stringify ( blockedUris ) ;
2021-03-04 07:03:58 +01:00
const showUris = ( viewMode === VIEW _MUTED && hasLocalMuteList ) || ( viewMode === VIEW _BLOCKED && hasLocalBlockList ) ;
2021-03-03 19:50:16 +01:00
React . useEffect ( ( ) => {
const jsonMutedChannels = stringifiedMutedChannels && JSON . parse ( stringifiedMutedChannels ) ;
if ( ! hasLocalMuteList && jsonMutedChannels && jsonMutedChannels . length > 0 ) {
setLocalMutedList ( jsonMutedChannels ) ;
}
} , [ stringifiedMutedChannels , hasLocalMuteList ] ) ;
React . useEffect ( ( ) => {
const jsonBlockedChannels = stringifiedBlockedChannels && JSON . parse ( stringifiedBlockedChannels ) ;
if ( ! hasLocalBlockList && jsonBlockedChannels && jsonBlockedChannels . length > 0 ) {
setLocalBlockedList ( jsonBlockedChannels ) ;
}
} , [ stringifiedBlockedChannels , hasLocalBlockList ] ) ;
React . useEffect ( ( ) => {
if ( justMuted && stringifiedMutedChannels ) {
setLocalMutedList ( JSON . parse ( stringifiedMutedChannels ) ) ;
}
} , [ stringifiedMutedChannels , justMuted , setLocalMutedList ] ) ;
React . useEffect ( ( ) => {
if ( justBlocked && stringifiedBlockedChannels ) {
setLocalBlockedList ( JSON . parse ( stringifiedBlockedChannels ) ) ;
}
} , [ stringifiedBlockedChannels , justBlocked , setLocalBlockedList ] ) ;
2019-07-08 22:54:58 +02:00
2021-03-04 07:03:58 +01:00
const mutedHelpText = _ _ (
'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.'
) ;
const blockedHelpText = _ _ (
"Blocked channels will be invisible to you in the app. They will not be able to comment on your content, or reply to you comments left on other channels' content."
) ;
2019-07-08 22:54:58 +02:00
return (
2019-08-02 17:11:31 +02:00
< Page >
2021-03-04 07:03:58 +01:00
{ fetchingModerationBlockList && (
2019-07-08 22:54:58 +02:00
< div className = "main--empty" >
2021-03-03 19:50:16 +01:00
< Spinner / >
2019-07-08 22:54:58 +02:00
< / div >
) }
2021-03-03 19:50:16 +01:00
2021-03-04 07:03:58 +01:00
{ ! fetchingModerationBlockList && (
2021-03-03 19:50:16 +01:00
< >
< div className = "section__header--actions" >
< div className = "section__actions--inline" >
< Button
icon = { ICONS . BLOCK }
button = "alt"
label = { _ _ ( 'Blocked' ) }
className = { classnames ( ` button-toggle ` , {
'button-toggle--active' : viewMode === VIEW _BLOCKED ,
} ) }
onClick = { ( ) => setViewMode ( VIEW _BLOCKED ) }
/ >
< Button
icon = { ICONS . MUTE }
button = "alt"
label = { _ _ ( 'Muted' ) }
className = { classnames ( ` button-toggle ` , {
'button-toggle--active' : viewMode === VIEW _MUTED ,
} ) }
onClick = { ( ) => setViewMode ( VIEW _MUTED ) }
/ >
< / div >
< / div >
2021-03-04 07:03:58 +01:00
{ showUris && < div className = "help--notice" > { viewMode === VIEW _MUTED ? mutedHelpText : blockedHelpText } < / div > }
{ showUris ? (
< ClaimList
uris = { viewMode === VIEW _MUTED ? localMutedList : localBlockedList }
showUnresolvedClaims
showHiddenByUser
hideMenu
renderActions = { ( claim ) => {
return (
< div className = "section__actions" >
{ viewMode === VIEW _MUTED ? (
< >
< ChannelMuteButton uri = { claim . permanent _url } / >
< ChannelBlockButton uri = { claim . permanent _url } / >
< / >
) : (
< >
< ChannelBlockButton uri = { claim . permanent _url } / >
< ChannelMuteButton uri = { claim . permanent _url } / >
< / >
) }
< / div >
) ;
} }
/ >
) : (
< div className = "main--empty" >
< Yrbl
title = {
viewMode === VIEW _MUTED
? _ _ ( 'You do not have any muted channels' )
: _ _ ( 'You do not have any blocked channels' )
}
subtitle = { viewMode === VIEW _MUTED ? mutedHelpText : blockedHelpText }
actions = {
< div className = "section__actions" >
< Button button = "primary" label = { _ _ ( 'Go Home' ) } navigate = "/" / >
< / div >
}
/ >
< / div >
) }
2021-03-03 19:50:16 +01:00
< / >
) }
2019-07-08 22:54:58 +02:00
< / Page >
) ;
}
export default ListBlocked ;