mirror of
https://github.com/langgenius/dify.git
synced 2026-04-30 15:38:08 +08:00
refactor(web): move sandbox tree builder to workflow artifacts utils
This commit is contained in:
@ -7,10 +7,11 @@ import { useCallback, useMemo, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import FolderSpark from '@/app/components/base/icons/src/vender/workflow/FolderSpark'
|
||||
import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
|
||||
import { buildTreeFromFlatList, sandboxFilesTreeOptions, useDownloadSandboxFile } from '@/service/use-sandbox-file'
|
||||
import { sandboxFilesTreeOptions, useDownloadSandboxFile } from '@/service/use-sandbox-file'
|
||||
import { cn } from '@/utils/classnames'
|
||||
import { downloadUrl } from '@/utils/download'
|
||||
import ArtifactsTree from './artifacts-tree'
|
||||
import { buildTreeFromFlatList } from './utils'
|
||||
|
||||
type ArtifactsSectionProps = {
|
||||
className?: string
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
import type {
|
||||
SandboxFileNode,
|
||||
SandboxFileTreeNode,
|
||||
} from '@/types/sandbox-file'
|
||||
|
||||
export function buildTreeFromFlatList(nodes: SandboxFileNode[]): SandboxFileTreeNode[] {
|
||||
const nodeMap = new Map<string, SandboxFileTreeNode>()
|
||||
const roots: SandboxFileTreeNode[] = []
|
||||
|
||||
const sorted = [...nodes].sort((a, b) =>
|
||||
a.path.split('/').length - b.path.split('/').length,
|
||||
)
|
||||
|
||||
for (const node of sorted) {
|
||||
const parts = node.path.split('/')
|
||||
const name = parts[parts.length - 1]
|
||||
const parentPath = parts.slice(0, -1).join('/')
|
||||
|
||||
const treeNode: SandboxFileTreeNode = {
|
||||
id: node.path,
|
||||
name,
|
||||
path: node.path,
|
||||
node_type: node.is_dir ? 'folder' : 'file',
|
||||
size: node.size,
|
||||
mtime: node.mtime,
|
||||
extension: node.extension,
|
||||
children: [],
|
||||
}
|
||||
|
||||
nodeMap.set(node.path, treeNode)
|
||||
|
||||
if (parentPath === '') {
|
||||
roots.push(treeNode)
|
||||
}
|
||||
else {
|
||||
const parent = nodeMap.get(parentPath)
|
||||
if (parent)
|
||||
parent.children.push(treeNode)
|
||||
}
|
||||
}
|
||||
|
||||
return roots
|
||||
}
|
||||
@ -11,9 +11,10 @@ import Loading from '@/app/components/base/loading'
|
||||
import ArtifactsTree from '@/app/components/workflow/skill/file-tree/artifacts/artifacts-tree'
|
||||
import ReadOnlyFilePreview from '@/app/components/workflow/skill/viewer/read-only-file-preview'
|
||||
import { useDocLink } from '@/context/i18n'
|
||||
import { buildTreeFromFlatList, sandboxFileDownloadUrlOptions, sandboxFilesTreeOptions, useDownloadSandboxFile } from '@/service/use-sandbox-file'
|
||||
import { sandboxFileDownloadUrlOptions, sandboxFilesTreeOptions, useDownloadSandboxFile } from '@/service/use-sandbox-file'
|
||||
import { cn } from '@/utils/classnames'
|
||||
import { downloadUrl } from '@/utils/download'
|
||||
import { buildTreeFromFlatList } from '../skill/file-tree/artifacts/utils'
|
||||
import { useStore } from '../store'
|
||||
import { WorkflowRunningStatus } from '../types'
|
||||
import InspectLayout from './inspect-layout'
|
||||
|
||||
@ -1,7 +1,3 @@
|
||||
import type {
|
||||
SandboxFileNode,
|
||||
SandboxFileTreeNode,
|
||||
} from '@/types/sandbox-file'
|
||||
import { skipToken, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useCallback } from 'react'
|
||||
import { consoleClient, consoleQuery } from '@/service/client'
|
||||
@ -54,42 +50,3 @@ export function useDownloadSandboxFile(appId: string | undefined) {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function buildTreeFromFlatList(nodes: SandboxFileNode[]): SandboxFileTreeNode[] {
|
||||
const nodeMap = new Map<string, SandboxFileTreeNode>()
|
||||
const roots: SandboxFileTreeNode[] = []
|
||||
|
||||
const sorted = [...nodes].sort((a, b) =>
|
||||
a.path.split('/').length - b.path.split('/').length,
|
||||
)
|
||||
|
||||
for (const node of sorted) {
|
||||
const parts = node.path.split('/')
|
||||
const name = parts[parts.length - 1]
|
||||
const parentPath = parts.slice(0, -1).join('/')
|
||||
|
||||
const treeNode: SandboxFileTreeNode = {
|
||||
id: node.path,
|
||||
name,
|
||||
path: node.path,
|
||||
node_type: node.is_dir ? 'folder' : 'file',
|
||||
size: node.size,
|
||||
mtime: node.mtime,
|
||||
extension: node.extension,
|
||||
children: [],
|
||||
}
|
||||
|
||||
nodeMap.set(node.path, treeNode)
|
||||
|
||||
if (parentPath === '') {
|
||||
roots.push(treeNode)
|
||||
}
|
||||
else {
|
||||
const parent = nodeMap.get(parentPath)
|
||||
if (parent)
|
||||
parent.children.push(treeNode)
|
||||
}
|
||||
}
|
||||
|
||||
return roots
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user