index.tsx 16 KB

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