index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. 'use client'
  2. import { useCallback, useRef, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { useRouter } from 'next/navigation'
  5. import { useContext } from 'use-context-selector'
  6. // zlwu12 2025-09-18 注释
  7. // import { RiArrowRightLine, RiArrowRightSLine, RiCommandLine, RiCornerDownLeftLine, RiExchange2Fill } from '@remixicon/react'
  8. import { RiArrowRightSLine, RiCommandLine, RiCornerDownLeftLine, RiExchange2Fill } from '@remixicon/react'
  9. // zlwu12 2025-09-18 注释
  10. // import Link from 'next/link'
  11. import { useDebounceFn, useKeyPress } from 'ahooks'
  12. import Image from 'next/image'
  13. import AppIconPicker from '../../base/app-icon-picker'
  14. import type { AppIconSelection } from '../../base/app-icon-picker'
  15. import Button from '@/app/components/base/button'
  16. import Divider from '@/app/components/base/divider'
  17. import cn from '@/utils/classnames'
  18. import { basePath } from '@/utils/var'
  19. import { useAppContext } from '@/context/app-context'
  20. import { useProviderContext } from '@/context/provider-context'
  21. import { ToastContext } from '@/app/components/base/toast'
  22. import type { AppMode } from '@/types/app'
  23. import { createApp } from '@/service/apps'
  24. import Input from '@/app/components/base/input'
  25. import Textarea from '@/app/components/base/textarea'
  26. import AppIcon from '@/app/components/base/app-icon'
  27. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  28. import { BubbleTextMod, ChatBot, ListSparkle, Logic } from '@/app/components/base/icons/src/vender/solid/communication'
  29. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  30. import { getRedirection } from '@/utils/app-redirection'
  31. import FullScreenModal from '@/app/components/base/fullscreen-modal'
  32. import useTheme from '@/hooks/use-theme'
  33. import { useDocLink } from '@/context/i18n'
  34. type CreateAppProps = {
  35. onSuccess: () => void
  36. onClose: () => void
  37. onCreateFromTemplate?: () => void
  38. }
  39. function CreateApp({ onClose, onSuccess, onCreateFromTemplate }: CreateAppProps) {
  40. const { t } = useTranslation()
  41. const { push } = useRouter()
  42. const { notify } = useContext(ToastContext)
  43. const [appMode, setAppMode] = useState<AppMode>('advanced-chat')
  44. const [appIcon, setAppIcon] = useState<AppIconSelection>({ type: 'emoji', icon: '📚', background: '#FFEAD5' })
  45. const [showAppIconPicker, setShowAppIconPicker] = useState(false)
  46. const [name, setName] = useState('')
  47. const [description, setDescription] = useState('')
  48. const [isAppTypeExpanded, setIsAppTypeExpanded] = useState(false)
  49. const { plan, enableBilling } = useProviderContext()
  50. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  51. const { isCurrentWorkspaceEditor } = useAppContext()
  52. const isCreatingRef = useRef(false)
  53. const onCreate = useCallback(async () => {
  54. if (!appMode) {
  55. notify({ type: 'error', message: t('app.newApp.appTypeRequired') })
  56. return
  57. }
  58. if (!name.trim()) {
  59. notify({ type: 'error', message: t('app.newApp.nameNotEmpty') })
  60. return
  61. }
  62. if (isCreatingRef.current)
  63. return
  64. isCreatingRef.current = true
  65. try {
  66. const app = await createApp({
  67. name,
  68. description,
  69. icon_type: appIcon.type,
  70. icon: appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId,
  71. icon_background: appIcon.type === 'emoji' ? appIcon.background : undefined,
  72. mode: appMode,
  73. })
  74. notify({ type: 'success', message: t('app.newApp.appCreated') })
  75. onSuccess()
  76. onClose()
  77. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  78. getRedirection(isCurrentWorkspaceEditor, app, push)
  79. }
  80. catch (e: any) {
  81. notify({
  82. type: 'error',
  83. message: e.message || t('app.newApp.appCreateFailed'),
  84. })
  85. }
  86. isCreatingRef.current = false
  87. }, [name, notify, t, appMode, appIcon, description, onSuccess, onClose, push, isCurrentWorkspaceEditor])
  88. const { run: handleCreateApp } = useDebounceFn(onCreate, { wait: 300 })
  89. useKeyPress(['meta.enter', 'ctrl.enter'], () => {
  90. if (isAppsFull)
  91. return
  92. handleCreateApp()
  93. })
  94. return <>
  95. <div className='flex h-full justify-center overflow-y-auto overflow-x-hidden'>
  96. <div className='flex flex-1 shrink-0 justify-end'>
  97. <div className='px-10'>
  98. <div className='h-6 w-full 2xl:h-[139px]' />
  99. <div className='pb-6 pt-1'>
  100. <span className='title-2xl-semi-bold text-text-primary'>{t('app.newApp.startFromBlank')}</span>
  101. </div>
  102. <div className='mb-2 leading-6'>
  103. <span className='system-sm-semibold text-text-secondary'>{t('app.newApp.chooseAppType')}</span>
  104. </div>
  105. <div className='flex w-[660px] flex-col gap-4'>
  106. <div>
  107. <div className='flex flex-row gap-2'>
  108. <AppTypeCard
  109. active={appMode === 'workflow'}
  110. title={t('app.types.workflow')}
  111. description={t('app.newApp.workflowShortDescription')}
  112. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-indigo-solid'>
  113. <RiExchange2Fill className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  114. </div>}
  115. onClick={() => {
  116. setAppMode('workflow')
  117. }} />
  118. <AppTypeCard
  119. active={appMode === 'advanced-chat'}
  120. title={t('app.types.advanced')}
  121. description={t('app.newApp.advancedShortDescription')}
  122. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-blue-light-solid'>
  123. <BubbleTextMod className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  124. </div>}
  125. onClick={() => {
  126. setAppMode('advanced-chat')
  127. }} />
  128. </div>
  129. </div>
  130. <div>
  131. <div className='mb-2 flex items-center'>
  132. <button
  133. className='flex cursor-pointer items-center border-0 bg-transparent p-0'
  134. onClick={() => setIsAppTypeExpanded(!isAppTypeExpanded)}
  135. >
  136. <span className='system-2xs-medium-uppercase text-text-tertiary'>{t('app.newApp.forBeginners')}</span>
  137. <RiArrowRightSLine className={`ml-1 h-4 w-4 text-text-tertiary transition-transform ${isAppTypeExpanded ? 'rotate-90' : ''}`} />
  138. </button>
  139. </div>
  140. {isAppTypeExpanded && (
  141. <div className='flex flex-row gap-2'>
  142. <AppTypeCard
  143. active={appMode === 'chat'}
  144. title={t('app.types.chatbot')}
  145. description={t('app.newApp.chatbotShortDescription')}
  146. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-blue-solid'>
  147. <ChatBot className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  148. </div>}
  149. onClick={() => {
  150. setAppMode('chat')
  151. }} />
  152. <AppTypeCard
  153. active={appMode === 'agent-chat'}
  154. title={t('app.types.agent')}
  155. description={t('app.newApp.agentShortDescription')}
  156. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-violet-solid'>
  157. <Logic className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  158. </div>}
  159. onClick={() => {
  160. setAppMode('agent-chat')
  161. }} />
  162. <AppTypeCard
  163. active={appMode === 'completion'}
  164. title={t('app.newApp.completeApp')}
  165. description={t('app.newApp.completionShortDescription')}
  166. icon={<div className='flex h-6 w-6 items-center justify-center rounded-md bg-components-icon-bg-teal-solid'>
  167. <ListSparkle className='h-4 w-4 text-components-avatar-shape-fill-stop-100' />
  168. </div>}
  169. onClick={() => {
  170. setAppMode('completion')
  171. }} />
  172. </div>
  173. )}
  174. </div>
  175. <Divider style={{ margin: 0 }} />
  176. <div className='flex items-center space-x-3'>
  177. <div className='flex-1'>
  178. <div className='mb-1 flex h-6 items-center'>
  179. <label className='system-sm-semibold text-text-secondary'>{t('app.newApp.captionName')}</label>
  180. </div>
  181. <Input
  182. value={name}
  183. onChange={e => setName(e.target.value)}
  184. placeholder={t('app.newApp.appNamePlaceholder') || ''}
  185. />
  186. </div>
  187. <AppIcon
  188. iconType={appIcon.type}
  189. icon={appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId}
  190. background={appIcon.type === 'emoji' ? appIcon.background : undefined}
  191. imageUrl={appIcon.type === 'image' ? appIcon.url : undefined}
  192. size='xxl' className='cursor-pointer rounded-2xl'
  193. onClick={() => { setShowAppIconPicker(true) }}
  194. />
  195. {showAppIconPicker && <AppIconPicker
  196. onSelect={(payload) => {
  197. setAppIcon(payload)
  198. setShowAppIconPicker(false)
  199. }}
  200. onClose={() => {
  201. setShowAppIconPicker(false)
  202. }}
  203. />}
  204. </div>
  205. <div>
  206. <div className='mb-1 flex h-6 items-center'>
  207. <label className='system-sm-semibold text-text-secondary'>{t('app.newApp.captionDescription')}</label>
  208. <span className='system-xs-regular ml-1 text-text-tertiary'>({t('app.newApp.optional')})</span>
  209. </div>
  210. <Textarea
  211. className='resize-none'
  212. placeholder={t('app.newApp.appDescriptionPlaceholder') || ''}
  213. value={description}
  214. onChange={e => setDescription(e.target.value)}
  215. />
  216. </div>
  217. </div>
  218. {isAppsFull && <AppsFull className='mt-4' loc='app-create' />}
  219. <div className='flex items-center justify-between pb-10 pt-5'>
  220. {/* zlwu12 2025-09-18 注释 */}
  221. {/* <div className='system-xs-regular flex cursor-pointer items-center gap-1 text-text-tertiary' onClick={onCreateFromTemplate}>
  222. <span>{t('app.newApp.noIdeaTip')}</span>
  223. <div className='p-[1px]'>
  224. <RiArrowRightLine className='h-3.5 w-3.5' />
  225. </div>
  226. </div> */}
  227. <div className='flex gap-2'>
  228. <Button onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  229. <Button disabled={isAppsFull || !name} className='gap-1' variant="primary" onClick={handleCreateApp}>
  230. <span>{t('app.newApp.Create')}</span>
  231. <div className='flex gap-0.5'>
  232. <RiCommandLine size={14} className='system-kbd rounded-sm bg-components-kbd-bg-white p-0.5' />
  233. <RiCornerDownLeftLine size={14} className='system-kbd rounded-sm bg-components-kbd-bg-white p-0.5' />
  234. </div>
  235. </Button>
  236. </div>
  237. </div>
  238. </div>
  239. </div>
  240. <div className='relative flex h-full flex-1 shrink justify-start overflow-hidden'>
  241. <div className='absolute left-0 right-0 top-0 h-6 border-b border-b-divider-subtle 2xl:h-[139px]'></div>
  242. <div className='max-w-[760px] border-x border-x-divider-subtle'>
  243. <div className='h-6 2xl:h-[139px]' />
  244. <AppPreview mode={appMode} />
  245. <div className='absolute left-0 right-0 border-b border-b-divider-subtle'></div>
  246. <div className='flex h-[448px] w-[664px] items-center justify-center' style={{ background: 'repeating-linear-gradient(135deg, transparent, transparent 2px, rgba(16,24,40,0.04) 4px,transparent 3px, transparent 6px)' }}>
  247. <AppScreenShot show={appMode === 'chat'} mode='chat' />
  248. <AppScreenShot show={appMode === 'advanced-chat'} mode='advanced-chat' />
  249. <AppScreenShot show={appMode === 'agent-chat'} mode='agent-chat' />
  250. <AppScreenShot show={appMode === 'completion'} mode='completion' />
  251. <AppScreenShot show={appMode === 'workflow'} mode='workflow' />
  252. </div>
  253. <div className='absolute left-0 right-0 border-b border-b-divider-subtle'></div>
  254. </div>
  255. </div>
  256. </div>
  257. </>
  258. }
  259. type CreateAppDialogProps = CreateAppProps & {
  260. show: boolean
  261. }
  262. const CreateAppModal = ({ show, onClose, onSuccess, onCreateFromTemplate }: CreateAppDialogProps) => {
  263. return (
  264. <FullScreenModal
  265. overflowVisible
  266. closable
  267. open={show}
  268. onClose={onClose}
  269. >
  270. <CreateApp onClose={onClose} onSuccess={onSuccess} onCreateFromTemplate={onCreateFromTemplate} />
  271. </FullScreenModal>
  272. )
  273. }
  274. export default CreateAppModal
  275. type AppTypeCardProps = {
  276. icon: React.JSX.Element
  277. title: string
  278. description: string
  279. active: boolean
  280. onClick: () => void
  281. }
  282. function AppTypeCard({ icon, title, description, active, onClick }: AppTypeCardProps) {
  283. return <div
  284. className={
  285. cn(`relative box-content h-[84px] w-[191px] cursor-pointer rounded-xl
  286. border-[0.5px] border-components-option-card-option-border
  287. bg-components-panel-on-panel-item-bg p-3 shadow-xs hover:shadow-md`, active
  288. ? 'shadow-md outline outline-[1.5px] outline-components-option-card-option-selected-border'
  289. : '')
  290. }
  291. onClick={onClick}
  292. >
  293. {icon}
  294. <div className='system-sm-semibold mb-0.5 mt-2 text-text-secondary'>{title}</div>
  295. <div className='system-xs-regular line-clamp-2 text-text-tertiary' title={description}>{description}</div>
  296. </div>
  297. }
  298. function AppPreview({ mode }: { mode: AppMode }) {
  299. const { t } = useTranslation()
  300. const docLink = useDocLink()
  301. const modeToPreviewInfoMap = {
  302. 'chat': {
  303. title: t('app.types.chatbot'),
  304. description: t('app.newApp.chatbotUserDescription'),
  305. link: docLink('/guides/application-orchestrate/chatbot-application'),
  306. },
  307. 'advanced-chat': {
  308. title: t('app.types.advanced'),
  309. description: t('app.newApp.advancedUserDescription'),
  310. link: docLink('/guides/workflow/README', {
  311. 'zh-Hans': '/guides/workflow/readme',
  312. 'ja-JP': '/guides/workflow/concepts',
  313. }),
  314. },
  315. 'agent-chat': {
  316. title: t('app.types.agent'),
  317. description: t('app.newApp.agentUserDescription'),
  318. link: docLink('/guides/application-orchestrate/agent'),
  319. },
  320. 'completion': {
  321. title: t('app.newApp.completeApp'),
  322. description: t('app.newApp.completionUserDescription'),
  323. link: docLink('/guides/application-orchestrate/text-generator', {
  324. 'zh-Hans': '/guides/application-orchestrate/readme',
  325. 'ja-JP': '/guides/application-orchestrate/README',
  326. }),
  327. },
  328. 'workflow': {
  329. title: t('app.types.workflow'),
  330. description: t('app.newApp.workflowUserDescription'),
  331. link: docLink('/guides/workflow/README', {
  332. 'zh-Hans': '/guides/workflow/readme',
  333. 'ja-JP': '/guides/workflow/concepts',
  334. }),
  335. },
  336. }
  337. const previewInfo = modeToPreviewInfoMap[mode]
  338. return <div className='px-8 py-4'>
  339. <h4 className='system-sm-semibold-uppercase text-text-secondary'>{previewInfo.title}</h4>
  340. <div className='system-xs-regular mt-1 min-h-8 max-w-96 text-text-tertiary'>
  341. <span>{previewInfo.description}</span>
  342. {/* zlwu12 2025-09-18 注释 */}
  343. {/* {previewInfo.link && <Link target='_blank' href={previewInfo.link} className='ml-1 text-text-accent'>{t('app.newApp.learnMore')}</Link>} */}
  344. </div>
  345. </div>
  346. }
  347. function AppScreenShot({ mode, show }: { mode: AppMode; show: boolean }) {
  348. const { theme } = useTheme()
  349. const modeToImageMap = {
  350. 'chat': 'Chatbot',
  351. 'advanced-chat': 'Chatflow',
  352. 'agent-chat': 'Agent',
  353. 'completion': 'TextGenerator',
  354. 'workflow': 'Workflow',
  355. }
  356. return <picture>
  357. <source media="(resolution: 1x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}.png`} />
  358. <source media="(resolution: 2x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}@2x.png`} />
  359. <source media="(resolution: 3x)" srcSet={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}@3x.png`} />
  360. <Image className={show ? '' : 'hidden'}
  361. src={`${basePath}/screenshots/${theme}/${modeToImageMap[mode]}.png`}
  362. alt='App Screen Shot'
  363. width={664} height={448} />
  364. </picture>
  365. }