import { memo, useCallback, useEffect, useMemo, } from 'react' import { useContext } from 'use-context-selector' import { BlockEnum } from '../../types' import type { CollectionWithExpanded, ToolDefaultValue, ToolInWorkflow, } from '../types' import AppIcon from '@/app/components/base/app-icon' import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows' import { fetchBuiltInToolList, fetchCustomToolList, } from '@/service/tools' import I18n from '@/context/i18n' import { getLanguage } from '@/i18n/language' import Loading from '@/app/components/base/loading' type ItemProps = { data: CollectionWithExpanded tools: ToolInWorkflow[] onExpand: (toolsetId: string) => void onAddTools: (toolsetId: string, tools: ToolInWorkflow[]) => void onFetched: (toolsetId: string) => void onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void } const Item = ({ data, tools, onExpand, onAddTools, onFetched, onSelect, }: ItemProps) => { const { locale } = useContext(I18n) const language = getLanguage(locale) const fetchToolList = useMemo(() => { return data.type === 'api' ? fetchCustomToolList : fetchBuiltInToolList }, [data.type]) const handleFetchToolList = useCallback(() => { fetchToolList(data.name).then((list) => { onAddTools(data.id, list) }).finally(() => { onFetched(data.id) }) }, [data.id, data.name, fetchToolList, onAddTools, onFetched]) useEffect(() => { if (data.fetching) handleFetchToolList() }, [data.fetching, handleFetchToolList]) return ( <>