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.

index.tsx 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React, { useMemo, useRef, useState } from 'react'
  2. import { MagicBox } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  3. import { FileZip } from '@/app/components/base/icons/src/vender/solid/files'
  4. import { Github } from '@/app/components/base/icons/src/vender/solid/general'
  5. import InstallFromGitHub from '@/app/components/plugins/install-plugin/install-from-github'
  6. import InstallFromLocalPackage from '@/app/components/plugins/install-plugin/install-from-local-package'
  7. import { usePluginPageContext } from '../context'
  8. import { Group } from '@/app/components/base/icons/src/vender/other'
  9. import Line from '../../marketplace/empty/line'
  10. import { useInstalledPluginList } from '@/service/use-plugins'
  11. import { useTranslation } from 'react-i18next'
  12. import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
  13. import { noop } from 'lodash-es'
  14. import { useGlobalPublicStore } from '@/context/global-public-context'
  15. import Button from '@/app/components/base/button'
  16. const Empty = () => {
  17. const { t } = useTranslation()
  18. const fileInputRef = useRef<HTMLInputElement>(null)
  19. const [selectedAction, setSelectedAction] = useState<string | null>(null)
  20. const [selectedFile, setSelectedFile] = useState<File | null>(null)
  21. const { enable_marketplace } = useGlobalPublicStore(s => s.systemFeatures)
  22. const setActiveTab = usePluginPageContext(v => v.setActiveTab)
  23. const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  24. const file = event.target.files?.[0]
  25. if (file) {
  26. setSelectedFile(file)
  27. setSelectedAction('local')
  28. }
  29. }
  30. const filters = usePluginPageContext(v => v.filters)
  31. const { data: pluginList } = useInstalledPluginList()
  32. const text = useMemo(() => {
  33. if (pluginList?.plugins.length === 0)
  34. return t('plugin.list.noInstalled')
  35. if (filters.categories.length > 0 || filters.tags.length > 0 || filters.searchQuery)
  36. return t('plugin.list.notFound')
  37. }, [pluginList?.plugins.length, t, filters.categories.length, filters.tags.length, filters.searchQuery])
  38. return (
  39. <div className='relative z-0 w-full grow'>
  40. {/* skeleton */}
  41. <div className='absolute top-0 z-10 grid h-full w-full grid-cols-2 gap-2 overflow-hidden px-12'>
  42. {Array.from({ length: 20 }).fill(0).map((_, i) => (
  43. <div key={i} className='h-24 rounded-xl bg-components-card-bg' />
  44. ))}
  45. </div>
  46. {/* mask */}
  47. <div className='absolute z-20 h-full w-full bg-gradient-to-b from-components-panel-bg-transparent to-components-panel-bg' />
  48. <div className='relative z-30 flex h-full items-center justify-center'>
  49. <div className='flex flex-col items-center gap-y-3'>
  50. <div className='relative -z-10 flex size-14 items-center justify-center rounded-xl
  51. border-[1px] border-dashed border-divider-deep bg-components-card-bg shadow-xl shadow-shadow-shadow-5'>
  52. <Group className='h-5 w-5 text-text-tertiary' />
  53. <Line className='absolute right-[-1px] top-1/2 -translate-y-1/2' />
  54. <Line className='absolute left-[-1px] top-1/2 -translate-y-1/2' />
  55. <Line className='absolute left-1/2 top-0 -translate-x-1/2 -translate-y-1/2 rotate-90' />
  56. <Line className='absolute left-1/2 top-full -translate-x-1/2 -translate-y-1/2 rotate-90' />
  57. </div>
  58. <div className='system-md-regular text-text-tertiary'>
  59. {text}
  60. </div>
  61. <div className='flex w-[236px] flex-col'>
  62. <input
  63. type='file'
  64. ref={fileInputRef}
  65. style={{ display: 'none' }}
  66. onChange={handleFileChange}
  67. accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
  68. />
  69. <div className='flex w-full flex-col gap-y-1'>
  70. {[
  71. ...(
  72. (enable_marketplace)
  73. ? [{ icon: MagicBox, text: t('plugin.list.source.marketplace'), action: 'marketplace' }]
  74. : []
  75. ),
  76. { icon: Github, text: t('plugin.list.source.github'), action: 'github' },
  77. { icon: FileZip, text: t('plugin.list.source.local'), action: 'local' },
  78. ].map(({ icon: Icon, text, action }) => (
  79. <Button
  80. key={action}
  81. className='justify-start gap-x-0.5 px-3'
  82. onClick={() => {
  83. if (action === 'local')
  84. fileInputRef.current?.click()
  85. else if (action === 'marketplace')
  86. setActiveTab('discover')
  87. else
  88. setSelectedAction(action)
  89. }}
  90. >
  91. <Icon className='size-4' />
  92. <span className='px-0.5'>{text}</span>
  93. </Button>
  94. ))}
  95. </div>
  96. </div>
  97. </div>
  98. {selectedAction === 'github' && <InstallFromGitHub
  99. onSuccess={noop}
  100. onClose={() => setSelectedAction(null)}
  101. />}
  102. {selectedAction === 'local' && selectedFile
  103. && (<InstallFromLocalPackage
  104. file={selectedFile}
  105. onClose={() => setSelectedAction(null)}
  106. onSuccess={noop}
  107. />
  108. )
  109. }
  110. </div>
  111. </div>
  112. )
  113. }
  114. Empty.displayName = 'Empty'
  115. export default React.memo(Empty)