From fe9984dd9c91d3c9b301dc8e85c12a7285d0cb6b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 17 Jun 2022 13:05:37 -0500 Subject: [PATCH] ChatRoom: convert to TSX --- app/soapbox/features/chats/chat_room.js | 95 ------------------------ app/soapbox/features/chats/chat_room.tsx | 68 +++++++++++++++++ 2 files changed, 68 insertions(+), 95 deletions(-) delete mode 100644 app/soapbox/features/chats/chat_room.js create mode 100644 app/soapbox/features/chats/chat_room.tsx diff --git a/app/soapbox/features/chats/chat_room.js b/app/soapbox/features/chats/chat_room.js deleted file mode 100644 index 4d9140650..000000000 --- a/app/soapbox/features/chats/chat_room.js +++ /dev/null @@ -1,95 +0,0 @@ -import { Map as ImmutableMap } from 'immutable'; -import PropTypes from 'prop-types'; -import React from 'react'; -import ImmutablePropTypes from 'react-immutable-proptypes'; -import ImmutablePureComponent from 'react-immutable-pure-component'; -import { injectIntl } from 'react-intl'; -import { connect } from 'react-redux'; - -import { fetchChat, markChatRead } from 'soapbox/actions/chats'; -import { Column } from 'soapbox/components/ui'; -import { makeGetChat } from 'soapbox/selectors'; -import { getAcct } from 'soapbox/utils/accounts'; -import { displayFqn } from 'soapbox/utils/state'; - -import ChatBox from './components/chat_box'; - -const mapStateToProps = (state, { params }) => { - const getChat = makeGetChat(); - const chat = state.getIn(['chats', 'items', params.chatId], ImmutableMap()).toJS(); - - return { - me: state.get('me'), - chat: getChat(state, chat), - displayFqn: displayFqn(state), - }; -}; - -export default @connect(mapStateToProps) -@injectIntl -class ChatRoom extends ImmutablePureComponent { - - static propTypes = { - dispatch: PropTypes.func.isRequired, - intl: PropTypes.object.isRequired, - chat: ImmutablePropTypes.map, - displayFqn: PropTypes.bool, - me: PropTypes.node, - } - - handleInputRef = (el) => { - this.inputElem = el; - this.focusInput(); - }; - - focusInput = () => { - if (!this.inputElem) return; - this.inputElem.focus(); - } - - markRead = () => { - const { dispatch, chat } = this.props; - if (!chat) return; - dispatch(markChatRead(chat.get('id'))); - } - - componentDidMount() { - const { dispatch, params } = this.props; - dispatch(fetchChat(params.chatId)); - this.markRead(); - } - - componentDidUpdate(prevProps) { - const markReadConditions = [ - () => this.props.chat, - () => this.props.chat.get('unread') > 0, - ]; - - if (markReadConditions.every(c => c())) - this.markRead(); - } - - render() { - const { chat, displayFqn } = this.props; - if (!chat) return null; - const account = chat.get('account'); - - return ( - - {/*
- - -
- @{getAcct(account, displayFqn)} -
- -
*/} - -
- ); - } - -} diff --git a/app/soapbox/features/chats/chat_room.tsx b/app/soapbox/features/chats/chat_room.tsx new file mode 100644 index 000000000..41b108e3f --- /dev/null +++ b/app/soapbox/features/chats/chat_room.tsx @@ -0,0 +1,68 @@ +import { Map as ImmutableMap } from 'immutable'; +import React, { useEffect, useRef } from 'react'; + +import { fetchChat, markChatRead } from 'soapbox/actions/chats'; +import { Column } from 'soapbox/components/ui'; +import { useAppSelector, useAppDispatch } from 'soapbox/hooks'; +import { makeGetChat } from 'soapbox/selectors'; +import { getAcct } from 'soapbox/utils/accounts'; +import { displayFqn as getDisplayFqn } from 'soapbox/utils/state'; + +import ChatBox from './components/chat_box'; + +const getChat = makeGetChat(); + +interface IChatRoom { + params: { + chatId: string, + } +} + +/** Fullscreen chat UI. */ +const ChatRoom: React.FC = ({ params }) => { + const dispatch = useAppDispatch(); + const displayFqn = useAppSelector(getDisplayFqn); + const inputElem = useRef(null); + + const chat = useAppSelector(state => { + const chat = state.chats.items.get(params.chatId, ImmutableMap()).toJS() as any; + return getChat(state, chat); + }); + + const focusInput = () => { + inputElem.current?.focus(); + }; + + const handleInputRef = (el: HTMLInputElement) => { + inputElem.current = el; + focusInput(); + }; + + const markRead = () => { + if (!chat) return; + dispatch(markChatRead(chat.id)); + }; + + useEffect(() => { + dispatch(fetchChat(params.chatId)); + markRead(); + }, [params.chatId]); + + // If this component is loaded at all, we can instantly mark new messages as read. + useEffect(() => { + markRead(); + }, [chat?.unread]); + + if (!chat) return null; + + return ( + + + + ); +}; + +export default ChatRoom;