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. }, [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. console.log(icon, icon_background)
  90. setEmoji({ icon, icon_background })
  91. setShowEmojiPicker(false)
  92. }}
  93. onClose={() => {
  94. setEmoji({ icon: '🍌', icon_background: '#FFEAD5' })
  95. setShowEmojiPicker(false)
  96. }}
  97. />}
  98. <Dialog
  99. show={show}
  100. title={t('app.newApp.startToCreate')}
  101. footer={
  102. <>
  103. <Button onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  104. <Button type="primary" onClick={onCreate}>{t('app.newApp.Create')}</Button>
  105. </>
  106. }
  107. >
  108. <h3 className={style.newItemCaption}>{t('app.newApp.captionName')}</h3>
  109. <div className='flex items-center justify-between gap-3 mb-8'>
  110. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.icon} background={emoji.icon_background} />
  111. <input ref={nameInputRef} className='h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg grow' />
  112. </div>
  113. <div className='h-[247px]'>
  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. <ul className='grid grid-cols-2 gap-4'>
  131. {templates?.data?.map((template, index) => (
  132. <li
  133. key={index}
  134. className={classNames(style.listItem, style.selectable, selectedTemplateIndex === index && style.selected)}
  135. onClick={() => setSelectedTemplateIndex(index)}
  136. >
  137. <div className={style.listItemTitle}>
  138. <AppIcon size='small' />
  139. <div className={style.listItemHeading}>
  140. <div className={style.listItemHeadingContent}>{template.name}</div>
  141. </div>
  142. </div>
  143. <div className={style.listItemDescription}>{template.model_config?.pre_prompt}</div>
  144. <AppModeLabel mode={template.mode} className='mt-2' />
  145. {/* <AppModeLabel mode='chat' className='mt-2' /> */}
  146. </li>
  147. ))}
  148. </ul>
  149. )
  150. : (
  151. <>
  152. <ul className='grid grid-cols-2 gap-4'>
  153. <li
  154. className={classNames(style.listItem, style.selectable, newAppMode === 'chat' && style.selected)}
  155. onClick={() => setNewAppMode('chat')}
  156. >
  157. <div className={style.listItemTitle}>
  158. <span className={style.newItemIcon}>
  159. <span className={classNames(style.newItemIconImage, style.newItemIconChat)} />
  160. </span>
  161. <div className={style.listItemHeading}>
  162. <div className={style.listItemHeadingContent}>{t('app.newApp.chatApp')}</div>
  163. </div>
  164. </div>
  165. <div className={style.listItemDescription}>{t('app.newApp.chatAppIntro')}</div>
  166. <div className={classNames(style.listItemFooter, 'justify-end')}>
  167. <a className={style.listItemLink} href='https://udify.app/chat/7CQBa5yyvYLSkZtx' target='_blank'>{t('app.newApp.previewDemo')}<span className={classNames(style.linkIcon, style.grayLinkIcon)} /></a>
  168. </div>
  169. </li>
  170. <li
  171. className={classNames(style.listItem, style.selectable, newAppMode === 'completion' && style.selected)}
  172. onClick={() => setNewAppMode('completion')}
  173. >
  174. <div className={style.listItemTitle}>
  175. <span className={style.newItemIcon}>
  176. <span className={classNames(style.newItemIconImage, style.newItemIconComplete)} />
  177. </span>
  178. <div className={style.listItemHeading}>
  179. <div className={style.listItemHeadingContent}>{t('app.newApp.completeApp')}</div>
  180. </div>
  181. </div>
  182. <div className={style.listItemDescription}>{t('app.newApp.completeAppIntro')}</div>
  183. <div className={classNames(style.listItemFooter, 'justify-end')}>
  184. <a className={style.listItemLink} href='https://udify.app/completion/aeFTj0VCb3Ok3TUE' target='_blank'>{t('app.newApp.previewDemo')}<span className={classNames(style.linkIcon, style.grayLinkIcon)} /></a>
  185. </div>
  186. </li>
  187. </ul>
  188. <div className='flex items-center h-[34px] mt-2'>
  189. <span
  190. className='inline-flex items-center gap-1 text-xs font-medium cursor-pointer text-primary-600'
  191. onClick={() => setIsWithTemplate(true)}
  192. >
  193. {t('app.newApp.showTemplates')}<span className={style.rightIcon} />
  194. </span>
  195. </div>
  196. </>
  197. )}
  198. </div>
  199. </Dialog>
  200. </>
  201. }
  202. export default NewAppDialog