Updated flows tree
Deploy Frontend / build-and-deploy (push) Successful in 1m3s
Details
Deploy Frontend / build-and-deploy (push) Successful in 1m3s
Details
This commit is contained in:
parent
9f201fe6fd
commit
863f38cdb5
|
|
@ -68,7 +68,7 @@ export interface PreviewFlowResponse {
|
||||||
|
|
||||||
export const flowNodesApi = {
|
export const flowNodesApi = {
|
||||||
getTree: (categoryId: number) =>
|
getTree: (categoryId: number) =>
|
||||||
apiClient.get<ApiResponse<ApprovalFlowNode[]>>(`/flow-nodes/category/${categoryId}`),
|
apiClient.get<ApiResponse<ApprovalFlowNode[]>>(`/flow-nodes/category/${categoryId}?limit=1000`),
|
||||||
|
|
||||||
getById: (id: number) =>
|
getById: (id: number) =>
|
||||||
apiClient.get<ApprovalFlowNode>(`/flow-nodes/${id}`),
|
apiClient.get<ApprovalFlowNode>(`/flow-nodes/${id}`),
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,28 @@
|
||||||
import { useQueries } from '@tanstack/react-query'
|
import { useQuery } from '@tanstack/react-query'
|
||||||
import { useCategories } from '@/features/categories/hooks/useCategories'
|
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 { flowNodesApi } from '@/api/flowNodes'
|
||||||
import type { ApprovalFlowNode, ApiResponse } from '@/types/api'
|
import type { ApprovalFlowNode } from '@/types/api'
|
||||||
|
|
||||||
export interface SystemOption {
|
export interface SystemOption {
|
||||||
value: string
|
value: string
|
||||||
label: string
|
label: string
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined, contextLabel = 'unknown'): SystemOption[] {
|
function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined): SystemOption[] {
|
||||||
console.log(`[extractSystemOptions] Анализ узлов (${contextLabel}):`, nodes)
|
|
||||||
if (!nodes) return []
|
if (!nodes) return []
|
||||||
|
|
||||||
const seen = new Map<string, string>()
|
const seen = new Map<string, string>()
|
||||||
|
|
||||||
|
// Рекурсивный обход всех вложенностей
|
||||||
function traverse(nodeList: ApprovalFlowNode[]) {
|
function traverse(nodeList: ApprovalFlowNode[]) {
|
||||||
for (const n of nodeList) {
|
for (const n of nodeList) {
|
||||||
|
// Ищем мультиселект с ключом systems
|
||||||
if (n.is_multi_select && n.multi_select_key === 'systems' && n.multi_select_value) {
|
if (n.is_multi_select && n.multi_select_key === 'systems' && n.multi_select_value) {
|
||||||
if (!seen.has(n.multi_select_value)) {
|
if (!seen.has(n.multi_select_value)) {
|
||||||
seen.set(n.multi_select_value, n.label || n.multi_select_value)
|
seen.set(n.multi_select_value, n.label || n.multi_select_value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Погружаемся в дочерние узлы
|
||||||
if (n.children && n.children.length > 0) {
|
if (n.children && n.children.length > 0) {
|
||||||
traverse(n.children)
|
traverse(n.children)
|
||||||
}
|
}
|
||||||
|
|
@ -29,16 +30,12 @@ function extractSystemOptions(nodes: ApprovalFlowNode[] | null | undefined, cont
|
||||||
}
|
}
|
||||||
|
|
||||||
traverse(nodes)
|
traverse(nodes)
|
||||||
|
return Array.from(seen.entries()).map(([value, label]) => ({ value, label }))
|
||||||
const result = Array.from(seen.entries()).map(([value, label]) => ({ value, label }))
|
|
||||||
console.log(`[extractSystemOptions] Итог для (${contextLabel}):`, result)
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useCategorySystemOptions(categoryId: number | null) {
|
export function useCategorySystemOptions(categoryId: number | null) {
|
||||||
const { data, isLoading } = useFlowNodesTree(categoryId)
|
const { data, isLoading } = useFlowNodesTree(categoryId)
|
||||||
console.log(`[useCategorySystemOptions] categoryId: ${categoryId}, получено узлов:`, data?.data?.length)
|
const options = extractSystemOptions(data?.data)
|
||||||
const options = extractSystemOptions(data?.data, `category-${categoryId}`)
|
|
||||||
return { options, isLoading: categoryId ? isLoading : false }
|
return { options, isLoading: categoryId ? isLoading : false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,29 +45,28 @@ export function useAllSystemOptions(enabled: boolean = true) {
|
||||||
.map((c) => c.id)
|
.map((c) => c.id)
|
||||||
.filter((id): id is number => !!id)
|
.filter((id): id is number => !!id)
|
||||||
|
|
||||||
const results = useQueries({
|
// Используем один запрос и последовательно опрашиваем категории, чтобы избежать 429 Too Many Requests
|
||||||
queries: categoryIds.map((id) => ({
|
const { data: options = [], isLoading } = useQuery({
|
||||||
queryKey: [FLOW_NODES_QUERY_KEY, id],
|
queryKey: ['all-systems-scan', categoryIds],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const response = await flowNodesApi.getTree(id)
|
const seen = new Map<string, string>()
|
||||||
return response.data as ApiResponse<ApprovalFlowNode[]>
|
|
||||||
},
|
for (const id of categoryIds) {
|
||||||
enabled,
|
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 }
|
return { options, isLoading }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue