uploading.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React from 'react'
  4. import { RiLoader2Line } from '@remixicon/react'
  5. import Card from '../../../card'
  6. import type { Dependency, PluginDeclaration } from '../../../types'
  7. import Button from '@/app/components/base/button'
  8. import { useTranslation } from 'react-i18next'
  9. import { uploadFile } from '@/service/plugins'
  10. const i18nPrefix = 'plugin.installModal'
  11. type Props = {
  12. isBundle: boolean
  13. file: File
  14. onCancel: () => void
  15. onPackageUploaded: (result: {
  16. uniqueIdentifier: string
  17. manifest: PluginDeclaration
  18. }) => void
  19. onBundleUploaded: (result: Dependency[]) => void
  20. onFailed: (errorMsg: string) => void
  21. }
  22. const Uploading: FC<Props> = ({
  23. isBundle,
  24. file,
  25. onCancel,
  26. onPackageUploaded,
  27. onBundleUploaded,
  28. onFailed,
  29. }) => {
  30. const { t } = useTranslation()
  31. const fileName = file.name
  32. const handleUpload = async () => {
  33. try {
  34. await uploadFile(file, isBundle)
  35. }
  36. catch (e: any) {
  37. if (e.response?.message) {
  38. onFailed(e.response?.message)
  39. }
  40. else { // Why it would into this branch?
  41. const res = e.response
  42. if (isBundle) {
  43. onBundleUploaded(res)
  44. return
  45. }
  46. onPackageUploaded({
  47. uniqueIdentifier: res.unique_identifier,
  48. manifest: res.manifest,
  49. })
  50. }
  51. }
  52. }
  53. React.useEffect(() => {
  54. handleUpload()
  55. // eslint-disable-next-line react-hooks/exhaustive-deps
  56. }, [])
  57. return (
  58. <>
  59. <div className='flex flex-col px-6 py-3 justify-center items-start gap-4 self-stretch'>
  60. <div className='flex items-center gap-1 self-stretch'>
  61. <RiLoader2Line className='text-text-accent w-4 h-4 animate-spin-slow' />
  62. <div className='text-text-secondary system-md-regular'>
  63. {t(`${i18nPrefix}.uploadingPackage`, {
  64. packageName: fileName,
  65. })}
  66. </div>
  67. </div>
  68. <div className='flex p-2 items-start content-start gap-1 self-stretch flex-wrap rounded-2xl bg-background-section-burn'>
  69. <Card
  70. className='w-full'
  71. payload={{ name: fileName } as any}
  72. isLoading
  73. loadingFileName={fileName}
  74. installed={false}
  75. />
  76. </div>
  77. </div>
  78. {/* Action Buttons */}
  79. <div className='flex p-6 pt-5 justify-end items-center gap-2 self-stretch'>
  80. <Button variant='secondary' className='min-w-[72px]' onClick={onCancel}>
  81. {t('common.operation.cancel')}
  82. </Button>
  83. <Button
  84. variant='primary'
  85. className='min-w-[72px]'
  86. disabled
  87. >
  88. {t(`${i18nPrefix}.install`)}
  89. </Button>
  90. </div>
  91. </>
  92. )
  93. }
  94. export default React.memo(Uploading)