feat(Comment): enhance like functionality for comments and replies
Some checks failed
Frontend CI / build-and-push-web (push) Failing after 1m54s
Frontend CI / ota-android (push) Failing after 5m36s
Frontend CI / build-android-apk (push) Failing after 9m3s

- Updated CommentItem component to accept the comment object in the onLike callback, allowing for more detailed like handling.
- Added a like button for replies, improving user interaction with nested comments.
- Refactored handleLikeComment function in PostDetailScreen for optimized state management and error handling during like operations.
- Enhanced SettingsScreen to provide debug information for theme preferences, improving user experience in theme selection.
- Updated themeStore to dynamically manage system theme changes and improve overall theme handling.
This commit is contained in:
lafay
2026-03-26 01:25:42 +08:00
parent 9529ea39c4
commit 405cd271db
6 changed files with 202 additions and 70 deletions

View File

@@ -18,7 +18,7 @@ interface CommentItemProps {
comment: Comment;
onUserPress: () => void;
onReply: () => void;
onLike: () => void;
onLike: (comment: Comment) => void; // 点赞回调,传入评论对象
floorNumber?: number; // 楼层号
isAuthor?: boolean; // 是否是楼主
replyToUser?: string; // 回复给哪位用户
@@ -504,6 +504,25 @@ const CommentItem: React.FC<CommentItemProps> = ({
</Text>
</TouchableOpacity>
{/* 点赞按钮 */}
<TouchableOpacity
style={styles.subActionButton}
onPress={() => onLike?.(reply)}
activeOpacity={0.7}
>
<MaterialCommunityIcons
name={reply.is_liked ? 'heart' : 'heart-outline'}
size={12}
color={reply.is_liked ? colors.error.main : colors.text.hint}
/>
<Text
variant="caption"
color={reply.is_liked ? colors.error.main : colors.text.hint}
style={styles.subActionText}
>
{reply.likes_count > 0 ? formatNumber(reply.likes_count) : '赞'}
</Text>
</TouchableOpacity>
{/* 删除按钮 - 子评论作者可见 */}
{isSubReplyAuthor && (
<TouchableOpacity
@@ -592,7 +611,7 @@ const CommentItem: React.FC<CommentItemProps> = ({
{/* 操作按钮 - 更紧凑 */}
<View style={styles.actions}>
{/* 点赞 */}
<TouchableOpacity style={styles.actionButton} onPress={onLike}>
<TouchableOpacity style={styles.actionButton} onPress={() => onLike(comment)}>
<MaterialCommunityIcons
name={comment.is_liked ? 'heart' : 'heart-outline'}
size={14}

View File

@@ -895,61 +895,48 @@ export const PostDetailScreen: React.FC = () => {
}
};
// 点赞评论(包括回复)
// 点赞评论(包括回复)- 优化版
const handleLikeComment = async (comment: Comment) => {
// 先保存旧状态用于回滚
const oldIsLiked = comment.is_liked;
const oldLikesCount = comment.likes_count;
const { id, is_liked, likes_count } = comment;
// 乐观更新本地状态 - 辅助函数用于更新评论或回复
const updateCommentLike = (c: Comment): Comment => {
// 如果是目标评论/回复
if (c.id === comment.id) {
return {
...c,
is_liked: !c.is_liked,
likes_count: c.is_liked ? c.likes_count - 1 : c.likes_count + 1
};
// 乐观更新:切换点赞状态
const newIsLiked = !is_liked;
const newLikesCount = newIsLiked ? likes_count + 1 : Math.max(0, likes_count - 1);
// 递归更新评论树中的点赞状态
const updateLikeInTree = (c: Comment): Comment => {
if (c.id === id) {
return { ...c, is_liked: newIsLiked, likes_count: newLikesCount };
}
// 如果有回复,递归查找
if (c.replies && c.replies.length > 0) {
return {
...c,
replies: c.replies.map(r => updateCommentLike(r))
};
if (c.replies?.length) {
return { ...c, replies: c.replies.map(r => updateLikeInTree(r)) };
}
return c;
};
// 更新本地状态
setComments(prev => prev.map(c => updateCommentLike(c)));
setComments(prev => prev.map(c => updateLikeInTree(c)));
try {
if (oldIsLiked) {
await commentService.unlikeComment(comment.id);
} else {
await commentService.likeComment(comment.id);
const success = newIsLiked
? await commentService.likeComment(id)
: await commentService.unlikeComment(id);
if (!success) {
throw new Error('点赞操作失败');
}
} catch (error) {
console.error('评论点赞操作失败:', error);
// 失败时回滚状态
const rollbackCommentLike = (c: Comment): Comment => {
if (c.id === comment.id) {
return {
...c,
is_liked: oldIsLiked,
likes_count: oldLikesCount
};
// 回滚状态
const rollbackLikeInTree = (c: Comment): Comment => {
if (c.id === id) {
return { ...c, is_liked, likes_count };
}
if (c.replies && c.replies.length > 0) {
return {
...c,
replies: c.replies.map(r => rollbackCommentLike(r))
};
if (c.replies?.length) {
return { ...c, replies: c.replies.map(r => rollbackLikeInTree(r)) };
}
return c;
};
setComments(prev => prev.map(c => rollbackCommentLike(c)));
setComments(prev => prev.map(c => rollbackLikeInTree(c)));
}
};
@@ -1373,7 +1360,7 @@ export const PostDetailScreen: React.FC = () => {
return (
<CommentItem
comment={item}
onLike={() => handleLikeComment(item)}
onLike={handleLikeComment}
onReply={() => handleReply(item)}
onUserPress={() => authorId && handleUserPress(authorId)}
floorNumber={index + 1}

View File

@@ -23,6 +23,7 @@ import {
borderRadius,
useThemePreference,
useSetThemePreference,
useThemeStore,
type AppColors,
} from '../../theme';
import { useAuthStore } from '../../stores';
@@ -204,29 +205,41 @@ export const SettingsScreen: React.FC = () => {
const handleItemPress = (key: string) => {
switch (key) {
case 'theme':
Alert.alert('主题模式', '选择应用界面明暗', [
{ text: '取消', style: 'cancel' },
{
text: '跟随系统',
onPress: () => {
void setThemePreference('system');
case 'theme': {
// 获取调试信息
const { Appearance } = require('react-native');
const systemScheme = Appearance?.getColorScheme?.() || 'undefined';
const resolvedScheme = useThemeStore.getState().resolvedScheme;
const storedPref = useThemeStore.getState().preference;
const storedSystem = useThemeStore.getState().systemScheme;
Alert.alert(
'主题模式',
`选择应用界面明暗\n\n[调试信息]\n系统主题: ${systemScheme}\n存储偏好: ${storedPref}\n存储系统: ${storedSystem}\n实际应用: ${resolvedScheme}`,
[
{ text: '取消', style: 'cancel' },
{
text: '跟随系统',
onPress: () => {
void setThemePreference('system');
},
},
},
{
text: '浅色',
onPress: () => {
void setThemePreference('light');
{
text: '浅色',
onPress: () => {
void setThemePreference('light');
},
},
},
{
text: '深色',
onPress: () => {
void setThemePreference('dark');
{
text: '深色',
onPress: () => {
void setThemePreference('dark');
},
},
},
]);
]
);
break;
}
case 'edit_profile':
router.push(hrefs.hrefProfileEdit());
break;

View File

@@ -1,7 +1,7 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useColorScheme } from 'react-native';
import { Appearance, AppState, AppStateStatus } from 'react-native';
import { create } from 'zustand';
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';
import { lightColors, darkColors, type AppColors } from '../theme/palettes';
import { buildPaperTheme } from '../theme/paperTheme';
import type { MD3Theme } from 'react-native-paper';
@@ -46,13 +46,19 @@ type ThemeState = {
hydrate: () => Promise<void>;
};
const initialSystem: ResolvedScheme = 'light';
/** 获取系统主题 */
function getSystemScheme(): ResolvedScheme {
const scheme = Appearance.getColorScheme();
return scheme === 'dark' ? 'dark' : 'light';
}
const initialSystemScheme = getSystemScheme();
export const useThemeStore = create<ThemeState>((set, get) => ({
preference: 'system',
systemScheme: initialSystem,
systemScheme: initialSystemScheme,
hydrated: false,
...buildState('system', initialSystem),
...buildState('system', initialSystemScheme),
setSystemScheme: (systemScheme) => {
const { preference, systemScheme: cur } = get();
@@ -86,10 +92,11 @@ export const useThemeStore = create<ThemeState>((set, get) => ({
} catch {
/* ignore */
}
const { systemScheme } = get();
const systemScheme = getSystemScheme();
set({
preference,
hydrated: true,
systemScheme,
...buildState(preference, systemScheme),
});
},
@@ -97,17 +104,42 @@ export const useThemeStore = create<ThemeState>((set, get) => ({
/** 在根布局中同步系统配色并恢复持久化偏好 */
export function ThemeBootstrap() {
const system = useColorScheme();
const setSystemScheme = useThemeStore((s) => s.setSystemScheme);
const hydrate = useThemeStore((s) => s.hydrate);
const appState = useRef<AppStateStatus>(AppState.currentState);
useEffect(() => {
void hydrate();
}, [hydrate]);
// 监听系统主题变化
useEffect(() => {
setSystemScheme(system === 'dark' ? 'dark' : 'light');
}, [system, setSystemScheme]);
const subscription = Appearance.addChangeListener(({ colorScheme }) => {
const newScheme = colorScheme === 'dark' ? 'dark' : 'light';
setSystemScheme(newScheme);
});
return () => {
subscription.remove();
};
}, [setSystemScheme]);
// 监听应用状态变化,从后台恢复时重新检测系统主题
useEffect(() => {
const subscription = AppState.addEventListener('change', (nextAppState) => {
if (
appState.current.match(/inactive|background/) &&
nextAppState === 'active'
) {
setSystemScheme(getSystemScheme());
}
appState.current = nextAppState;
});
return () => {
subscription.remove();
};
}, [setSystemScheme]);
return null;
}
@@ -131,3 +163,6 @@ export function useSetThemePreference() {
export function usePaperThemeFromStore() {
return useThemeStore((s) => s.paperTheme);
}
// 导出调试函数
export { getSystemScheme };