NewAppDialog.tsx 7.8 KB

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