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.

install-plugin-dropdown.tsx 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use client'
  2. import { useEffect, useRef, useState } from 'react'
  3. import { RiAddLine, RiArrowDownSLine } from '@remixicon/react'
  4. import Button from '@/app/components/base/button'
  5. import { MagicBox } from '@/app/components/base/icons/src/vender/solid/mediaAndDevices'
  6. import { FileZip } from '@/app/components/base/icons/src/vender/solid/files'
  7. import { Github } from '@/app/components/base/icons/src/vender/solid/general'
  8. import InstallFromGitHub from '@/app/components/plugins/install-plugin/install-from-github'
  9. import InstallFromLocalPackage from '@/app/components/plugins/install-plugin/install-from-local-package'
  10. import cn from '@/utils/classnames'
  11. import {
  12. PortalToFollowElem,
  13. PortalToFollowElemContent,
  14. PortalToFollowElemTrigger,
  15. } from '@/app/components/base/portal-to-follow-elem'
  16. import { useTranslation } from 'react-i18next'
  17. import { SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS } from '@/config'
  18. import { noop } from 'lodash-es'
  19. import { useGlobalPublicStore } from '@/context/global-public-context'
  20. type Props = {
  21. onSwitchToMarketplaceTab: () => void
  22. }
  23. type InstallMethod = {
  24. icon: React.FC<{ className?: string }>
  25. text: string
  26. action: string
  27. }
  28. const InstallPluginDropdown = ({
  29. onSwitchToMarketplaceTab,
  30. }: Props) => {
  31. const { t } = useTranslation()
  32. const fileInputRef = useRef<HTMLInputElement>(null)
  33. const [isMenuOpen, setIsMenuOpen] = useState(false)
  34. const [selectedAction, setSelectedAction] = useState<string | null>(null)
  35. const [selectedFile, setSelectedFile] = useState<File | null>(null)
  36. const { enable_marketplace, plugin_installation_permission } = useGlobalPublicStore(s => s.systemFeatures)
  37. const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  38. const file = event.target.files?.[0]
  39. if (file) {
  40. setSelectedFile(file)
  41. setSelectedAction('local')
  42. setIsMenuOpen(false)
  43. }
  44. }
  45. // TODO TEST INSTALL : uninstall
  46. // const [pluginLists, setPluginLists] = useState<any>([])
  47. // useEffect(() => {
  48. // (async () => {
  49. // const list: any = await get('workspaces/current/plugin/list')
  50. // })()
  51. // })
  52. // const handleUninstall = async (id: string) => {
  53. // const res = await post('workspaces/current/plugin/uninstall', { body: { plugin_installation_id: id } })
  54. // console.log(res)
  55. // }
  56. const [installMethods, setInstallMethods] = useState<InstallMethod[]>([])
  57. useEffect(() => {
  58. const methods = []
  59. if (enable_marketplace)
  60. methods.push({ icon: MagicBox, text: t('plugin.source.marketplace'), action: 'marketplace' })
  61. if (plugin_installation_permission.restrict_to_marketplace_only) {
  62. setInstallMethods(methods)
  63. }
  64. else {
  65. methods.push({ icon: Github, text: t('plugin.source.github'), action: 'github' })
  66. methods.push({ icon: FileZip, text: t('plugin.source.local'), action: 'local' })
  67. setInstallMethods(methods)
  68. }
  69. }, [plugin_installation_permission, enable_marketplace, t])
  70. return (
  71. <PortalToFollowElem
  72. open={isMenuOpen}
  73. onOpenChange={setIsMenuOpen}
  74. placement='bottom-start'
  75. offset={4}
  76. >
  77. <div className="relative">
  78. <PortalToFollowElemTrigger onClick={() => setIsMenuOpen(v => !v)}>
  79. <Button
  80. className={cn('h-full w-full p-2 text-components-button-secondary-text', isMenuOpen && 'bg-state-base-hover')}
  81. >
  82. <RiAddLine className='h-4 w-4' />
  83. <span className='pl-1'>{t('plugin.installPlugin')}</span>
  84. <RiArrowDownSLine className='ml-1 h-4 w-4' />
  85. </Button>
  86. </PortalToFollowElemTrigger>
  87. <PortalToFollowElemContent className='z-[1002]'>
  88. <div className='shadows-shadow-lg flex w-[200px] flex-col items-start rounded-xl border border-components-panel-border bg-components-panel-bg-blur p-1 pb-2'>
  89. <span className='system-xs-medium-uppercase flex items-start self-stretch pb-0.5 pl-2 pr-3 pt-1 text-text-tertiary'>
  90. {t('plugin.installFrom')}
  91. </span>
  92. <input
  93. type='file'
  94. ref={fileInputRef}
  95. style={{ display: 'none' }}
  96. onChange={handleFileChange}
  97. accept={SUPPORT_INSTALL_LOCAL_FILE_EXTENSIONS}
  98. />
  99. <div className='w-full'>
  100. {installMethods.map(({ icon: Icon, text, action }) => (
  101. <div
  102. key={action}
  103. className='flex w-full !cursor-pointer items-center gap-1 rounded-lg px-2 py-1.5 hover:bg-state-base-hover'
  104. onClick={() => {
  105. if (action === 'local') {
  106. fileInputRef.current?.click()
  107. }
  108. else if (action === 'marketplace') {
  109. onSwitchToMarketplaceTab()
  110. setIsMenuOpen(false)
  111. }
  112. else {
  113. setSelectedAction(action)
  114. setIsMenuOpen(false)
  115. }
  116. }}
  117. >
  118. <Icon className="h-4 w-4 text-text-tertiary" />
  119. <span className='system-md-regular px-1 text-text-secondary'>{text}</span>
  120. </div>
  121. ))}
  122. </div>
  123. </div>
  124. </PortalToFollowElemContent>
  125. </div>
  126. {selectedAction === 'github' && <InstallFromGitHub
  127. onSuccess={noop}
  128. onClose={() => setSelectedAction(null)}
  129. />}
  130. {selectedAction === 'local' && selectedFile
  131. && (<InstallFromLocalPackage
  132. file={selectedFile}
  133. onClose={() => setSelectedAction(null)}
  134. onSuccess={noop}
  135. />
  136. )
  137. }
  138. {/* {pluginLists.map((item: any) => (
  139. <div key={item.id} onClick={() => handleUninstall(item.id)}>{item.name} 卸载</div>
  140. ))} */}
  141. </PortalToFollowElem>
  142. )
  143. }
  144. export default InstallPluginDropdown