import { useState, useEffect, useRef, useCallback } from 'react' import { View, Text, StyleSheet, TextInput, ScrollView, Pressable, StatusBar as RNStatusBar, Dimensions, } from 'react-native' import { StatusBar } from 'expo-status-bar' import { SafeAreaView } from 'react-native-safe-area-context' import { useRouter, useLocalSearchParams, useFocusEffect } from 'expo-router' import { LeftArrowIcon, CloseIcon, DeleteIcon, ChangeIcon, Close1Icon } from '@/components/icon' import { useTags, useSearchHistory } from '@/hooks/data' const { width: screenWidth } = Dimensions.get('window') export default function SearchTemplateScreen() { const router = useRouter() const params = useLocalSearchParams() const [searchText, setSearchText] = useState('') const [isDeleteMode, setIsDeleteMode] = useState(false) const inputRef = useRef(null) const { load, data: tagsData } = useTags() const { history: searchHistory, addToHistory, removeFromHistory, clearHistory } = useSearchHistory() useEffect(() => { if (params.q && typeof params.q === 'string') { setSearchText(params.q) } }, [params.q]) useEffect(() => { load({ limit: 12 }) }, []) // 当页面获得焦点且需要聚焦时,自动聚焦输入框 useFocusEffect( useCallback(() => { if (params.focus === 'true') { // 延迟聚焦,确保页面渲染完成 const timer = setTimeout(() => { inputRef.current?.focus() }, 100) return () => clearTimeout(timer) } }, [params.focus]) ) return ( {/* Top Bar with Search */} router.push('/(tabs)')} > {searchText.length > 0 && ( setSearchText('')} style={styles.clearButton} > )} { if (searchText.trim()) { await addToHistory(searchText.trim()) router.push({ pathname: '/searchResults', params: { q: searchText.trim() }, }) } }} > 搜索 {/* 搜索历史和推荐标签 */} {/* 搜索历史 */} 搜索历史 {isDeleteMode ? ( { await clearHistory() setIsDeleteMode(false) }} > 全部清除 setIsDeleteMode(false)} > 完成 ) : ( setIsDeleteMode(true)} > )} {searchHistory.map((item, index) => ( { if (!isDeleteMode) { setSearchText(item) } }} > {item} {isDeleteMode && ( { await removeFromHistory(item) }} > )} ))} {/* 探索更多 */} 探索更多 load({ limit: 12 })} > 换一换 {tagsData?.tags.map((tag) => ( setSearchText(tag.name)} > {tag.name} ))} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#090A0B', }, topBar: { height: 44, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, marginTop:8, marginBottom: 20, }, scrollView: { flex: 1, paddingHorizontal: 12, }, searchInputContainer: { flex: 1, marginLeft: 4, marginRight: 12, position: 'relative', height: 40, }, searchInput: { flex: 1, color: '#F5F5F5', fontSize: 14, height: 40, paddingVertical: 8, paddingLeft: 12, paddingRight: 36, borderRadius: 100, backgroundColor: '#16181B', }, clearButton: { position: 'absolute', right: 8, top: 0, bottom: 0, justifyContent: 'center', alignItems: 'center', padding: 4, }, searchButton: { paddingHorizontal: 0, paddingVertical: 8, }, searchButtonText: { color: '#F5F5F5', fontSize: 14, fontWeight: '400', }, 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', }, refreshIcon: { width: 18, height: 18, borderRadius: 9, backgroundColor: '#1C1E22', justifyContent: 'center', alignItems: 'center', }, 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', }, })