Files
ai-platform/src/modules/bomx-reuse-analyzer/pages/BomxReuseHome.tsx
T
dongskandClaude Sonnet 4.6 noreply@anthropic.com> 8699798ea5 feat: 模组全维度独立,消除全局 DOM/Portal/存储污染
- jieya-ibp/welltrust-qap ThemeContext 改为作用域容器承载 CSS 变量与背景色,不再写 document
- 删除 order-cost CostCockpit 对 documentElement 的死变量写入
- bomx Drawer createPortal 挂载点内移到 .bomx-app,移除 document.body 兜底
- knowledge-graph 拖拽 cursor 改为根容器 ref
- 新增 check-module-independence 门禁并接入 build
- SOP 与融合技能文档补充「模组完全独立互不影响」章节

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com>
2026-10-19 09:10:30 +08:00

187 lines
6.9 KiB
TypeScript

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<any[]>([]);
const [thresholds, setThresholds] = useState<any>({ minCoveragePct: 0.5, minNodes: 3 });
const [data, setData] = useState<any>(null);
const [loading, setLoading] = useState(true);
const [dataSource, setDataSource] = useState('compact-10k');
// LoadingScreen 状态
const [phase, setPhase] = useState('download');
const [stageStatus, setStageStatus] = useState<Record<string, any>>({});
const [stageDetail, setStageDetail] = useState<Record<string, any>>({});
const [totalBytes, setTotalBytes] = useState(0);
const [loadError, setLoadError] = useState<any>(null);
const [fadingOut, setFadingOut] = useState(false);
// Drawer Portal 挂载点:位于 .bomx-app 根容器内部,使 Drawer DOM 节点留在模块作用域内
const portalMountRef = useRef<HTMLDivElement>(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 (
<div className="bomx-app">
<header className="app-header">
<span className="app-logo"><img src="/bomx/logo.png" alt="logo" /></span>
<h1 className="app-title">企业级BOM标准化治理</h1>
<nav className="app-nav">
{VIEWS.map(v => {
const Icon = v.icon;
return (
<button
key={v.key}
className={`nav-btn ${current === v.key ? 'active' : ''}`}
onClick={() => { setCurrent(v.key); closeSidePanel(); }}
disabled={loading}
>
<span className="nav-icon"><Icon /></span>
{v.label}
</button>
);
})}
</nav>
<button
onClick={() => navigate('/')}
style={{
marginLeft: 'auto', flexShrink: 0,
padding: '6px 14px', borderRadius: 8, cursor: 'pointer',
border: '1px solid #4F46E5',
background: 'rgba(99,102,241,0.08)',
color: '#4F46E5', fontSize: 13, fontWeight: 600,
transition: 'all 0.18s ease',
}}
onMouseEnter={e => { e.currentTarget.style.background = 'rgba(99,102,241,0.18)'; }}
onMouseLeave={e => { e.currentTarget.style.background = 'rgba(99,102,241,0.08)'; }}
>
← 返回平台
</button>
</header>
<div className="app-body">
{loading ? (
<LoadingScreen
phase={phase}
stageStatus={stageStatus}
stageDetail={stageDetail}
totalBytes={totalBytes}
error={loadError}
onRetry={() => loadData(dataSource)}
fadingOut={fadingOut}
stages={LOAD_STAGES}
/>
) : (
<>
<main id="view-container" key={current}>
{current === 'bom-browser' && <BomBrowser products={products} />}
{current === 'mining-view' && <MiningView products={products} miningResult={miningResult} setMiningResult={setMiningResult} thresholds={thresholds} setThresholds={setThresholds} />}
{current === 'similarity-view' && <SimilarityView products={products} miningResult={miningResult} />}
{current === 'standard-library' && <StandardLibrary />}
{current === 'reuse-search' && <ReuseSearch newRequirements={newRequirements} />}
</main>
<SidePanelHost />
</>
)}
</div>
<ToastContainer />
{/* Drawer Portal 挂载点:Drawer 通过 createPortal 将内容渲染到此节点内 */}
<div ref={portalMountRef} />
{data && (
<div className="data-source-switch">
<select
value={dataSource}
onChange={e => setDataSource(e.target.value)}
className="data-source-select"
>
{DATA_SOURCES.map(s => <option key={s.key} value={s.key}>{s.label}</option>)}
</select>
</div>
)}
</div>
);
}