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

212 lines
7.6 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 ColumnSettingsContainer from './containers/column_settings_container';
2020-03-27 13:59:38 -07:00
import { NavLink } from 'react-router-dom';
import { getSettings } from 'soapbox/actions/settings';
import { getSoapboxConfig } from 'soapbox/actions/soapbox';
2022-01-10 14:01:24 -08:00
import Column from 'soapbox/components/column';
import Icon from 'soapbox/components/icon';
import MissingIndicator from 'soapbox/components/missing_indicator';
import SubNavigation from 'soapbox/components/sub_navigation';
import { makeGetStatusIds, findAccountByUsername } from 'soapbox/selectors';
import { fetchAccount, fetchAccountByUsername } from '../../actions/accounts';
2022-01-10 14:01:24 -08:00
import { fetchAccountIdentityProofs } from '../../actions/identity_proofs';
import { fetchPatronAccount } from '../../actions/patron';
import { expandAccountFeaturedTimeline, expandAccountTimeline } from '../../actions/timelines';
2022-01-10 14:01:24 -08:00
import LoadingIndicator from '../../components/loading_indicator';
import StatusList from '../../components/status_list';
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);
2020-03-27 13:59:38 -07:00
let accountId = -1;
let accountUsername = username;
let accountApId = null;
if (accountFetchError) {
accountId = null;
} else {
const 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;
const showPins = getSettings(state).getIn(['account_timeline', 'shows', 'pinned']) && !withReplies;
return {
accountId,
unavailable,
accountUsername,
accountApId,
2021-09-09 09:53:19 -07:00
isBlocked,
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,
};
state = {
collapsed: true,
animating: false,
}
componentDidMount() {
2020-07-04 19:25:43 -07:00
const { params: { username }, accountId, accountApId, withReplies, me, patronEnabled } = this.props;
2020-03-27 13:59:38 -07:00
if (accountId && accountId !== -1) {
this.props.dispatch(fetchAccount(accountId));
if (me) this.props.dispatch(fetchAccountIdentityProofs(accountId));
2020-03-27 13:59:38 -07:00
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) {
const { params: { username }, me, accountId, withReplies, accountApId, patronEnabled } = this.props;
if (accountId && (accountId !== -1) && (accountId !== prevProps.accountId) || withReplies !== prevProps.withReplies) {
this.props.dispatch(fetchAccount(accountId));
if (me) this.props.dispatch(fetchAccountIdentityProofs(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 }));
}
}
handleToggleClick = (e) => {
e.stopPropagation();
this.setState({ collapsed: !this.state.collapsed, animating: true });
}
handleTransitionEnd = () => {
this.setState({ animating: false });
}
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 (
<Column>
<MissingIndicator />
</Column>
);
}
2020-04-14 13:45:38 -07:00
if (accountId === -1 || (!statusIds && isLoading)) {
2020-03-27 13:59:38 -07:00
return (
<Column>
<LoadingIndicator />
</Column>
);
}
if (unavailable) {
return (
<Column>
<div className='empty-column-indicator'>
2020-11-02 07:25:21 -08:00
{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' />}
2020-03-27 13:59:38 -07:00
</div>
</Column>
);
}
return (
2021-10-19 13:13:04 -07:00
<Column className='account-timeline' transparent>
<SubNavigation message={`@${accountUsername}`} /*settings={ColumnSettingsContainer}*/ />
2020-03-27 13:59:38 -07:00
<div className='account__section-headline'>
<NavLink exact to={`/@${accountUsername}`}>
<FormattedMessage id='account.posts' defaultMessage='Posts' />
</NavLink>
<NavLink exact to={`/@${accountUsername}/with_replies`}>
<FormattedMessage id='account.posts_with_replies' defaultMessage='Posts and replies' />
</NavLink>
<NavLink exact to={`/@${accountUsername}/media`}>
<FormattedMessage id='account.media' defaultMessage='Media' />
</NavLink>
<div className='column-header__buttons'>
<button onClick={this.handleToggleClick}>
<Icon id='sliders' />
</button>
</div>
</div>
2020-03-27 13:59:38 -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!' />}
/>
</Column>
);
}
}