import { useEffect, useState, useCallback, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import { initStore } from '../core/store'; import BomBrowser from '../components/BomBrowser'; import MiningView from '../components/MiningView'; import SimilarityView from '../components/SimilarityView'; import StandardLibrary from '../components/StandardLibrary'; import ReuseSearch from '../components/ReuseSearch'; import ToastContainer from '../components/Toast'; import SidePanelHost, { closeSidePanel } from '../components/SidePanel'; import LoadingScreen from '../components/LoadingScreen'; import { IconBox, IconNetwork, IconCompare, IconLayers, IconTarget } from '../components/Icons'; import '../styles.css'; const VIEWS = [ { key: 'bom-browser', label: '产品BOM库', icon: IconBox }, { key: 'mining-view', label: '公共组件挖掘', icon: IconNetwork }, { key: 'standard-library', label: '标准组件库', icon: IconLayers }, { key: 'similarity-view', label: '产品相似度', icon: IconCompare }, { key: 'reuse-search', label: '新需求复用', icon: IconTarget }, ]; const DATA_SOURCES = [ { key: 'compact-10k', label: '10K 模拟数据', url: '/bomx/bom-data.json' }, { key: 'real-137', label: '137 真实数据', url: '/bomx/bom-data-real.json' }, ]; const LOAD_STAGES = [ { key: 'download', label: '下载数据', icon: '↓' }, { key: 'parse', label: '解析 JSON', icon: '{}' }, ]; export default function BomxReuseHome() { const navigate = useNavigate(); const [current, setCurrent] = useState('bom-browser'); const [miningResult, setMiningResult] = useState([]); const [thresholds, setThresholds] = useState({ minCoveragePct: 0.5, minNodes: 3 }); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [dataSource, setDataSource] = useState('compact-10k'); // LoadingScreen 状态 const [phase, setPhase] = useState('download'); const [stageStatus, setStageStatus] = useState>({}); const [stageDetail, setStageDetail] = useState>({}); const [totalBytes, setTotalBytes] = useState(0); const [loadError, setLoadError] = useState(null); const [fadingOut, setFadingOut] = useState(false); // Drawer Portal 挂载点:位于 .bomx-app 根容器内部,使 Drawer DOM 节点留在模块作用域内 const portalMountRef = useRef(null); const loadData = useCallback(async (sourceKey: string) => { setLoading(true); setFadingOut(false); setLoadError(null); setMiningResult([]); setData(null); setStageStatus({}); setStageDetail({}); setTotalBytes(0); setPhase('download'); const source = DATA_SOURCES.find(s => s.key === sourceKey); if (!source) return; try { // 阶段 1:下载数据 setPhase('download'); const res = await fetch(source.url); if (!res.ok) throw new Error(`HTTP ${res.status}`); const contentLength = parseInt(res.headers.get('content-length') || '0', 10); setTotalBytes(contentLength); setStageStatus(prev => ({ ...prev, download: 'done' })); setStageDetail(prev => ({ ...prev, download: contentLength ? `${(contentLength / 1024 / 1024).toFixed(1)} MB` : '完成', })); // 阶段 2:解析 JSON setPhase('parse'); const bomData = await res.json(); setStageStatus(prev => ({ ...prev, parse: 'done' })); setStageDetail(prev => ({ ...prev, parse: `${bomData.products.length} 个产品` })); setData(bomData); // 渐隐过渡 setFadingOut(true); await new Promise(r => setTimeout(r, 350)); setLoading(false); setFadingOut(false); } catch (e: any) { setLoadError(e.message); } }, []); useEffect(() => { initStore(); loadData(dataSource); }, [dataSource, loadData]); const products = data?.products || []; const newRequirements = data?.newRequirements || []; return (
logo

企业级BOM标准化治理

{loading ? ( loadData(dataSource)} fadingOut={fadingOut} stages={LOAD_STAGES} /> ) : ( <>
{current === 'bom-browser' && } {current === 'mining-view' && } {current === 'similarity-view' && } {current === 'standard-library' && } {current === 'reuse-search' && }
)}
{/* Drawer Portal 挂载点:Drawer 通过 createPortal 将内容渲染到此节点内 */}
{data && (
)}
); }