index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. 'use client'
  2. import type { MouseEventHandler } from 'react'
  3. import { useCallback, useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. import { useRouter } from 'next/navigation'
  7. import { useContext, useContextSelector } from 'use-context-selector'
  8. import s from './style.module.css'
  9. import AppsContext, { useAppContext } from '@/context/app-context'
  10. import { useProviderContext } from '@/context/provider-context'
  11. import { ToastContext } from '@/app/components/base/toast'
  12. import type { AppMode } from '@/types/app'
  13. import { createApp } from '@/service/apps'
  14. import Modal from '@/app/components/base/modal'
  15. import Button from '@/app/components/base/button'
  16. import AppIcon from '@/app/components/base/app-icon'
  17. import EmojiPicker from '@/app/components/base/emoji-picker'
  18. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  19. import { AiText, ChatBot, CuteRobote } from '@/app/components/base/icons/src/vender/solid/communication'
  20. import { HelpCircle, XClose } from '@/app/components/base/icons/src/vender/line/general'
  21. import { Route } from '@/app/components/base/icons/src/vender/solid/mapsAndTravel'
  22. import TooltipPlus from '@/app/components/base/tooltip-plus'
  23. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  24. import { getRedirection } from '@/utils/app-redirection'
  25. type CreateAppDialogProps = {
  26. show: boolean
  27. onSuccess: () => void
  28. onClose: () => void
  29. }
  30. const CreateAppModal = ({ show, onSuccess, onClose }: CreateAppDialogProps) => {
  31. const { t } = useTranslation()
  32. const { push } = useRouter()
  33. const { notify } = useContext(ToastContext)
  34. const mutateApps = useContextSelector(AppsContext, state => state.mutateApps)
  35. const [appMode, setAppMode] = useState<AppMode>('chat')
  36. const [showChatBotType, setShowChatBotType] = useState<boolean>(true)
  37. const [emoji, setEmoji] = useState({ icon: '🤖', icon_background: '#FFEAD5' })
  38. const [showEmojiPicker, setShowEmojiPicker] = useState(false)
  39. const [name, setName] = useState('')
  40. const [description, setDescription] = useState('')
  41. const { plan, enableBilling } = useProviderContext()
  42. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  43. const { isCurrentWorkspaceEditor } = useAppContext()
  44. const isCreatingRef = useRef(false)
  45. const onCreate: MouseEventHandler = useCallback(async () => {
  46. if (!appMode) {
  47. notify({ type: 'error', message: t('app.newApp.appTypeRequired') })
  48. return
  49. }
  50. if (!name.trim()) {
  51. notify({ type: 'error', message: t('app.newApp.nameNotEmpty') })
  52. return
  53. }
  54. if (isCreatingRef.current)
  55. return
  56. isCreatingRef.current = true
  57. try {
  58. const app = await createApp({
  59. name,
  60. description,
  61. icon: emoji.icon,
  62. icon_background: emoji.icon_background,
  63. mode: appMode,
  64. })
  65. notify({ type: 'success', message: t('app.newApp.appCreated') })
  66. onSuccess()
  67. onClose()
  68. mutateApps()
  69. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  70. getRedirection(isCurrentWorkspaceEditor, app, push)
  71. }
  72. catch (e) {
  73. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  74. }
  75. isCreatingRef.current = false
  76. }, [name, notify, t, appMode, emoji.icon, emoji.icon_background, description, onSuccess, onClose, mutateApps, push, isCurrentWorkspaceEditor])
  77. return (
  78. <Modal
  79. overflowVisible
  80. wrapperClassName='z-20'
  81. className='!p-0 !max-w-[720px] !w-[720px] rounded-xl'
  82. isShow={show}
  83. onClose={() => {}}
  84. >
  85. {/* Heading */}
  86. <div className='shrink-0 flex flex-col h-full bg-white rounded-t-xl'>
  87. <div className='shrink-0 pl-8 pr-6 pt-6 pb-3 bg-white text-xl rounded-t-xl leading-[30px] font-semibold text-gray-900 z-10'>{t('app.newApp.startFromBlank')}</div>
  88. </div>
  89. {/* app type */}
  90. <div className='py-2 px-8'>
  91. <div className='py-2 text-sm leading-[20px] font-medium text-gray-900'>{t('app.newApp.captionAppType')}</div>
  92. <div className='flex'>
  93. <TooltipPlus
  94. hideArrow
  95. popupContent={
  96. <div className='max-w-[280px] leading-[18px] text-xs text-gray-700'>{t('app.newApp.chatbotDescription')}</div>
  97. }
  98. >
  99. <div
  100. className={cn(
  101. 'relative grow box-border w-[158px] mr-2 px-0.5 pt-3 pb-2 flex flex-col items-center justify-center gap-1 rounded-lg border border-gray-100 bg-white text-gray-700 cursor-pointer shadow-xs hover:border-gray-300',
  102. showChatBotType && 'border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  103. s['grid-bg-chat'],
  104. )}
  105. onClick={() => {
  106. setAppMode('chat')
  107. setShowChatBotType(true)
  108. }}
  109. >
  110. <ChatBot className='w-6 h-6 text-[#1570EF]' />
  111. <div className='h-5 text-[13px] font-medium leading-[18px]'>{t('app.types.chatbot')}</div>
  112. </div>
  113. </TooltipPlus>
  114. <TooltipPlus
  115. hideArrow
  116. popupContent={
  117. <div className='flex flex-col max-w-[320px] leading-[18px] text-xs'>
  118. <div className='text-gray-700'>{t('app.newApp.completionDescription')}</div>
  119. </div>
  120. }
  121. >
  122. <div
  123. className={cn(
  124. 'relative grow box-border w-[158px] mr-2 px-0.5 pt-3 pb-2 flex flex-col items-center justify-center gap-1 rounded-lg border border-gray-100 text-gray-700 cursor-pointer bg-white shadow-xs hover:border-gray-300',
  125. s['grid-bg-completion'],
  126. appMode === 'completion' && 'border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  127. )}
  128. onClick={() => {
  129. setAppMode('completion')
  130. setShowChatBotType(false)
  131. }}
  132. >
  133. <AiText className='w-6 h-6 text-[#0E9384]' />
  134. <div className='h-5 text-[13px] font-medium leading-[18px]'>{t('app.newApp.completeApp')}</div>
  135. </div>
  136. </TooltipPlus>
  137. <TooltipPlus
  138. hideArrow
  139. popupContent={
  140. <div className='max-w-[280px] leading-[18px] text-xs text-gray-700'>{t('app.newApp.agentDescription')}</div>
  141. }
  142. >
  143. <div
  144. className={cn(
  145. 'relative grow box-border w-[158px] mr-2 px-0.5 pt-3 pb-2 flex flex-col items-center justify-center gap-1 rounded-lg border border-gray-100 text-gray-700 cursor-pointer bg-white shadow-xs hover:border-gray-300',
  146. s['grid-bg-agent-chat'],
  147. appMode === 'agent-chat' && 'border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  148. )}
  149. onClick={() => {
  150. setAppMode('agent-chat')
  151. setShowChatBotType(false)
  152. }}
  153. >
  154. <CuteRobote className='w-6 h-6 text-indigo-600' />
  155. <div className='h-5 text-[13px] font-medium leading-[18px]'>{t('app.types.agent')}</div>
  156. </div>
  157. </TooltipPlus>
  158. <TooltipPlus
  159. hideArrow
  160. popupContent={
  161. <div className='flex flex-col max-w-[320px] leading-[18px] text-xs'>
  162. <div className='text-gray-700'>{t('app.newApp.workflowDescription')}</div>
  163. </div>
  164. }
  165. >
  166. <div
  167. className={cn(
  168. 'relative grow box-border w-[158px] px-0.5 pt-3 pb-2 flex flex-col items-center justify-center gap-1 rounded-lg border border-gray-100 text-gray-700 cursor-pointer bg-white shadow-xs hover:border-gray-300',
  169. s['grid-bg-workflow'],
  170. appMode === 'workflow' && 'border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  171. )}
  172. onClick={() => {
  173. setAppMode('workflow')
  174. setShowChatBotType(false)
  175. }}
  176. >
  177. <Route className='w-6 h-6 text-[#f79009]' />
  178. <div className='h-5 text-[13px] font-medium leading-[18px]'>{t('app.types.workflow')}</div>
  179. <span className='absolute top-[-3px] right-[-3px] px-1 rounded-[5px] bg-white border border-black/8 text-gray-500 text-[10px] leading-[18px] font-medium'>BETA</span>
  180. </div>
  181. </TooltipPlus>
  182. </div>
  183. </div>
  184. {showChatBotType && (
  185. <div className='py-2 px-8'>
  186. <div className='py-2 text-sm leading-[20px] font-medium text-gray-900'>{t('app.newApp.chatbotType')}</div>
  187. <div className='flex gap-2'>
  188. <div
  189. className={cn(
  190. 'relative grow flex-[50%] pl-4 py-[10px] pr-[10px] rounded-lg border border-gray-100 bg-gray-25 text-gray-700 cursor-pointer hover:bg-white hover:shadow-xs hover:border-gray-300',
  191. appMode === 'chat' && 'bg-white shadow-xs border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  192. )}
  193. onClick={() => {
  194. setAppMode('chat')
  195. }}
  196. >
  197. <div className='flex items-center justify-between'>
  198. <div className='h-5 text-sm font-medium leading-5'>{t('app.newApp.basic')}</div>
  199. <div className='group'>
  200. <HelpCircle className='w-[14px] h-[14px] text-gray-400 hover:text-gray-500' />
  201. <div
  202. className={cn(
  203. 'hidden z-20 absolute left-[327px] top-[-158px] w-[376px] rounded-xl bg-white border-[0.5px] border-[rgba(0,0,0,0.05)] shadow-lg group-hover:block',
  204. )}
  205. >
  206. <div className={cn('w-full h-[256px] bg-center bg-no-repeat bg-contain rounded-xl', s.basicPic)}/>
  207. <div className='px-4 pb-2'>
  208. <div className='flex items-center justify-between'>
  209. <div className='text-gray-700 text-md leading-6 font-semibold'>{t('app.newApp.basic')}</div>
  210. <div className='text-orange-500 text-xs leading-[18px] font-medium'>{t('app.newApp.basicFor')}</div>
  211. </div>
  212. <div className='mt-1 text-gray-500 text-sm leading-5'>{t('app.newApp.basicDescription')}</div>
  213. </div>
  214. </div>
  215. </div>
  216. </div>
  217. <div className='mt-[2px] text-gray-500 text-xs leading-[18px]'>{t('app.newApp.basicTip')}</div>
  218. </div>
  219. <div
  220. className={cn(
  221. 'relative grow flex-[50%] pl-3 py-2 pr-2 rounded-lg border border-gray-100 bg-gray-25 text-gray-700 cursor-pointer hover:bg-white hover:shadow-xs hover:border-gray-300',
  222. appMode === 'advanced-chat' && 'bg-white shadow-xs border-[1.5px] border-primary-400 hover:border-[1.5px] hover:border-primary-400',
  223. )}
  224. onClick={() => {
  225. setAppMode('advanced-chat')
  226. }}
  227. >
  228. <div className='flex items-center justify-between'>
  229. <div className='flex items-center'>
  230. <div className='mr-1 h-5 text-sm font-medium leading-5'>{t('app.newApp.advanced')}</div>
  231. <span className='px-1 rounded-[5px] bg-white border border-black/8 text-gray-500 text-[10px] leading-[18px] font-medium'>BETA</span>
  232. </div>
  233. <div className='group'>
  234. <HelpCircle className='w-[14px] h-[14px] text-gray-400 hover:text-gray-500' />
  235. <div
  236. className={cn(
  237. 'hidden z-20 absolute right-[26px] top-[-158px] w-[376px] rounded-xl bg-white border-[0.5px] border-[rgba(0,0,0,0.05)] shadow-lg group-hover:block',
  238. )}
  239. >
  240. <div className={cn('w-full h-[256px] bg-center bg-no-repeat bg-contain rounded-xl', s.advancedPic)}/>
  241. <div className='px-4 pb-2'>
  242. <div className='flex items-center justify-between'>
  243. <div className='flex items-center'>
  244. <div className='mr-1 text-gray-700 text-md leading-6 font-semibold'>{t('app.newApp.advanced')}</div>
  245. <span className='px-1 rounded-[5px] bg-white border border-black/8 text-gray-500 text-[10px] leading-[18px] font-medium'>BETA</span>
  246. </div>
  247. <div className='text-orange-500 text-xs leading-[18px] font-medium'>{t('app.newApp.advancedFor').toLocaleUpperCase()}</div>
  248. </div>
  249. <div className='mt-1 text-gray-500 text-sm leading-5'>{t('app.newApp.advancedDescription')}</div>
  250. </div>
  251. </div>
  252. </div>
  253. </div>
  254. <div className='mt-[2px] text-gray-500 text-xs leading-[18px]'>{t('app.newApp.advancedFor')}</div>
  255. </div>
  256. </div>
  257. </div>
  258. )}
  259. {/* icon & name */}
  260. <div className='pt-2 px-8'>
  261. <div className='py-2 text-sm font-medium leading-[20px] text-gray-900'>{t('app.newApp.captionName')}</div>
  262. <div className='flex items-center justify-between space-x-2'>
  263. <AppIcon size='large' onClick={() => { setShowEmojiPicker(true) }} className='cursor-pointer' icon={emoji.icon} background={emoji.icon_background} />
  264. <input
  265. value={name}
  266. onChange={e => setName(e.target.value)}
  267. placeholder={t('app.newApp.appNamePlaceholder') || ''}
  268. className='grow h-10 px-3 text-sm font-normal bg-gray-100 rounded-lg border border-transparent outline-none appearance-none caret-primary-600 placeholder:text-gray-400 hover:bg-gray-50 hover:border hover:border-gray-300 focus:bg-gray-50 focus:border focus:border-gray-300 focus:shadow-xs'
  269. />
  270. </div>
  271. {showEmojiPicker && <EmojiPicker
  272. onSelect={(icon, icon_background) => {
  273. setEmoji({ icon, icon_background })
  274. setShowEmojiPicker(false)
  275. }}
  276. onClose={() => {
  277. setEmoji({ icon: '🤖', icon_background: '#FFEAD5' })
  278. setShowEmojiPicker(false)
  279. }}
  280. />}
  281. </div>
  282. {/* description */}
  283. <div className='pt-2 px-8'>
  284. <div className='py-2 text-sm font-medium leading-[20px] text-gray-900'>{t('app.newApp.captionDescription')}</div>
  285. <textarea
  286. className='w-full h-10 px-3 py-2 text-sm font-normal bg-gray-100 rounded-lg border border-transparent outline-none appearance-none caret-primary-600 placeholder:text-gray-400 hover:bg-gray-50 hover:border hover:border-gray-300 focus:bg-gray-50 focus:border focus:border-gray-300 focus:shadow-xs h-[80px] resize-none'
  287. placeholder={t('app.newApp.appDescriptionPlaceholder') || ''}
  288. value={description}
  289. onChange={e => setDescription(e.target.value)}
  290. />
  291. </div>
  292. {isAppsFull && (
  293. <div className='px-8 py-2'>
  294. <AppsFull loc='app-create' />
  295. </div>
  296. )}
  297. <div className='px-8 py-6 flex justify-end'>
  298. <Button className='mr-2 text-gray-700 text-sm font-medium' onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  299. <Button className='text-sm font-medium' disabled={isAppsFull || !name} type="primary" onClick={onCreate}>{t('app.newApp.Create')}</Button>
  300. </div>
  301. <div className='absolute right-6 top-6 p-2 cursor-pointer z-20' onClick={onClose}>
  302. <XClose className='w-4 h-4 text-gray-500' />
  303. </div>
  304. </Modal>
  305. )
  306. }
  307. export default CreateAppModal