fix: 刷新页面后恢复滑动进度,防止重复 swipe

服务端 GET room 返回 swipeCounts,前端据此恢复 currentIndex、
swipeHistory 和引导状态;swipe API 增加幂等性检查,跳过已滑过的卡片。
This commit is contained in:
2026-02-24 18:40:42 +08:00
parent d83e5ec6c4
commit 998d0a4e15
6 changed files with 20 additions and 4 deletions
+1
View File
@@ -51,6 +51,7 @@ export async function GET(
matchType,
matchLikes,
likeCounts,
swipeCounts: data.swipeCounts,
restaurants: data.restaurants,
});
} catch (e) {
+7
View File
@@ -20,6 +20,13 @@ export async function POST(
const rid = String(restaurantId);
const updated = await atomicUpdateRoom(id, (data) => {
const restaurantIndex = data.restaurants.findIndex((r) => r.id === rid);
const alreadySwiped =
restaurantIndex >= 0 &&
restaurantIndex < (data.swipeCounts[userId] ?? 0);
if (alreadySwiped) return data;
if (action === "like") {
if (!data.likes[rid]) {
data.likes[rid] = [];
+3 -1
View File
@@ -15,7 +15,7 @@ export default function RoomPage() {
const [joined, setJoined] = useState(false);
const {
userCount, match, matchType, matchLikes, likeCounts, restaurants, mutate,
userCount, match, matchType, matchLikes, likeCounts, swipeCounts, restaurants, mutate,
} = useRoomPolling(roomId);
useEffect(() => {
@@ -34,6 +34,7 @@ export default function RoomPage() {
await mutate();
}, [roomId, mutate]);
const initialIndex = swipeCounts[userId] ?? 0;
const ready = joined && userId && restaurants.length > 0;
if (!ready) {
@@ -52,6 +53,7 @@ export default function RoomPage() {
restaurants={restaurants}
roomId={roomId}
userId={userId}
initialIndex={initialIndex}
matchedRestaurantId={match}
matchType={matchType}
matchLikes={matchLikes}