2022-01-10 14:17:52 -08:00
import { List as ImmutableList } from 'immutable' ;
import { debounce } from 'lodash' ;
2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types' ;
2022-01-10 14:17:52 -08:00
import React from 'react' ;
2020-03-27 13:59:38 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
2022-01-10 14:17:52 -08:00
import { connect } from 'react-redux' ;
2022-04-18 17:00:19 -07:00
import { Virtuoso } from 'react-virtuoso' ;
2020-03-27 13:59:38 -07:00
import { createSelector } from 'reselect' ;
2022-01-10 14:25:06 -08:00
2020-05-28 15:52:07 -07:00
import { getSettings } from 'soapbox/actions/settings' ;
2022-04-18 17:00:19 -07:00
import PullToRefresh from 'soapbox/components/pull-to-refresh' ;
2022-01-10 14:17:52 -08:00
import PlaceholderNotification from 'soapbox/features/placeholder/components/placeholder_notification' ;
2022-01-10 14:25:06 -08:00
2022-01-10 14:01:24 -08:00
import {
expandNotifications ,
scrollTopNotifications ,
dequeueNotifications ,
} from '../../actions/notifications' ;
2022-01-10 14:17:52 -08:00
import TimelineQueueButtonHeader from '../../components/timeline_queue_button_header' ;
2022-04-18 17:00:19 -07:00
import { Column , Text } from '../../components/ui' ;
2022-01-10 14:25:06 -08:00
2022-01-10 14:17:52 -08:00
import FilterBarContainer from './containers/filter_bar_container' ;
2022-01-10 14:01:24 -08:00
import NotificationContainer from './containers/notification_container' ;
2020-03-27 13:59:38 -07:00
const messages = defineMessages ( {
title : { id : 'column.notifications' , defaultMessage : 'Notifications' } ,
2020-06-07 09:25:48 -07:00
queue : { id : 'notifications.queue_label' , defaultMessage : 'Click to see {count} new {count, plural, one {notification} other {notifications}}' } ,
2020-03-27 13:59:38 -07:00
} ) ;
2022-04-18 17:00:19 -07:00
const Footer = ( { context } ) => (
context . hasMore ? (
< PlaceholderNotification / >
) : null
) ;
const Item = ( { context , ... rest } ) => (
2022-05-04 07:50:53 -07:00
< div className = 'border-solid border-b border-gray-200 dark:border-slate-700' { ... rest } / >
2022-04-18 17:00:19 -07:00
) ;
2020-03-27 13:59:38 -07:00
const getNotifications = createSelector ( [
2020-04-28 11:49:39 -07:00
state => getSettings ( state ) . getIn ( [ 'notifications' , 'quickFilter' , 'show' ] ) ,
state => getSettings ( state ) . getIn ( [ 'notifications' , 'quickFilter' , 'active' ] ) ,
state => ImmutableList ( getSettings ( state ) . getIn ( [ 'notifications' , 'shows' ] ) . filter ( item => ! item ) . keys ( ) ) ,
2020-09-23 16:57:10 -07:00
state => state . getIn ( [ 'notifications' , 'items' ] ) . toList ( ) ,
2020-03-27 13:59:38 -07:00
] , ( showFilterBar , allowedType , excludedTypes , notifications ) => {
if ( ! showFilterBar || allowedType === 'all' ) {
// used if user changed the notification settings after loading the notifications from the server
// otherwise a list of notifications will come pre-filtered from the backend
// we need to turn it off for FilterBar in order not to block ourselves from seeing a specific category
return notifications . filterNot ( item => item !== null && excludedTypes . includes ( item . get ( 'type' ) ) ) ;
}
return notifications . filter ( item => item !== null && allowedType === item . get ( 'type' ) ) ;
} ) ;
2022-01-20 13:28:49 -08:00
const mapStateToProps = state => {
const settings = getSettings ( state ) ;
return {
showFilterBar : settings . getIn ( [ 'notifications' , 'quickFilter' , 'show' ] ) ,
notifications : getNotifications ( state ) ,
isLoading : state . getIn ( [ 'notifications' , 'isLoading' ] , true ) ,
isUnread : state . getIn ( [ 'notifications' , 'unread' ] ) > 0 ,
hasMore : state . getIn ( [ 'notifications' , 'hasMore' ] ) ,
totalQueuedNotificationsCount : state . getIn ( [ 'notifications' , 'totalQueuedNotificationsCount' ] , 0 ) ,
} ;
} ;
2020-03-27 13:59:38 -07:00
export default @ connect ( mapStateToProps )
@ injectIntl
class Notifications extends React . PureComponent {
static propTypes = {
2020-09-23 16:57:10 -07:00
notifications : ImmutablePropTypes . list . isRequired ,
2020-03-27 13:59:38 -07:00
showFilterBar : PropTypes . bool . isRequired ,
dispatch : PropTypes . func . isRequired ,
intl : PropTypes . object . isRequired ,
isLoading : PropTypes . bool ,
isUnread : PropTypes . bool ,
hasMore : PropTypes . bool ,
dequeueNotifications : PropTypes . func ,
totalQueuedNotificationsCount : PropTypes . number ,
} ;
2020-04-14 14:47:35 -07:00
componentWillUnmount ( ) {
2020-03-27 13:59:38 -07:00
this . handleLoadOlder . cancel ( ) ;
this . handleScrollToTop . cancel ( ) ;
this . handleScroll . cancel ( ) ;
this . props . dispatch ( scrollTopNotifications ( false ) ) ;
}
componentDidMount ( ) {
this . handleDequeueNotifications ( ) ;
this . props . dispatch ( scrollTopNotifications ( true ) ) ;
}
handleLoadGap = ( maxId ) => {
this . props . dispatch ( expandNotifications ( { maxId } ) ) ;
} ;
handleLoadOlder = debounce ( ( ) => {
const last = this . props . notifications . last ( ) ;
this . props . dispatch ( expandNotifications ( { maxId : last && last . get ( 'id' ) } ) ) ;
} , 300 , { leading : true } ) ;
handleScrollToTop = debounce ( ( ) => {
this . props . dispatch ( scrollTopNotifications ( true ) ) ;
} , 100 ) ;
handleScroll = debounce ( ( ) => {
this . props . dispatch ( scrollTopNotifications ( false ) ) ;
} , 100 ) ;
setColumnRef = c => {
this . column = c ;
}
handleMoveUp = id => {
2022-04-18 21:35:40 -07:00
const elementIndex = this . props . notifications . findIndex ( item => item !== null && item . get ( 'id' ) === id ) - 1 ;
2020-03-27 13:59:38 -07:00
this . _selectChild ( elementIndex , true ) ;
}
handleMoveDown = id => {
2022-04-18 21:35:40 -07:00
const elementIndex = this . props . notifications . findIndex ( item => item !== null && item . get ( 'id' ) === id ) + 1 ;
2020-03-27 13:59:38 -07:00
this . _selectChild ( elementIndex , false ) ;
}
2020-04-14 14:47:35 -07:00
_selectChild ( index , align _top ) {
2022-03-21 11:09:01 -07:00
const container = this . column ;
2020-03-27 13:59:38 -07:00
const element = container . querySelector ( ` article:nth-of-type( ${ index + 1 } ) .focusable ` ) ;
if ( element ) {
if ( align _top && container . scrollTop > element . offsetTop ) {
element . scrollIntoView ( true ) ;
} else if ( ! align _top && container . scrollTop + container . clientHeight < element . offsetTop + element . offsetHeight ) {
element . scrollIntoView ( false ) ;
}
element . focus ( ) ;
}
}
handleDequeueNotifications = ( ) => {
this . props . dispatch ( dequeueNotifications ( ) ) ;
} ;
2021-11-04 12:07:20 -07:00
handleRefresh = ( ) => {
const { dispatch } = this . props ;
return dispatch ( expandNotifications ( ) ) ;
}
2020-04-14 14:47:35 -07:00
render ( ) {
2022-04-18 21:35:40 -07:00
const { intl , notifications , isLoading , hasMore , showFilterBar , totalQueuedNotificationsCount } = this . props ;
2020-03-27 13:59:38 -07:00
const emptyMessage = < FormattedMessage id = 'empty_column.notifications' defaultMessage = "You don't have any notifications yet. Interact with others to start the conversation." / > ;
const filterBarContainer = showFilterBar
? ( < FilterBarContainer / > )
: null ;
2022-04-22 11:05:40 -07:00
const showLoading = isLoading && ! notifications || notifications . isEmpty ( ) ;
2022-04-18 17:00:19 -07:00
const scrollContainer = (
< PullToRefresh onRefresh = { this . handleRefresh } >
< Virtuoso
useWindowScroll
2022-04-22 11:05:40 -07:00
data = { showLoading ? Array ( 20 ) . fill ( ) : notifications . toArray ( ) }
2022-04-18 17:00:19 -07:00
startReached = { this . handleScrollToTop }
endReached = { this . handleLoadOlder }
2022-04-21 19:49:34 -07:00
isScrolling = { isScrolling => isScrolling && this . handleScroll ( ) }
2022-04-18 17:00:19 -07:00
itemContent = { ( _index , notification ) => (
2022-04-22 11:05:40 -07:00
showLoading ? (
2022-04-21 18:24:04 -07:00
< PlaceholderNotification / >
) : (
< NotificationContainer
key = { notification . id }
notification = { notification }
onMoveUp = { this . handleMoveUp }
onMoveDown = { this . handleMoveDown }
/ >
)
2022-04-18 17:00:19 -07:00
) }
context = { {
hasMore ,
} }
components = { {
ScrollSeekPlaceholder : PlaceholderNotification ,
Footer ,
2022-04-21 18:24:04 -07:00
EmptyPlaceholder : ( ) => < Text > { emptyMessage } < / T e x t > ,
2022-04-18 17:00:19 -07:00
Item ,
} }
2020-03-27 13:59:38 -07:00
/ >
2022-04-18 17:00:19 -07:00
< / P u l l T o R e f r e s h >
2020-03-27 13:59:38 -07:00
) ;
return (
2022-03-21 11:09:01 -07:00
< Column ref = { this . setColumnRef } label = { intl . formatMessage ( messages . title ) } withHeader = { false } >
2020-03-27 13:59:38 -07:00
{ filterBarContainer }
2020-06-07 09:25:48 -07:00
< TimelineQueueButtonHeader
onClick = { this . handleDequeueNotifications }
count = { totalQueuedNotificationsCount }
message = { messages . queue }
/ >
2021-11-04 12:07:20 -07:00
{ scrollContainer }
2020-03-27 13:59:38 -07:00
< / C o l u m n >
) ;
}
}