2022-05-30 12:03:52 -07:00
import React , { useEffect , useState } from 'react' ;
import { defineMessages , FormattedDate , useIntl } from 'react-intl' ;
import { fetchModerationLog } from 'soapbox/actions/admin' ;
2022-11-15 08:00:49 -08:00
import ScrollableList from 'soapbox/components/scrollable-list' ;
2022-05-30 12:03:52 -07:00
import { useAppDispatch , useAppSelector } from 'soapbox/hooks' ;
import Column from '../ui/components/column' ;
const messages = defineMessages ( {
heading : { id : 'column.admin.moderation_log' , defaultMessage : 'Moderation Log' } ,
emptyMessage : { id : 'admin.moderation_log.empty_message' , defaultMessage : 'You have not performed any moderation actions yet. When you do, a history will be shown here.' } ,
} ) ;
const ModerationLog = ( ) = > {
const intl = useIntl ( ) ;
const dispatch = useAppDispatch ( ) ;
2022-06-05 07:17:26 -07:00
const items = useAppSelector ( ( state ) = > {
return state . admin_log . index . map ( ( i ) = > state . admin_log . items . get ( String ( i ) ) ) ;
} ) ;
const hasMore = useAppSelector ( ( state ) = > state . admin_log . total - state . admin_log . index . count ( ) > 0 ) ;
2022-05-30 12:03:52 -07:00
const [ isLoading , setIsLoading ] = useState ( true ) ;
const [ lastPage , setLastPage ] = useState ( 0 ) ;
const showLoading = isLoading && items . count ( ) === 0 ;
useEffect ( ( ) = > {
dispatch ( fetchModerationLog ( ) )
. then ( ( ) = > {
setIsLoading ( false ) ;
setLastPage ( 1 ) ;
} )
2022-10-14 08:38:37 -07:00
. catch ( ( ) = > { } ) ;
2022-05-30 12:03:52 -07:00
} , [ ] ) ;
const handleLoadMore = ( ) = > {
const page = lastPage + 1 ;
setIsLoading ( true ) ;
dispatch ( fetchModerationLog ( { page } ) )
. then ( ( ) = > {
setIsLoading ( false ) ;
setLastPage ( page ) ;
2022-10-14 08:38:37 -07:00
} ) . catch ( ( ) = > { } ) ;
2022-05-30 12:03:52 -07:00
} ;
return (
< Column icon = 'balance-scale' label = { intl . formatMessage ( messages . heading ) } >
< ScrollableList
isLoading = { isLoading }
showLoading = { showLoading }
scrollKey = 'moderation-log'
emptyMessage = { intl . formatMessage ( messages . emptyMessage ) }
hasMore = { hasMore }
onLoadMore = { handleLoadMore }
>
2022-06-05 07:17:26 -07:00
{ items . map ( ( item ) = > item && (
< div className = 'logentry' key = { item . id } >
< div className = 'logentry__message' > { item . message } < / div >
2022-05-30 12:03:52 -07:00
< div className = 'logentry__timestamp' >
< FormattedDate
2022-06-05 07:17:26 -07:00
value = { new Date ( item . time * 1000 ) }
2022-10-14 08:38:37 -07:00
hour12
2022-05-30 12:03:52 -07:00
year = 'numeric'
month = 'short'
day = '2-digit'
2022-10-14 08:38:37 -07:00
hour = 'numeric'
2022-05-30 12:03:52 -07:00
minute = '2-digit'
/ >
< / div >
< / div >
) ) }
< / ScrollableList >
< / Column >
) ;
} ;
export default ModerationLog ;