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-10-15 15:12:32 +03:00
|
|
|
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;
|
2025-05-05 15:23:38 +02:00
|
|
|
}
|
2025-05-13 08:24:40 -04:00
|
|
|
|
2025-10-15 15:12:32 +03:00
|
|
|
textAreaRef.current?.focus();
|
|
|
|
|
|
|
|
|
|
navigate(path, {
|
2025-05-13 08:24:40 -04:00
|
|
|
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
|
|
|
}
|