NewAppDialog.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import { useCallback, useEffect, useRef, useState } from 'react'
  4. import useSWR from 'swr'
  5. import classNames from 'classnames'
  6. import { useRouter } from 'next/navigation'
  7. import { useContext, useContextSelector } from 'use-context-selector'
  8. import { useTranslation } from 'react-i18next'
  9. import style from '../list.module.css'
  10. import AppModeLabel from './AppModeLabel'
  11. import Button from '@/app/components/base/button'
  12. import Dialog from '@/app/components/base/dialog'
  13. import type { AppMode } from '@/types/app'
  14. import { ToastContext } from '@/app/components/base/toast'
  15. import { createApp, fetchAppTemplates } from '@/service/apps'
  16. import AppIcon from '@/app/components/base/app-icon'
  17. import AppsContext from '@/context/app-context'
  18. import EmojiPicker from '@/app/components/base/emoji-picker'
  19. import { useProviderContext } from '@/context/provider-context'
  20. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  21. type NewAppDialogProps = {
  22. show: boolean
  23. onSuccess?: () => void
  24. onClose?: () => void
  25. }
  26. const NewAppDialog = ({ show, onSuccess, onClose }: NewAppDialogProps) => {
  27. const router = useRouter()
  28. const { notify } = useContext(ToastContext)
  29. const { t } = useTranslation()
  30. const nameInputRef = useRef<HTMLInputElement>(null)
  31. const [newAppMode, setNewAppMode] = useState<AppMode>()
  32. const [isWithTemplate, setIsWithTemplate] = useState(false)
  33. const [selectedTemplateIndex, setSelectedTemplateIndex] = useState<number>(-1)
  34. // Emoji Picker
  35. const [showEmojiPicker, setShowEmojiPicker] = useState(false)
  36. const [emoji, setEmoji] = useState({ icon: '🤖', icon_background: '#FFEAD5' })
  37. const mutateApps = useContextSelector(AppsContext, state => state.mutateApps)
  38. const { data: templates, mutate } = useSWR({ url: '/app-templates' }, fetchAppTemplates)
  39. const mutateTemplates = useCallback(
  40. () => mutate(),
  41. [],
  42. )
  43. useEffect(() => {
  44. if (show) {
  45. mutateTemplates()
  46. setIsWithTemplate(false)
  47. }
  48. }, [mutateTemplates, show])
  49. const { plan, enableBilling } = useProviderContext()
  50. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  51. const isCreatingRef = useRef(false)
  52. const onCreate: MouseEventHandler = useCallback(async () => {
  53. const name = nameInputRef.current?.value
  54. if (!name) {
  55. notify({ type: 'error', message: t('app.newApp.nameNotEmpty') })
  56. return
  57. }
  58. if (!templates || (isWithTemplate && !(selectedTemplateIndex > -1))) {
  59. notify({ type: 'error', message: t('app.newApp.appTemplateNotSelected') })
  60. return
  61. }
  62. if (!isWithTemplate && !newAppMode) {
  63. notify({ type: 'error', message: t('app.newApp.appTypeRequired') })
  64. return
  65. }
  66. if (isCreatingRef.current)
  67. return
  68. isCreatingRef.current = true
  69. try {
  70. const app = await createApp({
  71. name,
  72. icon: emoji.icon,
  73. icon_background: emoji.icon_background,
  74. mode: isWithTemplate ? templates.data[selectedTemplateIndex].mode : newAppMode!,
  75. config: isWithTemplate ? templates.data[selectedTemplateIndex].model_config : undefined,
  76. })
  77. if (onSuccess)
  78. onSuccess()
  79. if (onClose)
  80. onClose()
  81. notify({ type: 'success', message: t('app.newApp.appCreated') })
  82. mutateApps()
  83. router.push(`/app/${app.id}/overview`)
  84. }
  85. catch (e) {
  86. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  87. }
  88. isCreatingRef.current = false
  89. }, [isWithTemplate, newAppMode, notify, router, templates, selectedTemplateIndex, emoji])
  90. return <>
  91. {showEmojiPicker && <EmojiPicker
  92. onSelect={(icon, icon_background) => {
  93. setEmoji({ icon, icon_background })
  94. setShowEmojiPicker(false)
  95. }}
  96. onClose={() => {
  97. setEmoji({ icon: '🤖', icon_background: '#FFEAD5' })
  98. setShowEmojiPicker(false)
  99. }}
  100. />}
  101. <Dialog
  102. show={show}
  103. title={t('app.newApp.startToCreate')}
  104. footer={
  105. <>
  106. <Button onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  107. <Button disabled={isAppsFull} type="primary" onClick={onCreate}>{t('app.newApp.Create')}</Button>
  108. </>
  109. }
  110. >
  111. <h3 className={style.newItemCaption}>{t('app.newApp.captionName')}</h3>
  112. <div className='flex items-center justify-between gap-3 mb-8'>
  113. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.icon} background={emoji.icon_background} />
  114. <input ref={nameInputRef} className='h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('app.appNamePlaceholder') || ''}/>
  115. </div>
  116. <div className='overflow-y-auto'>
  117. <div className={style.newItemCaption}>
  118. <h3 className='inline'>{t('app.newApp.captionAppType')}</h3>
  119. {isWithTemplate && (
  120. <>
  121. <span className='block ml-[9px] mr-[9px] w-[1px] h-[13px] bg-gray-200' />
  122. <span
  123. className='inline-flex items-center gap-1 text-xs font-medium cursor-pointer text-primary-600'
  124. onClick={() => setIsWithTemplate(false)}
  125. >
  126. {t('app.newApp.hideTemplates')}
  127. </span>
  128. </>
  129. )}
  130. </div>
  131. {isWithTemplate
  132. ? (
  133. <ul className='grid grid-cols-1 md:grid-cols-2 gap-4'>
  134. {templates?.data?.map((template, index) => (
  135. <li
  136. key={index}
  137. className={classNames(style.listItem, style.selectable, selectedTemplateIndex === index && style.selected)}
  138. onClick={() => setSelectedTemplateIndex(index)}
  139. >
  140. <div className={style.listItemTitle}>
  141. <AppIcon size='small' />
  142. <div className={style.listItemHeading}>
  143. <div className={style.listItemHeadingContent}>{template.name}</div>
  144. </div>
  145. </div>
  146. <div className={style.listItemDescription}>{template.model_config?.pre_prompt}</div>
  147. <AppModeLabel mode={template.mode} className='mt-2' />
  148. {/* <AppModeLabel mode='chat' className='mt-2' /> */}
  149. </li>
  150. ))}
  151. </ul>
  152. )
  153. : (
  154. <>
  155. <ul className='grid grid-cols-1 md:grid-cols-2 gap-4'>
  156. <li
  157. className={classNames(style.listItem, style.selectable, newAppMode === 'chat' && style.selected)}
  158. onClick={() => setNewAppMode('chat')}
  159. >
  160. <div className={style.listItemTitle}>
  161. <span className={style.newItemIcon}>
  162. <span className={classNames(style.newItemIconImage, style.newItemIconChat)} />
  163. </span>
  164. <div className={style.listItemHeading}>
  165. <div className={style.listItemHeadingContent}>{t('app.newApp.chatApp')}</div>
  166. </div>
  167. </div>
  168. <div className={style.listItemDescription}>{t('app.newApp.chatAppIntro')}</div>
  169. <div className={classNames(style.listItemFooter, 'justify-end')}>
  170. <a className={style.listItemLink} href='https://udify.app/chat/7CQBa5yyvYLSkZtx' target='_blank'>{t('app.newApp.previewDemo')}<span className={classNames(style.linkIcon, style.grayLinkIcon)} /></a>
  171. </div>
  172. </li>
  173. <li
  174. className={classNames(style.listItem, style.selectable, newAppMode === 'completion' && style.selected)}
  175. onClick={() => setNewAppMode('completion')}
  176. >
  177. <div className={style.listItemTitle}>
  178. <span className={style.newItemIcon}>
  179. <span className={classNames(style.newItemIconImage, style.newItemIconComplete)} />
  180. </span>
  181. <div className={style.listItemHeading}>
  182. <div className={style.listItemHeadingContent}>{t('app.newApp.completeApp')}</div>
  183. </div>
  184. </div>
  185. <div className={style.listItemDescription}>{t('app.newApp.completeAppIntro')}</div>
  186. <div className={classNames(style.listItemFooter, 'justify-end')}>
  187. <a className={style.listItemLink} href='https://udify.app/completion/aeFTj0VCb3Ok3TUE' target='_blank'>{t('app.newApp.previewDemo')}<span className={classNames(style.linkIcon, style.grayLinkIcon)} /></a>
  188. </div>
  189. </li>
  190. </ul>
  191. <div className='flex items-center h-[34px] mt-2'>
  192. <span
  193. className='inline-flex items-center gap-1 text-xs font-medium cursor-pointer text-primary-600'
  194. onClick={() => setIsWithTemplate(true)}
  195. >
  196. {t('app.newApp.showTemplates')}<span className={style.rightIcon} />
  197. </span>
  198. </div>
  199. </>
  200. )}
  201. </div>
  202. {isAppsFull && <AppsFull loc='app-create' />}
  203. </Dialog>
  204. </>
  205. }
  206. export default NewAppDialog