use useFocusEffect to update the query term on back navigation
This commit is contained in:
@@ -39,6 +39,7 @@ import {
|
|||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
import {useSetDrawerOpen} from '#/state/shell'
|
import {useSetDrawerOpen} from '#/state/shell'
|
||||||
import {useSetDrawerSwipeDisabled, useSetMinimalShellMode} from '#/state/shell'
|
import {useSetDrawerSwipeDisabled, useSetMinimalShellMode} from '#/state/shell'
|
||||||
|
import {useNonReactiveCallback} from 'lib/hooks/useNonReactiveCallback'
|
||||||
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
|
||||||
import {
|
import {
|
||||||
NativeStackScreenProps,
|
NativeStackScreenProps,
|
||||||
@@ -543,7 +544,6 @@ export function SearchScreen(
|
|||||||
|
|
||||||
// Query terms
|
// Query terms
|
||||||
const q = props.route?.params?.q ?? ''
|
const q = props.route?.params?.q ?? ''
|
||||||
const [query, setQuery] = React.useState<string>(q)
|
|
||||||
const [searchText, setSearchText] = React.useState<string>(q)
|
const [searchText, setSearchText] = React.useState<string>(q)
|
||||||
const throttledInput = useThrottledValue(searchText, 300)
|
const throttledInput = useThrottledValue(searchText, 300)
|
||||||
|
|
||||||
@@ -555,10 +555,11 @@ export function SearchScreen(
|
|||||||
const [inputIsFocused, setInputIsFocused] = React.useState(false)
|
const [inputIsFocused, setInputIsFocused] = React.useState(false)
|
||||||
const [searchHistory, setSearchHistory] = React.useState<string[]>([])
|
const [searchHistory, setSearchHistory] = React.useState<string[]>([])
|
||||||
|
|
||||||
if (q !== query) {
|
useFocusEffect(
|
||||||
setQuery(q)
|
useNonReactiveCallback(() => {
|
||||||
setSearchText(q)
|
setSearchText(q)
|
||||||
}
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const loadSearchHistory = async () => {
|
const loadSearchHistory = async () => {
|
||||||
@@ -600,16 +601,16 @@ export function SearchScreen(
|
|||||||
scrollToTopWeb()
|
scrollToTopWeb()
|
||||||
|
|
||||||
if (inputIsFocused) {
|
if (inputIsFocused) {
|
||||||
setSearchText(query)
|
setSearchText(q)
|
||||||
textInput.current?.blur()
|
textInput.current?.blur()
|
||||||
} else {
|
} else {
|
||||||
if (isWeb && query) {
|
if (isWeb && q) {
|
||||||
navigation.goBack()
|
navigation.goBack()
|
||||||
} else {
|
} else {
|
||||||
navigation.setParams({q: ''})
|
navigation.setParams({q: ''})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [inputIsFocused, navigation, query])
|
}, [inputIsFocused, navigation, q])
|
||||||
|
|
||||||
const onChangeText = React.useCallback(async (text: string) => {
|
const onChangeText = React.useCallback(async (text: string) => {
|
||||||
scrollToTopWeb()
|
scrollToTopWeb()
|
||||||
@@ -643,7 +644,6 @@ export function SearchScreen(
|
|||||||
const onSubmit = React.useCallback(() => {
|
const onSubmit = React.useCallback(() => {
|
||||||
scrollToTopWeb()
|
scrollToTopWeb()
|
||||||
updateSearchHistory(searchText)
|
updateSearchHistory(searchText)
|
||||||
setQuery(searchText)
|
|
||||||
|
|
||||||
if (isWeb) {
|
if (isWeb) {
|
||||||
navigation.push('Search', {q: searchText})
|
navigation.push('Search', {q: searchText})
|
||||||
@@ -658,9 +658,9 @@ export function SearchScreen(
|
|||||||
}, [onPressCancelSearch])
|
}, [onPressCancelSearch])
|
||||||
|
|
||||||
const queryMaybeHandle = React.useMemo(() => {
|
const queryMaybeHandle = React.useMemo(() => {
|
||||||
const match = MATCH_HANDLE.exec(query)
|
const match = MATCH_HANDLE.exec(q)
|
||||||
return match && match[1]
|
return match && match[1]
|
||||||
}, [query])
|
}, [q])
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
React.useCallback(() => {
|
React.useCallback(() => {
|
||||||
@@ -669,9 +669,8 @@ export function SearchScreen(
|
|||||||
}, [onSoftReset, setMinimalShellMode]),
|
}, [onSoftReset, setMinimalShellMode]),
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleHistoryItemClick = (item: React.SetStateAction<string>) => {
|
const handleHistoryItemClick = (item: string) => {
|
||||||
setQuery(item)
|
navigation.setParams({q: item})
|
||||||
onSubmit()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleRemoveHistoryItem = (itemToRemove: string) => {
|
const handleRemoveHistoryItem = (itemToRemove: string) => {
|
||||||
@@ -768,7 +767,7 @@ export function SearchScreen(
|
|||||||
) : undefined}
|
) : undefined}
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
{(query || inputIsFocused) && (
|
{(q || inputIsFocused) && (
|
||||||
<View style={styles.headerCancelBtn}>
|
<View style={styles.headerCancelBtn}>
|
||||||
<Pressable
|
<Pressable
|
||||||
onPress={onPressCancelSearch}
|
onPress={onPressCancelSearch}
|
||||||
@@ -827,7 +826,7 @@ export function SearchScreen(
|
|||||||
</ScrollView>
|
</ScrollView>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
) : !query && inputIsFocused ? (
|
) : !q && inputIsFocused ? (
|
||||||
<CenteredView
|
<CenteredView
|
||||||
sideBorders={isTabletOrDesktop}
|
sideBorders={isTabletOrDesktop}
|
||||||
// @ts-ignore web only -prf
|
// @ts-ignore web only -prf
|
||||||
@@ -872,7 +871,7 @@ export function SearchScreen(
|
|||||||
</View>
|
</View>
|
||||||
</CenteredView>
|
</CenteredView>
|
||||||
) : (
|
) : (
|
||||||
<SearchScreenInner query={query} />
|
<SearchScreenInner query={q} />
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user