NewAppDialog.tsx 9.3 KB

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