index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 Modal from '@/app/components/base/modal'
  7. import Button from '@/app/components/base/button'
  8. import Toast from '@/app/components/base/toast'
  9. import AppIcon from '@/app/components/base/app-icon'
  10. import { useProviderContext } from '@/context/provider-context'
  11. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  12. import type { AppIconType } from '@/types/app'
  13. export type CreateAppModalProps = {
  14. show: boolean
  15. isEditModal?: boolean
  16. appName: string
  17. appDescription: string
  18. appIconType: AppIconType | null
  19. appIcon: string
  20. appIconBackground?: string | null
  21. appIconUrl?: string | null
  22. onConfirm: (info: {
  23. name: string
  24. icon_type: AppIconType
  25. icon: string
  26. icon_background?: string
  27. description: string
  28. }) => Promise<void>
  29. onHide: () => void
  30. }
  31. const CreateAppModal = ({
  32. show = false,
  33. isEditModal = false,
  34. appIconType,
  35. appIcon: _appIcon,
  36. appIconBackground,
  37. appIconUrl,
  38. appName,
  39. appDescription,
  40. onConfirm,
  41. onHide,
  42. }: CreateAppModalProps) => {
  43. const { t } = useTranslation()
  44. const [name, setName] = React.useState(appName)
  45. const [appIcon, setAppIcon] = useState(
  46. () => appIconType === 'image'
  47. ? { type: 'image' as const, fileId: _appIcon, url: appIconUrl }
  48. : { type: 'emoji' as const, icon: _appIcon, background: appIconBackground },
  49. )
  50. const [showAppIconPicker, setShowAppIconPicker] = useState(false)
  51. const [description, setDescription] = useState(appDescription || '')
  52. const { plan, enableBilling } = useProviderContext()
  53. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  54. const submit = () => {
  55. if (!name.trim()) {
  56. Toast.notify({ type: 'error', message: t('explore.appCustomize.nameRequired') })
  57. return
  58. }
  59. onConfirm({
  60. name,
  61. icon_type: appIcon.type,
  62. icon: appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId,
  63. icon_background: appIcon.type === 'emoji' ? appIcon.background! : undefined,
  64. description,
  65. })
  66. onHide()
  67. }
  68. return (
  69. <>
  70. <Modal
  71. isShow={show}
  72. onClose={() => {}}
  73. className='relative !max-w-[480px] px-8'
  74. >
  75. <div className='absolute right-4 top-4 p-2 cursor-pointer' onClick={onHide}>
  76. <RiCloseLine className='w-4 h-4 text-gray-500' />
  77. </div>
  78. {isEditModal && (
  79. <div className='mb-9 font-semibold text-xl leading-[30px] text-gray-900'>{t('app.editAppTitle')}</div>
  80. )}
  81. {!isEditModal && (
  82. <div className='mb-9 font-semibold text-xl leading-[30px] text-gray-900'>{t('explore.appCustomize.title', { name: appName })}</div>
  83. )}
  84. <div className='mb-9'>
  85. {/* icon & name */}
  86. <div className='pt-2'>
  87. <div className='py-2 text-sm font-medium leading-[20px] text-gray-900'>{t('app.newApp.captionName')}</div>
  88. <div className='flex items-center justify-between space-x-2'>
  89. <AppIcon
  90. size='large'
  91. onClick={() => { setShowAppIconPicker(true) }}
  92. className='cursor-pointer'
  93. iconType={appIcon.type}
  94. icon={appIcon.type === 'image' ? appIcon.fileId : appIcon.icon}
  95. background={appIcon.type === 'image' ? undefined : appIcon.background}
  96. imageUrl={appIcon.type === 'image' ? appIcon.url : undefined}
  97. />
  98. <input
  99. value={name}
  100. onChange={e => setName(e.target.value)}
  101. placeholder={t('app.newApp.appNamePlaceholder') || ''}
  102. className='grow h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg border border-transparent outline-none appearance-none caret-primary-600 placeholder:text-gray-400 hover:bg-gray-50 hover:border hover:border-gray-300 focus:bg-gray-50 focus:border focus:border-gray-300 focus:shadow-xs'
  103. />
  104. </div>
  105. </div>
  106. {/* description */}
  107. <div className='pt-2'>
  108. <div className='py-2 text-sm font-medium leading-[20px] text-gray-900'>{t('app.newApp.captionDescription')}</div>
  109. <textarea
  110. className='w-full h-10 px-3 py-2 text-sm font-normal bg-gray-100 rounded-lg border border-transparent outline-none appearance-none caret-primary-600 placeholder:text-gray-400 hover:bg-gray-50 hover:border hover:border-gray-300 focus:bg-gray-50 focus:border focus:border-gray-300 focus:shadow-xs h-[80px] resize-none'
  111. placeholder={t('app.newApp.appDescriptionPlaceholder') || ''}
  112. value={description}
  113. onChange={e => setDescription(e.target.value)}
  114. />
  115. </div>
  116. {!isEditModal && isAppsFull && <AppsFull loc='app-explore-create' />}
  117. </div>
  118. <div className='flex flex-row-reverse'>
  119. <Button disabled={!isEditModal && isAppsFull} className='w-24 ml-2' variant='primary' onClick={submit}>{!isEditModal ? t('common.operation.create') : t('common.operation.save')}</Button>
  120. <Button className='w-24' onClick={onHide}>{t('common.operation.cancel')}</Button>
  121. </div>
  122. </Modal>
  123. {showAppIconPicker && <AppIconPicker
  124. onSelect={(payload) => {
  125. setAppIcon(payload)
  126. setShowAppIconPicker(false)
  127. }}
  128. onClose={() => {
  129. setAppIcon(appIconType === 'image'
  130. ? { type: 'image' as const, url: appIconUrl, fileId: _appIcon }
  131. : { type: 'emoji' as const, icon: _appIcon, background: appIconBackground })
  132. setShowAppIconPicker(false)
  133. }}
  134. />}
  135. </>
  136. )
  137. }
  138. export default CreateAppModal