|
| 1 | +/* @flow strict-local */ |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { View, Platform } from 'react-native'; |
| 5 | +import { WebView } from 'react-native-webview'; |
| 6 | +import type { NavigationScreenProp } from 'react-navigation'; |
| 7 | +import type { Dispatch, Auth, ThemeName, UserOrBot } from '../types'; |
| 8 | +import SpinningProgress from '../common/SpinningProgress'; |
| 9 | +import type { MessageSnapshot } from '../api/modelTypes'; |
| 10 | +import { connect } from '../react-redux'; |
| 11 | +import { getAuth, getAllUsersById } from '../selectors'; |
| 12 | +import { Screen } from '../common'; |
| 13 | +import * as api from '../api'; |
| 14 | +import editHistoryHtml from '../webview/html/editHistoryHtml'; |
| 15 | + |
| 16 | +type SelectorProps = {| |
| 17 | + auth: Auth, |
| 18 | + usersById: Map<number, UserOrBot>, |
| 19 | +|}; |
| 20 | + |
| 21 | +type Props = $ReadOnly<{| |
| 22 | + navigation: NavigationScreenProp<{ params: {| messageId: number, theme: ThemeName |} }>, |
| 23 | + |
| 24 | + dispatch: Dispatch, |
| 25 | + ...SelectorProps, |
| 26 | +|}>; |
| 27 | + |
| 28 | +type State = $ReadOnly<{| |
| 29 | + messageHistory: MessageSnapshot[] | null, |
| 30 | +|}>; |
| 31 | + |
| 32 | +class EditHistory extends React.Component<Props, State> { |
| 33 | + state = { |
| 34 | + messageHistory: null, |
| 35 | + }; |
| 36 | + |
| 37 | + async componentDidMount() { |
| 38 | + const { auth, navigation } = this.props; |
| 39 | + |
| 40 | + this.setState({ |
| 41 | + messageHistory: (await api.getMessageHistory(auth, navigation.state.params.messageId)) |
| 42 | + .message_history, |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + render() { |
| 47 | + const { messageHistory } = this.state; |
| 48 | + |
| 49 | + if (messageHistory === null) { |
| 50 | + return ( |
| 51 | + <Screen title="Edit History"> |
| 52 | + <View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}> |
| 53 | + <SpinningProgress color="white" size={48} /> |
| 54 | + </View> |
| 55 | + </Screen> |
| 56 | + ); |
| 57 | + } |
| 58 | + const { usersById, auth } = this.props; |
| 59 | + const theme = this.props.navigation.state.params.theme; |
| 60 | + |
| 61 | + // For the logic behind these, see comments in MessageList component's render method. |
| 62 | + const assetsPath = Platform.OS === 'ios' ? './webview' : 'file:///android_asset/webview'; |
| 63 | + const baseUrl = `${assetsPath}/editHistory.html`; |
| 64 | + |
| 65 | + const html: string = editHistoryHtml(messageHistory, theme, usersById, auth); |
| 66 | + |
| 67 | + return ( |
| 68 | + <Screen title="Edit History"> |
| 69 | + <WebView |
| 70 | + source={{ baseUrl, html }} |
| 71 | + originWhitelist={['file://']} |
| 72 | + style={{ backgroundColor: 'transparent' }} |
| 73 | + onShouldStartLoadWithRequest={(_: string) => false} // Don't load any webview requests. |
| 74 | + onError={(msg: mixed) => { |
| 75 | + // eslint-disable-next-line no-console |
| 76 | + console.error(msg); |
| 77 | + }} |
| 78 | + /> |
| 79 | + </Screen> |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export default connect<SelectorProps, _, _>((state, props) => ({ |
| 85 | + auth: getAuth(state), |
| 86 | + usersById: getAllUsersById(state), |
| 87 | +}))(EditHistory); |
0 commit comments