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' ;
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-01-20 13:28:49 -08:00
import BirthdayReminders from 'soapbox/components/birthday_reminders' ;
2021-10-15 16:55:04 -07:00
import SubNavigation from 'soapbox/components/sub_navigation' ;
2022-01-10 14:17:52 -08:00
import PlaceholderNotification from 'soapbox/features/placeholder/components/placeholder_notification' ;
2022-01-20 13:28:49 -08:00
import { getFeatures } from 'soapbox/utils/features' ;
2022-01-10 14:25:06 -08:00
2022-01-10 14:01:24 -08:00
import {
expandNotifications ,
scrollTopNotifications ,
dequeueNotifications ,
} from '../../actions/notifications' ;
import Column from '../../components/column' ;
2022-01-10 14:17:52 -08:00
import LoadGap from '../../components/load_gap' ;
import ScrollableList from '../../components/scrollable_list' ;
import TimelineQueueButtonHeader from '../../components/timeline_queue_button_header' ;
2022-01-10 14:25:06 -08:00
2022-01-10 14:01:24 -08:00
import ColumnSettingsContainer from './containers/column_settings_container' ;
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
} ) ;
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 ) ;
const instance = state . get ( 'instance' ) ;
const features = getFeatures ( instance ) ;
2022-01-25 12:09:30 -08:00
const showBirthdayReminders = settings . getIn ( [ 'notifications' , 'birthdays' , 'show' ] ) && settings . getIn ( [ 'notifications' , 'quickFilter' , 'active' ] ) === 'all' && features . birthdays ;
2022-01-24 14:51:22 -08:00
const birthdays = showBirthdayReminders && state . getIn ( [ 'user_lists' , 'birthday_reminders' , state . get ( 'me' ) ] ) ;
2022-01-20 13:28:49 -08:00
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 ) ,
2022-01-24 14:51:22 -08:00
showBirthdayReminders ,
hasBirthdays : ! ! birthdays ,
2022-01-20 13:28:49 -08:00
} ;
} ;
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 ,
2022-01-20 13:28:49 -08:00
showBirthdayReminders : PropTypes . bool ,
2022-01-24 14:51:22 -08:00
hasBirthdays : PropTypes . bool ,
2020-03-27 13:59:38 -07:00
} ;
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-01-24 14:51:22 -08:00
const { hasBirthdays } = this . props ;
let elementIndex = this . props . notifications . findIndex ( item => item !== null && item . get ( 'id' ) === id ) - 1 ;
if ( hasBirthdays ) elementIndex ++ ;
2020-03-27 13:59:38 -07:00
this . _selectChild ( elementIndex , true ) ;
}
handleMoveDown = id => {
2022-01-24 14:51:22 -08:00
const { hasBirthdays } = this . props ;
let elementIndex = this . props . notifications . findIndex ( item => item !== null && item . get ( 'id' ) === id ) + 1 ;
if ( hasBirthdays ) elementIndex ++ ;
2020-03-27 13:59:38 -07:00
this . _selectChild ( elementIndex , false ) ;
}
2022-01-24 14:51:22 -08:00
handleMoveBelowBirthdays = ( ) => {
this . _selectChild ( 1 , false ) ;
}
2020-04-14 14:47:35 -07:00
_selectChild ( index , align _top ) {
2020-03-27 13:59:38 -07:00
const container = this . column . node ;
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-01-20 13:28:49 -08:00
const { intl , notifications , isLoading , hasMore , showFilterBar , totalQueuedNotificationsCount , showBirthdayReminders } = 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." / > ;
let scrollableContent = null ;
const filterBarContainer = showFilterBar
? ( < FilterBarContainer / > )
: null ;
if ( isLoading && this . scrollableContent ) {
scrollableContent = this . scrollableContent ;
} else if ( notifications . size > 0 || hasMore ) {
scrollableContent = notifications . map ( ( item , index ) => item === null ? (
< LoadGap
key = { 'gap:' + notifications . getIn ( [ index + 1 , 'id' ] ) }
disabled = { isLoading }
maxId = { index > 0 ? notifications . getIn ( [ index - 1 , 'id' ] ) : null }
onClick = { this . handleLoadGap }
/ >
) : (
< NotificationContainer
key = { item . get ( 'id' ) }
notification = { item }
onMoveUp = { this . handleMoveUp }
onMoveDown = { this . handleMoveDown }
/ >
) ) ;
2022-01-20 13:28:49 -08:00
2022-01-24 14:51:22 -08:00
if ( showBirthdayReminders ) scrollableContent = scrollableContent . unshift (
< BirthdayReminders
key = 'birthdays'
onMoveDown = { this . handleMoveBelowBirthdays }
/ > ,
) ;
2020-03-27 13:59:38 -07:00
} else {
scrollableContent = null ;
}
this . scrollableContent = scrollableContent ;
const scrollContainer = (
< ScrollableList
scrollKey = 'notifications'
isLoading = { isLoading }
showLoading = { isLoading && notifications . size === 0 }
hasMore = { hasMore }
emptyMessage = { emptyMessage }
2021-10-12 12:00:01 -07:00
placeholderComponent = { PlaceholderNotification }
placeholderCount = { 20 }
2020-03-27 13:59:38 -07:00
onLoadMore = { this . handleLoadOlder }
2021-11-04 12:07:20 -07:00
onRefresh = { this . handleRefresh }
2020-03-27 13:59:38 -07:00
onScrollToTop = { this . handleScrollToTop }
onScroll = { this . handleScroll }
>
{ scrollableContent }
< / S c r o l l a b l e L i s t >
) ;
return (
2021-10-05 20:01:16 -07:00
< Column ref = { this . setColumnRef } label = { intl . formatMessage ( messages . title ) } className = 'column--notifications' >
2021-10-15 16:55:04 -07:00
< SubNavigation message = { intl . formatMessage ( messages . title ) } settings = { ColumnSettingsContainer } / >
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 >
) ;
}
}