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.7KB

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