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.

plugins-panel.tsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use client'
  2. import { useMemo } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import type { FilterState } from './filter-management'
  5. import FilterManagement from './filter-management'
  6. import List from './list'
  7. import { useInstalledLatestVersion, useInstalledPluginList, useInvalidateInstalledPluginList } from '@/service/use-plugins'
  8. import PluginDetailPanel from '@/app/components/plugins/plugin-detail-panel'
  9. import { usePluginPageContext } from './context'
  10. import { useDebounceFn } from 'ahooks'
  11. import Button from '@/app/components/base/button'
  12. import Empty from './empty'
  13. import Loading from '../../base/loading'
  14. import { PluginSource } from '../types'
  15. const PluginsPanel = () => {
  16. const { t } = useTranslation()
  17. const filters = usePluginPageContext(v => v.filters) as FilterState
  18. const setFilters = usePluginPageContext(v => v.setFilters)
  19. const { data: pluginList, isLoading: isPluginListLoading, isFetching, isLastPage, loadNextPage } = useInstalledPluginList()
  20. const { data: installedLatestVersion } = useInstalledLatestVersion(
  21. pluginList?.plugins
  22. .filter(plugin => plugin.source === PluginSource.marketplace)
  23. .map(plugin => plugin.plugin_id) ?? [],
  24. )
  25. const invalidateInstalledPluginList = useInvalidateInstalledPluginList()
  26. const currentPluginID = usePluginPageContext(v => v.currentPluginID)
  27. const setCurrentPluginID = usePluginPageContext(v => v.setCurrentPluginID)
  28. const { run: handleFilterChange } = useDebounceFn((filters: FilterState) => {
  29. setFilters(filters)
  30. }, { wait: 500 })
  31. const pluginListWithLatestVersion = useMemo(() => {
  32. return pluginList?.plugins.map(plugin => ({
  33. ...plugin,
  34. latest_version: installedLatestVersion?.versions[plugin.plugin_id]?.version ?? '',
  35. latest_unique_identifier: installedLatestVersion?.versions[plugin.plugin_id]?.unique_identifier ?? '',
  36. status: installedLatestVersion?.versions[plugin.plugin_id]?.status ?? 'active',
  37. deprecated_reason: installedLatestVersion?.versions[plugin.plugin_id]?.deprecated_reason ?? '',
  38. alternative_plugin_id: installedLatestVersion?.versions[plugin.plugin_id]?.alternative_plugin_id ?? '',
  39. })) || []
  40. }, [pluginList, installedLatestVersion])
  41. const filteredList = useMemo(() => {
  42. const { categories, searchQuery, tags } = filters
  43. const filteredList = pluginListWithLatestVersion.filter((plugin) => {
  44. return (
  45. (categories.length === 0 || categories.includes(plugin.declaration.category))
  46. && (tags.length === 0 || tags.some(tag => plugin.declaration.tags.includes(tag)))
  47. && (searchQuery === '' || plugin.plugin_id.toLowerCase().includes(searchQuery.toLowerCase()))
  48. )
  49. })
  50. return filteredList
  51. }, [pluginListWithLatestVersion, filters])
  52. const currentPluginDetail = useMemo(() => {
  53. const detail = pluginListWithLatestVersion.find(plugin => plugin.plugin_id === currentPluginID)
  54. return detail
  55. }, [currentPluginID, pluginListWithLatestVersion])
  56. const handleHide = () => setCurrentPluginID(undefined)
  57. return (
  58. <>
  59. <div className='flex flex-col items-start justify-center gap-3 self-stretch px-12 pb-3 pt-1'>
  60. <div className='h-px self-stretch bg-divider-subtle'></div>
  61. <FilterManagement
  62. onFilterChange={handleFilterChange}
  63. />
  64. </div>
  65. {isPluginListLoading && <Loading type='app' />}
  66. {!isPluginListLoading && (
  67. <>
  68. {(filteredList?.length ?? 0) > 0 ? (
  69. <div className='flex grow flex-wrap content-start items-start justify-center gap-2 self-stretch px-12'>
  70. <div className='w-full'>
  71. <List pluginList={filteredList || []} />
  72. </div>
  73. {!isLastPage && !isFetching && (
  74. <Button onClick={loadNextPage}>
  75. {t('workflow.common.loadMore')}
  76. </Button>
  77. )}
  78. {isFetching && <div className='system-md-semibold text-text-secondary'>{t('appLog.detail.loading')}</div>}
  79. </div>
  80. ) : (
  81. <Empty />
  82. )}
  83. </>
  84. )}
  85. <PluginDetailPanel
  86. detail={currentPluginDetail}
  87. onUpdate={() => invalidateInstalledPluginList()}
  88. onHide={handleHide}
  89. />
  90. </>
  91. )
  92. }
  93. export default PluginsPanel