'use client' import React, {useEffect, useMemo, useState} from 'react'; import Link from 'next/link'; import Image from 'next/image'; interface ProductMenuProps { products: ProductCategory[] } export default function CoreProducts({products}: ProductMenuProps) { const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL as string const filteredProducts = useMemo(() => { return (products ?? []).filter((item) => item.productCategoryName !== "软件产品") }, [products]) const orderedProducts = useMemo(() => { return [...filteredProducts].reverse() }, [filteredProducts]) const tabs = useMemo(() => { const allTypes = (orderedProducts ?? []).flatMap((category) => category.productTypes ?? []) return Array.from(new Set(allTypes.map((type) => type.productTypeName))) }, [orderedProducts]) const [activeTab, setActiveTab] = useState(() => tabs[0] ?? "") const [isPaused, setIsPaused] = useState(false) useEffect(() => { if (tabs.length === 0) return if (!activeTab || !tabs.includes(activeTab)) { setActiveTab(tabs[0]) } }, [tabs, activeTab]) useEffect(() => { if (tabs.length === 0 || isPaused) return const intervalId = setInterval(() => { setActiveTab((prev) => { const currentIndex = tabs.indexOf(prev) const safeIndex = currentIndex === -1 ? 0 : currentIndex const nextIndex = (safeIndex + 1) % tabs.length return tabs[nextIndex] }) }, 5000) return () => clearInterval(intervalId) }, [tabs, isPaused]) const showProducts = useMemo(() => { if (!activeTab) return [] const allTypes = (orderedProducts ?? []).flatMap((category) => category.productTypes ?? []) const matched = allTypes .filter((type) => type.productTypeName === activeTab) .flatMap((type) => type.productCenters ?? []) return matched.slice(0, 4) }, [orderedProducts, activeTab]) return (
{tabs.map((tab) => (
setActiveTab(tab)} className={`flex-1 min-w-36 sm:min-w-44 py-3 sm:py-4 text-center shuheiti transition-colors cursor-pointer ${ activeTab === tab ? "bg-blue-600 text-white font-semibold" : "bg-white text-gray-700 hover:bg-gray-100" }`} > {tab}
))}
setIsPaused(true)} onMouseLeave={() => setIsPaused(false)} > {showProducts.map((product) => (

{product.productName}

{product.productIntroduction}

{product.productName}
{product.productType} {product.productModel}
))}
); };