diff --git a/src/app/api/room/[id]/route.ts b/src/app/api/room/[id]/route.ts index 954ea11..c3b0fca 100644 --- a/src/app/api/room/[id]/route.ts +++ b/src/app/api/room/[id]/route.ts @@ -51,6 +51,7 @@ export async function GET( matchType, matchLikes, likeCounts, + swipeCounts: data.swipeCounts, restaurants: data.restaurants, }); } catch (e) { diff --git a/src/app/api/room/[id]/swipe/route.ts b/src/app/api/room/[id]/swipe/route.ts index 2caca9e..fd02522 100644 --- a/src/app/api/room/[id]/swipe/route.ts +++ b/src/app/api/room/[id]/swipe/route.ts @@ -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] = []; diff --git a/src/app/room/[id]/page.tsx b/src/app/room/[id]/page.tsx index 51ff22e..1665216 100644 --- a/src/app/room/[id]/page.tsx +++ b/src/app/room/[id]/page.tsx @@ -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} diff --git a/src/components/SwipeDeck.tsx b/src/components/SwipeDeck.tsx index 97a41db..3706138 100644 --- a/src/components/SwipeDeck.tsx +++ b/src/components/SwipeDeck.tsx @@ -13,6 +13,7 @@ interface SwipeDeckProps { restaurants: Restaurant[]; roomId: string; userId: string; + initialIndex: number; matchedRestaurantId: string | null; matchType: MatchType; matchLikes: number; @@ -25,6 +26,7 @@ export default function SwipeDeck({ restaurants, roomId, userId, + initialIndex, matchedRestaurantId, matchType, matchLikes, @@ -32,13 +34,15 @@ export default function SwipeDeck({ userCount, onReset, }: SwipeDeckProps) { - const [currentIndex, setCurrentIndex] = useState(0); + const [currentIndex, setCurrentIndex] = useState(initialIndex); const [showMatch, setShowMatch] = useState(false); const [localMatchId, setLocalMatchId] = useState(null); const [resetting, setResetting] = useState(false); const [bubble, setBubble] = useState(""); - const [guideVisible, setGuideVisible] = useState(true); - const [swipeHistory, setSwipeHistory] = useState([]); + const [guideVisible, setGuideVisible] = useState(initialIndex === 0); + const [swipeHistory, setSwipeHistory] = useState( + () => restaurants.slice(0, initialIndex).map((r) => r.id), + ); const swipeFnRef = useRef<((direction: SwipeDirection) => void) | null>(null); const swipingRef = useRef(false); const prevLikeCounts = useRef>({}); diff --git a/src/hooks/useRoomPolling.ts b/src/hooks/useRoomPolling.ts index 03d9012..23d49d8 100644 --- a/src/hooks/useRoomPolling.ts +++ b/src/hooks/useRoomPolling.ts @@ -30,6 +30,7 @@ export function useRoomPolling(roomId: string) { matchType: data?.matchType ?? null, matchLikes: data?.matchLikes ?? 0, likeCounts: data?.likeCounts ?? {}, + swipeCounts: data?.swipeCounts ?? {}, restaurants: data?.restaurants ?? [], isLoading, error, diff --git a/src/types/index.ts b/src/types/index.ts index cae0294..626a4f5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -24,5 +24,6 @@ export interface RoomStatus { matchType: MatchType; matchLikes: number; likeCounts: Record; + swipeCounts: Record; restaurants: Restaurant[]; }