/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import React, {
memo,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import {
ReactFlow,
ReactFlowProvider,
Controls,
MiniMap,
Panel,
Handle,
Position,
useNodesState,
useEdgesState,
useReactFlow,
useUpdateNodeInternals,
} from '@xyflow/react';
import dagre from '@dagrejs/dagre';
import {
Avatar,
Button,
Popconfirm,
RadioGroup,
Radio,
SideSheet,
Tag,
Tooltip,
Typography,
} from '@/components/ui';
import {
Plus,
Pencil,
Trash2,
LayoutGrid,
Network,
Building2,
ChevronDown,
ChevronRight,
Users,
} from 'lucide-react';
import OrgScopedLimitButtons from './OrgScopedLimitButtons';
import { buildDeptFullNameMap } from './deptTree';
import { renderQuota } from '../../../helpers/render';
import '@xyflow/react/dist/style.css';
import './DeptFlowCanvas.css';
const { Text } = Typography;
// dagre 布局用的节点占位尺寸(与卡片实际渲染尺寸保持一致,决定节点间距)。
// 高度按「带预算行」的卡片取值——无预算的卡片略矮只会让间距更宽松,不会重叠。
const NODE_WIDTH = 264;
const NODE_HEIGHT = 144;
const parseGroups = (raw) => {
if (Array.isArray(raw)) return raw;
if (typeof raw === 'string' && raw.trim()) {
try {
const arr = JSON.parse(raw);
if (Array.isArray(arr)) return arr;
} catch {
/* ignore */
}
}
return [];
};
// 部门预算行 → 卡片展示用摘要:优先周期口径,无周期上限再看总上限;双 0(不限)不展示。
const summarizeBudget = (t, budget) => {
if (!budget || budget.status !== 1) return null;
const cycleLabels = {
daily: t('每日'),
weekly: t('每周'),
monthly: t('每月'),
custom: t('自定义'),
};
// used 含预留中份额:与预算中心「used (+reserved 预留)」口径一致,
// 否则两页同开时百分比自相矛盾,且接近 cap 时画布不变红失去预警
if (budget.cycle_quota > 0) {
return {
label: cycleLabels[budget.cycle_type] || t('周期上限'),
used: (budget.used_in_cycle || 0) + (budget.reserved_in_cycle || 0),
cap: budget.cycle_quota,
};
}
if (budget.total_quota > 0) {
return {
label: t('总上限'),
used: (budget.used_total || 0) + (budget.reserved_total || 0),
cap: budget.total_quota,
};
}
return null;
};
/** 部门卡片节点:信息展示 + 卡片内操作区(操作区带 nodrag,避免与画布拖拽冲突) */
const DeptNode = memo(({ data, selected }) => {
const { t } = useTranslation();
const {
dept,
childCount,
memberCount,
collapsed,
hiddenCount,
direction,
orgId,
budget,
onAddChild,
onEdit,
onDelete,
onSaved,
onToggleCollapse,
onShowMembers,
} = data;
const budgetInfo = summarizeBudget(t, budget);
const active = dept.status === 1;
const isRoot = !dept.parent_id;
const groupCount = parseGroups(dept.groups).length;
const modelCount =
dept.model_limits_enabled && dept.model_limits
? dept.model_limits.split(',').filter(Boolean).length
: 0;
const targetPos = direction === 'LR' ? Position.Left : Position.Top;
const sourcePos = direction === 'LR' ? Position.Right : Position.Bottom;
const DeptIcon = isRoot ? Building2 : Network;
return (