mirror of
https://github.com/langgenius/dify.git
synced 2026-05-06 02:18:08 +08:00
refactor(skill): simplify file tree search state management
Move searchTerm from props drilling to zustand store for cleaner architecture. Remove unnecessary controlled/uncontrolled pattern and unused debounce logic since search is pure frontend filtering. - Add fileTreeSearchTerm state to file-tree-slice - Remove useState and props from main.tsx - Simplify sidebar-search-add.tsx to read/write store directly - Add empty state UI with reset filter button
This commit is contained in:
@ -10,6 +10,8 @@ import * as React from 'react'
|
|||||||
import { useCallback, useEffect, useMemo, useRef } from 'react'
|
import { useCallback, useEffect, useMemo, useRef } from 'react'
|
||||||
import { Tree } from 'react-arborist'
|
import { Tree } from 'react-arborist'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import Button from '@/app/components/base/button'
|
||||||
|
import SearchMenu from '@/app/components/base/icons/src/vender/knowledge/SearchMenu'
|
||||||
import Loading from '@/app/components/base/loading'
|
import Loading from '@/app/components/base/loading'
|
||||||
import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
|
import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
|
||||||
import { cn } from '@/utils/classnames'
|
import { cn } from '@/utils/classnames'
|
||||||
@ -23,7 +25,6 @@ import TreeNode from './tree-node'
|
|||||||
|
|
||||||
type FileTreeProps = {
|
type FileTreeProps = {
|
||||||
className?: string
|
className?: string
|
||||||
searchTerm?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const emptyTreeNodes: TreeNodeData[] = []
|
const emptyTreeNodes: TreeNodeData[] = []
|
||||||
@ -40,7 +41,7 @@ const DropTip = () => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const FileTree: React.FC<FileTreeProps> = ({ className, searchTerm = '' }) => {
|
const FileTree: React.FC<FileTreeProps> = ({ className }) => {
|
||||||
const { t } = useTranslation('workflow')
|
const { t } = useTranslation('workflow')
|
||||||
const treeRef = useRef<TreeApi<TreeNodeData>>(null)
|
const treeRef = useRef<TreeApi<TreeNodeData>>(null)
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
@ -61,6 +62,7 @@ const FileTree: React.FC<FileTreeProps> = ({ className, searchTerm = '' }) => {
|
|||||||
const activeTabId = useStore(s => s.activeTabId)
|
const activeTabId = useStore(s => s.activeTabId)
|
||||||
const selectedTreeNodeId = useStore(s => s.selectedTreeNodeId)
|
const selectedTreeNodeId = useStore(s => s.selectedTreeNodeId)
|
||||||
const dragOverFolderId = useStore(s => s.dragOverFolderId)
|
const dragOverFolderId = useStore(s => s.dragOverFolderId)
|
||||||
|
const searchTerm = useStore(s => s.fileTreeSearchTerm)
|
||||||
const storeApi = useWorkflowStore()
|
const storeApi = useWorkflowStore()
|
||||||
|
|
||||||
// Root dropzone highlight (when dragging to root, not to a specific folder)
|
// Root dropzone highlight (when dragging to root, not to a specific folder)
|
||||||
@ -88,6 +90,25 @@ const FileTree: React.FC<FileTreeProps> = ({ className, searchTerm = '' }) => {
|
|||||||
)
|
)
|
||||||
}, [expandedFolderIds])
|
}, [expandedFolderIds])
|
||||||
|
|
||||||
|
// Check if search has no results (has search term but no matches)
|
||||||
|
const hasSearchNoResults = useMemo(() => {
|
||||||
|
if (!searchTerm || treeChildren.length === 0)
|
||||||
|
return false
|
||||||
|
|
||||||
|
const lowerSearchTerm = searchTerm.toLowerCase()
|
||||||
|
|
||||||
|
const checkMatch = (nodes: TreeNodeData[]): boolean => {
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (node.name.toLowerCase().includes(lowerSearchTerm))
|
||||||
|
return true
|
||||||
|
if (node.children && checkMatch(node.children))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return !checkMatch(treeChildren)
|
||||||
|
}, [searchTerm, treeChildren])
|
||||||
|
|
||||||
const handleToggle = useCallback((id: string) => {
|
const handleToggle = useCallback((id: string) => {
|
||||||
storeApi.getState().toggleFolder(id)
|
storeApi.getState().toggleFolder(id)
|
||||||
}, [storeApi])
|
}, [storeApi])
|
||||||
@ -155,6 +176,27 @@ const FileTree: React.FC<FileTreeProps> = ({ className, searchTerm = '' }) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search has no matching results
|
||||||
|
if (hasSearchNoResults) {
|
||||||
|
return (
|
||||||
|
<div className={cn('flex min-h-0 flex-1 flex-col', className)}>
|
||||||
|
<div className="flex flex-1 flex-col items-center justify-center gap-2 pb-20">
|
||||||
|
<SearchMenu className="size-8 text-text-tertiary" />
|
||||||
|
<span className="system-xs-regular text-text-secondary">
|
||||||
|
{t('skillSidebar.searchNoResults')}
|
||||||
|
</span>
|
||||||
|
<Button
|
||||||
|
variant="secondary-accent"
|
||||||
|
size="small"
|
||||||
|
onClick={() => storeApi.getState().setFileTreeSearchTerm('')}
|
||||||
|
>
|
||||||
|
{t('skillSidebar.resetFilter')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import { useCallback, useState } from 'react'
|
|
||||||
import ContentArea from './content-area'
|
import ContentArea from './content-area'
|
||||||
import ContentBody from './content-body'
|
import ContentBody from './content-body'
|
||||||
import FileContentPanel from './file-content-panel'
|
import FileContentPanel from './file-content-panel'
|
||||||
@ -13,18 +12,12 @@ import SidebarSearchAdd from './sidebar-search-add'
|
|||||||
import SkillPageLayout from './skill-page-layout'
|
import SkillPageLayout from './skill-page-layout'
|
||||||
|
|
||||||
const SkillMain: FC = () => {
|
const SkillMain: FC = () => {
|
||||||
const [searchTerm, setSearchTerm] = useState('')
|
|
||||||
|
|
||||||
const handleSearchChange = useCallback((term: string) => {
|
|
||||||
setSearchTerm(term)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="h-full bg-workflow-canvas-workflow-top-bar-1 pl-3 pt-[52px]">
|
<div className="h-full bg-workflow-canvas-workflow-top-bar-1 pl-3 pt-[52px]">
|
||||||
<SkillPageLayout>
|
<SkillPageLayout>
|
||||||
<Sidebar>
|
<Sidebar>
|
||||||
<SidebarSearchAdd onSearchChange={handleSearchChange} />
|
<SidebarSearchAdd />
|
||||||
<FileTree searchTerm={searchTerm} />
|
<FileTree />
|
||||||
</Sidebar>
|
</Sidebar>
|
||||||
<ContentArea>
|
<ContentArea>
|
||||||
<FileTabs />
|
<FileTabs />
|
||||||
|
|||||||
@ -8,9 +8,8 @@ import {
|
|||||||
RiFolderUploadLine,
|
RiFolderUploadLine,
|
||||||
RiUploadLine,
|
RiUploadLine,
|
||||||
} from '@remixicon/react'
|
} from '@remixicon/react'
|
||||||
import { useDebounce } from 'ahooks'
|
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useMemo, useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import Button from '@/app/components/base/button'
|
import Button from '@/app/components/base/button'
|
||||||
import {
|
import {
|
||||||
@ -19,17 +18,13 @@ import {
|
|||||||
PortalToFollowElemTrigger,
|
PortalToFollowElemTrigger,
|
||||||
} from '@/app/components/base/portal-to-follow-elem'
|
} from '@/app/components/base/portal-to-follow-elem'
|
||||||
import SearchInput from '@/app/components/base/search-input'
|
import SearchInput from '@/app/components/base/search-input'
|
||||||
import { useStore } from '@/app/components/workflow/store'
|
import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
|
||||||
import { cn } from '@/utils/classnames'
|
import { cn } from '@/utils/classnames'
|
||||||
import { ROOT_ID } from './constants'
|
import { ROOT_ID } from './constants'
|
||||||
import { useFileOperations } from './hooks/use-file-operations'
|
import { useFileOperations } from './hooks/use-file-operations'
|
||||||
import { useSkillAssetTreeData } from './hooks/use-skill-asset-tree'
|
import { useSkillAssetTreeData } from './hooks/use-skill-asset-tree'
|
||||||
import { getTargetFolderIdFromSelection } from './utils/tree-utils'
|
import { getTargetFolderIdFromSelection } from './utils/tree-utils'
|
||||||
|
|
||||||
type SidebarSearchAddProps = {
|
|
||||||
onSearchChange?: (searchTerm: string) => void
|
|
||||||
}
|
|
||||||
|
|
||||||
type MenuItemProps = {
|
type MenuItemProps = {
|
||||||
icon: React.ElementType
|
icon: React.ElementType
|
||||||
label: string
|
label: string
|
||||||
@ -55,16 +50,12 @@ const MenuItem: React.FC<MenuItemProps> = ({ icon: Icon, label, onClick, disable
|
|||||||
</button>
|
</button>
|
||||||
)
|
)
|
||||||
|
|
||||||
const SidebarSearchAdd: FC<SidebarSearchAddProps> = ({ onSearchChange }) => {
|
const SidebarSearchAdd: FC = () => {
|
||||||
const { t } = useTranslation('workflow')
|
const { t } = useTranslation('workflow')
|
||||||
const [searchValue, setSearchValue] = useState('')
|
const searchValue = useStore(s => s.fileTreeSearchTerm)
|
||||||
const debouncedSearchValue = useDebounce(searchValue, { wait: 300 })
|
const storeApi = useWorkflowStore()
|
||||||
const [showMenu, setShowMenu] = useState(false)
|
const [showMenu, setShowMenu] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
onSearchChange?.(debouncedSearchValue)
|
|
||||||
}, [debouncedSearchValue, onSearchChange])
|
|
||||||
|
|
||||||
const { data: treeData } = useSkillAssetTreeData()
|
const { data: treeData } = useSkillAssetTreeData()
|
||||||
const selectedTreeNodeId = useStore(s => s.selectedTreeNodeId)
|
const selectedTreeNodeId = useStore(s => s.selectedTreeNodeId)
|
||||||
const treeChildren = treeData?.children
|
const treeChildren = treeData?.children
|
||||||
@ -93,7 +84,7 @@ const SidebarSearchAdd: FC<SidebarSearchAddProps> = ({ onSearchChange }) => {
|
|||||||
<div className="flex items-center gap-1 p-2">
|
<div className="flex items-center gap-1 p-2">
|
||||||
<SearchInput
|
<SearchInput
|
||||||
value={searchValue}
|
value={searchValue}
|
||||||
onChange={setSearchValue}
|
onChange={v => storeApi.getState().setFileTreeSearchTerm(v)}
|
||||||
className="!h-6 flex-1 !rounded-md"
|
className="!h-6 flex-1 !rounded-md"
|
||||||
placeholder={t('skillSidebar.searchPlaceholder')}
|
placeholder={t('skillSidebar.searchPlaceholder')}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -80,4 +80,10 @@ export const createFileTreeSlice: StateCreator<
|
|||||||
setDragOverFolderId: (folderId) => {
|
setDragOverFolderId: (folderId) => {
|
||||||
set({ dragOverFolderId: folderId })
|
set({ dragOverFolderId: folderId })
|
||||||
},
|
},
|
||||||
|
|
||||||
|
fileTreeSearchTerm: '',
|
||||||
|
|
||||||
|
setFileTreeSearchTerm: (term) => {
|
||||||
|
set({ fileTreeSearchTerm: term })
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@ -33,6 +33,7 @@ export const createSkillEditorSlice: StateCreator<SkillEditorSliceShape> = (...a
|
|||||||
fileMetadata: new Map<string, Record<string, unknown>>(),
|
fileMetadata: new Map<string, Record<string, unknown>>(),
|
||||||
dirtyMetadataIds: new Set<string>(),
|
dirtyMetadataIds: new Set<string>(),
|
||||||
contextMenu: null,
|
contextMenu: null,
|
||||||
|
fileTreeSearchTerm: '',
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@ -37,6 +37,8 @@ export type FileTreeSliceShape = {
|
|||||||
clearCreateNode: () => void
|
clearCreateNode: () => void
|
||||||
dragOverFolderId: string | null
|
dragOverFolderId: string | null
|
||||||
setDragOverFolderId: (folderId: string | null) => void
|
setDragOverFolderId: (folderId: string | null) => void
|
||||||
|
fileTreeSearchTerm: string
|
||||||
|
setFileTreeSearchTerm: (term: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type DirtySliceShape = {
|
export type DirtySliceShape = {
|
||||||
|
|||||||
Reference in New Issue
Block a user