'use client'; import React, { useState } from "react"; import Image from "next/image"; import { SimpleCarousel } from "./SimpleCarousel"; import type { HeroData } from "../types"; interface HomeHeroCarouselProps { data: HeroData; } export function HomeHeroCarousel({ data }: HomeHeroCarouselProps) { const { eyebrow, title, subtitle, carousel } = data; // 跟踪第一张图片是否已加载完成 const [isFirstImageLoaded, setIsFirstImageLoaded] = useState(false); return (
{eyebrow && (

{eyebrow}

)}

{title}

{subtitle}

{ // 处理第一张图片的加载完成事件 const handleImageLoad = () => { if (index === 0) { setIsFirstImageLoaded(true); } }; // 如果有文字,根据 layout 决定布局方式 if (item.text && item.text.length > 0) { // 上下布局(上面文字,下面图片) if (item.layout === "vertical" && item.imageBottom) { return (
{/* 上面:文字内容 */}
{item.title && (

{item.title}

)}
{item.text.map((paragraph, index) => { // 如果有高亮关键词,渲染带高亮的段落 if (item.highlights && item.highlights.length > 0) { let parts: (string | JSX.Element)[] = [paragraph]; item.highlights.forEach((highlight, highlightIndex) => { const newParts: (string | JSX.Element)[] = []; parts.forEach((part) => { if (typeof part === 'string') { const regex = new RegExp(`(${highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi'); const matches = part.split(regex); matches.forEach((match, matchIndex) => { if (matchIndex % 2 === 1) { newParts.push( {match} ); } else if (match) { newParts.push(match); } }); } else { newParts.push(part); } }); parts = newParts; }); return (

{parts}

); } return

{paragraph}

; })} {/* 统计信息框 */} {item.stats && (
{item.highlights && item.highlights.length > 0 ? (() => { let parts: (string | JSX.Element)[] = [item.stats]; item.highlights.forEach((highlight, highlightIndex) => { const newParts: (string | JSX.Element)[] = []; parts.forEach((part) => { if (typeof part === 'string') { const regex = new RegExp(`(${highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi'); const matches = part.split(regex); matches.forEach((match, matchIndex) => { if (matchIndex % 2 === 1) { newParts.push( {match} ); } else if (match) { newParts.push(match); } }); } else { newParts.push(part); } }); parts = newParts; }); return

{parts}

; })() : (

{item.stats}

)}
)}
{/* 下面:图片 */}
{item.alt}
); } // 左右布局(左边文字,右边图片) return (
{/* 左边:文字内容 */}
{item.title && (

{item.title}

)}
{item.text.map((paragraph, index) => { // 如果有高亮关键词,渲染带高亮的段落 if (item.highlights && item.highlights.length > 0) { let parts: (string | JSX.Element)[] = [paragraph]; item.highlights.forEach((highlight, highlightIndex) => { const newParts: (string | JSX.Element)[] = []; parts.forEach((part) => { if (typeof part === 'string') { const regex = new RegExp(`(${highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi'); const matches = part.split(regex); matches.forEach((match, matchIndex) => { if (matchIndex % 2 === 1) { newParts.push( {match} ); } else if (match) { newParts.push(match); } }); } else { newParts.push(part); } }); parts = newParts; }); return (

{parts}

); } return

{paragraph}

; })} {/* 统计信息框 */} {item.stats && (
{item.highlights && item.highlights.length > 0 ? (() => { let parts: (string | JSX.Element)[] = [item.stats]; item.highlights.forEach((highlight) => { const newParts: (string | JSX.Element)[] = []; parts.forEach((part) => { if (typeof part === 'string') { const regex = new RegExp(`(${highlight.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`, 'gi'); const matches = part.split(regex); matches.forEach((match, matchIndex) => { if (matchIndex % 2 === 1) { newParts.push( {match} ); } else if (match) { newParts.push(match); } }); } else { newParts.push(part); } }); parts = newParts; }); return

{parts}

; })() : (

{item.stats}

)}
)}
{/* 右边:图片和 KPI 指标 */}
{/* 图片区域 */}
{item.alt}
{/* KPI 指标卡片 */} {item.kpis && item.kpis.length > 0 && (
{item.kpis.map((kpi, index) => (
{kpi.value}
{kpi.label}
))}
)}
); } // 没有文字,使用原来的全屏图片布局 return (
{item.alt}
); }} keyExtractor={(item) => item.id} interval={6000} />
); }