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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11x 2x 9x 10x 10x 10x 2x | /**
* CommentSection - 评论区组件
*
* 优雅的空状态设计,精致插画与行动号召
* 注意:不使用 FlatList,因为该组件被嵌套在 ScrollView 中
*/
import React, { memo, useMemo } from 'react';
import { MessageCircle, Sparkles } from '@tamagui/lucide-icons';
import { styled, YStack, XStack, Text, Spinner } from 'tamagui';
import Animated, {
useAnimatedStyle,
withRepeat,
withSequence,
withTiming,
Easing,
} from 'react-native-reanimated';
import type { Comment } from '@/src/lib/supabase';
import { CommentItem } from './CommentItem';
export interface CommentSectionProps {
/** 评论列表 */
comments: Comment[];
/** 是否加载中 */
isLoading: boolean;
/** 当前用户 ID */
currentUserId: string | null;
/** 新评论内容 */
newComment: string;
/** 回复目标 */
replyTarget: Comment | null;
/** 正在编辑的评论 */
editingComment: { id: number; content: string } | null;
// 评论操作
onCommentChange: (text: string) => void;
onSubmitComment: () => void;
onToggleLike: (commentId: number) => void;
onSetReplyTarget: (comment: Comment | null) => void;
/** 点击作者头像 */
onAuthorPress?: (author: { id: string; username: string; avatar?: string }) => void;
// 编辑操作
onStartEdit: (comment: Comment) => void;
onSaveEdit: () => void;
onCancelEdit: () => void;
onEditChange: (content: string) => void;
onDeleteComment: (commentId: number) => void;
}
// 样式组件
const Container = styled(YStack, {
name: 'CommentSection',
backgroundColor: '#fff',
});
const HeaderContainer = styled(XStack, {
name: 'CommentHeader',
alignItems: 'center',
gap: 10,
paddingHorizontal: 20,
paddingVertical: 16,
backgroundColor: '#fff',
});
const HeaderTitle = styled(Text, {
name: 'CommentHeaderTitle',
fontSize: 17,
fontWeight: '700',
color: '#1a1a1a',
letterSpacing: -0.3,
});
const CommentCount = styled(Text, {
name: 'CommentCount',
fontSize: 15,
color: '#8e8e93',
fontWeight: '500',
});
const ListContainer = styled(YStack, {
name: 'CommentList',
backgroundColor: '#fff',
});
// 精致的空状态设计
const EmptyContainer = styled(YStack, {
name: 'EmptyComments',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 56,
paddingHorizontal: 32,
gap: 20,
backgroundColor: '#fff',
});
const EmptyIllustration = styled(YStack, {
name: 'EmptyIllustration',
width: 100,
height: 100,
borderRadius: 50,
backgroundColor: '#f8f9fa',
alignItems: 'center',
justifyContent: 'center',
position: 'relative',
});
const BubbleDecor = styled(YStack, {
name: 'BubbleDecor',
position: 'absolute',
width: 24,
height: 24,
borderRadius: 12,
backgroundColor: '#e8f4fd',
alignItems: 'center',
justifyContent: 'center',
});
const EmptyTextContainer = styled(YStack, {
name: 'EmptyTextContainer',
alignItems: 'center',
gap: 8,
});
const EmptyTitle = styled(Text, {
name: 'EmptyTitle',
fontSize: 18,
fontWeight: '600',
color: '#1a1a1a',
letterSpacing: -0.3,
});
const EmptySubtitle = styled(Text, {
name: 'EmptySubtitle',
fontSize: 14,
color: '#8e8e93',
textAlign: 'center',
lineHeight: 20,
});
const LoadingContainer = styled(YStack, {
name: 'LoadingContainer',
alignItems: 'center',
justifyContent: 'center',
paddingVertical: 48,
backgroundColor: '#fff',
});
const AnimatedBubble = Animated.createAnimatedComponent(BubbleDecor);
/**
* 空状态组件 - 精致动画插画
*/
const EmptyState = memo(function EmptyState() {
// 气泡浮动动画
const floatStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateY: withRepeat(
withSequence(
withTiming(-4, { duration: 1500, easing: Easing.inOut(Easing.ease) }),
withTiming(4, { duration: 1500, easing: Easing.inOut(Easing.ease) })
),
-1,
true
),
},
],
};
});
return (
<EmptyContainer>
<EmptyIllustration>
<MessageCircle size={42} color="#007aff" strokeWidth={1.5} />
{/* 装饰性小气泡 */}
<AnimatedBubble style={[{ top: -8, right: -4 }, floatStyle]}>
<Sparkles size={12} color="#007aff" />
</AnimatedBubble>
<BubbleDecor style={{ bottom: -4, left: -8, backgroundColor: '#fff0f0' }}>
<Text fontSize={10}>💬</Text>
</BubbleDecor>
</EmptyIllustration>
<EmptyTextContainer>
<EmptyTitle>还没有评论</EmptyTitle>
<EmptySubtitle>期待你的独到见解!{'\n'}成为第一个留言的人吧</EmptySubtitle>
</EmptyTextContainer>
</EmptyContainer>
);
});
/**
* 评论区组件
*/
function CommentSectionComponent({
comments,
isLoading,
currentUserId,
editingComment,
onToggleLike,
onSetReplyTarget,
onStartEdit,
onSaveEdit,
onCancelEdit,
onEditChange,
onDeleteComment,
}: CommentSectionProps) {
/**
* 加载状态
*/
if (isLoading && comments.length === 0) {
return (
<Container>
<HeaderContainer>
<HeaderTitle>评论</HeaderTitle>
</HeaderContainer>
<LoadingContainer>
<Spinner size="large" color="#007aff" />
</LoadingContainer>
</Container>
);
}
return (
<Container>
{/* 评论区标题 */}
<HeaderContainer>
<HeaderTitle>评论</HeaderTitle>
{comments.length > 0 && <CommentCount>{comments.length}</CommentCount>}
</HeaderContainer>
{/* 评论列表 - 使用 map 渲染避免嵌套 VirtualizedList */}
<ListContainer>
{comments.length === 0 ? (
<EmptyState />
) : (
comments.map((comment) => {
const isOwner = currentUserId === comment.author?.id;
const isEditing = editingComment?.id === comment.id;
return (
<CommentItem
key={comment.id}
comment={comment}
isOwner={isOwner}
isEditing={isEditing}
editingContent={isEditing ? editingComment?.content : undefined}
onLike={onToggleLike}
onReply={onSetReplyTarget}
onStartEdit={onStartEdit}
onSaveEdit={onSaveEdit}
onCancelEdit={onCancelEdit}
onEditChange={onEditChange}
onDelete={onDeleteComment}
/>
);
})
)}
</ListContainer>
</Container>
);
}
export const CommentSection = memo(CommentSectionComponent);
|