"use client" import { useState, useEffect } from "react" import Link from "next/link" import { ChevronLeft, Lock, Share2, BookOpen, Clock, MessageCircle } from "lucide-react" import { Button } from "@/components/ui/button" import { type Section, getFullBookPrice, isSectionUnlocked } from "@/lib/book-data" import { useStore } from "@/lib/store" import { AuthModal } from "./modules/auth/auth-modal" import { PaymentModal } from "./modules/payment/payment-modal" import { UserMenu } from "./user-menu" import { QRCodeModal } from "./modules/marketing/qr-code-modal" import { ReferralShare } from "./modules/referral/referral-share" interface ChapterContentProps { section: Section & { filePath: string } partTitle: string chapterTitle: string } export function ChapterContent({ section, partTitle, chapterTitle }: ChapterContentProps) { const [content, setContent] = useState("") const [isLoading, setIsLoading] = useState(true) const [isAuthOpen, setIsAuthOpen] = useState(false) const [isPaymentOpen, setIsPaymentOpen] = useState(false) const [isQRModalOpen, setIsQRModalOpen] = useState(false) const [paymentType, setPaymentType] = useState<"section" | "fullbook">("section") const [fullBookPrice, setFullBookPrice] = useState(9.9) const { user, isLoggedIn, hasPurchased, settings } = useStore() const distributorShare = settings?.distributorShare || 90 const isUnlocked = isSectionUnlocked(section) const canAccess = section.isFree || isUnlocked || (isLoggedIn && hasPurchased(section.id)) useEffect(() => { setFullBookPrice(getFullBookPrice()) }, []) useEffect(() => { async function loadContent() { try { if (section.content) { setContent(section.content) setIsLoading(false) return } if (typeof window !== "undefined" && section.filePath.startsWith("custom/")) { const customSections = JSON.parse(localStorage.getItem("custom_sections") || "[]") as Section[] const customSection = customSections.find((s) => s.id === section.id) if (customSection?.content) { setContent(customSection.content) setIsLoading(false) return } } const response = await fetch(`/api/content?path=${encodeURIComponent(section.filePath)}`) if (response.ok) { const data = await response.json() if (!data.isCustom) { setContent(data.content) } } } catch (error) { console.error("Failed to load content:", error) } finally { setIsLoading(false) } } loadContent() }, [section.filePath, section.id, section.content]) const handlePurchaseClick = (type: "section" | "fullbook") => { if (!isLoggedIn) { setIsAuthOpen(true) return } setPaymentType(type) setIsPaymentOpen(true) } const handleShare = async () => { const url = user?.referralCode ? `${window.location.href}?ref=${user.referralCode}` : window.location.href const shareData = { title: section.title, text: `来自Soul派对房的真实商业故事: ${section.title}`, url: url, } try { if (navigator.share && navigator.canShare && navigator.canShare(shareData)) { await navigator.share(shareData) } else { navigator.clipboard.writeText(url) alert( `链接已复制!分享后他人购买,你可获得${distributorShare}%返利 (¥${((fullBookPrice * distributorShare) / 100).toFixed(1)})`, ) } } catch (error) { if ((error as Error).name !== "AbortError") { navigator.clipboard.writeText(url) alert(`链接已复制!分享后他人购买,你可获得${distributorShare}%返利`) } } } const previewContent = content.slice(0, 500) return (
{/* Header */}
目录

{partTitle}

{chapterTitle &&

{chapterTitle}

}
{/* Content */}
{/* Title */}
{section.id} {section.unlockAfterDays && !section.isFree && ( {isUnlocked ? "已免费解锁" : `${section.unlockAfterDays}天后免费`} )}

{section.title}

{isLoading ? (
) : canAccess ? ( <>
{content.split("\n").map((paragraph, index) => (

{paragraph}

))}
{/* Join Party Group CTA */}

想听更多商业故事?

每天早上6-9点,卡若在Soul派对房分享真实案例

) : (
{previewContent.split("\n").map((paragraph, index) => (

{paragraph}

))}
{/* Purchase prompt */}

解锁完整内容

{isLoggedIn ? "购买本节或整本书以阅读完整内容" : "登录后购买即可阅读完整内容"}

{section.unlockAfterDays && (

本节将在{section.unlockAfterDays}天后免费解锁

)}

分享本书,他人购买你可获得 {distributorShare}%返利

{/* Join Party Group */}

不想花钱?来派对群免费听!

每天早上6-9点,卡若在Soul派对房免费分享

)} {/* Navigation */}
← 返回目录
{/* Modals */} setIsAuthOpen(false)} /> setIsPaymentOpen(false)} type={paymentType} sectionId={section.id} sectionTitle={section.title} amount={paymentType === "section" ? section.price : fullBookPrice} onSuccess={() => window.location.reload()} /> setIsQRModalOpen(false)} />
) }