ready-to-install.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useCallback } from 'react'
  4. import type { PluginDeclaration } from '../../types'
  5. import { InstallStep } from '../../types'
  6. import Install from './steps/install'
  7. import Installed from '../base/installed'
  8. import useRefreshPluginList from '../hooks/use-refresh-plugin-list'
  9. type Props = {
  10. step: InstallStep
  11. onStepChange: (step: InstallStep) => void,
  12. onStartToInstall: () => void
  13. setIsInstalling: (isInstalling: boolean) => void
  14. onClose: () => void
  15. uniqueIdentifier: string | null,
  16. manifest: PluginDeclaration | null,
  17. errorMsg: string | null,
  18. onError: (errorMsg: string) => void,
  19. }
  20. const ReadyToInstall: FC<Props> = ({
  21. step,
  22. onStepChange,
  23. onStartToInstall,
  24. setIsInstalling,
  25. onClose,
  26. uniqueIdentifier,
  27. manifest,
  28. errorMsg,
  29. onError,
  30. }) => {
  31. const { refreshPluginList } = useRefreshPluginList()
  32. const handleInstalled = useCallback((notRefresh?: boolean) => {
  33. onStepChange(InstallStep.installed)
  34. if (!notRefresh)
  35. refreshPluginList(manifest)
  36. setIsInstalling(false)
  37. }, [manifest, onStepChange, refreshPluginList, setIsInstalling])
  38. const handleFailed = useCallback((errorMsg?: string) => {
  39. onStepChange(InstallStep.installFailed)
  40. setIsInstalling(false)
  41. if (errorMsg)
  42. onError(errorMsg)
  43. }, [onError, onStepChange, setIsInstalling])
  44. return (
  45. <>
  46. {
  47. step === InstallStep.readyToInstall && (
  48. <Install
  49. uniqueIdentifier={uniqueIdentifier!}
  50. payload={manifest!}
  51. onCancel={onClose}
  52. onInstalled={handleInstalled}
  53. onFailed={handleFailed}
  54. onStartToInstall={onStartToInstall}
  55. />
  56. )
  57. }
  58. {
  59. ([InstallStep.uploadFailed, InstallStep.installed, InstallStep.installFailed].includes(step)) && (
  60. <Installed
  61. payload={manifest}
  62. isFailed={[InstallStep.uploadFailed, InstallStep.installFailed].includes(step)}
  63. errMsg={errorMsg}
  64. onCancel={onClose}
  65. />
  66. )
  67. }
  68. </>
  69. )
  70. }
  71. export default React.memo(ReadyToInstall)