NewAppDialog.tsx 8.7 KB

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