From ca2aad2de022299cfbef656924d958b4b551f7ad Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 17 Oct 2022 10:50:33 -0500 Subject: [PATCH] Chats: focus textarea when navigating between chats (janky) --- .../features/chats/components/chat-composer.tsx | 2 +- .../components/chat-page/components/chat-page-main.tsx | 10 ++++++++-- .../chats/components/chat-widget/chat-window.tsx | 4 ++-- app/soapbox/features/chats/components/chat.tsx | 10 ++++++++-- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/soapbox/features/chats/components/chat-composer.tsx b/app/soapbox/features/chats/components/chat-composer.tsx index 5784396b65..2ad75e51a5 100644 --- a/app/soapbox/features/chats/components/chat-composer.tsx +++ b/app/soapbox/features/chats/components/chat-composer.tsx @@ -17,7 +17,7 @@ interface IChatComposer extends Pick(({ +const ChatComposer = React.forwardRef(({ onKeyDown, onChange, value, diff --git a/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx b/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx index a2b32723c7..eef4725c38 100644 --- a/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx +++ b/app/soapbox/features/chats/components/chat-page/components/chat-page-main.tsx @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useRef } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { blockAccount } from 'soapbox/actions/accounts'; @@ -32,6 +32,8 @@ const ChatPageMain = () => { const intl = useIntl(); const account = useOwnAccount(); + const inputRef = useRef(null); + const { chat, setChat } = useChatContext(); const { isSilenced, handleSilence, fetchChatSilence } = useChatSilence(chat); const { deleteChat } = useChatActions(chat?.id as string); @@ -162,7 +164,11 @@ const ChatPageMain = () => {
- +
); diff --git a/app/soapbox/features/chats/components/chat-widget/chat-window.tsx b/app/soapbox/features/chats/components/chat-widget/chat-window.tsx index f3bc845769..3510011d41 100644 --- a/app/soapbox/features/chats/components/chat-widget/chat-window.tsx +++ b/app/soapbox/features/chats/components/chat-widget/chat-window.tsx @@ -26,7 +26,7 @@ const LinkWrapper = ({ enabled, to, children }: { enabled: boolean, to: string, const ChatWindow = () => { const { chat, setChat, isOpen, isEditing, needsAcceptance, setEditing, setSearching, toggleChatPane } = useChatContext(); - const inputRef = useRef(); + const inputRef = useRef(null); const closeChat = () => setChat(null); @@ -93,7 +93,7 @@ const ChatWindow = () => { /> - + ); diff --git a/app/soapbox/features/chats/components/chat.tsx b/app/soapbox/features/chats/components/chat.tsx index 686c840fd6..6933b98284 100644 --- a/app/soapbox/features/chats/components/chat.tsx +++ b/app/soapbox/features/chats/components/chat.tsx @@ -1,6 +1,6 @@ import { useMutation } from '@tanstack/react-query'; import classNames from 'clsx'; -import React, { MutableRefObject, useState } from 'react'; +import React, { MutableRefObject, useEffect, useState } from 'react'; import { useIntl } from 'react-intl'; import { uploadMedia } from 'soapbox/actions/media'; @@ -20,7 +20,7 @@ const fileKeyGen = (): number => Math.floor((Math.random() * 0x10000)); interface ChatInterface { chat: IChat, autosize?: boolean, - inputRef?: MutableRefObject, + inputRef?: MutableRefObject, className?: string, } @@ -203,6 +203,12 @@ const Chat: React.FC = ({ chat, autosize, inputRef, className }) // ); }; + useEffect(() => { + if (inputRef?.current) { + inputRef.current.focus(); + } + }, [chat.id, inputRef?.current]); + return (