mattermost-mobile/app/screens/saved_posts/saved_posts.js
Elias Nahum 9f238d5ef4
Post List & post components refactored (#5409)
* Update transform to make Android's post list scroll smooth

* set start since metric when appStarted is false

* Refactor Formatted components

* Downgrade RNN to 7.13.0 & patch XCDYouTube to allow video playback

* Refactor Post list and all related components

* review suggestion rename hour12 to isMilitaryTime

* feedback review use aliases

* feedback review deconstruct actions in markdown_link

* feedback review simplify if/else statement in combined_used_activity

* Simplify if statement for consecutive posts

* Specify npm version to build iOS on CI

* Refactor network_indicator

* render Icon in file gallery with transparent background

* Increase timeout to scroll to bottom when posting a new message

* fix: scroll when tapping on the new messages bar

* fix: dismiss all modals

* fix navigation tests

* Handle dismissAllModals for iOS to prevent blank screens

* Prevent modal from dismissing when showing the thread screen in the stack

* Update app/components/image_viewport.tsx

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* Update app/utils/post.ts

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>

* fix: rename selector and prop

* Fix XCDYouTube patch

* Fix posting from a thread in the right channel

* do not render reply bar on the thread screen

* close previous permalink before showing a new one

* move XCDYouTube patch to ios/patches folder

* closePermalink directly instead of using an onClose prop

Co-authored-by: Miguel Alatzar <migbot@users.noreply.github.com>
Co-authored-by: Miguel Alatzar <this.migbot@gmail.com>
2021-06-03 11:12:15 -07:00

204 lines
5.7 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 {intlShape} from 'react-intl';
import {
DeviceEventEmitter,
FlatList,
StyleSheet,
View,
} from 'react-native';
import {Navigation} from 'react-native-navigation';
import {SafeAreaView} from 'react-native-safe-area-context';
import {dismissModal} from '@actions/navigation';
import ChannelLoader from '@components/channel_loader';
import DateSeparator from '@components/post_list/date_separator';
import FailedNetworkAction from '@components/failed_network_action';
import NoResults from '@components/no_results';
import PostSeparator from '@components/post_separator';
import StatusBar from '@components/status_bar';
import {isDateLine, getDateForDateLine} from '@mm-redux/utils/post_list';
import SearchResultPost from '@screens/search/search_result_post';
import ChannelDisplayName from '@screens/search/channel_display_name';
export default class SavedPosts extends PureComponent {
static propTypes = {
actions: PropTypes.shape({
clearSearch: PropTypes.func.isRequired,
getFlaggedPosts: PropTypes.func.isRequired,
}).isRequired,
postIds: PropTypes.array,
theme: PropTypes.object.isRequired,
};
static defaultProps = {
postIds: [],
};
static contextTypes = {
intl: intlShape.isRequired,
};
constructor(props) {
super(props);
props.actions.clearSearch();
this.state = {
didFail: false,
isLoading: false,
};
}
getFlaggedPosts = async () => {
const {actions} = this.props;
this.setState({isLoading: true});
const {error} = await actions.getFlaggedPosts();
this.setState({
isLoading: false,
didFail: Boolean(error),
});
}
componentDidMount() {
this.navigationEventListener = Navigation.events().bindComponent(this);
this.getFlaggedPosts();
}
navigationButtonPressed({buttonId}) {
if (buttonId === 'close-settings') {
dismissModal();
}
}
setListRef = (ref) => {
this.listRef = ref;
}
keyExtractor = (item) => item;
onViewableItemsChanged = ({viewableItems}) => {
if (!viewableItems.length) {
return;
}
const viewableItemsMap = viewableItems.reduce((acc, {item, isViewable}) => {
if (isViewable) {
acc[item] = true;
}
return acc;
}, {});
DeviceEventEmitter.emit('scrolled', viewableItemsMap);
};
renderEmpty = () => {
const {formatMessage} = this.context.intl;
const {theme} = this.props;
return (
<NoResults
description={formatMessage({
id: 'mobile.flagged_posts.empty_description',
defaultMessage: 'Saved messages are only visible to you. Mark messages for follow-up or save something for later by long-pressing a message and choosing Save from the menu.',
})}
iconName='bookmark-outline'
title={formatMessage({id: 'mobile.flagged_posts.empty_title', defaultMessage: 'No Saved messages yet'})}
theme={theme}
/>
);
};
renderPost = ({item, index}) => {
const {postIds, theme} = this.props;
if (isDateLine(item)) {
return (
<DateSeparator
date={getDateForDateLine(item)}
theme={theme}
/>
);
}
let separator;
const nextPost = postIds[index + 1];
if (nextPost && !isDateLine(nextPost)) {
separator = <PostSeparator theme={theme}/>;
}
return (
<View>
<ChannelDisplayName postId={item}/>
<SearchResultPost
postId={item}
highlightPinnedOrFlagged={false}
skipFlaggedHeader={true}
skipPinnedHeader={true}
theme={theme}
/>
{separator}
</View>
);
};
retry = () => {
this.getFlaggedPosts();
};
render() {
const {postIds, theme} = this.props;
const {didFail, isLoading} = this.state;
let component;
if (didFail) {
component = (
<FailedNetworkAction
onRetry={this.retry}
theme={theme}
/>
);
} else if (isLoading) {
component = (
<ChannelLoader channelIsLoading={true}/>
);
} else if (postIds.length) {
component = (
<FlatList
ref={this.setListRef}
contentContainerStyle={style.sectionList}
data={postIds}
keyExtractor={this.keyExtractor}
keyboardShouldPersistTaps='always'
keyboardDismissMode='interactive'
removeClippedSubviews={true}
renderItem={this.renderPost}
onViewableItemsChanged={this.onViewableItemsChanged}
/>
);
} else {
component = this.renderEmpty();
}
return (
<SafeAreaView
testID='saved_messages.screen'
style={style.container}
>
<StatusBar/>
{component}
</SafeAreaView>
);
}
}
const style = StyleSheet.create({
container: {
flex: 1,
},
});