// src/views/Community/components/HeroPanel.js // 顶部说明面板组件:事件中心 + 沪深指数K线图 + 热门概念3D动画 import React, { useEffect, useState, useMemo, useCallback, useRef } from 'react'; import { Box, Card, CardBody, Flex, VStack, HStack, Text, Heading, useColorModeValue, Icon, Spinner, Center, } from '@chakra-ui/react'; import { AlertCircle, Clock, TrendingUp, Info } from 'lucide-react'; import ReactECharts from 'echarts-for-react'; import { logger } from '../../../utils/logger'; // 定义动画 const animations = ` @keyframes pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.6; transform: scale(1.1); } } @keyframes shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } } @keyframes floatSlow { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } } `; // 注入样式 if (typeof document !== 'undefined') { const styleId = 'hero-panel-animations'; if (!document.getElementById(styleId)) { const styleSheet = document.createElement('style'); styleSheet.id = styleId; styleSheet.innerText = animations; document.head.appendChild(styleSheet); } } /** * 获取指数行情数据(日线数据) */ const fetchIndexKline = async (indexCode) => { try { const response = await fetch(`/api/index/${indexCode}/kline?type=daily`); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); const data = await response.json(); return data; } catch (error) { logger.error('HeroPanel', 'fetchIndexKline error', { indexCode, error: error.message }); return null; } }; /** * 获取热门概念数据 */ const fetchPopularConcepts = async () => { try { const response = await fetch('/concept-api/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query: '', size: 60, page: 1, sort_by: 'change_pct' }) }); const data = await response.json(); if (data.results?.length > 0) { return data.results.map(item => ({ name: item.concept, change_pct: item.price_info?.avg_change_pct || 0, })); } return []; } catch (error) { logger.error('HeroPanel', 'fetchPopularConcepts error', error); return []; } }; /** * 判断当前是否在交易时间内 */ const isInTradingTime = () => { const now = new Date(); const timeInMinutes = now.getHours() * 60 + now.getMinutes(); return timeInMinutes >= 570 && timeInMinutes <= 900; }; /** * 紧凑型K线指数卡片 */ const CompactIndexCard = ({ indexCode, indexName }) => { const [chartData, setChartData] = useState(null); const [loading, setLoading] = useState(true); const [latestData, setLatestData] = useState(null); const upColor = '#ec0000'; const downColor = '#00da3c'; const loadData = useCallback(async () => { const data = await fetchIndexKline(indexCode); if (data?.data?.length > 0) { const latest = data.data[data.data.length - 1]; const prevClose = latest.prev_close || latest.close; setLatestData({ close: latest.close, change: prevClose ? (((latest.close - prevClose) / prevClose) * 100).toFixed(2) : '0.00', isPositive: latest.close >= prevClose }); const recentData = data.data.slice(-40); setChartData({ dates: recentData.map(item => item.time), klineData: recentData.map(item => [item.open, item.close, item.low, item.high]), rawData: recentData }); } setLoading(false); }, [indexCode]); useEffect(() => { loadData(); }, [loadData]); const chartOption = useMemo(() => { if (!chartData) return {}; return { backgroundColor: 'transparent', grid: { left: 5, right: 5, top: 5, bottom: 5, containLabel: false }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross', lineStyle: { color: 'rgba(255, 215, 0, 0.5)', width: 1, type: 'dashed' } }, backgroundColor: 'rgba(20, 20, 20, 0.95)', borderColor: '#FFD700', borderWidth: 1, textStyle: { color: '#fff', fontSize: 10, fontFamily: 'monospace' }, padding: [6, 10], formatter: (params) => { const idx = params[0].dataIndex; const raw = chartData.rawData[idx]; if (!raw) return ''; const prevClose = raw.prev_close || raw.open; const changePct = prevClose ? (((raw.close - prevClose) / prevClose) * 100).toFixed(2) : '0.00'; const isUp = raw.close >= prevClose; const color = isUp ? '#ec0000' : '#00da3c'; return `