NewAppDialog.tsx 8.6 KB

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