import type { EditorView } from 'codemirror'; import type { FormEvent } from 'react'; import { memo, useRef, useState } from 'react'; import { useHotKey } from '../hooks/useHotKey'; import type { HttpRequest } from '../lib/models'; import type { IconProps } from './core/Icon'; import { IconButton } from './core/IconButton'; import { Input } from './core/Input'; import { RequestMethodDropdown } from './RequestMethodDropdown'; type Props = Pick & { className?: string; method: HttpRequest['method'] | null; placeholder: string; onSubmit: (e: FormEvent) => void; onUrlChange: (url: string) => void; submitIcon?: IconProps['icon'] | null; onMethodChange?: (method: string) => void; isLoading: boolean; forceUpdateKey: string; }; export const UrlBar = memo(function UrlBar({ forceUpdateKey, onUrlChange, url, method, placeholder, className, onSubmit, onMethodChange, submitIcon = 'sendHorizontal', isLoading, }: Props) { const inputRef = useRef(null); const [isFocused, setIsFocused] = useState(false); useHotKey('urlBar.focus', () => { const head = inputRef.current?.state.doc.length ?? 0; inputRef.current?.dispatch({ selection: { anchor: 0, head }, }); inputRef.current?.focus(); }); return (
setIsFocused(true)} onBlur={() => setIsFocused(false)} containerClassName="shadow shadow-gray-100 dark:shadow-gray-50" onChange={onUrlChange} defaultValue={url} placeholder={placeholder} leftSlot={ method != null && onMethodChange != null && ( ) } rightSlot={ submitIcon !== null && ( ) } /> ); });