Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1007 B
Raw Blame
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { consumeChatFocus, logger } from '~/utils';

export default function useFocusChatEffect(textAreaRef: React.RefObject<HTMLTextAreaElement>) {
  const location = useLocation();
  useEffect(() => {
    if (!textAreaRef?.current || !consumeChatFocus()) {
      return;
    }
    logger.log('conversation', `Focusing textarea on navigation: ${location.pathname}`);

    const hasCoarsePointer = window.matchMedia?.('(pointer: coarse)').matches;
    const hasHover = window.matchMedia?.('(hover: hover)').matches;

    /* Skip focusing if mobile-like: has coarse pointer OR lacks hover */
    if (hasCoarsePointer || !hasHover) {
      return;
    }

    textAreaRef.current?.focus();
    /** `location.key` changes on EVERY navigation (including same-path pushes),
     * so a pending focus request is always consumed by the navigation that
     * requested it. */
  }, [textAreaRef, location.key, location.pathname]);
}