import { useState, useEffect, useRef } from 'react' import { View, Text, StyleSheet, ScrollView, Pressable, StatusBar as RNStatusBar, Dimensions, TextInput, ActivityIndicator, } from 'react-native' import { StatusBar } from 'expo-status-bar' import { SafeAreaView } from 'react-native-safe-area-context' import { useRouter, useLocalSearchParams } from 'expo-router' import { useTranslation } from 'react-i18next' import { DeleteIcon, ChangeIcon, Close1Icon } from '@/components/icon' import SearchBar from '@/components/SearchBar' import { useSearchHistory, useTags } from '@/hooks' const { width: screenWidth } = Dimensions.get('window') export default function SearchTemplateScreen() { const { t } = useTranslation() const router = useRouter() const params = useLocalSearchParams() const [searchText, setSearchText] = useState('') const [isDeleteMode, setIsDeleteMode] = useState(false) const inputRef = useRef(null) const isNavigatingRef = useRef(false) // 使用搜索历史 hook const { history: searchHistory, addToHistory, removeFromHistory, clearHistory, isLoading: historyLoading } = useSearchHistory() // 使用推荐标签 hook const { load: loadTags, data: tagsData, loading: tagsLoading } = useTags() useEffect(() => { // 加载推荐标签 loadTags({ page: 1, limit: 20, orderBy: 'sortOrder', order: 'desc' }) }, []) // 当从搜索结果页面返回时,更新搜索文本 useEffect(() => { if (params.q && typeof params.q === 'string') { setSearchText(params.q) // 将搜索关键词添加到历史记录 addToHistory(params.q) } }, [params.q, addToHistory]) // 推荐标签数据 const recommendedTags = tagsData?.tags?.map(tag => tag.name) || [] return ( {/* Top Bar with Search */} { // 执行搜索时,添加到历史记录 addToHistory(text) router.push({ pathname: '/searchResults', params: { q: text }, }) }} onBack={() => router.push('/(tabs)')} inputRef={inputRef} autoFocus={params.focus === 'true'} /> {/* 搜索历史和推荐标签 */} {/* 搜索历史 */} {!historyLoading && searchHistory.length > 0 && ( {t('search.history')} {isDeleteMode ? ( { clearHistory() setIsDeleteMode(false) }} > {t('search.clearAll')} setIsDeleteMode(false)} > {t('search.done')} ) : ( setIsDeleteMode(true)} > )} {searchHistory.map((item, index) => ( { if (!isDeleteMode && !isNavigatingRef.current) { isNavigatingRef.current = true requestAnimationFrame(() => { router.push({ pathname: '/searchResults', params: { q: item }, }) setTimeout(() => { isNavigatingRef.current = false }, 500) }) } }} > {item} {isDeleteMode && ( { removeFromHistory(item) }} > )} ))} )} {/* 探索更多 - 推荐标签 */} {t('search.exploreMore')} loadTags({ page: 1, limit: 20, orderBy: 'sortOrder', order: 'desc' })} > {t('search.refresh')} {tagsLoading ? ( ) : recommendedTags.length > 0 ? ( {recommendedTags.map((item, index) => ( { if (!isNavigatingRef.current) { isNavigatingRef.current = true requestAnimationFrame(() => { addToHistory(item) router.push({ pathname: '/searchResults', params: { q: item }, }) setTimeout(() => { isNavigatingRef.current = false }, 500) }) } }} > {item} ))} ) : ( {t('search.noTags') || '暂无推荐标签'} )} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#090A0B', }, scrollView: { flex: 1, paddingHorizontal: 12, }, historySection: { paddingBottom:32, }, historyHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, sectionTitle: { color: '#F5F5F5', fontSize: 14, fontWeight: '500', lineHeight: 20, }, historyTags: { flexDirection: 'row', flexWrap: 'wrap', gap: 4, marginTop: 16, }, historyTagContainer: { flexDirection: 'row', alignItems: 'center', gap: 4, }, historyTag: { backgroundColor: '#191B1F', borderRadius: 100, paddingHorizontal: 11, paddingVertical: 6, borderWidth: 1, borderColor: '#191B1F', height: 29, justifyContent: 'center', flexDirection: 'row', alignItems: 'center', gap: 4, }, historyTagText: { color: '#F5F5F5', fontSize: 12, }, historyTagDeleteButton: { justifyContent: 'center', alignItems: 'center', }, deleteActions: { flexDirection: 'row', alignItems: 'center', }, deleteActionText: { color: '#ABABAB', fontSize: 14, fontWeight: '400', }, deleteActionSeparator: { width: 1, height: 14, backgroundColor: '#ABABAB', marginHorizontal: 12, }, exploreHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16, }, refreshButton: { flexDirection: 'row', alignItems: 'center', gap: 1, paddingVertical: 4, }, refreshButtonText: { color: '#8A8A8A', fontSize: 12, fontWeight: '400', }, loadingContainer: { paddingVertical: 24, alignItems: 'center', justifyContent: 'center', }, emptyContainer: { paddingVertical: 40, alignItems: 'center', justifyContent: 'center', }, emptyText: { color: '#ABABAB', fontSize: 14, }, exploreTags: { flexDirection: 'row', flexWrap: 'wrap', gap: 8, }, exploreTag: { width: (screenWidth - 24 - 8) / 2, // 屏幕宽度 - 左右padding(12*2) - gap(8) 除以2 justifyContent: 'center', paddingVertical: 2, }, exploreTagText: { color: '#F5F5F5', fontSize: 14, fontWeight: '400', }, })