Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Message input not focusing after enable #408

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/components/chat/ui/ChatInput.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { css, cx } from '@linaria/core';
import { useRef, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import useSendbirdStateContext from '@uikit/hooks/useSendbirdStateContext';
import MessageInputWrapperView from '@uikit/modules/GroupChannel/components/MessageInputWrapper/MessageInputWrapperView';

import { themedColors } from '../../../foundation/colors/css';
import { useBlockWhileBotResponding } from '../../../hooks/useBlockWhileBotResponding';
import { usePrevious } from '../../../hooks/usePrevious';
import { isIOSMobile } from '../../../utils';
import { AlertModal } from '../../ui/AlertModal';
import { useChatContext } from '../context/ChatProvider';
Expand All @@ -15,13 +16,24 @@ export const ChatInput = () => {
const { channel, botUser, dataSource, handlers } = useChatContext();

const ref = useRef<HTMLDivElement>(null);

const [limitError, setLimitError] = useState(false);

const { config } = useSendbirdStateContext();
const isMessageInputDisabled = useBlockWhileBotResponding({
lastMessage: dataSource.messages[dataSource.messages.length - 1],
botUser,
});
const prevIsMessageInputDisabled = usePrevious(isMessageInputDisabled);

// Focus the input only when isMessageInputDisabled changes from true to false
useEffect(() => {
if (prevIsMessageInputDisabled === true && !isMessageInputDisabled) {
if (ref.current) {
ref.current.focus();
}
}
}, [isMessageInputDisabled, prevIsMessageInputDisabled]);

return (
<div className={cx(container, isIOSMobile && iosMobileContainer)}>
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/usePrevious.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect, useRef } from 'react';

export function usePrevious(value: any) {
const ref = useRef();

useEffect(() => {
ref.current = value;
});

return ref.current; // Return the previous value
}
Loading