2026-04-12 13:14:18 +02:00
|
|
|
import { useCallback, useEffect } from 'react'
|
|
|
|
|
|
|
|
|
|
interface UseAiPanelFocusArgs {
|
|
|
|
|
inputRef: React.RefObject<HTMLDivElement | null>
|
|
|
|
|
panelRef: React.RefObject<HTMLElement | null>
|
2026-04-20 10:28:48 +02:00
|
|
|
hasMessages: boolean
|
2026-04-12 13:14:18 +02:00
|
|
|
isActive: boolean
|
|
|
|
|
onClose: () => void
|
2026-05-26 16:26:51 +02:00
|
|
|
enabled?: boolean
|
2026-04-12 13:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-20 10:28:48 +02:00
|
|
|
function focusPreferredElement(
|
|
|
|
|
panelRef: React.RefObject<HTMLElement | null>,
|
|
|
|
|
inputRef: React.RefObject<HTMLDivElement | null>,
|
|
|
|
|
shouldFocusPanel: boolean,
|
|
|
|
|
) {
|
2026-05-06 11:35:30 +02:00
|
|
|
if (panelRef.current?.contains(document.activeElement)) return
|
|
|
|
|
|
2026-04-20 10:28:48 +02:00
|
|
|
if (shouldFocusPanel) {
|
|
|
|
|
panelRef.current?.focus()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputRef.current?.focus()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldHandleEscape(
|
|
|
|
|
event: KeyboardEvent,
|
|
|
|
|
panelRef: React.RefObject<HTMLElement | null>,
|
|
|
|
|
): boolean {
|
|
|
|
|
return event.key === 'Escape' && !!panelRef.current?.contains(document.activeElement)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-12 13:14:18 +02:00
|
|
|
export function useAiPanelFocus({
|
|
|
|
|
inputRef,
|
|
|
|
|
panelRef,
|
2026-04-20 10:28:48 +02:00
|
|
|
hasMessages,
|
2026-04-12 13:14:18 +02:00
|
|
|
isActive,
|
|
|
|
|
onClose,
|
2026-05-26 16:26:51 +02:00
|
|
|
enabled = true,
|
2026-04-12 13:14:18 +02:00
|
|
|
}: UseAiPanelFocusArgs) {
|
2026-04-20 10:28:48 +02:00
|
|
|
const shouldFocusPanel = hasMessages || isActive
|
|
|
|
|
|
2026-04-12 13:14:18 +02:00
|
|
|
useEffect(() => {
|
2026-05-26 16:26:51 +02:00
|
|
|
if (!enabled) return
|
|
|
|
|
|
2026-04-20 10:28:48 +02:00
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
focusPreferredElement(panelRef, inputRef, shouldFocusPanel)
|
|
|
|
|
}, 0)
|
2026-04-12 13:14:18 +02:00
|
|
|
return () => clearTimeout(timer)
|
2026-05-26 16:26:51 +02:00
|
|
|
}, [enabled, inputRef, panelRef, shouldFocusPanel])
|
2026-04-12 13:14:18 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-26 16:26:51 +02:00
|
|
|
if (!enabled) return
|
2026-04-20 10:28:48 +02:00
|
|
|
focusPreferredElement(panelRef, inputRef, shouldFocusPanel)
|
2026-05-26 16:26:51 +02:00
|
|
|
}, [enabled, inputRef, panelRef, shouldFocusPanel])
|
2026-04-12 13:14:18 +02:00
|
|
|
|
|
|
|
|
const handleEscape = useCallback((event: KeyboardEvent) => {
|
2026-04-20 10:28:48 +02:00
|
|
|
if (!shouldHandleEscape(event, panelRef)) return
|
2026-04-12 13:14:18 +02:00
|
|
|
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
onClose()
|
|
|
|
|
}, [onClose, panelRef])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-26 16:26:51 +02:00
|
|
|
if (!enabled) return
|
2026-04-12 13:14:18 +02:00
|
|
|
window.addEventListener('keydown', handleEscape)
|
|
|
|
|
return () => window.removeEventListener('keydown', handleEscape)
|
2026-05-26 16:26:51 +02:00
|
|
|
}, [enabled, handleEscape])
|
2026-04-12 13:14:18 +02:00
|
|
|
}
|