NewAppDialog.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 className='flex items-center h-[18px] border border-indigo-300 px-1 rounded-[5px] text-xs font-medium text-indigo-600 uppercase truncate'>{t('app.newApp.agentAssistant')}</div>
  144. </div>
  145. <div className={`${style.listItemDescription} ${style.noClip}`}>{t('app.newApp.chatAppIntro')}</div>
  146. {/* <div className={classNames(style.listItemFooter, 'justify-end')}>
  147. <a className={style.listItemLink} href='https://udify.app/chat/7CQBa5yyvYLSkZtx' target='_blank' rel='noopener noreferrer'>{t('app.newApp.previewDemo')}<span className={classNames(style.linkIcon, style.grayLinkIcon)} /></a>
  148. </div> */}
  149. </li>
  150. <li
  151. className={classNames(style.listItem, style.selectable, newAppMode === 'completion' && style.selected)}
  152. onClick={() => setNewAppMode('completion')}
  153. >
  154. <div className={style.listItemTitle}>
  155. <span className={style.newItemIcon}>
  156. {/* <span className={classNames(style.newItemIconImage, style.newItemIconComplete)} /> */}
  157. <AiText className={classNames('w-5 h-5', newAppMode === 'completion' ? 'text-[#155EEF]' : 'text-gray-700')} />
  158. </span>
  159. <div className={style.listItemHeading}>
  160. <div className={style.listItemHeadingContent}>{t('app.newApp.completeApp')}</div>
  161. </div>
  162. </div>
  163. <div className={`${style.listItemDescription} ${style.noClip}`}>{t('app.newApp.completeAppIntro')}</div>
  164. </li>
  165. </ul>
  166. </>
  167. )
  168. )}
  169. {isWithTemplate && (
  170. <ul className='grid grid-cols-1 md:grid-cols-2 gap-4'>
  171. {templates?.data?.map((template, index) => (
  172. <li
  173. key={index}
  174. className={classNames(style.listItem, style.selectable, selectedTemplateIndex === index && style.selected)}
  175. onClick={() => setSelectedTemplateIndex(index)}
  176. >
  177. <div className={style.listItemTitle}>
  178. <AppIcon size='small' />
  179. <div className={style.listItemHeading}>
  180. <div className={style.listItemHeadingContent}>{template.name}</div>
  181. </div>
  182. </div>
  183. <div className={style.listItemDescription}>{template.model_config?.pre_prompt}</div>
  184. <div className='inline-block pl-3.5'>
  185. <AppModeLabel mode={template.mode} isAgent={template.model_config.agent_mode.enabled} className='mt-2' />
  186. </div>
  187. </li>
  188. ))}
  189. </ul>
  190. )}
  191. <div className='mt-8'>
  192. <h3 className={style.newItemCaption}>{t('app.newApp.captionName')}</h3>
  193. <div className='flex items-center justify-between gap-3'>
  194. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.icon} background={emoji.icon_background} />
  195. <input ref={nameInputRef} className='h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' placeholder={t('app.appNamePlaceholder') || ''} />
  196. </div>
  197. </div>
  198. {
  199. !isWithTemplate && (
  200. <div className='flex items-center h-[34px] mt-2'>
  201. <span
  202. className='inline-flex items-center gap-1 text-xs font-medium cursor-pointer text-primary-600'
  203. onClick={() => setIsWithTemplate(true)}
  204. >
  205. {t('app.newApp.showTemplates')}<span className={style.rightIcon} />
  206. </span>
  207. </div>
  208. )
  209. }
  210. </div>
  211. {isAppsFull && <AppsFull loc='app-create' />}
  212. </Dialog>
  213. </>
  214. }
  215. export default NewAppDialog