provider-list.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use client'
  2. import { useEffect, useMemo, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiCloseLine } from '@remixicon/react'
  5. import type { Collection } from './types'
  6. import cn from '@/utils/classnames'
  7. import { useTabSearchParams } from '@/hooks/use-tab-searchparams'
  8. import TabSliderNew from '@/app/components/base/tab-slider-new'
  9. import LabelFilter from '@/app/components/tools/labels/filter'
  10. import Input from '@/app/components/base/input'
  11. import { DotsGrid } from '@/app/components/base/icons/src/vender/line/general'
  12. import { Colors } from '@/app/components/base/icons/src/vender/line/others'
  13. import { Route } from '@/app/components/base/icons/src/vender/line/mapsAndTravel'
  14. import CustomCreateCard from '@/app/components/tools/provider/custom-create-card'
  15. import ContributeCard from '@/app/components/tools/provider/contribute'
  16. import ProviderCard from '@/app/components/tools/provider/card'
  17. import ProviderDetail from '@/app/components/tools/provider/detail'
  18. import Empty from '@/app/components/tools/add-tool-modal/empty'
  19. import { fetchCollectionList } from '@/service/tools'
  20. const ProviderList = () => {
  21. const { t } = useTranslation()
  22. const [activeTab, setActiveTab] = useTabSearchParams({
  23. defaultTab: 'builtin',
  24. })
  25. const options = [
  26. { value: 'builtin', text: t('tools.type.builtIn'), icon: <DotsGrid className='w-[14px] h-[14px] mr-1' /> },
  27. { value: 'api', text: t('tools.type.custom'), icon: <Colors className='w-[14px] h-[14px] mr-1' /> },
  28. { value: 'workflow', text: t('tools.type.workflow'), icon: <Route className='w-[14px] h-[14px] mr-1' /> },
  29. ]
  30. const [tagFilterValue, setTagFilterValue] = useState<string[]>([])
  31. const handleTagsChange = (value: string[]) => {
  32. setTagFilterValue(value)
  33. }
  34. const [keywords, setKeywords] = useState<string>('')
  35. const handleKeywordsChange = (value: string) => {
  36. setKeywords(value)
  37. }
  38. const [collectionList, setCollectionList] = useState<Collection[]>([])
  39. const filteredCollectionList = useMemo(() => {
  40. return collectionList.filter((collection) => {
  41. if (collection.type !== activeTab)
  42. return false
  43. if (tagFilterValue.length > 0 && (!collection.labels || collection.labels.every(label => !tagFilterValue.includes(label))))
  44. return false
  45. if (keywords)
  46. return collection.name.toLowerCase().includes(keywords.toLowerCase())
  47. return true
  48. })
  49. }, [activeTab, tagFilterValue, keywords, collectionList])
  50. const getProviderList = async () => {
  51. const list = await fetchCollectionList()
  52. setCollectionList([...list])
  53. }
  54. useEffect(() => {
  55. getProviderList()
  56. }, [])
  57. const [currentProvider, setCurrentProvider] = useState<Collection | undefined>()
  58. useEffect(() => {
  59. if (currentProvider && collectionList.length > 0) {
  60. const newCurrentProvider = collectionList.find(collection => collection.id === currentProvider.id)
  61. setCurrentProvider(newCurrentProvider)
  62. }
  63. }, [collectionList, currentProvider])
  64. return (
  65. <div className='relative flex overflow-hidden bg-gray-100 shrink-0 h-0 grow'>
  66. <div className='relative flex flex-col overflow-y-auto bg-gray-100 grow'>
  67. <div className={cn(
  68. 'sticky top-0 flex justify-between items-center pt-4 px-12 pb-2 leading-[56px] bg-gray-100 z-20 flex-wrap gap-y-2',
  69. currentProvider && 'pr-6',
  70. )}>
  71. <TabSliderNew
  72. value={activeTab}
  73. onChange={(state) => {
  74. setActiveTab(state)
  75. if (state !== activeTab)
  76. setCurrentProvider(undefined)
  77. }}
  78. options={options}
  79. />
  80. <div className='flex items-center gap-2'>
  81. <LabelFilter value={tagFilterValue} onChange={handleTagsChange} />
  82. <Input
  83. showLeftIcon
  84. showClearIcon
  85. wrapperClassName='w-[200px]'
  86. value={keywords}
  87. onChange={e => handleKeywordsChange(e.target.value)}
  88. onClear={() => handleKeywordsChange('')}
  89. />
  90. </div>
  91. </div>
  92. <div className={cn(
  93. 'relative grid content-start grid-cols-1 gap-4 px-12 pt-2 pb-4 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 grow shrink-0',
  94. currentProvider && 'pr-6 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
  95. )}>
  96. {activeTab === 'builtin' && <ContributeCard />}
  97. {activeTab === 'api' && <CustomCreateCard onRefreshData={getProviderList} />}
  98. {filteredCollectionList.map(collection => (
  99. <ProviderCard
  100. active={currentProvider?.id === collection.id}
  101. onSelect={() => setCurrentProvider(collection)}
  102. key={collection.id}
  103. collection={collection}
  104. />
  105. ))}
  106. {!filteredCollectionList.length && <div className='absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2'><Empty /></div>}
  107. </div>
  108. </div>
  109. <div className={cn(
  110. 'shrink-0 w-0 border-l-[0.5px] border-black/8 overflow-y-auto transition-all duration-200 ease-in-out',
  111. currentProvider && 'w-[420px]',
  112. )}>
  113. {currentProvider && <ProviderDetail collection={currentProvider} onRefreshData={getProviderList} />}
  114. </div>
  115. <div className='absolute top-5 right-5 p-1 cursor-pointer' onClick={() => setCurrentProvider(undefined)}><RiCloseLine className='w-4 h-4' /></div>
  116. </div>
  117. )
  118. }
  119. ProviderList.displayName = 'ToolProviderList'
  120. export default ProviderList