index.tsx 5.4 KB

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