Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState,} from 'react'import { type StyleProp, type TextInput, View, type ViewStyle,} from 'react-native'import Animated, { Easing, FadeInDown, FadeOutDown,} from 'react-native-reanimated'import {setStringAsync} from 'expo-clipboard'import {Trans, useLingui} from '@lingui/react/macro'import { useFocusEffect, useIsFocused, useNavigation, useRoute,} from '@react-navigation/native'import {useQueryClient} from '@tanstack/react-query'
import {HITSLOP_10, HITSLOP_20} from '#/lib/constants'import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'import {useViewportZoomLock} from '#/lib/hooks/useViewportZoomLock'import {MagnifyingGlassIcon} from '#/lib/icons'import { type CommonNavigatorParams, type NavigationProp, type SearchParams,} from '#/lib/routes/types'import {listenSoftReset} from '#/state/events'import { unstableCacheProfileView, useProfilesQuery,} from '#/state/queries/profile'import {extractFromMe} from '#/state/queries/search-posts-params'import {useSession} from '#/state/session'import { countActiveFilters, definedFilterParams, filtersToRouteParams, hasActiveFilters, parseHistoryEntry, readSearchFilters, type SearchFilters, serializeHistoryEntry, withoutFilterParams,} from '#/screens/Search/searchParams'import { atoms as a, native, platform, tokens, useBreakpoints, useTheme, web,} from '#/alf'import {useAutocomplete} from '#/components/Autocomplete'import {Button, ButtonIcon} from '#/components/Button'import {ArrowLeft_Stroke2_Corner0_Rounded as ArrowLeftIcon} from '#/components/icons/Arrow'import {ArrowShareRight_Stroke2_Corner2_Rounded as ShareIcon} from '#/components/icons/ArrowShareRight'import * as Layout from '#/components/Layout'import * as Toast from '#/components/Toast'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_NATIVE, IS_WEB} from '#/env'import {account, useStorage} from '#/storage'import type * as bsky from '#/types/bsky'import {AdvancedSearchDialog} from './components/AdvancedSearchDialog'import {AutocompleteResults} from './components/AutocompleteResults'import {DetectedLanguagesAdmonition} from './components/DetectedLanguagesAdmonition'import {SearchAutocompleteInput} from './components/SearchAutocompleteInput'import {SearchHistory} from './components/SearchHistory'import {Explore} from './Explore'import {SearchResults} from './SearchResults'
/** * Derived from the route param so the two can't drift. NonNullable because the * tab param is optional on the route but the handlers below always work with a * concrete value. */type TabParam = NonNullable<SearchParams['tab']>
// Map tab parameter to tab indexfunction getTabIndex(tabParam?: TabParam) { switch (tabParam) { case 'feed': return 3 // Feeds tab case 'user': case 'profile': return 2 // People tab case 'latest': return 1 // Latest tab default: return 0 // Top tab }}
export function SearchScreenShell({ queryParam, testID, fixedParams, navButton = 'menu', inputPlaceholder, isExplore,}: { queryParam: string testID: string fixedParams?: SearchFilters navButton?: 'back' | 'menu' inputPlaceholder?: string isExplore?: boolean}) { const ax = useAnalytics() const t = useTheme() const {gtMobile} = useBreakpoints() const navigation = useNavigation< NavigationProp<CommonNavigatorParams, 'Search' | 'ProfileSearch'> >() const route = useRoute() const textInput = useRef<React.ComponentRef<typeof TextInput>>(null) const {t: l} = useLingui() const {currentAccount} = useSession() const queryClient = useQueryClient() const isFocused = useIsFocused()
useViewportZoomLock({enabled: isFocused})
// Get tab parameter from route params const tabParam = (route.params as {q?: string; tab?: TabParam})?.tab const [activeTab, setActiveTab] = useState(() => getTabIndex(tabParam))
/* * A raw `from:me` operator stays visible in the search input. Submitting the * advanced dialog promotes it to a structured `from=me` filter and removes * it from `q`; the API layer reconstructs the operator for post search. */ const {query, fromMe, filters, setFilters, hasFilters} = useQueryManager({ initialQuery: queryParam, fixedParams, })
// Query terms const [searchText, setSearchText] = useState<string>(query) const searchTextRef = useRef(searchText) const updateSearchText = useCallback((text: string) => { searchTextRef.current = text setSearchText(text) }, [])
const {items: autocompleteItems, isFetching: isAutocompleteFetching} = useAutocomplete({ type: 'profile', /* * On web the dropdown (SearchAutocompleteInput) owns its own autocomplete * query; only the native inline list consumes this one, so pass an empty * query on web to keep the hook a no-op instead of doing wasted work. */ query: IS_NATIVE ? searchText : '', })
const [showAutocomplete, setShowAutocomplete] = useState(false)
const [termHistory = [], setTermHistory] = useStorage(account, [ currentAccount?.did ?? 'pwi', 'searchTermHistory', ] as const) const [accountHistory = [], setAccountHistory] = useStorage(account, [ currentAccount?.did ?? 'pwi', 'searchAccountHistory', ])
const {data: accountHistoryProfiles} = useProfilesQuery({ handles: accountHistory, maintainData: true, })
const updateSearchHistory = useCallback( (q: string, searchFilters: SearchFilters = {}) => { if (!q) return /* * Store the query plus any advanced-search filters. Term-only searches * serialize to a plain string (back-compatible with existing history); * filtered searches serialize to JSON. Dedupe on the serialized form. */ const item = serializeHistoryEntry(q, searchFilters) const newSearchHistory = [ item, ...termHistory.filter(search => search !== item), ].slice(0, 6) setTermHistory(newSearchHistory) }, [termHistory, setTermHistory], )
const updateProfileHistory = useCallback( (item: bsky.profile.AnyProfileView) => { const newAccountHistory = [ item.did, ...accountHistory.filter(p => p !== item.did), ].slice(0, 10) setAccountHistory(newAccountHistory) }, [accountHistory, setAccountHistory], )
const deleteSearchHistoryItem = useCallback( (item: string) => { setTermHistory(termHistory.filter(search => search !== item)) }, [termHistory, setTermHistory], ) const deleteProfileHistoryItem = useCallback( (item: bsky.profile.AnyProfileView) => { setAccountHistory(accountHistory.filter(p => p !== item.did)) }, [accountHistory, setAccountHistory], ) const clearAllHistory = useCallback(() => { ax.metric('search:recent:clearAll', { termCount: termHistory.length, profileCount: accountHistory.length, }) setTermHistory([]) setAccountHistory([]) }, [ax, termHistory, accountHistory, setTermHistory, setAccountHistory])
const showFilters = Boolean((query || hasFilters) && !showAutocomplete)
const onChangeLang = useCallback( (lang: string) => { setFilters({...filters, lang: lang || undefined}) }, [filters, setFilters], )
// web only - measure header height for sticky positioning const [headerHeight, setHeaderHeight] = useState(0) const headerRef = useRef(null) useLayoutEffect(() => { if (IS_WEB) { if (!headerRef.current) return const measurement = (headerRef.current as Element).getBoundingClientRect() setHeaderHeight(measurement.height) } }, [])
/* * On native, navigating to an already-mounted Search screen with a new `q` * (e.g. from a post hashtag/search link) updates the route param without * remounting, so re-sync the input. Web handles this via the focus effect * below, which fires on back/forward navigation. */ useEffect(() => { if (IS_NATIVE) { // eslint-disable-next-line react-hooks/set-state-in-effect updateSearchText(query) } }, [query, updateSearchText]) useFocusEffect( useNonReactiveCallback(() => { if (IS_WEB) { updateSearchText(query) } }), )
const onPressClearQuery = useCallback(() => { scrollToTopWeb() /* * Clearing the query also resets any advanced-search filters, then keeps * the input focused so the user can immediately type a new search. */ if (IS_WEB) { /* * Replace the param set so q/tab/filters drop out of the URL instead of * serializing as the literal string "undefined". fixedParams live on the * route already and are preserved by withoutSearchParams. */ const parameters = withoutSearchParams( route.params as Record<string, unknown>, ) // @ts-expect-error route is not typesafe navigation.replace(route.name, parameters) } else { updateSearchText('') navigation.setParams({...filtersToRouteParams({}), q: ''}) } textInput.current?.focus() }, [updateSearchText, navigation, route])
const onChangeText = useCallback( (text: string) => { scrollToTopWeb() updateSearchText(text) }, [updateSearchText], )
const navigateToItem = useCallback( (item: string, itemFilters: SearchFilters = filters) => { scrollToTopWeb() setShowAutocomplete(false) updateSearchHistory(item, itemFilters)
if (IS_WEB) { const nextParams = { ...withoutFilterParams(route.params as Record<string, unknown>), ...definedFilterParams(itemFilters), q: item, } // @ts-expect-error route is not typesafe navigation.push(route.name, nextParams) } else { textInput.current?.blur() navigation.setParams({...filtersToRouteParams(itemFilters), q: item}) } }, [updateSearchHistory, navigation, route, filters], )
const onPressCancelSearch = useCallback(() => { scrollToTopWeb() textInput.current?.blur() setShowAutocomplete(false) if (IS_WEB) { /* * Empty params resets the URL to be /search rather than /search?q= * Also clear tab and advanced-search filter parameters. */ const parameters = withoutSearchParams( route.params as Record<string, unknown>, ) // @ts-expect-error route is not typesafe navigation.replace(route.name, parameters) } else { updateSearchText('') navigation.setParams({ ...filtersToRouteParams({}), q: '', tab: undefined, }) } }, [ setShowAutocomplete, updateSearchText, navigation, route.params, route.name, ])
const onSubmit = (source: 'typed' | 'autocomplete') => () => { const nextQuery = searchTextRef.current ax.metric('search:query', { source, filterCount: countActiveFilters(filters), }) navigateToItem(nextQuery) }
const onSubmitAdvanced = useCallback( (text: string, nextFilters: SearchFilters) => { scrollToTopWeb() setShowAutocomplete(false) updateSearchText(text) updateSearchHistory(text, nextFilters) ax.metric('search:query', { source: 'typed', filterCount: countActiveFilters(nextFilters), }) if (IS_WEB) { /* * Build a fresh param set so removed filters drop out of the URL. * Only defined filters are included - undefined values would serialize * as the literal string "undefined". */ const nextParams = { ...withoutFilterParams(route.params as Record<string, unknown>), ...definedFilterParams(nextFilters), q: text, } // @ts-expect-error route is not typesafe navigation.push(route.name, nextParams) } else { textInput.current?.blur() // setParams merges, so pass undefined for absent keys to clear them. navigation.setParams({...filtersToRouteParams(nextFilters), q: text}) } }, [ax, navigation, route, updateSearchText, updateSearchHistory], )
const onAutocompleteResultPress = useCallback(() => { if (IS_WEB) { setShowAutocomplete(false) } else { textInput.current?.blur() } }, [])
const handleHistoryItemClick = useCallback( (item: string) => { /* * History entries may carry advanced-search filters (JSON-encoded); * term-only entries are plain strings. Restore both the query and * filters. */ const {q, filters: itemFilters} = parseHistoryEntry(item) updateSearchText(q) navigateToItem(q, itemFilters) }, [navigateToItem, updateSearchText], )
const handleProfileClick = useCallback( (profile: bsky.profile.AnyProfileView) => { updateSearchText('') unstableCacheProfileView(queryClient, profile) // Slight delay to avoid updating during push nav animation. setTimeout(() => { updateProfileHistory(profile) }, 400) }, [updateProfileHistory, queryClient, updateSearchText], )
/** * Web only. Selecting a profile from the anchored autocomplete dropdown. */ const onSelectProfile = useCallback( (profile: bsky.profile.AnyProfileView, position: number) => { ax.metric('search:autocomplete:press', { profileDid: profile.did, position, }) handleProfileClick(profile) navigation.navigate('Profile', {name: profile.handle}) }, [ax, handleProfileClick, navigation], )
/** * Web only. Selecting the "Search for X" row from the anchored autocomplete * dropdown. This runs the typed query as-is (not a suggested profile), so it * is attributed to `typed` rather than `autocomplete`. */ const onSelectSearch = useCallback( (value: string) => { ax.metric('search:query', { source: 'typed', filterCount: countActiveFilters(filters), }) updateSearchText(value) navigateToItem(value) }, [ax, filters, navigateToItem, updateSearchText], )
const onSoftReset = useCallback(() => { if (IS_WEB) { /* * Empty params resets the URL to be /search rather than /search?q= * Also clear tab and advanced-search filter parameters. */ const parameters = withoutSearchParams( route.params as Record<string, unknown>, ) // @ts-expect-error route is not typesafe navigation.replace(route.name, parameters) } else { updateSearchText('') navigation.setParams({ ...filtersToRouteParams({}), q: '', tab: undefined, }) textInput.current?.focus() } }, [navigation, route.name, route.params, updateSearchText])
useFocusEffect( useCallback(() => { return listenSoftReset(onSoftReset) }, [onSoftReset]), )
const onSearchInputFocus = useCallback(() => { if (IS_WEB) { // Prevent a jump on iPad by ensuring that // the initial focused render has no result list. requestAnimationFrame(() => { setShowAutocomplete(true) }) } else { setShowAutocomplete(true) } }, [setShowAutocomplete])
const focusSearchInput = useCallback( (tab?: TabParam) => { textInput.current?.focus()
// If a tab is specified, set the tab parameter if (tab) { if (IS_WEB) { navigation.setParams({...route.params, tab}) } else { navigation.setParams({tab}) } } }, [navigation, route], )
const onShareSearch = useCallback(() => { const url = new URL('https://bsky.app') url.pathname = '/search' if (query) url.searchParams.set('q', query) for (const [key, value] of Object.entries(definedFilterParams(filters))) { url.searchParams.set(key, value) } ax.metric('search:shareLink:press', { filterCount: countActiveFilters(filters), }) setStringAsync(url.toString()).then( () => Toast.show(l`Copied link to clipboard`, {type: 'success'}), () => Toast.show(l`Failed to copy link`, {type: 'error'}), ) }, [ax, query, filters, l])
const showHeader = !gtMobile || navButton !== 'menu'
return ( <Layout.Screen testID={testID}> <View ref={headerRef} onLayout={evt => { if (IS_WEB) setHeaderHeight(evt.nativeEvent.layout.height) }} style={[ a.relative, a.z_10, web({ position: 'sticky', top: 0, }), ]}> <Layout.Center style={t.atoms.bg}> {showHeader && ( <View // HACK: shift up search input. we can't remove the top padding // on the search input because it messes up the layout animation // if we add it only when the header is hidden style={{marginBottom: tokens.space.xs * -1}}> <Layout.Header.Outer noBottomBorder> {navButton === 'menu' ? ( <Layout.Header.MenuButton /> ) : ( <Layout.Header.BackButton /> )} <Layout.Header.Content align="left"> <Layout.Header.TitleText> {isExplore ? <Trans>Explore</Trans> : <Trans>Search</Trans>} </Layout.Header.TitleText> </Layout.Header.Content> {showFilters ? ( <View style={[a.flex_row, a.align_center, a.gap_sm]}> <AdvancedSearchDialog disabled={activeTab > 1} q={searchText} filters={filters} onSubmit={onSubmitAdvanced} /> <Button accessibilityRole="button" size="small" color="secondary" shape="round" label={l`Share this search`} onPress={onShareSearch}> <ButtonIcon icon={ShareIcon} /> </Button> </View> ) : ( <Layout.Header.Slot /> )} </Layout.Header.Outer> </View> )} <View style={[a.px_lg, a.pt_sm, a.pb_sm, a.overflow_hidden]}> <View style={[a.gap_sm]}> {!!query && !showAutocomplete && ( <DetectedLanguagesAdmonition query={query} filters={filters} sort={activeTab === 1 ? 'latest' : 'top'} enabled={activeTab === 0 || activeTab === 1} onPressLanguage={onChangeLang} /> )}
<View style={[a.w_full, a.flex_row, a.align_stretch, a.gap_sm]}> <View style={[a.flex_1, a.flex_row, a.align_center, a.gap_sm]}> {showAutocomplete && ( <Button label={l`Cancel search`} size="small" variant="ghost" color="secondary" shape="round" style={[a.px_sm]} onPress={onPressCancelSearch} hitSlop={HITSLOP_10}> <ButtonIcon icon={ArrowLeftIcon} size="lg" /> </Button> )} <View style={[a.flex_1]}> <SearchAutocompleteInput testID="searchScreenInput" ref={textInput} value={searchText} onFocus={onSearchInputFocus} onChangeText={onChangeText} onClearText={onPressClearQuery} onSubmitEditing={onSubmit('typed')} placeholder={inputPlaceholder ?? l`Search`} hitSlop={{...HITSLOP_20, top: 0}} hotkey={true} fixedParams={Boolean(fixedParams)} onSelectProfile={onSelectProfile} onSelectSearch={onSelectSearch} /> </View> </View> {showFilters && !showHeader ? ( <View style={[a.flex_row, a.align_center, a.gap_sm]}> <AdvancedSearchDialog disabled={activeTab > 1} q={searchText} filters={filters} onSubmit={onSubmitAdvanced} /> <Button accessibilityRole="button" size="small" color="secondary" shape="round" label={l`Share this search`} onPress={onShareSearch}> <ButtonIcon icon={ShareIcon} /> </Button> </View> ) : null} </View> </View> </View> </Layout.Center> </View>
<View style={[a.flex_1, a.relative]}> <View style={[a.flex_1, web(showAutocomplete && a.hidden)]}> <SearchScreenInner key={filters.lang ?? ''} activeTab={activeTab} setActiveTab={setActiveTab} query={query} filters={filters} hasFilters={hasFilters} fromMe={fromMe} headerHeight={headerHeight} focusSearchInput={focusSearchInput} /> </View>
{showAutocomplete && !fixedParams && ( <Animated.View entering={native(FadeInDown.easing(Easing.out(Easing.cubic)))} exiting={native(FadeOutDown.easing(Easing.out(Easing.cubic)))} style={platform({ web: [a.flex_1], native: [t.atoms.bg, a.absolute, a.inset_0], })} accessibilityViewIsModal accessibilityRole="list"> {searchText.length > 0 && IS_NATIVE ? ( <AutocompleteResults items={autocompleteItems} isFetching={isAutocompleteFetching} searchText={searchText} onSubmit={onSubmit('autocomplete')} onResultPress={onAutocompleteResultPress} onProfileClick={handleProfileClick} /> ) : ( <SearchHistory searchHistory={termHistory} selectedProfiles={ accountHistoryProfiles?.profiles.filter(p => accountHistory.includes(p.did), ) ?? [] } onItemClick={handleHistoryItemClick} onProfileClick={handleProfileClick} onRemoveItemClick={deleteSearchHistoryItem} onRemoveProfileClick={deleteProfileHistoryItem} onClearAll={clearAllHistory} /> )} </Animated.View> )} </View> </Layout.Screen> )}
let SearchScreenInner = ({ activeTab, setActiveTab, query, filters, hasFilters, fromMe, headerHeight, focusSearchInput,}: { activeTab: number setActiveTab: React.Dispatch<React.SetStateAction<number>> query: string filters: SearchFilters hasFilters: boolean fromMe: boolean headerHeight: number focusSearchInput: (tab?: TabParam) => void}): React.ReactNode => { const t = useTheme() const {hasSession} = useSession() const {gtTablet} = useBreakpoints()
const onPageSelected = (index: number) => { setActiveTab(index) }
return query || hasFilters ? ( <SearchResults query={query} filters={filters} hasFilters={hasFilters} fromMe={fromMe} activeTab={activeTab} headerHeight={headerHeight} onPageSelected={onPageSelected} /> ) : hasSession ? ( <Explore focusSearchInput={focusSearchInput} headerHeight={headerHeight} /> ) : ( <Layout.Center> <View style={a.flex_1}> {gtTablet && ( <View style={[ a.border_b, t.atoms.border_contrast_low, a.px_lg, a.pt_sm, a.pb_lg, ]}> <Text style={[a.text_2xl, a.font_bold]}> <Trans>Search</Trans> </Text> </View> )}
<View style={[a.align_center, a.justify_center, a.py_4xl, a.gap_lg]}> <MagnifyingGlassIcon strokeWidth={3} size={60} style={t.atoms.text_contrast_medium as StyleProp<ViewStyle>} /> <Text style={[t.atoms.text_contrast_medium, a.text_md]}> <Trans>Find posts, users, and feeds on Blacksky</Trans> </Text> </View> </View> </Layout.Center> )}SearchScreenInner = memo(SearchScreenInner)
function useQueryManager({ initialQuery, fixedParams,}: { initialQuery: string fixedParams?: SearchFilters}) { const navigation = useNavigation< NavigationProp<CommonNavigatorParams, 'Search' | 'ProfileSearch'> >() const route = useRoute()
// A raw Me operator remains part of the query until the advanced dialog // promotes it to the structured `from` filter. const query = initialQuery const fromMe = useMemo( () => extractFromMe(initialQuery).fromMe, [initialQuery], )
const filters = useMemo(() => { const fromRoute = readSearchFilters(route.params as Record<string, unknown>) // fixedParams (e.g. ProfileSearch's author) always win and can't be cleared. return {...fromRoute, ...fixedParams} }, [route.params, fixedParams])
const setFilters = useCallback( (next: SearchFilters) => { const merged = {...next, ...fixedParams} if (IS_WEB) { /* * Replace the param set so removed filters drop out of the URL instead * of serializing as the literal string "undefined". */ const nextParams = { ...withoutFilterParams(route.params as Record<string, unknown>), ...definedFilterParams(merged), } // @ts-expect-error route is not typesafe navigation.replace(route.name, nextParams) } else { navigation.setParams(filtersToRouteParams(merged)) } }, [navigation, route, fixedParams], )
return useMemo( () => ({ query, fromMe, filters, setFilters, hasFilters: hasActiveFilters(filters), }), [query, fromMe, filters, setFilters], )}
function scrollToTopWeb() { if (IS_WEB) { window.scrollTo(0, 0) }}
function withoutSearchParams( routeParams: Record<string, unknown> | undefined,): Record<string, unknown> { const parameters = withoutFilterParams(routeParams) delete parameters.q delete parameters.tab return parameters}