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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | 2x 2x 2x 2x 2x 2x 30x 30x 30x 30x 30x 30x 30x 30x 30x 10x 30x 30x 26x 26x 4x 4x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 10x 8x 8x 30x 6x 24x 2x | /**
* PostDetailScreen - 帖子详情页
*
* Premium Design with:
* - 全宽无边框媒体展示
* - 悬浮底部操作栏
* - 固定底部评论输入框
* - 流畅的过渡动画
*/
import React, { memo, useCallback, useEffect, useState, useRef } from 'react';
import { BackHandler, KeyboardAvoidingView, Platform, Dimensions } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { styled, YStack, ScrollView } from 'tamagui';
import Animated, {
useAnimatedStyle,
useSharedValue,
withSpring,
withTiming,
Easing,
runOnJS,
} from 'react-native-reanimated';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import { supabaseForumService, type Post, type PostMedia } from '@/src/lib/supabase';
import { UserProfileModal } from '@/src/components/UserProfileModal';
import { VideoPlayer } from '@/src/components/VideoPlayer';
import { CommentSection } from './CommentSection';
import { CommentInput } from './CommentInput';
import { PostActions } from './PostActions';
import { PostContent } from './PostContent';
import { PostDetailHeader } from './PostDetailHeader';
import { PostMediaGallery } from './PostMediaGallery';
import { usePostDetail } from './usePostDetail';
const { height: SCREEN_HEIGHT } = Dimensions.get('window');
export interface PostDetailScreenProps {
/** 是否显示 */
visible: boolean;
/** 帖子数据 */
post: Post | null;
/** 关闭详情页 */
onClose: () => void;
/** 顶部偏移(适配 header) */
headerOffset?: number;
/** 编辑帖子 */
onEditPost?: (post: Post) => void;
/** 帖子删除后回调 */
onPostDeleted?: () => void;
}
// 样式组件
const Container = styled(YStack, {
name: 'PostDetailScreen',
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
backgroundColor: '#fff',
zIndex: 300,
});
const ContentScrollView = styled(ScrollView, {
name: 'PostContentScroll',
flex: 1,
backgroundColor: '#fff',
});
const Divider = styled(YStack, {
name: 'Divider',
height: 8,
backgroundColor: '#f5f5f7',
});
// 底部固定区域
const BottomFixedContainer = styled(YStack, {
name: 'BottomFixed',
backgroundColor: '#fff',
borderTopWidth: 0.5,
borderTopColor: 'rgba(0, 0, 0, 0.06)',
});
const AnimatedContainer = Animated.createAnimatedComponent(Container);
/**
* 帖子详情页组件
*/
function PostDetailScreenComponent({
visible,
post,
onClose,
headerOffset = 0,
onEditPost,
onPostDeleted,
}: PostDetailScreenProps) {
const insets = useSafeAreaInsets();
const scrollRef = useRef<any>(null);
// 动画状态
const translateY = useSharedValue(SCREEN_HEIGHT);
const opacity = useSharedValue(0);
// 帖子状态(用于点赞/收藏)
const [localPost, setLocalPost] = useState<Post | null>(post);
// 用户信息模态框
const [selectedUser, setSelectedUser] = useState<{
id: string;
username: string;
avatar?: string;
} | null>(null);
// 视频播放器状态
const [videoPlayerVisible, setVideoPlayerVisible] = useState(false);
const [currentVideoUrl, setCurrentVideoUrl] = useState<string>('');
// 当外部 post 变化时同步
useEffect(() => {
setLocalPost(post);
}, [post]);
// 入场/出场 - 无动画,直接显示
useEffect(() => {
if (visible) {
translateY.value = 0;
opacity.value = 1;
} else {
translateY.value = SCREEN_HEIGHT;
opacity.value = 0;
}
}, [visible, translateY, opacity]);
const containerAnimatedStyle = useAnimatedStyle(() => ({
transform: [{ translateY: translateY.value }],
opacity: opacity.value,
}));
// 使用详情页 Hook 管理所有状态和逻辑
const {
comments,
isLoading,
newComment,
replyTarget,
editingComment,
currentUserId,
isPostAuthor,
setNewComment,
setReplyTarget,
submitComment,
toggleCommentLike,
startEditComment,
cancelEditComment,
saveEditComment,
setEditingContent,
deleteComment,
deletePost,
} = usePostDetail({
post,
visible,
onPostDeleted,
});
/**
* 处理编辑帖子
*/
const handleEdit = useCallback(() => {
if (post) {
onEditPost?.(post);
}
}, [post, onEditPost]);
/**
* 滚动到评论区
*/
const scrollToComments = useCallback(() => {
scrollRef.current?.scrollToEnd({ animated: true });
}, []);
/**
* 处理点击评论作者
*/
const handleAuthorPress = useCallback(
(author: { id: string; username: string; avatar?: string }) => {
setSelectedUser(author);
},
[]
);
/**
* 分享帖子
*/
const handleShare = useCallback(() => {
// TODO: 实现分享功能
}, []);
/**
* 点赞帖子
*/
const handleLikePost = useCallback(async () => {
if (!localPost) return;
// 乐观更新 UI
const wasLiked = localPost.isLiked ?? false;
const prevCount = localPost.likesCount ?? 0;
setLocalPost((prev) =>
prev
? {
...prev,
isLiked: !wasLiked,
likesCount: wasLiked ? Math.max(0, prevCount - 1) : prevCount + 1,
}
: null
);
try {
const { data, error } = await supabaseForumService.toggleLike(localPost.id);
if (error) throw error;
if (data) {
setLocalPost((prev) =>
prev
? {
...prev,
isLiked: data.action === 'liked',
likesCount: data.likesCount ?? prev.likesCount,
}
: null
);
}
} catch (error) {
// 出错时回滚
setLocalPost((prev) =>
prev
? {
...prev,
isLiked: wasLiked,
likesCount: prevCount,
}
: null
);
}
}, [localPost]);
/**
* 收藏帖子
*/
const handleBookmark = useCallback(async () => {
if (!localPost) return;
const wasBookmarked = localPost.isFavorited ?? false;
const prevCount = localPost.favoritesCount ?? 0;
setLocalPost((prev) =>
prev
? {
...prev,
isFavorited: !wasBookmarked,
favoritesCount: wasBookmarked ? Math.max(0, prevCount - 1) : prevCount + 1,
}
: null
);
try {
const { data, error } = await supabaseForumService.toggleFavorite(localPost.id);
if (error) throw error;
if (data) {
setLocalPost((prev) =>
prev
? {
...prev,
isFavorited: data.action === 'favorited',
favoritesCount: data.favoritesCount ?? prev.favoritesCount,
}
: null
);
}
} catch (error) {
setLocalPost((prev) =>
prev
? {
...prev,
isFavorited: wasBookmarked,
favoritesCount: prevCount,
}
: null
);
}
}, [localPost]);
// 处理媒体点击(视频播放)
const handleMediaPress = useCallback((media: PostMedia, _index: number) => {
if (media.mediaType === 'video') {
setCurrentVideoUrl(media.fileUrl);
setVideoPlayerVisible(true);
}
}, []);
// 处理 Android 系统返回键
useEffect(() => {
if (!visible) return;
const backHandler = BackHandler.addEventListener('hardwareBackPress', () => {
onClose();
return true;
});
return () => backHandler.remove();
}, [visible, onClose]);
// 不可见时不渲染
if (!visible || !localPost) {
return null;
}
return (
<AnimatedContainer
style={[{ top: headerOffset, paddingTop: insets.top }, containerAnimatedStyle]}
>
<KeyboardAvoidingView
style={{ flex: 1 }}
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
keyboardVerticalOffset={headerOffset + insets.top}
>
{/* 顶部导航栏 - 极简设计 */}
<PostDetailHeader
isAuthor={isPostAuthor}
onClose={onClose}
onEdit={handleEdit}
onDelete={deletePost}
onShare={handleShare}
/>
{/* 帖子内容区域 */}
<ContentScrollView
ref={scrollRef}
showsVerticalScrollIndicator={false}
contentContainerStyle={{ paddingBottom: 0 }}
>
{/* 媒体画廊 - 全宽沉浸式 */}
{localPost.media && localPost.media.length > 0 && (
<PostMediaGallery media={localPost.media} onMediaPress={handleMediaPress} />
)}
{/* 帖子内容 - 高质量排版 */}
<PostContent post={localPost} />
{/* 操作栏 */}
<PostActions
likeCount={localPost.likesCount || 0}
commentCount={comments.length}
isLiked={localPost.isLiked}
isBookmarked={localPost.isFavorited}
onLike={handleLikePost}
onComment={scrollToComments}
onShare={handleShare}
onBookmark={handleBookmark}
/>
{/* 分隔线 */}
<Divider />
{/* 评论区 */}
<CommentSection
comments={comments}
isLoading={isLoading}
currentUserId={currentUserId}
newComment={newComment}
replyTarget={replyTarget}
editingComment={editingComment}
onCommentChange={setNewComment}
onSubmitComment={submitComment}
onToggleLike={toggleCommentLike}
onSetReplyTarget={setReplyTarget}
onStartEdit={startEditComment}
onSaveEdit={saveEditComment}
onCancelEdit={cancelEditComment}
onEditChange={setEditingContent}
onDeleteComment={deleteComment}
onAuthorPress={handleAuthorPress}
/>
</ContentScrollView>
{/* 固定底部评论输入框 */}
<BottomFixedContainer>
<CommentInput
value={newComment}
onChangeText={setNewComment}
onSubmit={submitComment}
replyTarget={replyTarget}
onCancelReply={() => setReplyTarget(null)}
/>
</BottomFixedContainer>
</KeyboardAvoidingView>
{/* 用户信息模态框 */}
{selectedUser && (
<UserProfileModal
visible={!!selectedUser}
userId={selectedUser.id}
onClose={() => setSelectedUser(null)}
/>
)}
{/* 视频播放器 */}
<VideoPlayer
visible={videoPlayerVisible}
videoUrl={currentVideoUrl}
onClose={() => setVideoPlayerVisible(false)}
/>
</AnimatedContainer>
);
}
export const PostDetailScreen = memo(PostDetailScreenComponent);
|