mattermost-mobile/app/screens/thread/thread_base.js
Miguel Alatzar 81dd01ba2c
[MM-21155] Fix typing animations (#4373)
* Fix typing animations

* Remove animations on unmount

* Remove obsolete rn-placeholder mocks

* Apply thread animation for Android

* No need to call scrollToInitialIndexIfNeeded

* Fix snapshot test

* Add context
2020-07-15 14:28:02 -07:00

133 lines
3.8 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {Animated, Keyboard} from 'react-native';
import {intlShape} from 'react-intl';
import {General, RequestStatus} from '@mm-redux/constants';
import Loading from 'app/components/loading';
import DeletedPost from 'app/components/deleted_post';
import {popTopScreen, mergeNavigationOptions} from 'app/actions/navigation';
import {TYPING_HEIGHT} from '@constants/post_draft';
export default class ThreadBase extends PureComponent {
static propTypes = {
actions: PropTypes.shape({
selectPost: PropTypes.func.isRequired,
}).isRequired,
componentId: PropTypes.string,
channelId: PropTypes.string.isRequired,
channelType: PropTypes.string,
displayName: PropTypes.string,
myMember: PropTypes.object.isRequired,
rootId: PropTypes.string.isRequired,
theme: PropTypes.object.isRequired,
postIds: PropTypes.array.isRequired,
registerTypingAnimation: PropTypes.func.isRequired,
channelIsArchived: PropTypes.bool,
threadLoadingStatus: PropTypes.object,
};
static defaultProps = {
postIds: [],
};
static contextTypes = {
intl: intlShape,
};
constructor(props, context) {
super(props);
const {channelType, displayName} = props;
const {formatMessage} = context.intl;
let title;
if (channelType === General.DM_CHANNEL) {
title = formatMessage({id: 'mobile.routes.thread_dm', defaultMessage: 'Direct Message Thread'});
} else {
title = formatMessage({id: 'mobile.routes.thread', defaultMessage: '{channelName} Thread'}, {channelName: displayName});
}
this.postDraft = React.createRef();
const options = {
topBar: {
title: {
text: title,
},
},
};
mergeNavigationOptions(props.componentId, options);
this.state = {
lastViewedAt: props.myMember && props.myMember.last_viewed_at,
};
this.bottomPadding = new Animated.Value(0);
}
componentDidMount() {
this.removeTypingAnimation = this.props.registerTypingAnimation(this.bottomPaddingAnimation);
}
componentWillReceiveProps(nextProps) {
if (this.props.postIds !== nextProps.postIds && !nextProps.postIds.length) {
this.close();
return;
}
if (!this.state.lastViewedAt) {
this.setState({lastViewedAt: nextProps.myMember && nextProps.myMember.last_viewed_at});
}
}
componentWillUnmount() {
this.props.actions.selectPost('');
this.removeTypingAnimation();
}
close = () => {
const {componentId} = this.props;
popTopScreen(componentId);
};
hasRootPost = () => {
return this.props.postIds.includes(this.props.rootId);
};
hideKeyboard = () => {
Keyboard.dismiss();
};
renderFooter = () => {
const {theme, threadLoadingStatus} = this.props;
if (!this.hasRootPost() && threadLoadingStatus.status !== RequestStatus.STARTED) {
return (
<DeletedPost theme={theme}/>
);
} else if (threadLoadingStatus.status === RequestStatus.STARTED) {
return (
<Loading color={theme.centerChannelColor}/>
);
}
return null;
};
bottomPaddingAnimation = (visible) => {
const [padding, duration] = visible ?
[TYPING_HEIGHT, 200] :
[0, 400];
return Animated.timing(this.bottomPadding, {
toValue: padding,
duration,
useNativeDriver: false,
});
}
}