diff --git a/src/api/hierarchies.ts b/src/api/hierarchies.ts index e02d343..7cc3105 100644 --- a/src/api/hierarchies.ts +++ b/src/api/hierarchies.ts @@ -9,6 +9,7 @@ export interface HierarchiesListParams { department_id?: number topic_category_id?: number topic_id?: number + target_system?: string | null } export interface CreateHierarchyRequest { @@ -18,6 +19,7 @@ export interface CreateHierarchyRequest { topic_category_id?: number | null topic_id?: number | null priority?: number + target_system?: string | null } export interface UpdateHierarchyRequest { @@ -28,6 +30,7 @@ export interface UpdateHierarchyRequest { topic_category_id?: number | null topic_id?: number | null priority?: number + target_system?: string | null } export const hierarchiesApi = { diff --git a/src/features/hierarchies/hooks/useSystemOptions.ts b/src/features/hierarchies/hooks/useSystemOptions.ts new file mode 100644 index 0000000..b9db77a --- /dev/null +++ b/src/features/hierarchies/hooks/useSystemOptions.ts @@ -0,0 +1,62 @@ +import { useQueries } from '@tanstack/react-query' +import { useCategories } from '@/features/categories/hooks/useCategories' +import { useFlowNodesTree, FLOW_NODES_QUERY_KEY } from '@/features/flowNodes/hooks/useFlowNodes' +import { flowNodesApi } from '@/api/flowNodes' +import type { ApprovalFlowNode, ApiResponse } from '@/types/api' + +export interface SystemOption { + value: string + label: string +} + +function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined): SystemOption[] { + if (!nodes) return [] + const seen = new Map() + for (const n of nodes) { + if (n.is_multi_select && n.multi_select_key === 'systems' && n.multi_select_value) { + if (!seen.has(n.multi_select_value)) { + seen.set(n.multi_select_value, n.label || n.multi_select_value) + } + } + } + return Array.from(seen.entries()).map(([value, label]) => ({ value, label })) +} + +export function useCategorySystemOptions(categoryId: number | null) { + const { data, isLoading } = useFlowNodesTree(categoryId) + const options = extractSystemOptions(data?.data) + return { options, isLoading: categoryId ? isLoading : false } +} + +export function useAllSystemOptions(enabled: boolean = true) { + const { data: categories } = useCategories({ limit: 1000 }) + const categoryIds = (categories?.data ?? []) + .map((c) => c.id) + .filter((id): id is number => !!id) + + const results = useQueries({ + queries: categoryIds.map((id) => ({ + queryKey: [FLOW_NODES_QUERY_KEY, id], + queryFn: async () => { + const response = await flowNodesApi.getTree(id) + return response.data as ApiResponse + }, + enabled, + })), + }) + + const isLoading = enabled && results.some((r) => r.isLoading) + + const merged = new Map() + for (const r of results) { + const nodes = (r.data as ApiResponse | undefined)?.data + for (const opt of extractSystemOptions(nodes)) { + if (!merged.has(opt.value)) merged.set(opt.value, opt.label) + } + } + + const options = Array.from(merged.entries()).map(([value, label]) => ({ value, label })) + return { options, isLoading } +} + +export const ALL_SYSTEMS_VALUE = 'all' \ No newline at end of file diff --git a/src/pages/hierarchies/components/ChainVisualization.tsx b/src/pages/hierarchies/components/ChainVisualization.tsx index 275b4b4..74bb9d9 100644 --- a/src/pages/hierarchies/components/ChainVisualization.tsx +++ b/src/pages/hierarchies/components/ChainVisualization.tsx @@ -1,3 +1,5 @@ +// ChainVisualization.tsx +import { useState } from 'react' import { Paper, Group, @@ -9,20 +11,22 @@ import { Box, Divider, Tooltip, + Select, } from '@mantine/core' import { IconChevronRight, IconPencil, IconTrash, - IconUser, IconWorld, IconCategory, IconFileText, IconCopy, IconPlus, + IconServer, } from '@tabler/icons-react' import type { ChainGroup } from '@/features/hierarchies/utils' import { SCOPE_COLORS, SCOPE_LABELS } from '@/features/hierarchies/utils' +import { useCategorySystemOptions, ALL_SYSTEMS_VALUE } from '@/features/hierarchies/hooks/useSystemOptions' interface ChainVisualizationProps { group: ChainGroup @@ -30,6 +34,7 @@ interface ChainVisualizationProps { onDeleteStep: (hierarchyId: number) => void onAddStep: (group: ChainGroup) => void onDuplicateChain: (group: ChainGroup) => void + onBulkUpdateSystem?: (group: ChainGroup, targetSystem: string | null) => void } const ScopeIcon = ({ type }: { type: string }) => { @@ -51,7 +56,28 @@ export default function ChainVisualization({ onDeleteStep, onAddStep, onDuplicateChain, + onBulkUpdateSystem, }: ChainVisualizationProps) { + // Получаем доступные системы для категории (или темы) + const categoryId = group.scopeType === 'category' ? group.scopeObjectId : null + const { options: systemOptions } = useCategorySystemOptions(categoryId) + + // Текущая система цепочки (если у всех шагов одинаковая система, иначе 'all') + const currentChainSystem = group.steps.every( + (s) => s.target_system === group.steps[0]?.target_system + ) + ? group.steps[0]?.target_system || ALL_SYSTEMS_VALUE + : ALL_SYSTEMS_VALUE + + // Формируем опции для Select + const selectData = [ + { value: ALL_SYSTEMS_VALUE, label: 'Система: Все' }, + ...systemOptions.map((opt) => ({ + value: opt.value, + label: `Система: ${opt.label}`, + })), + ] + return ( {SCOPE_LABELS[group.scopeType]}: {group.scopeObjectName} + + {/* Выпадающий список систем отображается ТОЛЬКО если у темы/категории есть заданные systems */} + {systemOptions.length > 0 && ( + ({ value: s.value, label: s.label })), + ]} + value={form.values.target_system || ''} + onChange={(v) => form.setFieldValue('target_system', v || null)} + clearable + description="Укажите систему, если данный шаг согласующего применяется только к определенной системе" + /> + {/* Priority */}