2022-10-09 12:55:09 +11:00
import { debounce } from 'lodash' ;
2017-04-22 04:05:35 +10:00
import PropTypes from 'prop-types' ;
2022-10-09 12:55:09 +11:00
import React from 'react' ;
import { Helmet } from 'react-helmet' ;
2017-06-24 20:24:02 +10:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
2017-05-03 10:04:16 +10:00
import ImmutablePureComponent from 'react-immutable-pure-component' ;
2022-10-09 12:55:09 +11:00
import { defineMessages , injectIntl , FormattedMessage } from 'react-intl' ;
import { connect } from 'react-redux' ;
import { addColumn , removeColumn , moveColumn } from 'mastodon/actions/columns' ;
import { fetchFavouritedStatuses , expandFavouritedStatuses } from 'mastodon/actions/favourites' ;
import ColumnHeader from 'mastodon/components/column_header' ;
import StatusList from 'mastodon/components/status_list' ;
import Column from 'mastodon/features/ui/components/column' ;
2017-01-16 23:27:58 +11:00
const messages = defineMessages ( {
2017-05-21 01:31:47 +10:00
heading : { id : 'column.favourites' , defaultMessage : 'Favourites' } ,
2017-01-16 23:27:58 +11:00
} ) ;
const mapStateToProps = state => ( {
2017-06-24 20:24:02 +10:00
statusIds : state . getIn ( [ 'status_lists' , 'favourites' , 'items' ] ) ,
2017-12-09 12:22:13 +11:00
isLoading : state . getIn ( [ 'status_lists' , 'favourites' , 'isLoading' ] , true ) ,
2017-08-29 06:23:44 +10:00
hasMore : ! ! state . getIn ( [ 'status_lists' , 'favourites' , 'next' ] ) ,
2017-01-16 23:27:58 +11:00
} ) ;
2018-09-15 01:59:48 +10:00
class Favourites extends ImmutablePureComponent {
2017-01-16 23:27:58 +11:00
2017-05-12 22:44:10 +10:00
static propTypes = {
dispatch : PropTypes . func . isRequired ,
2017-06-24 20:24:02 +10:00
statusIds : ImmutablePropTypes . list . isRequired ,
2017-05-12 22:44:10 +10:00
intl : PropTypes . object . isRequired ,
2017-07-15 08:49:34 +10:00
columnId : PropTypes . string ,
multiColumn : PropTypes . bool ,
2017-08-29 06:23:44 +10:00
hasMore : PropTypes . bool ,
2017-12-09 12:22:13 +11:00
isLoading : PropTypes . bool ,
2017-05-12 22:44:10 +10:00
} ;
2017-01-16 23:27:58 +11:00
componentWillMount ( ) {
this . props . dispatch ( fetchFavouritedStatuses ( ) ) ;
2017-04-22 04:05:35 +10:00
}
2017-01-16 23:27:58 +11:00
2017-07-15 08:49:34 +10:00
handlePin = ( ) => {
const { columnId , dispatch } = this . props ;
if ( columnId ) {
dispatch ( removeColumn ( columnId ) ) ;
} else {
dispatch ( addColumn ( 'FAVOURITES' , { } ) ) ;
}
2023-01-30 11:45:35 +11:00
} ;
2017-07-15 08:49:34 +10:00
handleMove = ( dir ) => {
const { columnId , dispatch } = this . props ;
dispatch ( moveColumn ( columnId , dir ) ) ;
2023-01-30 11:45:35 +11:00
} ;
2017-07-15 08:49:34 +10:00
handleHeaderClick = ( ) => {
this . column . scrollTop ( ) ;
2023-01-30 11:45:35 +11:00
} ;
2017-07-15 08:49:34 +10:00
setRef = c => {
this . column = c ;
2023-01-30 11:45:35 +11:00
} ;
2017-07-15 08:49:34 +10:00
2018-03-06 05:31:40 +11:00
handleLoadMore = debounce ( ( ) => {
2017-01-16 23:27:58 +11:00
this . props . dispatch ( expandFavouritedStatuses ( ) ) ;
2023-01-30 11:45:35 +11:00
} , 300 , { leading : true } ) ;
2017-01-16 23:27:58 +11:00
render ( ) {
2021-07-13 23:45:17 +10:00
const { intl , statusIds , columnId , multiColumn , hasMore , isLoading } = this . props ;
2017-07-15 08:49:34 +10:00
const pinned = ! ! columnId ;
2017-01-16 23:27:58 +11:00
2022-04-29 08:24:31 +10:00
const emptyMessage = < FormattedMessage id = 'empty_column.favourited_statuses' defaultMessage = "You don't have any favourite posts yet. When you favourite one, it will show up here." / > ;
2018-08-27 00:39:37 +10:00
2017-01-16 23:27:58 +11:00
return (
2019-08-02 03:17:17 +10:00
< Column bindToDocument = { ! multiColumn } ref = { this . setRef } label = { intl . formatMessage ( messages . heading ) } >
2017-07-15 08:49:34 +10:00
< ColumnHeader
icon = 'star'
title = { intl . formatMessage ( messages . heading ) }
onPin = { this . handlePin }
onMove = { this . handleMove }
onClick = { this . handleHeaderClick }
pinned = { pinned }
multiColumn = { multiColumn }
2017-09-07 01:32:15 +10:00
showBackButton
2017-07-15 08:49:34 +10:00
/ >
< StatusList
trackScroll = { ! pinned }
statusIds = { statusIds }
scrollKey = { ` favourited_statuses- ${ columnId } ` }
2017-08-29 06:23:44 +10:00
hasMore = { hasMore }
2017-12-09 12:22:13 +11:00
isLoading = { isLoading }
2018-03-06 05:31:40 +11:00
onLoadMore = { this . handleLoadMore }
2018-08-27 00:39:37 +10:00
emptyMessage = { emptyMessage }
2019-07-19 17:25:22 +10:00
bindToDocument = { ! multiColumn }
2017-07-15 08:49:34 +10:00
/ >
2022-10-09 12:55:09 +11:00
< Helmet >
< title > { intl . formatMessage ( messages . heading ) } < / title >
2022-10-20 23:35:29 +11:00
< meta name = 'robots' content = 'noindex' / >
2022-10-09 12:55:09 +11:00
< / Helmet >
2017-01-16 23:27:58 +11:00
< / Column >
) ;
}
2017-04-22 04:05:35 +10:00
}
2023-03-24 13:17:53 +11:00
export default connect ( mapStateToProps ) ( injectIntl ( Favourites ) ) ;