mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 06:28:05 +08:00
Extract repeated appId retrieval and tree data fetching patterns into dedicated hooks (useSkillAssetTreeData, useSkillAssetNodeMap) to reduce code duplication across 6 components and leverage TanStack Query's select option for efficient nodeMap computation.
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
'use client'
|
|
|
|
import type { FC } from 'react'
|
|
import type { TreeApi } from 'react-arborist'
|
|
import type { TreeNodeData } from './type'
|
|
import { useClickAway } from 'ahooks'
|
|
import * as React from 'react'
|
|
import { useCallback, useMemo, useRef } from 'react'
|
|
import FileNodeMenu from './file-node-menu'
|
|
import FolderNodeMenu from './folder-node-menu'
|
|
import { useSkillAssetTreeData } from './hooks/use-skill-asset-tree'
|
|
import { useSkillEditorStore, useSkillEditorStoreApi } from './store'
|
|
import { findNodeById } from './utils/tree-utils'
|
|
|
|
type TreeContextMenuProps = {
|
|
treeRef: React.RefObject<TreeApi<TreeNodeData> | null>
|
|
}
|
|
|
|
const TreeContextMenu: FC<TreeContextMenuProps> = ({ treeRef }) => {
|
|
const ref = useRef<HTMLDivElement>(null)
|
|
const contextMenu = useSkillEditorStore(s => s.contextMenu)
|
|
const storeApi = useSkillEditorStoreApi()
|
|
const { data: treeData } = useSkillAssetTreeData()
|
|
|
|
const handleClose = useCallback(() => {
|
|
storeApi.getState().setContextMenu(null)
|
|
}, [storeApi])
|
|
|
|
useClickAway(() => {
|
|
handleClose()
|
|
}, ref)
|
|
|
|
const targetNode = useMemo(() => {
|
|
if (!contextMenu?.nodeId || !treeData?.children)
|
|
return null
|
|
return findNodeById(treeData.children, contextMenu.nodeId)
|
|
}, [contextMenu?.nodeId, treeData?.children])
|
|
|
|
const isFolder = targetNode?.node_type === 'folder'
|
|
|
|
if (!contextMenu)
|
|
return null
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className="fixed z-[100]"
|
|
style={{
|
|
top: contextMenu.top,
|
|
left: contextMenu.left,
|
|
}}
|
|
>
|
|
{isFolder
|
|
? (
|
|
<FolderNodeMenu
|
|
nodeId={contextMenu.nodeId}
|
|
onClose={handleClose}
|
|
treeRef={treeRef}
|
|
/>
|
|
)
|
|
: (
|
|
<FileNodeMenu
|
|
nodeId={contextMenu.nodeId}
|
|
onClose={handleClose}
|
|
treeRef={treeRef}
|
|
/>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default React.memo(TreeContextMenu)
|