2025-04-27 18:28:28 -04:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
|
|
|
import { logger } from '~/utils';
|
|
|
|
|
|
|
|
|
|
export default function useFocusChatEffect(textAreaRef: React.RefObject<HTMLTextAreaElement>) {
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (textAreaRef?.current && location.state?.focusChat) {
|
|
|
|
|
logger.log(
|
|
|
|
|
'conversation',
|
|
|
|
|
`Focusing textarea on location state change: ${location.pathname}`,
|
|
|
|
|
);
|
2025-05-13 08:24:40 -04:00
|
|
|
|
2025-05-05 15:23:38 +02:00
|
|
|
/** Check if the device is not a touchscreen */
|
|
|
|
|
if (!window.matchMedia?.('(pointer: coarse)').matches) {
|
|
|
|
|
textAreaRef.current?.focus();
|
|
|
|
|
}
|
2025-05-13 08:24:40 -04:00
|
|
|
|
|
|
|
|
navigate(`${location.pathname}${window.location.search ?? ''}`, {
|
|
|
|
|
replace: true,
|
|
|
|
|
state: {},
|
|
|
|
|
});
|
2025-04-27 18:28:28 -04:00
|
|
|
}
|
2025-05-13 08:24:40 -04:00
|
|
|
}, [navigate, textAreaRef, location.pathname, location.state?.focusChat]);
|
2025-04-27 18:28:28 -04:00
|
|
|
}
|