Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

plugins-panel.tsx 3.3KB

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