Skip to content

Commit

Permalink
Improve usability of search on web (#3663)
Browse files Browse the repository at this point in the history
* dont select the text on web

* TODO REVERT THESE CHANGES

* use `usethrottledvalue` for autocomplete

* use `isFetching` from query

* rm setTimeout

* getting there

* improve functionality of cancel button

* rm todo

* add comment back

* encode `searchText` rather than `queryTerm`

* use "back" on web in some cases

* don't flash results in autocomplete

* remove unnecesary usestate

* rename everything to `query` temporarily

* revert accidental lint

* rm todo

* rm comment

* use `useFocusEffect` to update the query term on back navigation

* `searchText` is always defined here

* Fix race

* remove back functionality

* use `keepPreviousData` for query

* rename `q` to `queryParam`

* remove hack

* remove `q=` on cancel

* blur on submit

* use `setParams` instead of `replace`

* use `replace` on web still

* clear the search input when we clear `q` on native

* onPress dismiss attempt

* Adjustments

* Fix search history

* Always hide autocomplete

* Clear right pane search on select

* `blur` on autosuggestion press

* Rename to reduce diff

* Fixes

* Unify codepaths

* Fixes

* precache the autosuggestion

* do the cache in the link card

* Revert "precache the autosuggestion"

This reverts commit 79c433e.

* use `throttledValue` and `keepPreviousData` in sidebar search

* show spinner when fetching pt 1

* show spinner when fetching pt 2

* show spinner properly for autocomplete

* Fix extra border

* Position fixed

* TS

* Revert "TS"

This reverts commit df187ea.

* Revert "Position fixed"

This reverts commit 9c721c9.

* Maybe fix iPad

* Revert "TODO REVERT THESE CHANGES"

This reverts commit 279f717.

* Rename var

---------

Co-authored-by: Dan Abramov <[email protected]>
  • Loading branch information
haileyok and gaearon authored Apr 27, 2024
1 parent d81a373 commit 5f91364
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 170 deletions.
8 changes: 6 additions & 2 deletions src/state/queries/actor-autocomplete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import {AppBskyActorDefs, moderateProfile, ModerationOpts} from '@atproto/api'
import {useQuery, useQueryClient} from '@tanstack/react-query'
import {keepPreviousData, useQuery, useQueryClient} from '@tanstack/react-query'

import {isJustAMute} from '#/lib/moderation'
import {logger} from '#/logger'
Expand All @@ -16,7 +16,10 @@ const DEFAULT_MOD_OPTS = {
const RQKEY_ROOT = 'actor-autocomplete'
export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]

export function useActorAutocompleteQuery(prefix: string) {
export function useActorAutocompleteQuery(
prefix: string,
maintainData?: boolean,
) {
const moderationOpts = useModerationOpts()
const {getAgent} = useAgent()

Expand All @@ -40,6 +43,7 @@ export function useActorAutocompleteQuery(prefix: string) {
},
[moderationOpts],
),
placeholderData: maintainData ? keepPreviousData : undefined,
})
}

