import type { EditorView } from '@codemirror/view'; import type { HttpRequest } from '@yaakapp-internal/models'; import classNames from 'classnames'; import type { FormEvent, ReactNode } from 'react'; import { memo, useRef, useState } from 'react'; import { useHotKey } from '../hooks/useHotKey'; import type { IconProps } from './core/Icon'; import { IconButton } from './core/IconButton'; import type { InputProps } from './core/Input'; import { Input } from './core/Input'; import { HStack } from './core/Stacks'; import { RequestMethodDropdown } from './RequestMethodDropdown'; type Props = Pick & { className?: string; method: HttpRequest['method'] | null; placeholder: string; onSend: () => void; onUrlChange: (url: string) => void; onPaste?: (v: string) => void; onPasteOverwrite?: InputProps['onPasteOverwrite']; onCancel: () => void; submitIcon?: IconProps['icon'] | null; onMethodChange?: (method: string) => void; isLoading: boolean; forceUpdateKey: string; rightSlot?: ReactNode; autocomplete?: InputProps['autocomplete']; stateKey: InputProps['stateKey']; }; export const UrlBar = memo(function UrlBar({ forceUpdateKey, onUrlChange, url, method, placeholder, className, onSend, onCancel, onMethodChange, onPaste, onPasteOverwrite, submitIcon = 'send_horizontal', autocomplete, rightSlot, isLoading, stateKey, }: Props) { const inputRef = useRef(null); const [isFocused, setIsFocused] = useState(false); useHotKey('url_bar.focus', () => { const head = inputRef.current?.state.doc.length ?? 0; inputRef.current?.dispatch({ selection: { anchor: 0, head }, }); inputRef.current?.focus(); }); 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={ method != null && onMethodChange != null && (
) } rightSlot={ {rightSlot &&
{rightSlot}
} {submitIcon !== null && (
{ // Prevent the button from taking focus e.preventDefault(); }} />
)}
} />
); });