bigbuffet-rw/app/soapbox/features/followers/index.js

142 lines
4.3 KiB
JavaScript
Raw Normal View History

import { debounce } from 'lodash';
2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2020-03-27 13:59:38 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
2022-01-10 14:01:24 -08:00
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
2022-01-10 14:01:24 -08:00
import MissingIndicator from 'soapbox/components/missing_indicator';
import { findAccountByUsername } from 'soapbox/selectors';
import { getFollowDifference } from 'soapbox/utils/accounts';
2020-03-27 13:59:38 -07:00
import {
fetchAccount,
fetchFollowers,
expandFollowers,
fetchAccountByUsername,
} from '../../actions/accounts';
import LoadingIndicator from '../../components/loading_indicator';
import ScrollableList from '../../components/scrollable_list';
2020-03-27 13:59:38 -07:00
import AccountContainer from '../../containers/account_container';
import Column from '../ui/components/column';
const messages = defineMessages({
heading: { id: 'column.followers', defaultMessage: 'Followers' },
});
const mapStateToProps = (state, { params, withReplies = false }) => {
const username = params.username || '';
const me = state.get('me');
2021-11-15 19:31:17 -08:00
const accountFetchError = ((state.getIn(['accounts', -1, 'username']) || '').toLowerCase() === username.toLowerCase());
2020-03-27 13:59:38 -07:00
let accountId = -1;
if (accountFetchError) {
accountId = null;
2020-04-14 11:44:40 -07:00
} else {
const account = findAccountByUsername(state, username);
2020-03-27 13:59:38 -07:00
accountId = account ? account.getIn(['id'], null) : -1;
}
2020-05-27 17:36:23 -07:00
const diffCount = getFollowDifference(state, accountId, 'followers');
2020-03-27 13:59:38 -07:00
const isBlocked = state.getIn(['relationships', accountId, 'blocked_by'], false);
const unavailable = (me === accountId) ? false : isBlocked;
2020-03-27 13:59:38 -07:00
return {
accountId,
unavailable,
isAccount: !!state.getIn(['accounts', accountId]),
accountIds: state.getIn(['user_lists', 'followers', accountId, 'items']),
hasMore: !!state.getIn(['user_lists', 'followers', accountId, 'next']),
2020-05-27 17:36:23 -07:00
diffCount,
2020-03-27 13:59:38 -07:00
};
};
export default @connect(mapStateToProps)
@injectIntl
2020-03-27 13:59:38 -07:00
class Followers extends ImmutablePureComponent {
static propTypes = {
intl: PropTypes.object.isRequired,
2020-03-27 13:59:38 -07:00
params: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
accountIds: ImmutablePropTypes.orderedSet,
2020-03-27 13:59:38 -07:00
hasMore: PropTypes.bool,
2020-05-27 17:36:23 -07:00
diffCount: PropTypes.number,
2020-03-27 13:59:38 -07:00
isAccount: PropTypes.bool,
unavailable: PropTypes.bool,
};
componentDidMount() {
2020-04-14 13:45:38 -07:00
const { params: { username }, accountId } = this.props;
2020-03-27 13:59:38 -07:00
if (accountId && accountId !== -1) {
this.props.dispatch(fetchAccount(accountId));
this.props.dispatch(fetchFollowers(accountId));
2020-04-14 11:44:40 -07:00
} else {
2020-03-27 13:59:38 -07:00
this.props.dispatch(fetchAccountByUsername(username));
}
}
componentDidUpdate(prevProps) {
const { accountId, dispatch } = this.props;
if (accountId && accountId !== -1 && (accountId !== prevProps.accountId && accountId)) {
dispatch(fetchAccount(accountId));
dispatch(fetchFollowers(accountId));
2020-03-27 13:59:38 -07:00
}
}
handleLoadMore = debounce(() => {
if (this.props.accountId && this.props.accountId !== -1) {
this.props.dispatch(expandFollowers(this.props.accountId));
}
}, 300, { leading: true });
render() {
const { intl, accountIds, hasMore, diffCount, isAccount, accountId, unavailable } = this.props;
2020-03-27 13:59:38 -07:00
if (!isAccount && accountId !== -1) {
return (
<Column>
<MissingIndicator />
</Column>
);
}
2020-04-14 13:45:38 -07:00
if (accountId === -1 || (!accountIds)) {
2020-03-27 13:59:38 -07:00
return (
<Column>
<LoadingIndicator />
</Column>
);
}
if (unavailable) {
return (
<Column>
<div className='empty-column-indicator'>
<FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />
</div>
</Column>
);
}
return (
<Column heading={intl.formatMessage(messages.heading)}>
2020-03-27 13:59:38 -07:00
<ScrollableList
scrollKey='followers'
hasMore={hasMore}
2020-05-27 17:36:23 -07:00
diffCount={diffCount}
2020-03-27 13:59:38 -07:00
onLoadMore={this.handleLoadMore}
emptyMessage={<FormattedMessage id='account.followers.empty' defaultMessage='No one follows this user yet.' />}
>
{accountIds.map(id =>
<AccountContainer key={id} id={id} withNote={false} />,
2020-03-27 13:59:38 -07:00
)}
</ScrollableList>
</Column>
);
}
}