import React, { useRef, useCallback } from 'react'; import { View, Text, StyleSheet, TextInput, TouchableOpacity, FlatList, ActivityIndicator, } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useRouter } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { VideoCard } from '../components/VideoCard'; import { useSearch } from '../hooks/useSearch'; import type { VideoItem } from '../services/types'; export default function SearchScreen() { const router = useRouter(); const { keyword, setKeyword, results, loading, hasMore, search, loadMore } = useSearch(); const inputRef = useRef(null); const handleSearch = useCallback(() => { if (keyword.trim()) { search(keyword, true); } }, [keyword, search]); const renderItem = useCallback( ({ item, index }: { item: VideoItem; index: number }) => { if (index % 2 !== 0) return null; const right = results[index + 1]; return ( router.push(`/video/${item.bvid}` as any)} /> {right ? ( router.push(`/video/${right.bvid}` as any)} /> ) : ( )} ); }, [results, router], ); const keyExtractor = useCallback( (_: VideoItem, index: number) => String(index), [], ); const ListEmptyComponent = () => { if (loading) return null; return ( {results.length === 0 && keyword.trim() ? '没有找到相关视频' : '输入关键词搜索'} ); }; return ( {/* Search header */} router.back()} style={styles.backBtn}> {keyword.length > 0 && ( setKeyword('')} style={styles.clearBtn}> )} 搜索 {/* Results */} } ListFooterComponent={ loading && results.length > 0 ? ( ) : null } keyboardShouldPersistTaps="handled" /> ); } const styles = StyleSheet.create({ safe: { flex: 1, backgroundColor: '#f4f4f4' }, header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 8, paddingVertical: 8, backgroundColor: '#fff', borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: '#eee', gap: 6, }, backBtn: { padding: 4 }, inputWrap: { flex: 1, height: 34, backgroundColor: '#f0f0f0', borderRadius: 17, flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, }, input: { flex: 1, fontSize: 14, color: '#212121', padding: 0, }, clearBtn: { paddingLeft: 4 }, searchBtn: { paddingHorizontal: 10, paddingVertical: 6, }, searchBtnText: { fontSize: 14, color: '#00AEEC', fontWeight: '600' }, listContent: { paddingTop: 6, paddingBottom: 20 }, row: { flexDirection: 'row', paddingHorizontal: 1, justifyContent: 'flex-start', }, leftCol: { flex: 1, marginLeft: 4, marginRight: 2 }, rightCol: { flex: 1, marginLeft: 2, marginRight: 4 }, emptyBox: { flex: 1, alignItems: 'center', justifyContent: 'center', paddingTop: 80, gap: 12, }, emptyText: { fontSize: 14, color: '#bbb' }, footer: { height: 48, alignItems: 'center', justifyContent: 'center' }, });