"use client"; import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { Link, useLocale, useTranslation } from "@/i18n/navigation"; import { useSearchParams } from "next/navigation"; import Footer from "@/app/components/Footer"; import { cities } from "@/app/data/cities"; import { fetchAuthMe } from "@/app/lib/api-client"; import IntentTabs from "./components/IntentTabs"; import CityFilter from "./components/CityFilter"; import LikesPanel from "./components/LikesPanel"; import SwipeStack from "./components/SwipeStack"; import MatchModal from "./components/MatchModal"; import AdvancedFilters from "./components/AdvancedFilters"; import SwipeQuota from "./components/SwipeQuota"; import { BADGE_KEYS } from "./constants"; import type { DatingProfile, MatchIntent } from "./types"; import type { MatchQuota } from "./utils"; import { fetchCandidates, fetchLikes, fetchMutualMatches, fetchQuota, filterFallbackProfiles, formatTimeAgo, saveSwipe, undoSwipe, } from "./utils"; function DatingSkeleton() { return (
); } function DatingPageInner() { const { t } = useTranslation("match"); const locale = useLocale(); const searchParams = useSearchParams(); const initialIntent = (searchParams.get("intent") as MatchIntent) || "friends"; const initialCity = searchParams.get("city") || "all"; const [activeTab, setActiveTab] = useState( ["friends", "dating", "partner", "roommate", "cofounder", "explore"].includes(initialIntent) ? initialIntent : "friends" ); const [cityFilter, setCityFilter] = useState(initialCity); const [profiles, setProfiles] = useState([]); const [likes, setLikes] = useState([]); const [mutual, setMutual] = useState([]); const [currentIndex, setCurrentIndex] = useState(0); const [loading, setLoading] = useState(true); const [usingFallback, setUsingFallback] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const [isVip, setIsVip] = useState(false); const [quota, setQuota] = useState({ vip: false, limit: 25, used: 0, remaining: 25 }); const [advancedFilters, setAdvancedFilters] = useState({ gender: "all", single: "all" }); const [showLoginPrompt, setShowLoginPrompt] = useState(false); const [showQuotaPrompt, setShowQuotaPrompt] = useState(false); const [canUndo, setCanUndo] = useState(false); const [matchCelebration, setMatchCelebration] = useState<{ profile: DatingProfile; conversationId?: string; } | null>(null); const [likingBackId, setLikingBackId] = useState(null); const [mobilePanelOpen, setMobilePanelOpen] = useState(false); const [dragX, setDragX] = useState(0); const [isDragging, setIsDragging] = useState(false); const [isAnimatingOut, setIsAnimatingOut] = useState<"left" | "right" | null>(null); const startXRef = useRef(0); const dragXRef = useRef(0); const swipeLockRef = useRef(false); const lastSwipedRef = useRef(null); const cityOptions = useMemo( () => cities.map((city) => ({ slug: city.slug || city.name, name: city.name, nameEn: city.nameEn, })), [] ); const refreshSidePanels = useCallback(async () => { if (!isLoggedIn) { setLikes([]); setMutual([]); return; } try { const [likesData, mutualData] = await Promise.all([fetchLikes(), fetchMutualMatches()]); setLikes(likesData); setMutual(mutualData); } catch { setLikes([]); setMutual([]); } }, [isLoggedIn]); const refreshQuota = useCallback(async () => { if (!isLoggedIn) return; try { const data = await fetchQuota(); setQuota(data); setIsVip(data.vip); } catch { // ignore } }, [isLoggedIn]); const loadCandidates = useCallback( async (intent: MatchIntent, city: string, advanced = advancedFilters) => { setLoading(true); setCurrentIndex(0); setCanUndo(false); try { const items = await fetchCandidates(intent, city, advanced); if (items.length) { setProfiles(items); setUsingFallback(false); } else { const fallback = filterFallbackProfiles(intent, city, advanced); setProfiles(fallback); setUsingFallback(true); } } catch { const fallback = filterFallbackProfiles(intent, city, advanced); setProfiles(fallback); setUsingFallback(true); } finally { setLoading(false); } }, [advancedFilters] ); useEffect(() => { fetchAuthMe().then((data) => setIsLoggedIn(Boolean(data?.user?.id))); }, []); useEffect(() => { if (isLoggedIn) refreshQuota(); }, [isLoggedIn, refreshQuota]); useEffect(() => { loadCandidates(activeTab, cityFilter, advancedFilters); }, [activeTab, cityFilter, advancedFilters, loadCandidates]); useEffect(() => { refreshSidePanels(); }, [refreshSidePanels]); useEffect(() => { const params = new URLSearchParams(); params.set("intent", activeTab); if (cityFilter && cityFilter !== "all") params.set("city", cityFilter); const next = `/dating?${params.toString()}`; window.history.replaceState(null, "", next); }, [activeTab, cityFilter]); const currentProfile = profiles[currentIndex]; const nextProfile = profiles[currentIndex + 1]; const hasMoreProfiles = currentIndex < profiles.length - 1; const badgeText = t(BADGE_KEYS[activeTab]); const formatTime = (value?: string) => formatTimeAgo(value, locale); const advanceProfile = () => { window.setTimeout(() => { setCurrentIndex((index) => (hasMoreProfiles ? index + 1 : index)); dragXRef.current = 0; setDragX(0); setIsAnimatingOut(null); swipeLockRef.current = false; }, 180); }; const performSwipe = async (action: "like" | "dislike" | "superlike") => { if (!currentProfile || swipeLockRef.current) return; if (!isLoggedIn) { setShowLoginPrompt(true); dragXRef.current = 0; setDragX(0); return; } if (!quota.vip && quota.remaining <= 0) { setShowQuotaPrompt(true); dragXRef.current = 0; setDragX(0); return; } if (action === "superlike" && !quota.vip && !isVip) { setShowQuotaPrompt(true); return; } swipeLockRef.current = true; setIsAnimatingOut(action === "dislike" ? "left" : "right"); const animX = action === "dislike" ? -260 : 260; dragXRef.current = animX; setDragX(animX); try { if (!usingFallback) { const result = await saveSwipe(currentProfile.id, action, activeTab); lastSwipedRef.current = currentProfile; setCanUndo(true); if (result.matched) { setMatchCelebration({ profile: currentProfile, conversationId: result.match?.conversationId, }); refreshSidePanels(); } else if (action === "like" || action === "superlike") { refreshSidePanels(); } refreshQuota(); } else { setCanUndo(false); } advanceProfile(); } catch (err) { const message = err instanceof Error ? err.message : ""; if (message.includes("滑动次数") || message.includes("升级会员") || message.includes("超级赞")) { setShowQuotaPrompt(true); } else if (message.includes("完善加入资料") || message.includes("登录")) { setShowLoginPrompt(true); } else if (message) { setShowLoginPrompt(true); } swipeLockRef.current = false; setIsAnimatingOut(null); dragXRef.current = 0; setDragX(0); } }; const handleUndo = async () => { const lastProfile = lastSwipedRef.current; if (!canUndo || !lastProfile) return; try { if (!usingFallback) await undoSwipe(); setProfiles((prev) => { if (prev.some((profile) => profile.id === lastProfile.id)) return prev; const next = [...prev]; next.splice(currentIndex, 0, lastProfile); return next; }); lastSwipedRef.current = null; setCanUndo(false); refreshQuota(); } catch { // ignore } }; const handleLike = () => performSwipe("like"); const handleDislike = () => performSwipe("dislike"); const handleSuperLike = () => performSwipe("superlike"); const beginDrag = (clientX: number) => { startXRef.current = clientX; setIsDragging(true); }; const moveDrag = (clientX: number) => { if (!isDragging) return; const next = Math.max(-180, Math.min(180, clientX - startXRef.current)); dragXRef.current = next; setDragX(next); }; const endDrag = () => { if (!isDragging) return; setIsDragging(false); const dx = dragXRef.current; if (dx > 90) { handleLike(); return; } if (dx < -90) { handleDislike(); return; } dragXRef.current = 0; setDragX(0); }; const jumpToProfile = (profile: DatingProfile) => { const index = profiles.findIndex((item) => item.id === profile.id); if (index >= 0) setCurrentIndex(index); setMobilePanelOpen(false); }; const handleLikeBack = async (profile: DatingProfile) => { if (!isLoggedIn) { setShowLoginPrompt(true); return; } if (usingFallback) { jumpToProfile(profile); return; } setLikingBackId(profile.id); try { const result = await saveSwipe(profile.id, "like", activeTab); if (result.matched) { setMatchCelebration({ profile, conversationId: result.match?.conversationId, }); } refreshSidePanels(); refreshQuota(); } catch (err) { const message = err instanceof Error ? err.message : ""; if (message.includes("滑动次数") || message.includes("升级会员")) { setShowQuotaPrompt(true); } } finally { setLikingBackId(null); } }; return (

{t("heroTitle")}

{t("heroSubtitle")}

{t("editProfile")}
{isLoggedIn ? : null} {!isLoggedIn ? (
{t("loginHint")}{" "} {t("loginAction")}
) : null} {usingFallback ? (

{t("fallbackHint")}

) : null}
{loading ? (
) : currentProfile ? ( <>

{currentIndex + 1} / {profiles.length}

{t("keyboardHint")}

) : (
🌏

{t("emptyTitle")}

{t("emptySubtitle")}

{t("editProfile")}
)}
{mobilePanelOpen ? (
) : null} {showQuotaPrompt ? (

{t("quotaExceededTitle")}

{t("quotaExceededBody")}

{t("upgradeVip")}
) : null} {showLoginPrompt ? (

{t("loginRequiredTitle")}

{t("loginRequiredBody")}

{t("loginAction")}
) : null} setMatchCelebration(null)} t={t} />
); } export default function DatingPage() { return ( }> ); }