選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

install-plugin-dropdown.tsx 5.4KB

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