feat: tool picker

This commit is contained in:
Joel 2024-10-31 18:37:42 +08:00
parent 0e873223d7
commit bca99cf4f8
3 changed files with 78 additions and 33 deletions

View File

@ -1,39 +1,13 @@
'use client' 'use client'
import { useEffect, useState } from 'react' import React from 'react'
import AllTools from '@/app/components/workflow/block-selector/all-tools' import ToolPicker from '@/app/components/workflow/block-selector/tool-picker'
import {
fetchAllBuiltInTools,
fetchAllCustomTools,
fetchAllWorkflowTools,
} from '@/service/tools'
import type { ToolWithProvider } from '@/app/components/workflow/types'
const ToolsPicker = () => { const ToolsPicker = () => {
const [buildInTools, setBuildInTools] = useState<ToolWithProvider[]>([])
const [customTools, setCustomTools] = useState<ToolWithProvider[]>([])
const [workflowTools, setWorkflowTools] = useState<ToolWithProvider[]>([])
useEffect(() => {
(async () => {
const buildInTools = await fetchAllBuiltInTools()
const customTools = await fetchAllCustomTools()
const workflowTools = await fetchAllWorkflowTools()
setBuildInTools(buildInTools)
setCustomTools(customTools)
setWorkflowTools(workflowTools)
})()
}, [])
return ( return (
<div className="relative mt-5 mx-auto w-[320px] bg-white"> <ToolPicker
<AllTools supportAddCustomTool={true}
searchText="" onSelect={() => { }}
onSelect={() => { }} />
buildInTools={buildInTools}
customTools={customTools}
workflowTools={workflowTools}
/>
</div>
) )
} }

View File

@ -15,20 +15,26 @@ import cn from '@/utils/classnames'
import { useGetLanguage } from '@/context/i18n' import { useGetLanguage } from '@/context/i18n'
import PluginList from '@/app/components/workflow/block-selector/market-place-plugin/list' import PluginList from '@/app/components/workflow/block-selector/market-place-plugin/list'
import { extensionDallE, modelGPT4, toolNotion } from '@/app/components/plugins/card/card-mock' import { extensionDallE, modelGPT4, toolNotion } from '@/app/components/plugins/card/card-mock'
import ActionButton from '../../base/action-button'
import { RiAddLine } from '@remixicon/react'
type AllToolsProps = { type AllToolsProps = {
className?: string
searchText: string searchText: string
buildInTools: ToolWithProvider[] buildInTools: ToolWithProvider[]
customTools: ToolWithProvider[] customTools: ToolWithProvider[]
workflowTools: ToolWithProvider[] workflowTools: ToolWithProvider[]
onSelect: OnSelectBlock onSelect: OnSelectBlock
supportAddCustomTool?: boolean
} }
const AllTools = ({ const AllTools = ({
className,
searchText, searchText,
onSelect, onSelect,
buildInTools, buildInTools,
workflowTools, workflowTools,
customTools, customTools,
supportAddCustomTool,
}: AllToolsProps) => { }: AllToolsProps) => {
const language = useGetLanguage() const language = useGetLanguage()
const tabs = useToolTabs() const tabs = useToolTabs()
@ -57,7 +63,7 @@ const AllTools = ({
const wrapElemRef = useRef<HTMLDivElement>(null) const wrapElemRef = useRef<HTMLDivElement>(null)
return ( return (
<div> <div className={cn(className)}>
<div className='flex items-center justify-between px-3 bg-background-default-hover border-b-[0.5px] border-black/[0.08] shadow-xs'> <div className='flex items-center justify-between px-3 bg-background-default-hover border-b-[0.5px] border-black/[0.08] shadow-xs'>
<div className='flex items-center h-8 space-x-1'> <div className='flex items-center h-8 space-x-1'>
{ {
@ -77,6 +83,11 @@ const AllTools = ({
} }
</div> </div>
<ViewTypeSelect viewType={activeView} onChange={setActiveView} /> <ViewTypeSelect viewType={activeView} onChange={setActiveView} />
{supportAddCustomTool && (
<ActionButton>
<RiAddLine className='w-4 h-4' />
</ActionButton>
)}
</div> </div>
<div <div
ref={wrapElemRef} ref={wrapElemRef}

View File

@ -0,0 +1,60 @@
'use client'
import type { FC } from 'react'
import React from 'react'
import { useEffect, useState } from 'react'
import AllTools from '@/app/components/workflow/block-selector/all-tools'
import type { ToolDefaultValue } from './types'
import {
fetchAllBuiltInTools,
fetchAllCustomTools,
fetchAllWorkflowTools,
} from '@/service/tools'
import type { BlockEnum, ToolWithProvider } from '@/app/components/workflow/types'
type Props = {
onSelect: (tool: ToolDefaultValue) => void
supportAddCustomTool?: boolean
}
const ToolPicker: FC<Props> = ({
onSelect,
supportAddCustomTool,
}) => {
const [searchText, setSearchText] = useState('')
const [buildInTools, setBuildInTools] = useState<ToolWithProvider[]>([])
const [customTools, setCustomTools] = useState<ToolWithProvider[]>([])
const [workflowTools, setWorkflowTools] = useState<ToolWithProvider[]>([])
useEffect(() => {
(async () => {
const buildInTools = await fetchAllBuiltInTools()
const customTools = await fetchAllCustomTools()
const workflowTools = await fetchAllWorkflowTools()
setBuildInTools(buildInTools)
setCustomTools(customTools)
setWorkflowTools(workflowTools)
})()
}, [])
const handleSelect = (_type: BlockEnum, tool?: ToolDefaultValue) => {
onSelect(tool!)
}
return (
<div className="relative mt-5 mx-auto w-[320px] bg-white">
<input placeholder='search holder' value={searchText} onChange={e => setSearchText(e.target.value)} />
<AllTools
className='mt-1'
searchText={searchText}
onSelect={handleSelect}
buildInTools={buildInTools}
customTools={customTools}
workflowTools={workflowTools}
supportAddCustomTool={supportAddCustomTool}
/>
</div>
)
}
export default React.memo(ToolPicker)