mirror of
https://github.com/langgenius/dify.git
synced 2026-02-25 12:16:29 +08:00
- Add constants.ts with ROOT_ID, CONTEXT_MENU_TYPE, NODE_MENU_TYPE - Add root utilities to tree-utils.ts (isRootId, toApiParentId, etc.) - Replace '__root__' with ROOT_ID for consistent root identifier - Replace inline 'blank'/'root' strings with constants - Use NodeMenuType for type-safe menu type props - Remove duplicate ContextMenuType from types.ts, use from constants.ts
53 lines
1.3 KiB
TypeScript
53 lines
1.3 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, useRef } from 'react'
|
|
import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
|
|
import { getMenuNodeId, getNodeMenuType } from '../utils/tree-utils'
|
|
import NodeMenu from './node-menu'
|
|
|
|
type TreeContextMenuProps = {
|
|
treeRef: React.RefObject<TreeApi<TreeNodeData> | null>
|
|
}
|
|
|
|
const TreeContextMenu: FC<TreeContextMenuProps> = ({ treeRef }) => {
|
|
const ref = useRef<HTMLDivElement>(null)
|
|
const contextMenu = useStore(s => s.contextMenu)
|
|
const storeApi = useWorkflowStore()
|
|
|
|
const handleClose = useCallback(() => {
|
|
storeApi.getState().setContextMenu(null)
|
|
}, [storeApi])
|
|
|
|
useClickAway(() => {
|
|
handleClose()
|
|
}, ref)
|
|
|
|
if (!contextMenu)
|
|
return null
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
className="fixed z-[100]"
|
|
style={{
|
|
top: contextMenu.top,
|
|
left: contextMenu.left,
|
|
}}
|
|
>
|
|
<NodeMenu
|
|
type={getNodeMenuType(contextMenu.type, contextMenu.isFolder)}
|
|
nodeId={getMenuNodeId(contextMenu.type, contextMenu.nodeId)}
|
|
onClose={handleClose}
|
|
treeRef={treeRef}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default React.memo(TreeContextMenu)
|