You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-tool-icon.ts 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {
  2. useCallback,
  3. useMemo,
  4. } from 'react'
  5. import type {
  6. Node,
  7. } from '../types'
  8. import {
  9. BlockEnum,
  10. } from '../types'
  11. import {
  12. useStore,
  13. useWorkflowStore,
  14. } from '../store'
  15. import { CollectionType } from '@/app/components/tools/types'
  16. import { canFindTool } from '@/utils'
  17. export const useToolIcon = (data?: Node['data']) => {
  18. const buildInTools = useStore(s => s.buildInTools)
  19. const customTools = useStore(s => s.customTools)
  20. const workflowTools = useStore(s => s.workflowTools)
  21. const mcpTools = useStore(s => s.mcpTools)
  22. const dataSourceList = useStore(s => s.dataSourceList)
  23. // const a = useStore(s => s.data)
  24. const toolIcon = useMemo(() => {
  25. if (!data)
  26. return ''
  27. if (data.type === BlockEnum.Tool) {
  28. // eslint-disable-next-line sonarjs/no-dead-store
  29. let targetTools = buildInTools
  30. if (data.provider_type === CollectionType.builtIn)
  31. targetTools = buildInTools
  32. else if (data.provider_type === CollectionType.custom)
  33. targetTools = customTools
  34. else if (data.provider_type === CollectionType.mcp)
  35. targetTools = mcpTools
  36. else
  37. targetTools = workflowTools
  38. return targetTools.find(toolWithProvider => canFindTool(toolWithProvider.id, data.provider_id))?.icon
  39. }
  40. if (data.type === BlockEnum.DataSource)
  41. return dataSourceList?.find(toolWithProvider => toolWithProvider.plugin_id === data.plugin_id)?.icon
  42. }, [data, dataSourceList, buildInTools, customTools, mcpTools, workflowTools])
  43. return toolIcon
  44. }
  45. export const useGetToolIcon = () => {
  46. const workflowStore = useWorkflowStore()
  47. const getToolIcon = useCallback((data: Node['data']) => {
  48. const {
  49. buildInTools,
  50. customTools,
  51. workflowTools,
  52. dataSourceList,
  53. } = workflowStore.getState()
  54. if (data.type === BlockEnum.Tool) {
  55. // eslint-disable-next-line sonarjs/no-dead-store
  56. let targetTools = buildInTools
  57. if (data.provider_type === CollectionType.builtIn)
  58. targetTools = buildInTools
  59. else if (data.provider_type === CollectionType.custom)
  60. targetTools = customTools
  61. else
  62. targetTools = workflowTools
  63. return targetTools.find(toolWithProvider => canFindTool(toolWithProvider.id, data.provider_id))?.icon
  64. }
  65. if (data.type === BlockEnum.DataSource)
  66. return dataSourceList?.find(toolWithProvider => toolWithProvider.plugin_id === data.plugin_id)?.icon
  67. }, [workflowStore])
  68. return getToolIcon
  69. }