import { cn } from '@/lib/utils' import { QrCodeIcon, SearchIcon, X } from 'lucide-react' import { ComponentProps, forwardRef, useEffect, useState } from 'react' import QrScannerModal from '../QrScannerModal' type SearchInputProps = ComponentProps<'input'> & { onQrScan?: (value: string) => void } const SearchInput = forwardRef( ({ value, onChange, className, onQrScan, ...props }, ref) => { const [displayClear, setDisplayClear] = useState(false) const [inputRef, setInputRef] = useState(null) const [showQrScanner, setShowQrScanner] = useState(false) useEffect(() => { setDisplayClear(!!value) }, [value]) function setRefs(el: HTMLInputElement) { setInputRef(el) if (typeof ref === 'function') { ref(el) } else if (ref) { ;(ref as React.MutableRefObject).current = el } } const handleQrScan = (result: string) => { // Strip nostr: prefix if present const value = result.startsWith('nostr:') ? result.slice(6) : result onQrScan?.(value) } return ( <>
inputRef?.focus()} /> {onQrScan && ( )} {displayClear && ( )}
{showQrScanner && ( setShowQrScanner(false)} /> )} ) } ) SearchInput.displayName = 'SearchInput' export default SearchInput