Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 9x 1x 1x 9x 1x 1x 9x 1x 9x 9x 9x 9x 9x 9x 1x | /**
* ForumHeader - 论坛页面头部组件
*
* 统一风格:左侧头像,中间标题,右侧通知图标
* 包含搜索框
*/
import React, { memo, useCallback, useState, useEffect } from 'react';
import { Pressable, Image as RNImage } from 'react-native';
import Animated, {
useSharedValue,
useAnimatedStyle,
withSpring,
interpolateColor,
useDerivedValue,
} from 'react-native-reanimated';
import { router } from 'expo-router';
import { Bell, Search, X, User } from '@tamagui/lucide-icons';
import { XStack, YStack, Text, Input, Stack, useTheme } from 'tamagui';
import { useUserStore } from '@/src/store/userStore';
import { useThemeColors, useIsDarkMode } from '@/src/hooks/useThemeColors';
export interface ForumHeaderProps {
title?: string;
unreadCount?: number;
onSearch?: (query: string) => void;
onNotificationPress?: () => void;
paddingTop?: number;
}
// 统一头部高度常量
const HEADER_HEIGHT = 56;
function ForumHeaderComponent({
title = '社区',
unreadCount = 0,
onSearch,
onNotificationPress,
paddingTop = 0,
}: ForumHeaderProps) {
const colors = useThemeColors();
const isDark = useIsDarkMode();
const [searchText, setSearchText] = useState('');
const [isSearchFocused, setIsSearchFocused] = useState(false);
const avatarUrl = useUserStore((state) => state.user?.avatarUrl);
const bellScale = useSharedValue(1);
const avatarScale = useSharedValue(1);
// 搜索框聚焦动画
const derivedFocus = useDerivedValue(() => {
return withSpring(isSearchFocused ? 1 : 0, { damping: 20, stiffness: 300 });
}, [isSearchFocused]);
const searchAnimatedStyle = useAnimatedStyle(() => {
return {
borderColor: interpolateColor(derivedFocus.value, [0, 1], [colors.border, colors.primary]),
transform: [{ scale: 1 + derivedFocus.value * 0.01 }],
};
});
const bellAnimatedStyle = useAnimatedStyle(() => ({
transform: [{ scale: bellScale.value }],
}));
const avatarAnimatedStyle = useAnimatedStyle(() => ({
transform: [{ scale: avatarScale.value }],
}));
const handleSearchSubmit = useCallback(() => {
onSearch?.(searchText.trim());
}, [searchText, onSearch]);
const handleClear = useCallback(() => {
setSearchText('');
onSearch?.('');
}, [onSearch]);
const handleNotificationPress = useCallback(() => {
if (onNotificationPress) {
onNotificationPress();
} else E{
router.push('/(tabs)/forum/notifications' as any);
}
}, [onNotificationPress]);
const handleAvatarPress = useCallback(() => {
router.push('/(tabs)/profile' as any);
}, []);
const handleBellPressIn = () => {
bellScale.value = withSpring(0.9, { damping: 15, stiffness: 400 });
};
const handleBellPressOut = () => {
bellScale.value = withSpring(1, { damping: 10, stiffness: 300 });
};
const handleAvatarPressIn = () => {
avatarScale.value = withSpring(0.9, { damping: 15, stiffness: 400 });
};
const handleAvatarPressOut = () => {
avatarScale.value = withSpring(1, { damping: 10, stiffness: 300 });
};
const AnimatedStack = Animated.createAnimatedComponent(Stack);
return (
<YStack
backgroundColor={colors.background as any}
paddingHorizontal={16}
gap={12}
paddingTop={paddingTop}
>
<XStack alignItems="center" justifyContent="space-between" height={HEADER_HEIGHT}>
{/* 左侧:头像 */}
<Pressable
onPress={handleAvatarPress}
onPressIn={handleAvatarPressIn}
onPressOut={handleAvatarPressOut}
>
<AnimatedStack
style={avatarAnimatedStyle}
width={40}
height={40}
borderRadius={20}
backgroundColor={(isDark ? '#3D2A1F' : colors.primaryLight) as any}
alignItems="center"
justifyContent="center"
borderWidth={2}
borderColor={(isDark ? '#4D3A2F' : colors.primaryLight) as any}
overflow="hidden"
>
{avatarUrl ? (
<RNImage
source={{ uri: avatarUrl }}
style={{ width: 40, height: 40, borderRadius: 20 }}
/>
) : (
<User size={20} color={colors.primary as any} />
)}
</AnimatedStack>
</Pressable>
{/* 中间:标题 */}
<Text fontSize={20} fontWeight="700" color={colors.text as any}>
{title}
</Text>
{/* 右侧:通知图标 */}
<Pressable
onPress={handleNotificationPress}
onPressIn={handleBellPressIn}
onPressOut={handleBellPressOut}
>
<AnimatedStack
style={bellAnimatedStyle}
width={40}
height={40}
borderRadius={20}
backgroundColor={colors.backgroundMuted as any}
alignItems="center"
justifyContent="center"
borderWidth={1.5}
borderColor={colors.border as any}
>
<Bell size={20} color={colors.icon as any} />
{unreadCount > 0 && (
<Stack
position="absolute"
top={-4}
right={-4}
minWidth={18}
height={18}
borderRadius={9}
backgroundColor={colors.error as any}
alignItems="center"
justifyContent="center"
paddingHorizontal={4}
borderWidth={2}
borderColor={colors.cardBackground as any}
>
<Text fontSize={10} fontWeight="700" color="white">
{unreadCount > 99 ? '99+' : unreadCount}
</Text>
</Stack>
)}
</AnimatedStack>
</Pressable>
</XStack>
{/* 搜索框 */}
<YStack paddingBottom={12}>
<AnimatedStack
style={[
searchAnimatedStyle,
{
flexDirection: 'row',
backgroundColor: colors.backgroundMuted,
borderRadius: 12,
paddingHorizontal: 14,
paddingVertical: 10,
alignItems: 'center',
gap: 10,
borderWidth: 1.5,
},
]}
>
<Search size={18} color={colors.textTertiary as any} />
<Input
value={searchText}
onChangeText={setSearchText}
placeholder="搜索帖子、标签、用户..."
placeholderTextColor={colors.textMuted}
returnKeyType="search"
onSubmitEditing={handleSearchSubmit}
onFocus={() => setIsSearchFocused(true)}
onBlur={() => setIsSearchFocused(false)}
flex={1}
backgroundColor="transparent"
borderWidth={0}
fontSize={15}
color={colors.text as any}
padding={0}
height={24}
focusStyle={{ borderWidth: 0 }}
/>
{searchText.length > 0 && (
<Pressable onPress={handleClear}>
<Stack
width={24}
height={24}
borderRadius={12}
backgroundColor={colors.border as any}
alignItems="center"
justifyContent="center"
>
<X size={14} color={colors.textSecondary as any} />
</Stack>
</Pressable>
)}
</AnimatedStack>
</YStack>
</YStack>
);
}
export const ForumHeader = memo(ForumHeaderComponent);
|