index.tsx 6.2 KB

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