import type { HttpRequest } from '@yaakapp-internal/models'; import classNames from 'classnames'; import type { FormEvent, ReactNode } from 'react'; import { memo, useCallback, useRef, useState } from 'react'; import { useHotKey } from '../hooks/useHotKey'; import type { IconProps } from './core/Icon'; import { IconButton } from './core/IconButton'; import type { InputHandle, InputProps } from './core/Input'; import { Input } from './core/Input'; import { HStack } from './core/Stacks'; type Props = Pick & { className?: string; placeholder: string; onSend: () => void; onUrlChange: (url: string) => void; onPaste?: (v: string) => void; onPasteOverwrite?: InputProps['onPasteOverwrite']; onCancel: () => void; submitIcon?: IconProps['icon'] | null; isLoading: boolean; forceUpdateKey: string; rightSlot?: ReactNode; leftSlot?: ReactNode; autocomplete?: InputProps['autocomplete']; stateKey: InputProps['stateKey']; }; export const UrlBar = memo(function UrlBar({ forceUpdateKey, onUrlChange, url, placeholder, className, onSend, onCancel, onPaste, onPasteOverwrite, submitIcon = 'send_horizontal', autocomplete, leftSlot, rightSlot, isLoading, stateKey, }: Props) { const inputRef = useRef(null); const [isFocused, setIsFocused] = useState(false); const handleInitInputRef = useCallback((h: InputHandle | null) => { inputRef.current = h; }, []); useHotKey('url_bar.focus', () => { inputRef.current?.selectAll(); }); const handleSubmit = (e: FormEvent) => { e.preventDefault(); if (isLoading) onCancel(); else onSend(); }; return (
setIsFocused(true)} onBlur={() => setIsFocused(false)} onPaste={onPaste} onPasteOverwrite={onPasteOverwrite} onChange={onUrlChange} defaultValue={url} placeholder={placeholder} leftSlot={leftSlot} rightSlot={ {rightSlot &&
{rightSlot}
} {submitIcon !== null && (
{ // Prevent the button from taking focus e.preventDefault(); }} />
)}
} />
); });