Expand Down
210 changes: 101 additions & 109 deletions src/view/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import {s} from '#/lib/styles'
import {logger} from '#/logger'
import {isNative, isWeb} from '#/platform/detection'
import {listenSoftReset} from '#/state/events'
import {useActorAutocompleteFn} from '#/state/queries/actor-autocomplete'
import {useActorAutocompleteQuery} from '#/state/queries/actor-autocomplete'
import {useActorSearch} from '#/state/queries/actor-search'
import {useModerationOpts} from '#/state/queries/preferences'
import {useSearchPostsQuery} from '#/state/queries/search-posts'
import {useSuggestedFollowsQuery} from '#/state/queries/suggested-follows'
import {useSession} from '#/state/session'
import {useSetDrawerOpen} from '#/state/shell'
import {useSetDrawerSwipeDisabled, useSetMinimalShellMode} from '#/state/shell'
import {useNonReactiveCallback} from 'lib/hooks/useNonReactiveCallback'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {
NativeStackScreenProps,
Expand Down Expand Up @@ -308,7 +309,7 @@ function SearchScreenUserResults({
const {_} = useLingui()

const {data: results, isFetched} = useActorSearch({
query,
query: query,
enabled: active,
})

Expand Down Expand Up @@ -478,43 +479,25 @@ export function SearchScreen(
const {track} = useAnalytics()
const setDrawerOpen = useSetDrawerOpen()
const moderationOpts = useModerationOpts()
const search = useActorAutocompleteFn()
const setMinimalShellMode = useSetMinimalShellMode()
const {isTabletOrDesktop, isTabletOrMobile} = useWebMediaQueries()

const searchDebounceTimeout = React.useRef<NodeJS.Timeout | undefined>(
undefined,
)
const [isFetching, setIsFetching] = React.useState<boolean>(false)
const [query, setQuery] = React.useState<string>(props.route?.params?.q || '')
const [searchResults, setSearchResults] = React.useState<
AppBskyActorDefs.ProfileViewBasic[]
>([])
const [inputIsFocused, setInputIsFocused] = React.useState(false)
const [showAutocompleteResults, setShowAutocompleteResults] =
React.useState(false)
const [searchHistory, setSearchHistory] = React.useState<string[]>([])

/**
* The Search screen's `q` param
*/
const queryParam = props.route?.params?.q
// Query terms
const queryParam = props.route?.params?.q ?? ''
const [searchText, setSearchText] = React.useState<string>(queryParam)
const {data: autocompleteData, isFetching: isAutocompleteFetching} =
useActorAutocompleteQuery(searchText, true)

/**
* If `true`, this means we received new instructions from the router. This
* is handled in a effect, and used to update the value of `query` locally
* within this screen.
*/
const routeParamsMismatch = queryParam && queryParam !== query
const [showAutocomplete, setShowAutocomplete] = React.useState(false)
const [searchHistory, setSearchHistory] = React.useState<string[]>([])

React.useEffect(() => {
if (queryParam && routeParamsMismatch) {
// reset immediately and let local state take over
navigation.setParams({q: ''})
// update query for next search
setQuery(queryParam)
}
}, [queryParam, routeParamsMismatch, navigation])
useFocusEffect(
useNonReactiveCallback(() => {
if (isWeb) {
setSearchText(queryParam)
}
}),
)

React.useEffect(() => {
const loadSearchHistory = async () => {
Expand All @@ -536,60 +519,45 @@ export function SearchScreen(
setDrawerOpen(true)
}, [track, setDrawerOpen])

const onPressCancelSearch = React.useCallback(() => {
scrollToTopWeb()
textInput.current?.blur()
setQuery('')
setShowAutocompleteResults(false)
if (searchDebounceTimeout.current)
clearTimeout(searchDebounceTimeout.current)
}, [textInput])

const onPressClearQuery = React.useCallback(() => {
scrollToTopWeb()
setQuery('')
setShowAutocompleteResults(false)
}, [setQuery])

const onChangeText = React.useCallback(
async (text: string) => {
scrollToTopWeb()

setQuery(text)

if (text.length > 0) {
setIsFetching(true)
setShowAutocompleteResults(true)

if (searchDebounceTimeout.current) {
clearTimeout(searchDebounceTimeout.current)
}
setSearchText('')
textInput.current?.focus()
}, [])

searchDebounceTimeout.current = setTimeout(async () => {
const results = await search({query: text, limit: 30})
const onPressCancelSearch = React.useCallback(() => {
scrollToTopWeb()

if (results) {
setSearchResults(results)
setIsFetching(false)
}
}, 300)
if (showAutocomplete) {
textInput.current?.blur()
setShowAutocomplete(false)
setSearchText(queryParam)
} else {
// If we just `setParams` and set `q` to an empty string, the URL still displays `q=`, which isn't pretty.
// However, `.replace()` on native has a "push" animation that we don't want. So we need to handle these
// differently.
if (isWeb) {
navigation.replace('Search', {})
} else {
if (searchDebounceTimeout.current) {
clearTimeout(searchDebounceTimeout.current)
}
setSearchResults([])
setIsFetching(false)
setShowAutocompleteResults(false)
setSearchText('')
navigation.setParams({q: ''})
}
},
[setQuery, search, setSearchResults],
)
}
}, [showAutocomplete, navigation, queryParam])

const onChangeText = React.useCallback(async (text: string) => {
scrollToTopWeb()
setSearchText(text)
}, [])

const updateSearchHistory = React.useCallback(
async (newQuery: string) => {
newQuery = newQuery.trim()
if (newQuery && !searchHistory.includes(newQuery)) {
let newHistory = [newQuery, ...searchHistory]
if (newQuery) {
let newHistory = [
newQuery,
...searchHistory.filter(q => q !== newQuery),
]

if (newHistory.length > 5) {
newHistory = newHistory.slice(0, 5)
Expand All @@ -609,21 +577,40 @@ export function SearchScreen(
[searchHistory, setSearchHistory],
)

const navigateToItem = React.useCallback(
(item: string) => {
scrollToTopWeb()
setShowAutocomplete(false)
updateSearchHistory(item)

if (isWeb) {
navigation.push('Search', {q: item})
} else {
textInput.current?.blur()
navigation.setParams({q: item})
}
},
[updateSearchHistory, navigation],
)

const onSubmit = React.useCallback(() => {
scrollToTopWeb()
setShowAutocompleteResults(false)
updateSearchHistory(query)
}, [query, setShowAutocompleteResults, updateSearchHistory])
navigateToItem(searchText)
}, [navigateToItem, searchText])

const handleHistoryItemClick = (item: string) => {
setSearchText(item)
navigateToItem(item)
}

const onSoftReset = React.useCallback(() => {
scrollToTopWeb()
onPressCancelSearch()
}, [onPressCancelSearch])

const queryMaybeHandle = React.useMemo(() => {
const match = MATCH_HANDLE.exec(query)
const match = MATCH_HANDLE.exec(queryParam)
return match && match[1]
}, [query])
}, [queryParam])

useFocusEffect(
React.useCallback(() => {
Expand All @@ -632,11 +619,6 @@ export function SearchScreen(
}, [onSoftReset, setMinimalShellMode]),
)

const handleHistoryItemClick = (item: React.SetStateAction<string>) => {
setQuery(item)
onSubmit()
}

const handleRemoveHistoryItem = (itemToRemove: string) => {
const updatedHistory = searchHistory.filter(item => item !== itemToRemove)
setSearchHistory(updatedHistory)
Expand Down Expand Up @@ -688,17 +670,21 @@ export function SearchScreen(
ref={textInput}
placeholder={_(msg`Search`)}
placeholderTextColor={pal.colors.textLight}
selectTextOnFocus
selectTextOnFocus={isNative}
returnKeyType="search"
value={query}
value={searchText}
style={[pal.text, styles.headerSearchInput]}
keyboardAppearance={theme.colorScheme}
onFocus={() => setInputIsFocused(true)}
onBlur={() => {
// HACK
// give 100ms to not stop click handlers in the search history
// -prf
setTimeout(() => setInputIsFocused(false), 100)
onFocus={() => {
if (isWeb) {
// Prevent a jump on iPad by ensuring that
// the initial focused render has no result list.
requestAnimationFrame(() => {
setShowAutocomplete(true)
})
} else {
setShowAutocomplete(true)
}
}}
onChangeText={onChangeText}
onSubmitEditing={onSubmit}
Expand All @@ -710,7 +696,7 @@ export function SearchScreen(
autoComplete="off"
autoCapitalize="none"
/>
{query ? (
{showAutocomplete ? (
<Pressable
testID="searchTextInputClearBtn"
onPress={onPressClearQuery}
Expand All @@ -727,7 +713,7 @@ export function SearchScreen(
) : undefined}
</View>

{query || inputIsFocused ? (
{(queryParam || showAutocomplete) && (
<View style={styles.headerCancelBtn}>
<Pressable
onPress={onPressCancelSearch}
Expand All @@ -738,12 +724,13 @@ export function SearchScreen(
</Text>
</Pressable>
</View>
) : undefined}
)}
</CenteredView>

{showAutocompleteResults ? (
{showAutocomplete && searchText.length > 0 ? (
<>
{isFetching || !moderationOpts ? (
{(isAutocompleteFetching && !autocompleteData?.length) ||
!moderationOpts ? (
<Loader />
) : (
<ScrollView
Expand All @@ -753,12 +740,12 @@ export function SearchScreen(
keyboardShouldPersistTaps="handled"
keyboardDismissMode="on-drag">
<SearchLinkCard
label={_(msg`Search for "${query}"`)}
label={_(msg`Search for "${searchText}"`)}
onPress={isNative ? onSubmit : undefined}
to={
isNative
? undefined
: `/search?q=${encodeURIComponent(query)}`
: `/search?q=${encodeURIComponent(searchText)}`
}
style={{borderBottomWidth: 1}}
/>
Expand All @@ -770,19 +757,26 @@ export function SearchScreen(
/>
) : null}

{searchResults.map(item => (
{autocompleteData?.map(item => (
<SearchProfileCard
key={item.did}
profile={item}
moderation={moderateProfile(item, moderationOpts)}
onPress={() => {
if (isWeb) {
setShowAutocomplete(false)
} else {
textInput.current?.blur()
}
}}
/>
))}

<View style={{height: 200}} />
</ScrollView>
)}
</>
) : !query && inputIsFocused ? (
) : !queryParam && showAutocomplete ? (
<CenteredView
sideBorders={isTabletOrDesktop}
// @ts-ignore web only -prf
Expand Down Expand Up @@ -826,10 +820,8 @@ export function SearchScreen(
)}
</View>
</CenteredView>
) : routeParamsMismatch ? (
<ActivityIndicator />
) : (
<SearchScreenInner query={query} />
<SearchScreenInner query={queryParam} />
)}
</View>
)
Expand Down
Loading

0 comments on commit 5f91364

Please sign in to comment.