This commit is contained in:
eric
2026-03-13 05:51:29 -05:00
parent 8a095f7d45
commit 15dba690d3
9 changed files with 103 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
"use client";
import { useState, useCallback, useEffect } from "react";
import { useState, useCallback, useEffect, useRef } from "react";
import { Link, useLocale } from "@/i18n/navigation";
import { useTranslation } from "@/i18n/navigation";
import JoinModal from "./JoinModal";
@@ -85,6 +85,9 @@ export default function HeroSection() {
return () => window.removeEventListener("auth:updated", onAuth);
}, []);
const checkUserCacheRef = useRef<{ email: string; data: { exists: boolean; vip: boolean }; ts: number } | null>(null);
const CACHE_TTL_MS = 60_000;
const handleCheckAndProceed = useCallback(
async (em: string) => {
if (checking) return;
@@ -96,6 +99,15 @@ export default function HeroSection() {
setError(null);
setChecking(true);
try {
const cached = checkUserCacheRef.current;
if (cached && cached.email === trimmed && Date.now() - cached.ts < CACHE_TTL_MS) {
const { exists, vip } = cached.data;
if (exists) {
setJoinModalVipOnly(!!vip);
setJoinModalOpen(true);
return;
}
}
const checkRes = await fetch("/api/meetup/check-user", {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -107,6 +119,7 @@ export default function HeroSection() {
throw new Error(checkData?.error || checkData?.detail || (locale === "zh" ? "操作失败" : "Operation failed"));
}
if (checkData.exists) {
checkUserCacheRef.current = { email: trimmed, data: { exists: true, vip: !!checkData.vip }, ts: Date.now() };
if (checkData.vip) {
setJoinModalOpen(true);
setJoinModalVipOnly(true);
@@ -136,6 +149,9 @@ export default function HeroSection() {
(locale === "zh" ? "创建用户失败" : "Failed to create user")
);
}
if (ensureData.is_new) {
await fetch("/api/meetup/set-needs-password-change", { method: "POST", credentials: "include" });
}
user_id = String(ensureData.user_id).trim();
if (ensureData?.token && ensureData?.record) {
await fetch("/api/auth/sync-session", {