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='relative z-0 w-full grow'>
  38. {/* skeleton */}
  39. <div className='absolute top-0 z-10 grid h-full w-full grid-cols-2 gap-2 overflow-hidden px-12'>
  40. {Array.from({ length: 20 }).fill(0).map((_, i) => (
  41. <div key={i} className='h-[100px] rounded-xl bg-components-card-bg' />
  42. ))}
  43. </div>
  44. {/* mask */}
  45. <div className='absolute z-20 h-full w-full bg-gradient-to-b from-background-gradient-mask-transparent to-white' />
  46. <div className='relative z-30 flex h-full items-center justify-center'>
  47. <div className='flex flex-col items-center gap-y-3'>
  48. <div className='relative -z-10 flex h-[52px] w-[52px] items-center justify-center rounded-xl
  49. border-[1px] border-dashed border-divider-deep bg-components-card-bg shadow-xl shadow-shadow-shadow-5'>
  50. <Group className='h-5 w-5 text-text-tertiary' />
  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 left-1/2 top-0 -translate-x-1/2 -translate-y-1/2 rotate-90' />
  54. <Line className='absolute left-1/2 top-full -translate-x-1/2 -translate-y-1/2 rotate-90' />
  55. </div>
  56. <div className='text-sm font-normal text-text-tertiary'>
  57. {text}
  58. </div>
  59. <div className='flex w-[240px] flex-col'>
  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='flex w-full 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 cursor-pointer items-center gap-x-1 rounded-lg border-[0.5px] bg-components-button-secondary-bg
  80. px-3 py-2 shadow-xs shadow-shadow-shadow-3 hover:bg-state-base-hover'
  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="h-4 w-4 text-text-tertiary" />
  91. <span className='system-md-regular text-text-secondary'>{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)