index.tsx 7.1 KB

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