install-plugin-dropdown.tsx 5.4 KB

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