'use client'; import { useEffect, useRef, useState } from 'react'; import type { Jurisdiction } from '@/lib/types'; const JURISDICTIONS = [ { value: '' as const, label: 'All Jurisdictions', shortLabel: 'All', dotClass: 'bg-white/35' }, { value: 'CENTRAL' as const, label: 'Central', shortLabel: 'Central', dotClass: 'bg-[#2db7a3]' }, { value: 'MAHARASHTRA' as const, label: 'Maharashtra', shortLabel: 'Maharashtra', dotClass: 'bg-[#4e7cff]' }, { value: 'UTTAR_PRADESH' as const, label: 'Uttar Pradesh', shortLabel: 'Uttar Pradesh', dotClass: 'bg-[#f4b63f]' }, { value: 'KARNATAKA' as const, label: 'Karnataka', shortLabel: 'Karnataka', dotClass: 'bg-[#78b94d]' }, { value: 'TAMIL_NADU' as const, label: 'Tamil Nadu', shortLabel: 'Tamil Nadu', dotClass: 'bg-[#ff7a59]' }, ]; interface Props { onSend: (query: string, jurisdiction: Jurisdiction | '') => void; disabled: boolean; pendingQuery?: string; } export function InputBar({ onSend, disabled, pendingQuery }: Props) { const [query, setQuery] = useState(''); const [jurisdiction, setJurisdiction] = useState(''); const [isJurisdictionOpen, setIsJurisdictionOpen] = useState(false); const textareaRef = useRef(null); const jurisdictionMenuRef = useRef(null); const MAX_VISIBLE_LINES = 9; const selectedJurisdiction = JURISDICTIONS.find(item => item.value === jurisdiction) ?? JURISDICTIONS[0]; useEffect(() => { if (!pendingQuery) { return; } setQuery(pendingQuery); // Use a slightly longer delay or multiple frames to ensure the DOM has updated // and the layout has settled before calculating height. setTimeout(() => { textareaRef.current?.focus(); autoResize(); }, 0); }, [pendingQuery]); // Also trigger autoResize when the query state changes to ensure it's always in sync useEffect(() => { autoResize(); }, [query]); useEffect(() => { if (!isJurisdictionOpen) { return; } function handlePointerDown(event: PointerEvent) { if (!jurisdictionMenuRef.current?.contains(event.target as Node)) { setIsJurisdictionOpen(false); } } window.addEventListener('pointerdown', handlePointerDown); return () => window.removeEventListener('pointerdown', handlePointerDown); }, [isJurisdictionOpen]); function autoResize() { const element = textareaRef.current; if (!element) { return; } const computed = window.getComputedStyle(element); const lineHeight = Number.parseFloat(computed.lineHeight) || 24; const maxHeight = lineHeight * MAX_VISIBLE_LINES; element.style.height = 'auto'; element.style.height = `${Math.min(element.scrollHeight, maxHeight)}px`; element.style.overflowY = element.scrollHeight > maxHeight ? 'auto' : 'hidden'; } function handleInput(event: React.ChangeEvent) { setQuery(event.target.value); autoResize(); } function handleSend() { const text = query.trim(); if (!text || disabled) { return; } onSend(text, jurisdiction); setQuery(''); if (textareaRef.current) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.overflowY = 'hidden'; } } function handleKeyDown(event: React.KeyboardEvent) { if (event.key === 'Enter' && !event.shiftKey) { event.preventDefault(); handleSend(); } } return (
{isJurisdictionOpen ? (
{JURISDICTIONS.map(item => ( ))}
) : null}