Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import {useRef, useState} from 'react'import {type TextInput, View} from 'react-native'import {useSift} from '@bsky.app/sift'
import {mergeRefs} from '#/lib/merge-refs'import {atoms as a} from '#/alf'import { Autocomplete, type AutocompleteItem, useAutocomplete,} from '#/components/Autocomplete'import {SearchInput} from '#/components/forms/SearchInput'import {type SearchAutocompleteInputProps} from './shared'
/** * Web: typed results float in a Sift dropdown anchored to the search input * (matching the desktop nav-bar search). Search history continues to render * full-page underneath the input in Shell.tsx, so it is not shown here. See * index.native.tsx for the native (inline list) variant. */export function SearchAutocompleteInput({ fixedParams, onSelectProfile, onSelectSearch, value = '', onFocus, onBlur, onChangeText, ref, ...rest}: SearchAutocompleteInputProps) { const [focused, setFocused] = useState(false) /* * Sift dismisses on Escape without blurring the input, so `focused` stays * true. Track that dismissal separately (reset on type or refocus) rather * than clearing `focused`, otherwise the dropdown wouldn't reopen on the next * keystroke while the input is still focused. */ const [dismissed, setDismissed] = useState(false) const inputRef = useRef<React.ComponentRef<typeof TextInput>>(null)
const sift = useSift({ offset: a.p_sm.padding, placement: 'bottom', })
const active = focused && !dismissed
const {items} = useAutocomplete({ type: 'profile', // The dropdown only shows while active, so don't fetch otherwise. This // avoids a typeahead request on mount when arriving with text already // present (e.g. /search?q=foo). query: active ? value : '', showSearchFallback: true, })
const showDropdown = active && !fixedParams && value.length > 0 && items.length > 0
function onSelect(item: AutocompleteItem) { if (item.type === 'profile') { const position = items.filter(i => i.type === 'profile').indexOf(item) onSelectProfile?.(item.profile, position) } else if (item.type === 'search') { onSelectSearch?.(item.value) } inputRef.current?.blur() }
/* * setAnchor goes on the full-width wrapper View so the dropdown matches the * input's container width; the input ref (targetProps.ref) lands on the inner * TextInput for Sift's positioning math, and the remaining combobox a11y * props are spread onto the input. */ const {setAnchor} = sift.refs const {ref: inputAnchorRef, ...comboboxProps} = sift.targetProps
return ( <View collapsable={false} ref={setAnchor} /* * Focusing the input reveals the Cancel button alongside it (see * Shell.tsx), which shrinks the anchor after Sift's initial measurement * and leaves the dropdown too wide until the next re-measure. Recompute on * layout (react-native-web backs onLayout with a ResizeObserver) so the * width tracks the anchor through that reflow and window resizes. */ onLayout={() => void sift.updatePosition()}> <SearchInput {...rest} {...comboboxProps} ref={mergeRefs([ref, inputAnchorRef, inputRef])} value={value} onChangeText={text => { setDismissed(false) onChangeText?.(text) }} onFocus={e => { setDismissed(false) setFocused(true) onFocus?.(e) }} onBlur={e => { setFocused(false) onBlur?.(e) }} /> {showDropdown && ( <Autocomplete sift={sift} data={items} onSelect={onSelect} onDismiss={() => setDismissed(true)} fullWidth /> )} </View> )}