"use client" import { useState, useEffect, useRef, useCallback } from "react" import { useRouter } from "next/navigation" import { ChevronRight, Sparkles, Lock, Share2 } from "lucide-react" import { useStore } from "@/lib/store" import { getFullBookPrice, getTotalSectionCount } from "@/lib/book-data" import { AuthModal } from "@/components/modules/auth/auth-modal" import { PaymentModal } from "@/components/payment-modal" export default function HomePage() { const router = useRouter() const { user, isLoggedIn, hasPurchased } = useStore() const [content, setContent] = useState("") const [isLoading, setIsLoading] = useState(true) const [showPaywall, setShowPaywall] = useState(false) const [readingProgress, setReadingProgress] = useState(0) const [isAuthOpen, setIsAuthOpen] = useState(false) const [isPaymentOpen, setIsPaymentOpen] = useState(false) const [paymentType, setPaymentType] = useState<"section" | "fullbook">("section") const contentRef = useRef(null) const fullBookPrice = getFullBookPrice() const totalSections = getTotalSectionCount() const hasFullBook = user?.hasFullBook || false // 最新章节 - 使用1.1作为示例 const latestSection = { id: "1.1", title: "荷包:电动车出租的被动收入模式", price: 1, isFree: true, filePath: "book/第一篇|真实的人/第1章|人与人之间的底层逻辑/1.1 荷包:电动车出租的被动收入模式.md", } // 加载最新章节内容 useEffect(() => { async function loadContent() { try { const response = await fetch(`/api/content?path=${encodeURIComponent(latestSection.filePath)}`) if (response.ok) { const data = await response.json() setContent(data.content || "") } } catch (error) { console.error("Failed to load content:", error) } finally { setIsLoading(false) } } loadContent() }, []) // 监听滚动进度 const handleScroll = useCallback(() => { if (!contentRef.current) return const scrollTop = window.scrollY const docHeight = document.documentElement.scrollHeight - window.innerHeight const progress = docHeight > 0 ? Math.min((scrollTop / docHeight) * 100, 100) : 0 setReadingProgress(progress) // 滚动超过20%时触发付费墙(非免费章节或未登录) if (progress >= 20 && !hasFullBook && !latestSection.isFree) { setShowPaywall(true) } }, [hasFullBook]) useEffect(() => { window.addEventListener("scroll", handleScroll) return () => window.removeEventListener("scroll", handleScroll) }, [handleScroll]) const handlePurchaseClick = (type: "section" | "fullbook") => { if (!isLoggedIn) { setIsAuthOpen(true) return } setPaymentType(type) setIsPaymentOpen(true) } const contentLines = content.split("\n").filter((line) => line.trim()) // 如果需要付费墙,只显示前20%内容 const displayContent = showPaywall && !hasFullBook && !latestSection.isFree ? contentLines.slice(0, Math.ceil(contentLines.length * 0.2)).join("\n") : content return (
{/* 阅读进度条 */}
{/* 顶部标题区 */}
Soul派对·创业实验 最新
{/* 章节标题 */}
{latestSection.id} {latestSection.isFree && ( 免费 )}

{latestSection.title}

第一篇|真实的人

{/* 内容区域 */}
{isLoading ? (
{[...Array(8)].map((_, i) => (
))}
) : (
{displayContent.split("\n").map( (paragraph, index) => paragraph.trim() && (

{paragraph}

), )}
)} {/* 付费墙 - 只在非免费章节且滚动超过20%时显示 */} {showPaywall && !hasFullBook && !latestSection.isFree && (
{/* 渐变遮罩 */}
{/* 付费卡片 */}

解锁完整内容

{isLoggedIn ? "支付1元继续阅读" : "登录并支付1元继续阅读"}

分享给好友购买,你可获得90%佣金

)} {/* 底部引导 - 免费章节或已购买时显示 */} {!showPaywall && (

想看更多内容?

共 {totalSections} 章精彩内容

)}
{/* 底部导航 */} {/* 登录弹窗 */} setIsAuthOpen(false)} /> {/* 支付弹窗 */} setIsPaymentOpen(false)} type={paymentType} sectionId={latestSection.id} sectionTitle={latestSection.title} amount={paymentType === "section" ? latestSection.price : fullBookPrice} onSuccess={() => window.location.reload()} />
) }