feat: 修复评论数量问题

This commit is contained in:
2025-09-24 15:33:01 +08:00
parent e89ed5943c
commit ea2f4bb905
3 changed files with 18 additions and 6 deletions

View File

@@ -249,13 +249,18 @@ export default forwardRef(function Comments(
const [comments, setComments] = useState<Comment[]>([]); const [comments, setComments] = useState<Comment[]>([]);
const inputRef = useRef<CommentInputRef>(null); const inputRef = useRef<CommentInputRef>(null);
const commentCountUpdateRef = useRef()
useEffect(() => { useEffect(() => {
getComments(1); getComments(1);
}, [game_id]); }, [game_id]);
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
addComment: handleReply, addComment: handleReply,
getCommentCount: () => comments.length, getCommentCount: (onUpdate) => {
commentCountUpdateRef.current = onUpdate
onUpdate(comments.length)
},
})); }));
async function getComments(page) { async function getComments(page) {
@@ -270,6 +275,7 @@ export default forwardRef(function Comments(
setComments((prev) => { setComments((prev) => {
const res = [...prev]; const res = [...prev];
res.splice(page * PAGESIZE - 1, newComments.length, ...newComments); res.splice(page * PAGESIZE - 1, newComments.length, ...newComments);
commentCountUpdateRef.current?.(res.length)
return res; return res;
}); });
} }
@@ -297,6 +303,7 @@ export default forwardRef(function Comments(
item.reply_count = res.data.count; item.reply_count = res.data.count;
} }
}); });
commentCountUpdateRef.current?.(newComments.length)
return newComments; return newComments;
}); });
} }
@@ -318,6 +325,7 @@ export default forwardRef(function Comments(
const res = await CommentServices.createComment({ game_id, content: val }); const res = await CommentServices.createComment({ game_id, content: val });
if (res.code === 0) { if (res.code === 0) {
setComments((prev) => { setComments((prev) => {
commentCountUpdateRef.current?.(prev.length + 1)
return [{ ...res.data, replies: [] }, ...prev]; return [{ ...res.data, replies: [] }, ...prev];
}); });
toast("发布成功"); toast("发布成功");
@@ -367,7 +375,7 @@ export default forwardRef(function Comments(
}); });
} else { } else {
setComments((prev) => { setComments((prev) => {
console.log(prev, parent_id, id); commentCountUpdateRef.current?.(prev.length - 1)
return prev.filter((item) => item.id !== id); return prev.filter((item) => item.id !== id);
}); });
} }

View File

@@ -247,6 +247,7 @@ function isFull(counts) {
// 底部操作栏 // 底部操作栏
function StickyButton(props) { function StickyButton(props) {
const { handleShare, handleJoinGame, detail, onStatusChange, handleAddComment, getCommentCount } = props; const { handleShare, handleJoinGame, detail, onStatusChange, handleAddComment, getCommentCount } = props;
const [commentCount, setCommentCount] = useState(0)
const ntrpRef = useRef(null); const ntrpRef = useRef(null);
const { id, price, user_action_status, match_status, start_time, end_time, is_organizer } = const { id, price, user_action_status, match_status, start_time, end_time, is_organizer } =
detail || {}; detail || {};
@@ -258,6 +259,12 @@ function StickyButton(props) {
ntrpRef?.current?.show(); ntrpRef?.current?.show();
} }
useEffect(() => {
getCommentCount?.((count) => {
setCommentCount(count)
})
}, [getCommentCount])
function generateTextAndAction( function generateTextAndAction(
user_action_status: null | { [key: string]: boolean } user_action_status: null | { [key: string]: boolean }
): undefined | { text: string | React.FC; action?: () => void; available?: boolean } { ): undefined | { text: string | React.FC; action?: () => void; available?: boolean } {
@@ -370,8 +377,6 @@ function StickyButton(props) {
}; };
} }
const commentCount = getCommentCount()
return ( return (
<> <>
<View className="sticky-bottom-bar"> <View className="sticky-bottom-bar">
@@ -1224,7 +1229,7 @@ function Index() {
detail={detail} detail={detail}
onStatusChange={onStatusChange} onStatusChange={onStatusChange}
handleAddComment={() => { commentRef.current && commentRef.current.addComment() }} handleAddComment={() => { commentRef.current && commentRef.current.addComment() }}
getCommentCount={() => commentRef.current && commentRef.current.getCommentCount()} getCommentCount={commentRef.current && commentRef.current.getCommentCount}
/> />
{/* share popup */} {/* share popup */}
<SharePopup <SharePopup

View File

@@ -218,7 +218,6 @@ function GameInfo(props) {
} }
} }
console.log(gameNotice)
return ( return (
<View className={styles.gameInfoContainer}> <View className={styles.gameInfoContainer}>
{["refund", "progress", "expired"].includes(orderStatus) && ( {["refund", "progress", "expired"].includes(orderStatus) && (