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

176 lines
6.1 KiB
JavaScript
Raw Normal View History

import { OrderedSet as ImmutableOrderedSet } from 'immutable';
import PropTypes from 'prop-types';
2020-03-27 13:59:38 -07:00
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import { getSettings } from 'soapbox/actions/settings';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import MissingIndicator from 'soapbox/components/missing_indicator';
import { makeGetStatusIds, findAccountByUsername } from 'soapbox/selectors';
import { getFeatures } from 'soapbox/utils/features';
import { fetchAccount, fetchAccountByUsername } from '../../actions/accounts';
2022-01-10 14:01:24 -08:00
import { fetchPatronAccount } from '../../actions/patron';
import { expandAccountFeaturedTimeline, expandAccountTimeline } from '../../actions/timelines';
2022-01-10 14:01:24 -08:00
import StatusList from '../../components/status_list';
2022-03-21 11:09:01 -07:00
import { Card, CardBody, Spinner, Text } from '../../components/ui';
2020-03-27 13:59:38 -07:00
const makeMapStateToProps = () => {
const getStatusIds = makeGetStatusIds();
2020-03-27 13:59:38 -07:00
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());
const soapboxConfig = getSoapboxConfig(state);
const features = getFeatures(state.get('instance'));
2020-03-27 13:59:38 -07:00
let accountId = -1;
2022-03-21 11:09:01 -07:00
let account = null;
let accountUsername = username;
let accountApId = null;
if (accountFetchError) {
accountId = null;
} else {
2022-03-21 11:09:01 -07:00
account = findAccountByUsername(state, username);
accountId = account ? account.getIn(['id'], null) : -1;
accountUsername = account ? account.getIn(['acct'], '') : '';
accountApId = account ? account.get('url') : '';
}
2020-03-27 13:59:38 -07:00
const path = withReplies ? `${accountId}:with_replies` : accountId;
2020-03-27 13:59:38 -07:00
const isBlocked = state.getIn(['relationships', accountId, 'blocked_by'], false);
const unavailable = (me === accountId) ? false : (isBlocked && !features.blockersVisible);
const showPins = getSettings(state).getIn(['account_timeline', 'shows', 'pinned']) && !withReplies;
return {
accountId,
unavailable,
accountUsername,
accountApId,
2021-09-09 09:53:19 -07:00
isBlocked,
2022-03-21 11:09:01 -07:00
account,
isAccount: !!state.getIn(['accounts', accountId]),
statusIds: getStatusIds(state, { type: `account:${path}`, prefix: 'account_timeline' }),
featuredStatusIds: showPins ? getStatusIds(state, { type: `account:${accountId}:pinned`, prefix: 'account_timeline' }) : ImmutableOrderedSet(),
isLoading: state.getIn(['timelines', `account:${path}`, 'isLoading']),
hasMore: state.getIn(['timelines', `account:${path}`, 'hasMore']),
me,
patronEnabled: soapboxConfig.getIn(['extensions', 'patron', 'enabled']),
};
2020-03-27 13:59:38 -07:00
};
return mapStateToProps;
2020-03-27 13:59:38 -07:00
};
export default @connect(makeMapStateToProps)
2020-03-27 13:59:38 -07:00
class AccountTimeline extends ImmutablePureComponent {
static propTypes = {
params: PropTypes.object.isRequired,
dispatch: PropTypes.func.isRequired,
statusIds: ImmutablePropTypes.orderedSet,
featuredStatusIds: ImmutablePropTypes.orderedSet,
2020-03-27 13:59:38 -07:00
isLoading: PropTypes.bool,
hasMore: PropTypes.bool,
withReplies: PropTypes.bool,
isAccount: PropTypes.bool,
unavailable: PropTypes.bool,
};
componentDidMount() {
2022-05-14 10:01:21 -07:00
const { params: { username }, accountId, accountApId, withReplies, patronEnabled } = this.props;
2020-03-27 13:59:38 -07:00
if (accountId && accountId !== -1) {
this.props.dispatch(fetchAccount(accountId));
if (!withReplies) {
this.props.dispatch(expandAccountFeaturedTimeline(accountId));
}
2020-07-04 19:25:43 -07:00
if (patronEnabled && accountApId) {
this.props.dispatch(fetchPatronAccount(accountApId));
}
2020-03-27 13:59:38 -07:00
this.props.dispatch(expandAccountTimeline(accountId, { withReplies }));
2020-04-14 11:44:40 -07:00
} else {
2020-03-27 13:59:38 -07:00
this.props.dispatch(fetchAccountByUsername(username));
}
}
componentDidUpdate(prevProps) {
2022-05-14 10:01:21 -07:00
const { params: { username }, accountId, withReplies, accountApId, patronEnabled } = this.props;
if (accountId && (accountId !== -1) && (accountId !== prevProps.accountId) || withReplies !== prevProps.withReplies) {
this.props.dispatch(fetchAccount(accountId));
if (!withReplies) {
this.props.dispatch(expandAccountFeaturedTimeline(accountId));
2020-03-27 13:59:38 -07:00
}
2020-07-04 19:25:43 -07:00
if (patronEnabled && accountApId) {
this.props.dispatch(fetchPatronAccount(accountApId));
}
this.props.dispatch(expandAccountTimeline(accountId, { withReplies }));
} else if (username && (username !== prevProps.params.username)) {
this.props.dispatch(fetchAccountByUsername(username));
2020-03-27 13:59:38 -07:00
}
}
handleLoadMore = maxId => {
if (this.props.accountId && this.props.accountId !== -1) {
this.props.dispatch(expandAccountTimeline(this.props.accountId, { maxId, withReplies: this.props.withReplies }));
}
}
render() {
2021-09-09 09:53:19 -07:00
const { statusIds, featuredStatusIds, isLoading, hasMore, isBlocked, isAccount, accountId, unavailable, accountUsername } = this.props;
2020-03-27 13:59:38 -07:00
if (!isAccount && accountId !== -1) {
return (
2022-03-21 11:09:01 -07:00
<MissingIndicator nested />
2020-03-27 13:59:38 -07:00
);
}
2020-04-14 13:45:38 -07:00
if (accountId === -1 || (!statusIds && isLoading)) {
2020-03-27 13:59:38 -07:00
return (
2022-03-21 11:09:01 -07:00
<Spinner />
2020-03-27 13:59:38 -07:00
);
}
if (unavailable) {
return (
2022-03-21 11:09:01 -07:00
<Card>
<CardBody>
<Text align='center'>
{isBlocked ? (
<FormattedMessage id='empty_column.account_blocked' defaultMessage='You are blocked by @{accountUsername}.' values={{ accountUsername: accountUsername }} />
) : (
<FormattedMessage id='empty_column.account_unavailable' defaultMessage='Profile unavailable' />
)}
</Text>
</CardBody>
</Card>
2020-03-27 13:59:38 -07:00
);
}
return (
2022-03-21 11:09:01 -07:00
<StatusList
scrollKey='account_timeline'
statusIds={statusIds}
featuredStatusIds={featuredStatusIds}
isLoading={isLoading}
hasMore={hasMore}
onLoadMore={this.handleLoadMore}
emptyMessage={<FormattedMessage id='empty_column.account_timeline' defaultMessage='No posts here!' />}
/>
2020-03-27 13:59:38 -07:00
);
}
}