|
1 | | -import React, { PureComponent } from 'react'; |
| 1 | +import React, { useContext, useEffect, useState } from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
3 | 3 |
|
4 | | -import { withTranslationContext } from '../../context'; |
5 | | - |
6 | | -class ChannelPreview extends PureComponent { |
7 | | - constructor(props) { |
8 | | - super(props); |
9 | | - |
10 | | - this.state = { |
11 | | - unread: 0, |
12 | | - lastMessage: {}, |
13 | | - lastRead: new Date(), |
| 4 | +import { ChatContext } from '../../context'; |
| 5 | +import { useLatestMessagePreview } from './hooks/useLatestMessagePreview'; |
| 6 | + |
| 7 | +/** |
| 8 | + * NOTE: We created an inner component which only receives `client` from the ChatContext. This |
| 9 | + * practice will prevent unnecessary renders of the component when items in ChatContext change |
| 10 | + */ |
| 11 | +const ChannelPreviewWithContext = React.memo((props) => { |
| 12 | + const { channel, client } = props; |
| 13 | + const [lastMessage, setLastMessage] = useState({}); |
| 14 | + const [unread, setUnread] = useState(channel.countUnread()); |
| 15 | + const latestMessage = useLatestMessagePreview(channel, lastMessage); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + const handleNewMessageEvent = (e) => { |
| 19 | + setLastMessage(e.message); |
| 20 | + setUnread(channel.countUnread()); |
14 | 21 | }; |
15 | | - } |
16 | | - |
17 | | - static propTypes = { |
18 | | - channel: PropTypes.object.isRequired, |
19 | | - client: PropTypes.object.isRequired, |
20 | | - setActiveChannel: PropTypes.func, |
21 | | - Preview: PropTypes.oneOfType([PropTypes.node, PropTypes.elementType]), |
22 | | - }; |
23 | | - |
24 | | - static defaultProps = { |
25 | | - // Preview: ChannelPreviewCountOnly, |
26 | | - }; |
27 | | - |
28 | | - componentDidMount() { |
29 | | - // listen to change... |
30 | | - const channel = this.props.channel; |
31 | | - this.setState({ unread: channel.countUnread() }); |
32 | | - channel.on('message.new', this.handleNewMessageEvent); |
33 | | - channel.on('message.read', this.handleReadEvent); |
34 | | - } |
35 | | - |
36 | | - componentWillUnmount() { |
37 | | - const channel = this.props.channel; |
38 | | - channel.off('message.new', this.handleNewMessageEvent); |
39 | | - channel.off('message.read', this.handleReadEvent); |
40 | | - } |
41 | 22 |
|
42 | | - handleReadEvent = (event) => { |
43 | | - if (event.user.id === this.props.client.userID) { |
44 | | - this.setState({ unread: this.props.channel.countUnread() }); |
45 | | - } |
46 | | - }; |
| 23 | + channel.on('message.new', handleNewMessageEvent); |
47 | 24 |
|
48 | | - handleNewMessageEvent = (event) => { |
49 | | - const channel = this.props.channel; |
50 | | - this.setState({ |
51 | | - lastMessage: event.message, |
52 | | - unread: channel.countUnread(), |
53 | | - }); |
54 | | - }; |
55 | | - |
56 | | - getLatestMessage = () => { |
57 | | - const { channel, t, tDateTimeParser } = this.props; |
58 | | - const message = channel.state.messages[channel.state.messages.length - 1]; |
| 25 | + return () => { |
| 26 | + channel.off('message.new', handleNewMessageEvent); |
| 27 | + }; |
| 28 | + }, []); |
59 | 29 |
|
60 | | - const latestMessage = { |
61 | | - text: '', |
62 | | - created_at: '', |
63 | | - messageObject: { ...message }, |
| 30 | + useEffect(() => { |
| 31 | + const handleReadEvent = (e) => { |
| 32 | + if (e.user.id === client.userID) { |
| 33 | + setUnread(0); |
| 34 | + } |
64 | 35 | }; |
65 | 36 |
|
66 | | - if (!message) { |
67 | | - latestMessage.text = t('Nothing yet...'); |
68 | | - return latestMessage; |
69 | | - } |
70 | | - if (message.deleted_at) { |
71 | | - latestMessage.text = t('Message deleted'); |
72 | | - return latestMessage; |
73 | | - } |
| 37 | + channel.on('message.read', handleReadEvent); |
74 | 38 |
|
75 | | - if (message.text) { |
76 | | - latestMessage.text = message.text; |
77 | | - } else { |
78 | | - if (message.command) { |
79 | | - latestMessage.text = '/' + message.command; |
80 | | - } else if (message.attachments.length) { |
81 | | - latestMessage.text = t('🏙 Attachment...'); |
82 | | - } else { |
83 | | - latestMessage.text = t('Empty message...'); |
84 | | - } |
85 | | - } |
| 39 | + return () => { |
| 40 | + channel.off('message.read', handleReadEvent); |
| 41 | + }; |
| 42 | + }, []); |
86 | 43 |
|
87 | | - if (tDateTimeParser(message.created_at).isSame(new Date(), 'day')) |
88 | | - latestMessage.created_at = tDateTimeParser(message.created_at).format( |
89 | | - 'LT', |
90 | | - ); |
91 | | - else { |
92 | | - latestMessage.created_at = tDateTimeParser(message.created_at).format( |
93 | | - 'L', |
94 | | - ); |
95 | | - } |
| 44 | + const { Preview } = props; |
| 45 | + return <Preview {...props} latestMessage={latestMessage} unread={unread} />; |
| 46 | +}); |
96 | 47 |
|
97 | | - return latestMessage; |
98 | | - }; |
| 48 | +const ChannelPreview = (props) => { |
| 49 | + const { client } = useContext(ChatContext); |
| 50 | + return <ChannelPreviewWithContext {...props} {...{ client }} />; |
| 51 | +}; |
99 | 52 |
|
100 | | - render() { |
101 | | - const props = { ...this.state, ...this.props }; |
102 | | - const { Preview } = this.props; |
103 | | - return <Preview {...props} latestMessage={this.getLatestMessage()} />; |
104 | | - } |
105 | | -} |
| 53 | +ChannelPreview.propTypes = { |
| 54 | + channel: PropTypes.object.isRequired, |
| 55 | + client: PropTypes.object.isRequired, |
| 56 | + setActiveChannel: PropTypes.func, |
| 57 | + Preview: PropTypes.oneOfType([PropTypes.node, PropTypes.elementType]), |
| 58 | +}; |
106 | 59 |
|
107 | | -export default withTranslationContext(ChannelPreview); |
| 60 | +export default ChannelPreview; |
0 commit comments