File size: 1,153 Bytes
f0743f4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 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}`,
);
const hasCoarsePointer = window.matchMedia?.('(pointer: coarse)').matches;
const hasHover = window.matchMedia?.('(hover: hover)').matches;
const path = `${location.pathname}${window.location.search ?? ''}`;
/* Early return if mobile-like: has coarse pointer OR lacks hover */
if (hasCoarsePointer || !hasHover) {
navigate(path, {
replace: true,
state: {},
});
return;
}
textAreaRef.current?.focus();
navigate(path, {
replace: true,
state: {},
});
}
}, [navigate, textAreaRef, location.pathname, location.state?.focusChat]);
}
|