Updated flows tree
Deploy Frontend / build-and-deploy (push) Successful in 1m3s Details

This commit is contained in:
romanskijdev 2026-08-04 12:34:11 +03:00
parent 9f201fe6fd
commit 863f38cdb5
2 changed files with 30 additions and 34 deletions

View File

@ -68,7 +68,7 @@ export interface PreviewFlowResponse {
export const flowNodesApi = {
getTree: (categoryId: number) =>
apiClient.get<ApiResponse<ApprovalFlowNode[]>>(`/flow-nodes/category/${categoryId}`),
apiClient.get<ApiResponse<ApprovalFlowNode[]>>(`/flow-nodes/category/${categoryId}?limit=1000`),
getById: (id: number) =>
apiClient.get<ApprovalFlowNode>(`/flow-nodes/${id}`),

View File

@ -1,27 +1,28 @@
import { useQueries } from '@tanstack/react-query'
import { useQuery } from '@tanstack/react-query'
import { useCategories } from '@/features/categories/hooks/useCategories'
import { useFlowNodesTree, FLOW_NODES_QUERY_KEY } from '@/features/flowNodes/hooks/useFlowNodes'
import { useFlowNodesTree } from '@/features/flowNodes/hooks/useFlowNodes'
import { flowNodesApi } from '@/api/flowNodes'
import type { ApprovalFlowNode, ApiResponse } from '@/types/api'
import type { ApprovalFlowNode } from '@/types/api'
export interface SystemOption {
value: string
label: string
}
function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined, contextLabel = 'unknown'): SystemOption[] {
console.log(`[extractSystemOptions] Анализ узлов (${contextLabel}):`, nodes)
function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined): SystemOption[] {
if (!nodes) return []
const seen = new Map<string, string>()
// Рекурсивный обход всех вложенностей
function traverse(nodeList: ApprovalFlowNode[]) {
for (const n of nodeList) {
// Ищем мультиселект с ключом systems
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)
}
}
// Погружаемся в дочерние узлы
if (n.children && n.children.length > 0) {
traverse(n.children)
}
@ -29,16 +30,12 @@ function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined, cont
}
traverse(nodes)
const result = Array.from(seen.entries()).map(([value, label]) => ({ value, label }))
console.log(`[extractSystemOptions] Итог для (${contextLabel}):`, result)
return result
return Array.from(seen.entries()).map(([value, label]) => ({ value, label }))
}
export function useCategorySystemOptions(categoryId: number | null) {
const { data, isLoading } = useFlowNodesTree(categoryId)
console.log(`[useCategorySystemOptions] categoryId: ${categoryId}, получено узлов:`, data?.data?.length)
const options = extractSystemOptions(data?.data, `category-${categoryId}`)
const options = extractSystemOptions(data?.data)
return { options, isLoading: categoryId ? isLoading : false }
}
@ -48,29 +45,28 @@ export function useAllSystemOptions(enabled: boolean = true) {
.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<ApprovalFlowNode[]>
},
enabled,
})),
// Используем один запрос и последовательно опрашиваем категории, чтобы избежать 429 Too Many Requests
const { data: options = [], isLoading } = useQuery({
queryKey: ['all-systems-scan', categoryIds],
queryFn: async () => {
const seen = new Map<string, string>()
for (const id of categoryIds) {
try {
const response = await flowNodesApi.getTree(id)
const opts = extractSystemOptions(response.data?.data)
opts.forEach(o => {
if (!seen.has(o.value)) seen.set(o.value, o.label)
})
} catch (e) {
console.warn(`[useAllSystemOptions] Ошибка загрузки дерева для категории ${id}`, e)
}
}
return Array.from(seen.entries()).map(([value, label]) => ({ value, label }))
},
enabled: enabled && categoryIds.length > 0,
})
const isLoading = enabled && results.some((r) => r.isLoading)
const merged = new Map<string, string>()
for (const r of results) {
const nodes = (r.data as ApiResponse<ApprovalFlowNode[]> | undefined)?.data
for (const opt of extractSystemOptions(nodes, 'all-systems-scan')) {
if (!merged.has(opt.value)) merged.set(opt.value, opt.label)
}
}
const options = Array.from(merged.entries()).map(([value, label]) => ({ value, label }))
console.log('[useAllSystemOptions] Итоговые уникальные системы по всей системе:', options)
return { options, isLoading }
}