index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use client'
  2. import { useEffect, useState } from 'react'
  3. import { useRouter } from 'next/navigation'
  4. import { useContext } from 'use-context-selector'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import AppIconPicker from '../../base/app-icon-picker'
  8. import s from './style.module.css'
  9. import cn from '@/utils/classnames'
  10. import Checkbox from '@/app/components/base/checkbox'
  11. import Button from '@/app/components/base/button'
  12. import Input from '@/app/components/base/input'
  13. import Modal from '@/app/components/base/modal'
  14. import Confirm from '@/app/components/base/confirm'
  15. import { ToastContext } from '@/app/components/base/toast'
  16. import { deleteApp, switchApp } from '@/service/apps'
  17. import { useAppContext } from '@/context/app-context'
  18. import { useProviderContext } from '@/context/provider-context'
  19. import AppsFull from '@/app/components/billing/apps-full-in-dialog'
  20. import { NEED_REFRESH_APP_LIST_KEY } from '@/config'
  21. import { getRedirection } from '@/utils/app-redirection'
  22. import type { App } from '@/types/app'
  23. import { AlertTriangle } from '@/app/components/base/icons/src/vender/solid/alertsAndFeedback'
  24. import AppIcon from '@/app/components/base/app-icon'
  25. import { useStore as useAppStore } from '@/app/components/app/store'
  26. type SwitchAppModalProps = {
  27. show: boolean
  28. appDetail: App
  29. onSuccess?: () => void
  30. onClose: () => void
  31. inAppDetail?: boolean
  32. }
  33. const SwitchAppModal = ({ show, appDetail, inAppDetail = false, onSuccess, onClose }: SwitchAppModalProps) => {
  34. const { push, replace } = useRouter()
  35. const { t } = useTranslation()
  36. const { notify } = useContext(ToastContext)
  37. const setAppDetail = useAppStore(s => s.setAppDetail)
  38. const { isCurrentWorkspaceEditor } = useAppContext()
  39. const { plan, enableBilling } = useProviderContext()
  40. const isAppsFull = (enableBilling && plan.usage.buildApps >= plan.total.buildApps)
  41. const [showAppIconPicker, setShowAppIconPicker] = useState(false)
  42. const [appIcon, setAppIcon] = useState(
  43. appDetail.icon_type === 'image'
  44. ? { type: 'image' as const, url: appDetail.icon_url, fileId: appDetail.icon }
  45. : { type: 'emoji' as const, icon: appDetail.icon, background: appDetail.icon_background },
  46. )
  47. const [name, setName] = useState(`${appDetail.name}(copy)`)
  48. const [removeOriginal, setRemoveOriginal] = useState<boolean>(false)
  49. const [showConfirmDelete, setShowConfirmDelete] = useState(false)
  50. const goStart = async () => {
  51. try {
  52. const { new_app_id: newAppID } = await switchApp({
  53. appID: appDetail.id,
  54. name,
  55. icon_type: appIcon.type,
  56. icon: appIcon.type === 'emoji' ? appIcon.icon : appIcon.fileId,
  57. icon_background: appIcon.type === 'emoji' ? appIcon.background : undefined,
  58. })
  59. if (onSuccess)
  60. onSuccess()
  61. if (onClose)
  62. onClose()
  63. notify({ type: 'success', message: t('app.newApp.appCreated') })
  64. if (inAppDetail)
  65. setAppDetail()
  66. if (removeOriginal)
  67. await deleteApp(appDetail.id)
  68. localStorage.setItem(NEED_REFRESH_APP_LIST_KEY, '1')
  69. getRedirection(
  70. isCurrentWorkspaceEditor,
  71. {
  72. id: newAppID,
  73. mode: appDetail.mode === 'completion' ? 'workflow' : 'advanced-chat',
  74. },
  75. removeOriginal ? replace : push,
  76. )
  77. }
  78. catch (e) {
  79. notify({ type: 'error', message: t('app.newApp.appCreateFailed') })
  80. }
  81. }
  82. useEffect(() => {
  83. if (removeOriginal)
  84. setShowConfirmDelete(true)
  85. }, [removeOriginal])
  86. return (
  87. <>
  88. <Modal
  89. className={cn('p-8 max-w-[600px] w-[600px]', s.bg)}
  90. isShow={show}
  91. onClose={() => { }}
  92. >
  93. <div className='absolute right-4 top-4 p-2 cursor-pointer' onClick={onClose}>
  94. <RiCloseLine className='w-4 h-4 text-gray-500' />
  95. </div>
  96. <div className='w-12 h-12 p-3 bg-white rounded-xl border-[0.5px] border-gray-100 shadow-xl'>
  97. <AlertTriangle className='w-6 h-6 text-[rgb(247,144,9)]' />
  98. </div>
  99. <div className='relative mt-3 text-xl font-semibold leading-[30px] text-gray-900'>{t('app.switch')}</div>
  100. <div className='my-1 text-gray-500 text-sm leading-5'>
  101. <span>{t('app.switchTipStart')}</span>
  102. <span className='text-gray-700 font-medium'>{t('app.switchTip')}</span>
  103. <span>{t('app.switchTipEnd')}</span>
  104. </div>
  105. <div className='pb-4'>
  106. <div className='py-2 text-sm font-medium leading-[20px] text-gray-900'>{t('app.switchLabel')}</div>
  107. <div className='flex items-center justify-between space-x-2'>
  108. <AppIcon
  109. size='large'
  110. onClick={() => { setShowAppIconPicker(true) }}
  111. className='cursor-pointer'
  112. iconType={appIcon.type}
  113. icon={appIcon.type === 'image' ? appIcon.fileId : appIcon.icon}
  114. background={appIcon.type === 'image' ? undefined : appIcon.background}
  115. imageUrl={appIcon.type === 'image' ? appIcon.url : undefined}
  116. />
  117. <Input
  118. value={name}
  119. onChange={e => setName(e.target.value)}
  120. placeholder={t('app.newApp.appNamePlaceholder') || ''}
  121. className='grow h-10'
  122. />
  123. </div>
  124. {showAppIconPicker && <AppIconPicker
  125. onSelect={(payload) => {
  126. setAppIcon(payload)
  127. setShowAppIconPicker(false)
  128. }}
  129. onClose={() => {
  130. setAppIcon(appDetail.icon_type === 'image'
  131. ? { type: 'image' as const, url: appDetail.icon_url, fileId: appDetail.icon }
  132. : { type: 'emoji' as const, icon: appDetail.icon, background: appDetail.icon_background })
  133. setShowAppIconPicker(false)
  134. }}
  135. />}
  136. </div>
  137. {isAppsFull && <AppsFull loc='app-switch' />}
  138. <div className='pt-6 flex justify-between items-center'>
  139. <div className='flex items-center'>
  140. <Checkbox className='shrink-0' checked={removeOriginal} onCheck={() => setRemoveOriginal(!removeOriginal)} />
  141. <div className="ml-2 text-sm leading-5 text-gray-700 cursor-pointer" onClick={() => setRemoveOriginal(!removeOriginal)}>{t('app.removeOriginal')}</div>
  142. </div>
  143. <div className='flex items-center'>
  144. <Button className='mr-2' onClick={onClose}>{t('app.newApp.Cancel')}</Button>
  145. <Button className='border-red-700' disabled={isAppsFull || !name} variant="warning" onClick={goStart}>{t('app.switchStart')}</Button>
  146. </div>
  147. </div>
  148. </Modal>
  149. {showConfirmDelete && (
  150. <Confirm
  151. title={t('app.deleteAppConfirmTitle')}
  152. content={t('app.deleteAppConfirmContent')}
  153. isShow={showConfirmDelete}
  154. onConfirm={() => setShowConfirmDelete(false)}
  155. onCancel={() => {
  156. setShowConfirmDelete(false)
  157. setRemoveOriginal(false)
  158. }}
  159. />
  160. )}
  161. </>
  162. )
  163. }
  164. export default SwitchAppModal