From d2d64e4ec4c4ee4627c23a9659c4179460c193b9 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 16 Aug 2022 16:35:06 -0400 Subject: [PATCH] Refactor and auto-accept chats --- app/soapbox/contexts/chat-context.tsx | 21 +++++++- .../features/chats/components/chat-box.tsx | 12 +++-- .../chats/components/chat-pane-header.tsx | 24 ++++++--- .../features/chats/components/chat-pane.tsx | 21 +++----- .../features/chats/components/chat-window.tsx | 50 +++++++++++-------- .../features/chats/components/ui/index.ts | 1 - .../features/chats/components/ui/pane.tsx | 11 ++-- .../components/placeholder_chat.tsx | 3 +- 8 files changed, 87 insertions(+), 56 deletions(-) diff --git a/app/soapbox/contexts/chat-context.tsx b/app/soapbox/contexts/chat-context.tsx index c005d9eea..9694e5664 100644 --- a/app/soapbox/contexts/chat-context.tsx +++ b/app/soapbox/contexts/chat-context.tsx @@ -1,22 +1,39 @@ import React, { createContext, useContext, useState } from 'react'; +import { useDispatch } from 'react-redux'; + +import { toggleMainWindow } from 'soapbox/actions/chats'; +import { useSettings } from 'soapbox/hooks'; import type { IChat } from 'soapbox/queries/chats'; +type WindowState = 'open' | 'minimized'; + const ChatContext = createContext({ chat: null, + isOpen: false, }); const ChatProvider: React.FC = ({ children }) => { - const [chat, setChat] = useState(); + const dispatch = useDispatch(); + const settings = useSettings(); + + const [chat, setChat] = useState(); + const mainWindowState = settings.getIn(['chats', 'mainWindow']) as WindowState; + + const isOpen = mainWindowState === 'open'; + + const toggleChatPane = () => dispatch(toggleMainWindow()); return ( - {children} + {children} ); }; interface IChatContext { chat: IChat | null + isOpen: boolean setChat: React.Dispatch> + toggleChatPane(): void } const useChatContext = (): IChatContext => useContext(ChatContext); diff --git a/app/soapbox/features/chats/components/chat-box.tsx b/app/soapbox/features/chats/components/chat-box.tsx index 6bb9d7b29..5f86c36bd 100644 --- a/app/soapbox/features/chats/components/chat-box.tsx +++ b/app/soapbox/features/chats/components/chat-box.tsx @@ -1,6 +1,6 @@ import { useMutation } from '@tanstack/react-query'; import { OrderedSet as ImmutableOrderedSet } from 'immutable'; -import React, { useRef, useState } from 'react'; +import React, { MutableRefObject, useRef, useState } from 'react'; import { useIntl, defineMessages } from 'react-intl'; import { @@ -31,13 +31,14 @@ interface IChatBox { chat: IChat, onSetInputRef: (el: HTMLTextAreaElement) => void, autosize?: boolean, + inputRef?: MutableRefObject } /** * Chat UI with just the messages and textarea. * Reused between floating desktop chats and fullscreen/mobile chats. */ -const ChatBox: React.FC = ({ chat, onSetInputRef, autosize }) => { +const ChatBox: React.FC = ({ chat, onSetInputRef, autosize, inputRef }) => { const intl = useIntl(); const dispatch = useAppDispatch(); const chatMessageIds = useAppSelector(state => state.chat_message_lists.get(chat.id, ImmutableOrderedSet())); @@ -128,6 +129,7 @@ const ChatBox: React.FC = ({ chat, onSetInputRef, autosize }) => { }; submitMessage.mutate({ chatId: chat.id, content }); + acceptChat.mutate(); } }; @@ -253,7 +255,10 @@ const ChatBox: React.FC = ({ chat, onSetInputRef, autosize }) => {