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