index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiCloseLine } from '@remixicon/react'
  5. import AppIconPicker from '../../base/app-icon-picker'
  6. import cn from '@/utils/classnames'
  7. import Modal from '@/app/components/base/modal'
  8. import Button from '@/app/components/base/button'
  9. import Input from '@/app/components/base/input'
  10. import Toast from '@/app/components/base/toast'
  11. import AppIcon from '@/app/components/base/app-icon'
  12. import { useProviderContext } from '@/context/provider-context'
  13. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  14. import type { AppIconType } from '@/types/app'
  15. export type DuplicateAppModalProps = {
  16. appName: string
  17. icon_type: AppIconType | null
  18. icon: string
  19. icon_background?: string | null
  20. icon_url?: string | null
  21. show: boolean
  22. onConfirm: (info: {
  23. name: string
  24. icon_type: AppIconType
  25. icon: string
  26. icon_background?: string | null
  27. }) => Promise<void>
  28. onHide: () => void
  29. }
  30. const DuplicateAppModal = ({
  31. appName,
  32. icon_type,
  33. icon,
  34. icon_background,
  35. icon_url,
  36. show = false,
  37. onConfirm,
  38. onHide,
  39. }: DuplicateAppModalProps) => {
  40. const { t } = useTranslation()
  41. const [name, setName] = React.useState(appName)
  42. const [showAppIconPicker, setShowAppIconPicker] = useState(false)
  43. const [appIcon, setAppIcon] = useState(
  44. icon_type === 'image'
  45. ? { type: 'image' as const, url: icon_url, fileId: icon }
  46. : { type: 'emoji' as const, icon, background: icon_background },
  47. )
  48. const { plan, enableBilling } = useProviderContext()
  49. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  50. const submit = () => {
  51. if (!name.trim()) {
  52. Toast.notify({ type: 'error', message: t('explore.appCustomize.nameRequired') })
  53. return
  54. }
  55. onConfirm({
  56. name,
  57. icon_type: appIcon.type,
  58. icon: appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId,
  59. icon_background: appIcon.type === 'emoji' ? appIcon.background : undefined,
  60. })
  61. onHide()
  62. }
  63. return (
  64. <>
  65. <Modal
  66. isShow={show}
  67. onClose={() => { }}
  68. className={cn('relative !max-w-[480px]', 'px-8')}
  69. >
  70. <div className='absolute right-4 top-4 cursor-pointer p-2' onClick={onHide}>
  71. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  72. </div>
  73. <div className='relative mb-9 mt-3 text-xl font-semibold leading-[30px] text-text-primary'>{t('app.duplicateTitle')}</div>
  74. <div className='system-sm-regular mb-9 text-text-secondary'>
  75. <div className='system-md-medium mb-2'>{t('explore.appCustomize.subTitle')}</div>
  76. <div className='flex items-center justify-between space-x-2'>
  77. <AppIcon
  78. size='large'
  79. onClick={() => { setShowAppIconPicker(true) }}
  80. className='cursor-pointer'
  81. iconType={appIcon.type}
  82. icon={appIcon.type === 'image' ? appIcon.fileId : appIcon.icon}
  83. background={appIcon.type === 'image' ? undefined : appIcon.background}
  84. imageUrl={appIcon.type === 'image' ? appIcon.url : undefined}
  85. />
  86. <Input
  87. value={name}
  88. onChange={e => setName(e.target.value)}
  89. className='h-10'
  90. />
  91. </div>
  92. {isAppsFull && <AppsFull loc='app-duplicate-create' />}
  93. </div>
  94. <div className='flex flex-row-reverse'>
  95. <Button disabled={isAppsFull} className='ml-2 w-24' variant='primary' onClick={submit}>{t('app.duplicate')}</Button>
  96. <Button className='w-24' onClick={onHide}>{t('common.operation.cancel')}</Button>
  97. </div>
  98. </Modal>
  99. {showAppIconPicker && <AppIconPicker
  100. onSelect={(payload) => {
  101. setAppIcon(payload)
  102. setShowAppIconPicker(false)
  103. }}
  104. onClose={() => {
  105. setAppIcon(icon_type === 'image'
  106. ? { type: 'image', url: icon_url!, fileId: icon }
  107. : { type: 'emoji', icon, background: icon_background! })
  108. setShowAppIconPicker(false)
  109. }}
  110. />}
  111. </>
  112. )
  113. }
  114. export default DuplicateAppModal