Update Review.js
This commit is contained in:
+51
-25
@@ -1,4 +1,4 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useRef, useCallback } from "react";
|
||||
import { Edit3, Loader2 } from "lucide-react";
|
||||
import {
|
||||
getDeals,
|
||||
@@ -22,6 +22,7 @@ export default function Review() {
|
||||
const [editingDeal, setEditingDeal] = useState(null);
|
||||
const [showReviewed, setShowReviewed] = useState(true);
|
||||
const [keyword, setKeyword] = useState("");
|
||||
const sentinelRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadCategories();
|
||||
@@ -90,10 +91,24 @@ export default function Review() {
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function handleLoadMore() {
|
||||
const handleLoadMore = useCallback(() => {
|
||||
if (loadingMore || page >= totalPages) return;
|
||||
loadReviewDeals(page + 1, true);
|
||||
}
|
||||
}, [loadingMore, page, totalPages, showReviewed, keyword]);
|
||||
|
||||
// 自动滚动加载:哨兵元素进入视口时触发
|
||||
useEffect(() => {
|
||||
const sentinel = sentinelRef.current;
|
||||
if (!sentinel) return;
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (entries[0].isIntersecting) handleLoadMore();
|
||||
},
|
||||
{ rootMargin: "200px" },
|
||||
);
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
}, [handleLoadMore]);
|
||||
|
||||
async function handleUpdateDeal(id, updates) {
|
||||
try {
|
||||
@@ -225,29 +240,40 @@ export default function Review() {
|
||||
</div>
|
||||
))}
|
||||
|
||||
{/* Load More */}
|
||||
{/* 自动加载哨兵 */}
|
||||
{hasMore && (
|
||||
<div style={{ textAlign: "center", padding: "16px 0 4px" }}>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={handleLoadMore}
|
||||
disabled={loadingMore}
|
||||
style={{ minWidth: 160 }}
|
||||
>
|
||||
{loadingMore ? (
|
||||
<>
|
||||
<Loader2
|
||||
size={14}
|
||||
style={{ animation: "spin 0.8s linear infinite" }}
|
||||
/>{" "}
|
||||
加载中...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
加载更多({deals.length} / {total})
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
<div
|
||||
ref={sentinelRef}
|
||||
style={{ textAlign: "center", padding: "24px 0 4px" }}
|
||||
>
|
||||
{loadingMore && (
|
||||
<span
|
||||
style={{ fontSize: 13, color: "var(--text-secondary)" }}
|
||||
>
|
||||
<Loader2
|
||||
size={14}
|
||||
style={{
|
||||
animation: "spin 0.8s linear infinite",
|
||||
marginRight: 6,
|
||||
verticalAlign: "middle",
|
||||
}}
|
||||
/>
|
||||
加载中...({deals.length} / {total})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{/* 全部加载完成 */}
|
||||
{!hasMore && deals.length > 0 && (
|
||||
<div
|
||||
style={{
|
||||
textAlign: "center",
|
||||
padding: "24px 0 4px",
|
||||
fontSize: 13,
|
||||
color: "var(--text-secondary)",
|
||||
}}
|
||||
>
|
||||
已全部加载 · 共 {total} 笔
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user