// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import React, {Component, PropTypes} from 'react'; import { ListView, StyleSheet, View } from 'react-native'; import {General, Posts} from 'mattermost-redux/constants'; import {addDatesToPostList} from 'mattermost-redux/utils/post_utils'; import ChannelIntro from 'app/components/channel_intro'; import Post from 'app/components/post'; import DateHeader from './date_header'; import LoadMorePosts from './load_more_posts'; import NewMessagesDivider from './new_messages_divider'; const LOAD_MORE_POSTS = 'load-more-posts'; export default class PostList extends Component { static propTypes = { channel: PropTypes.object, channelIsLoading: PropTypes.bool.isRequired, posts: PropTypes.array.isRequired, theme: PropTypes.object.isRequired, loadMore: PropTypes.func, isLoadingMore: PropTypes.bool, showLoadMore: PropTypes.bool, onPostPress: PropTypes.func, renderReplies: PropTypes.bool, indicateNewMessages: PropTypes.bool, currentUserId: PropTypes.string, lastViewedAt: PropTypes.number }; constructor(props) { super(props); this.state = { posts: this.getPostsWithDates(props), dataSource: new ListView.DataSource({ rowHasChanged: (a, b) => a !== b }) }; } componentWillReceiveProps(nextProps) { if (nextProps.posts !== this.props.posts) { const posts = this.getPostsWithDates(nextProps); this.setState({posts}); } } getPostsWithDates(props) { const {posts, indicateNewMessages, currentUserId, lastViewedAt} = props; return addDatesToPostList(posts, {indicateNewMessages, currentUserId, lastViewedAt}); } getPostsWithLoadMore() { const {showLoadMore} = this.props; const {posts} = this.state; if (showLoadMore) { return [...posts, LOAD_MORE_POSTS]; } return posts; } loadMore = () => { const {loadMore} = this.props; if (typeof loadMore === 'function') { loadMore(); } }; renderChannelIntro = () => { const {channel, channelIsLoading, posts} = this.props; if (channel) { const firstPostHasRendered = channel.total_msg_count ? posts.length > 0 : true; const messageCount = channel.total_msg_count - posts.length; if (channelIsLoading || !firstPostHasRendered || messageCount > Posts.POST_CHUNK_SIZE) { return null; } return ( ); } return null; }; renderRow = (row) => { if (row instanceof Date) { return this.renderDateHeader(row); } if (row === General.START_OF_NEW_MESSAGES) { return ( ); } if (row === LOAD_MORE_POSTS) { return ( ); } return this.renderPost(row); }; renderDateHeader = (date) => { return ( ); }; renderPost = (post) => { return ( ); }; render() { return ( ); } } const style = StyleSheet.create({ container: { transform: [{rotate: '180deg'}] }, row: { transform: [{rotate: '180deg'}] } });