338 lines
16 KiB
TypeScript
338 lines
16 KiB
TypeScript
import { useEffect, useMemo, useState, type MouseEvent } from 'react';
|
|
import { ChevronRight, ListTree, Maximize2, Network, Search, Workflow, X } from 'lucide-react';
|
|
import PanelShell, { PanelBody, CountBadge } from './layout/PanelShell';
|
|
import { useTheme } from '../context/ThemeContext';
|
|
import { useText } from '../i18n/useText';
|
|
import type { MetricNode } from '../data/sqlite';
|
|
import { alertColor, chainOf, execColor, EXEC_RED_LINE, fmt } from '../lib/metric';
|
|
import { achieveAt, levelAt, valueAt } from '../lib/time';
|
|
import { STAGES } from '../lib/process';
|
|
import ProcessFlowPanel from './process/ProcessFlowPanel';
|
|
|
|
const TreeIcon = ListTree;
|
|
|
|
interface Props {
|
|
expanded: boolean;
|
|
onToggle: () => void;
|
|
tree: MetricNode[];
|
|
flat: MetricNode[];
|
|
byCode: Map<string, MetricNode>;
|
|
selected: string;
|
|
onSelect: (code: string) => void;
|
|
/** 左栏两种口径:指标树=结果指标,流程效能=过程指标 */
|
|
mode: 'tree' | 'process';
|
|
onModeChange: (m: 'tree' | 'process') => void;
|
|
/** 指标网络全景:把图谱推到中央工作区 */
|
|
onMaximize?: () => void;
|
|
/** 全局时间游标:列表里的取值与达成率随时点重算 */
|
|
period: number;
|
|
}
|
|
|
|
interface Flat { node: MetricNode; depth: number; hasKids: boolean }
|
|
|
|
/** 三档视觉权重对应的字号:指标卡(L2)最突出,驱动因子(L3)从属,维度行只作分组标题 */
|
|
function typo(level: number) {
|
|
if (level === 0) return { name: 16, nameW: 800, val: 20, valW: 800, unit: 11.5, meta: 11.5, gap: 12.5, dot: 8 };
|
|
if (level === 1) return { name: 14.5, nameW: 800, val: 15, valW: 800, unit: 10.5, meta: 11.5, gap: 12, dot: 8 };
|
|
if (level === 2) return { name: 14.5, nameW: 600, val: 17, valW: 800, unit: 11, meta: 11.5, gap: 12.5, dot: 7 };
|
|
return { name: 13, nameW: 500, val: 14.5, valW: 700, unit: 10, meta: 11, gap: 11.5, dot: 6 };
|
|
}
|
|
|
|
export default function MetricTreePanel({
|
|
expanded, onToggle, tree, flat, byCode, selected, onSelect, mode, onModeChange, onMaximize, period,
|
|
}: Props) {
|
|
const { tokens, theme } = useTheme();
|
|
const { t } = useText();
|
|
const isLight = theme === 'light';
|
|
const [query, setQuery] = useState('');
|
|
const [collapsed, setCollapsed] = useState<Set<string>>(() => new Set(['D-G', 'D-M', 'D-P', 'D-D', 'D-K']));
|
|
|
|
// 外部(数字员工 / 中央看板)改变选中项时,自动展开归属链
|
|
useEffect(() => {
|
|
const chain = chainOf(selected, byCode);
|
|
setCollapsed(prev => {
|
|
const next = new Set(prev);
|
|
chain.forEach(c => next.delete(c));
|
|
return next;
|
|
});
|
|
}, [selected, byCode]);
|
|
|
|
const name = (n: MetricNode) => (n.name_zh);
|
|
|
|
const rows = useMemo<Flat[]>(() => {
|
|
const q = query.trim().toLowerCase();
|
|
const match = (n: MetricNode) =>
|
|
name(n).toLowerCase().includes(q)
|
|
|| n.code.toLowerCase().includes(q)
|
|
|| n.owner.toLowerCase().includes(q);
|
|
|
|
// 搜索态:命中的节点连同其父链一起保留,keepDepth 记录该节点应处的缩进层级
|
|
const keepDepth = new Map<string, number>();
|
|
if (q) {
|
|
flat.forEach(n => {
|
|
if (!match(n)) return;
|
|
chainOf(n.code, byCode).forEach((c, i) => {
|
|
if (!keepDepth.has(c) || keepDepth.get(c)! > i) keepDepth.set(c, i);
|
|
});
|
|
});
|
|
}
|
|
|
|
const out: Flat[] = [];
|
|
const walk = (nodes: MetricNode[], depth: number) => {
|
|
nodes.forEach(n => {
|
|
const kids = n.children ?? [];
|
|
if (!q) {
|
|
out.push({ node: n, depth, hasKids: kids.length > 0 });
|
|
if (kids.length && !collapsed.has(n.code)) walk(kids, depth + 1);
|
|
} else if (keepDepth.has(n.code)) {
|
|
out.push({ node: n, depth: keepDepth.get(n.code)!, hasKids: false });
|
|
walk(kids, depth + 1);
|
|
}
|
|
});
|
|
};
|
|
walk(tree, 0);
|
|
return out.slice(0, 160);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [tree, flat, byCode, collapsed, query]);
|
|
|
|
const toggleNode = (code: string) => {
|
|
setCollapsed(prev => {
|
|
const next = new Set(prev);
|
|
if (next.has(code)) next.delete(code); else next.add(code);
|
|
return next;
|
|
});
|
|
};
|
|
|
|
const segBtn = (active: boolean) => ({
|
|
display: 'flex' as const, alignItems: 'center' as const, justifyContent: 'center' as const,
|
|
gap: 5, flex: 1, minWidth: 0, padding: '5px 8px', borderRadius: 7,
|
|
fontSize: 13, fontWeight: 600, cursor: 'pointer', whiteSpace: 'nowrap' as const,
|
|
background: active ? tokens.accent : 'transparent',
|
|
color: active ? tokens.onAccent : tokens.textMuted,
|
|
border: `1px solid ${active ? tokens.accent : tokens.border}`,
|
|
});
|
|
|
|
const isTree = mode === 'tree';
|
|
|
|
return (
|
|
<PanelShell
|
|
side="left" expanded={expanded} onToggle={onToggle}
|
|
widthCollapsed={268} widthExpanded={468}
|
|
icon={isTree ? <TreeIcon size={16} /> : <Workflow size={16} />}
|
|
title={isTree ? t.panel.metricTree : t.flow.title}
|
|
badge={<CountBadge n={isTree ? flat.length : STAGES.length} />}
|
|
animClass="space-animate-left"
|
|
/* 指标网络不再占一个页签,改为标题行上的一颗按钮:点了就把图谱送到中央工作区。
|
|
两种页签下都保留这颗钮,不让人从流程效能绕路才能回图谱。 */
|
|
extra={onMaximize ? (
|
|
<button
|
|
onClick={onMaximize}
|
|
className="ghost-btn"
|
|
title={t.net.toStage}
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: 4, flexShrink: 0, cursor: 'pointer',
|
|
padding: '3px 8px', borderRadius: 7, fontSize: 12.5, fontWeight: 700,
|
|
color: tokens.accent, background: tokens.accentBg, border: `1px solid ${tokens.borderAccent}`,
|
|
}}
|
|
>
|
|
<Network size={13} />{/* 收起态只留图标:标题不被挤成一个字 */}
|
|
{expanded && t.panel.metricNetwork}
|
|
{expanded && <Maximize2 size={11} style={{ opacity: 0.75 }} />}
|
|
</button>
|
|
) : undefined}
|
|
tabs={
|
|
<>
|
|
<button style={segBtn(isTree)} onClick={() => onModeChange('tree')}>{t.panel.metricTree}</button>
|
|
<button style={segBtn(!isTree)} onClick={() => onModeChange('process')}>{t.flow.title}</button>
|
|
</>
|
|
}
|
|
>
|
|
{isTree ? (
|
|
<>
|
|
<div style={{ position: 'relative', marginBottom: 10, flex: '0 0 auto' }}>
|
|
<Search size={14} style={{ position: 'absolute', left: 10, top: 10, color: tokens.textMuted }} />
|
|
<input
|
|
value={query}
|
|
onChange={e => setQuery(e.target.value)}
|
|
placeholder={t.tree.search}
|
|
style={{
|
|
width: '100%', padding: '8px 30px 8px 31px', fontSize: 13.5, borderRadius: 9,
|
|
background: tokens.panelBgAlt, border: `1px solid ${tokens.border}`, color: tokens.text,
|
|
outline: 'none',
|
|
}}
|
|
/>
|
|
{query && (
|
|
<button onClick={() => setQuery('')} style={{ position: 'absolute', right: 9, top: 11, color: tokens.textMuted, cursor: 'pointer' }}>
|
|
<X size={14} />
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* 色条语义图例:三档与 execColor / EXEC_RED_LINE 同源,改阈值不会漏改 */}
|
|
{expanded && (
|
|
<div style={{
|
|
display: 'flex', alignItems: 'center', flexWrap: 'wrap', gap: '4px 11px',
|
|
marginBottom: 10, flex: '0 0 auto', fontSize: 11.5, color: tokens.textMuted,
|
|
}}>
|
|
<span style={{ fontWeight: 700, color: tokens.textSub }}>{t.panel.legendExec}</span>
|
|
{[
|
|
{ c: tokens.success, k: t.panel.legendOk },
|
|
{ c: tokens.warning, k: t.panel.legendWarn },
|
|
{ c: tokens.danger, k: t.panel.legendBad.replace('{n}', String(EXEC_RED_LINE)) },
|
|
].map(x => (
|
|
<span key={x.k} style={{ display: 'inline-flex', alignItems: 'center', gap: 4, whiteSpace: 'nowrap' }}>
|
|
<i style={{ display: 'inline-block', width: 14, height: 5, borderRadius: 99, background: x.c }} />{x.k}
|
|
</span>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
<PanelBody>
|
|
{rows.length === 0 && (
|
|
<div style={{ padding: '20px 0', textAlign: 'center', fontSize: 13, color: tokens.textMuted }}>{t.tree.noResult}</div>
|
|
)}
|
|
{rows.map(({ node: n, depth, hasKids }) => {
|
|
const active = selected === n.code;
|
|
const c = alertColor(levelAt(n, period), tokens);
|
|
const unit = n.unit;
|
|
const ty = typo(n.level);
|
|
const kind: 'head' | 'card' | 'drive' = n.level <= 1 ? 'head' : n.level === 2 ? 'card' : 'drive';
|
|
const rate = achieveAt(n, period); // 游标时点的预算执行率,超额时会大于 100
|
|
const rateW = Math.max(0, Math.min(rate, 100)); // 条宽封顶:满格即达标
|
|
const ec = execColor(rate, tokens); // 色条与右侧数字同色,成对引导视线
|
|
const surface = isLight ? '#ffffff' : tokens.cardBg;
|
|
const base = active ? tokens.accentBg
|
|
: kind === 'drive' ? tokens.panelBgAlt
|
|
: (kind === 'card' || n.level === 0) ? surface
|
|
: 'transparent';
|
|
const lift = isLight ? '0 1px 3px rgba(30,60,130,0.09), 0 1px 2px rgba(30,60,130,0.05)' : 'none';
|
|
const enter = (e: MouseEvent<HTMLDivElement>) => { if (!active) e.currentTarget.style.background = tokens.tableRowHover; };
|
|
const leave = (e: MouseEvent<HTMLDivElement>) => { if (!active) e.currentTarget.style.background = base; };
|
|
|
|
const dot = <span style={{ width: ty.dot, height: ty.dot, borderRadius: 99, background: c, flex: '0 0 auto', boxShadow: `0 0 7px ${c}66` }} />;
|
|
|
|
const label = (
|
|
<div style={{ minWidth: 0, flex: 1 }}>
|
|
<div style={{
|
|
fontSize: ty.name, fontWeight: ty.nameW, letterSpacing: '0.01em',
|
|
color: active ? tokens.accent : kind === 'drive' ? tokens.textSub : tokens.text,
|
|
whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
|
|
}}>{name(n)}</div>
|
|
{expanded && n.level >= 2 && (
|
|
<div style={{
|
|
fontSize: ty.meta, color: tokens.textMuted, marginTop: 2,
|
|
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
|
|
}}>{n.code} · {n.owner}</div>
|
|
)}
|
|
</div>
|
|
);
|
|
|
|
const nums = (
|
|
<div style={{ textAlign: 'right', flex: '0 0 auto', minWidth: 66, whiteSpace: 'nowrap' }}>
|
|
<div style={{
|
|
fontSize: ty.val, fontWeight: ty.valW, color: tokens.text, lineHeight: 1.12,
|
|
fontVariantNumeric: 'tabular-nums', letterSpacing: '0.01em',
|
|
}}>
|
|
{fmt(valueAt(n, period))}
|
|
<span style={{ fontSize: ty.unit, fontWeight: 500, color: tokens.textMuted, marginLeft: 3 }}>{unit}</span>
|
|
</div>
|
|
<div style={{
|
|
fontSize: ty.gap, fontWeight: kind === 'drive' ? 600 : 700, color: ec,
|
|
fontVariantNumeric: 'tabular-nums', marginTop: 1, letterSpacing: '0.01em',
|
|
}}>
|
|
{t.detail.reach} {fmt(rate)}%
|
|
</div>
|
|
</div>
|
|
);
|
|
|
|
/* L2 指标 → 独立指标卡:卡内留白撑开名称与数值,底边一条达成度进度条 */
|
|
if (kind === 'card') {
|
|
return (
|
|
<div
|
|
key={n.code} onClick={() => onSelect(n.code)} className="notice-in"
|
|
onMouseEnter={enter} onMouseLeave={leave}
|
|
style={{
|
|
cursor: 'pointer', marginBottom: 5, marginLeft: depth * 11,
|
|
padding: '8px 10px 7px', borderRadius: 10, background: base,
|
|
border: `1px solid ${active ? tokens.borderAccent : tokens.border}`,
|
|
boxShadow: active ? `0 0 0 1px ${tokens.accent}2e, 0 3px 14px rgba(0,0,0,0.2)` : lift,
|
|
}}
|
|
>
|
|
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
|
{hasKids && (
|
|
<span
|
|
onClick={e => { e.stopPropagation(); toggleNode(n.code); }}
|
|
style={{ width: 16, flex: '0 0 16px', display: 'flex', alignItems: 'center', color: tokens.textMuted, cursor: 'pointer' }}
|
|
>
|
|
<ChevronRight size={14} style={{ transform: `rotate(${collapsed.has(n.code) ? 0 : 90}deg)`, transition: 'transform 0.18s' }} />
|
|
</span>
|
|
)}
|
|
{dot}
|
|
{label}
|
|
{nums}
|
|
</div>
|
|
<div style={{ marginTop: 8, height: 6, borderRadius: 99, background: tokens.border, overflow: 'hidden' }}>
|
|
<div style={{ width: `${rateW}%`, height: '100%', borderRadius: 99, background: ec, boxShadow: `0 0 8px ${ec}59` }} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/* L3 驱动因子 → 从属子条目:左侧一道状态色连接线,挂在上方指标卡之下 */
|
|
if (kind === 'drive') {
|
|
return (
|
|
<div
|
|
key={n.code} onClick={() => onSelect(n.code)} className="notice-in"
|
|
onMouseEnter={enter} onMouseLeave={leave}
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: 7, cursor: 'pointer',
|
|
marginLeft: depth * 11 + 10, marginBottom: 4, padding: '5px 8px',
|
|
borderRadius: 8, background: base,
|
|
border: `1px solid ${active ? tokens.borderAccent : tokens.border}`,
|
|
}}
|
|
>
|
|
{dot}
|
|
{label}
|
|
{nums}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/* L0 / L1 → 分组标题行,不参与卡片化,避免 54 行同质方框 */
|
|
return (
|
|
<div
|
|
key={n.code} onClick={() => onSelect(n.code)} className="notice-in"
|
|
onMouseEnter={enter} onMouseLeave={leave}
|
|
style={{
|
|
display: 'flex', alignItems: 'center', gap: 7, cursor: 'pointer',
|
|
marginLeft: depth * 11, marginBottom: n.level === 0 ? 9 : 4,
|
|
padding: n.level === 0 ? '8px 10px' : '5px 7px', borderRadius: 9,
|
|
background: base,
|
|
border: `1px solid ${active ? tokens.borderAccent : n.level === 0 ? tokens.border : 'transparent'}`,
|
|
}}
|
|
>
|
|
<span
|
|
onClick={e => { e.stopPropagation(); if (hasKids) toggleNode(n.code); }}
|
|
style={{
|
|
width: 16, flex: '0 0 16px', display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
color: tokens.textMuted, cursor: hasKids ? 'pointer' : 'default',
|
|
}}
|
|
>
|
|
{hasKids && <ChevronRight size={14} style={{ transform: `rotate(${collapsed.has(n.code) ? 0 : 90}deg)`, transition: 'transform 0.18s' }} />}
|
|
</span>
|
|
{dot}
|
|
{label}
|
|
{nums}
|
|
</div>
|
|
);
|
|
})}
|
|
</PanelBody>
|
|
</>
|
|
) : (
|
|
<ProcessFlowPanel byCode={byCode} selected={selected} onSelect={onSelect} period={period} />
|
|
)}
|
|
</PanelShell>
|
|
);
|
|
}
|