index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import type { ChangeEvent } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiEditBoxLine,
  6. RiEqualizer2Line,
  7. RiExchange2Fill,
  8. RiImageAddLine,
  9. RiLayoutLeft2Line,
  10. RiLoader2Line,
  11. RiPlayLargeLine,
  12. } from '@remixicon/react'
  13. import LogoSite from '@/app/components/base/logo/logo-site'
  14. import Switch from '@/app/components/base/switch'
  15. import Button from '@/app/components/base/button'
  16. import Divider from '@/app/components/base/divider'
  17. import { useProviderContext } from '@/context/provider-context'
  18. import { Plan } from '@/app/components/billing/type'
  19. import { imageUpload } from '@/app/components/base/image-uploader/utils'
  20. import { useToastContext } from '@/app/components/base/toast'
  21. import { BubbleTextMod } from '@/app/components/base/icons/src/vender/solid/communication'
  22. import {
  23. updateCurrentWorkspace,
  24. } from '@/service/common'
  25. import { useAppContext } from '@/context/app-context'
  26. import cn from '@/utils/classnames'
  27. const ALLOW_FILE_EXTENSIONS = ['svg', 'png']
  28. const CustomWebAppBrand = () => {
  29. const { t } = useTranslation()
  30. const { notify } = useToastContext()
  31. const { plan, enableBilling } = useProviderContext()
  32. const {
  33. currentWorkspace,
  34. mutateCurrentWorkspace,
  35. isCurrentWorkspaceManager,
  36. } = useAppContext()
  37. const [fileId, setFileId] = useState('')
  38. const [imgKey, setImgKey] = useState(Date.now())
  39. const [uploadProgress, setUploadProgress] = useState(0)
  40. const isSandbox = enableBilling && plan.type === Plan.sandbox
  41. const uploading = uploadProgress > 0 && uploadProgress < 100
  42. const webappLogo = currentWorkspace.custom_config?.replace_webapp_logo || ''
  43. const webappBrandRemoved = currentWorkspace.custom_config?.remove_webapp_brand
  44. const uploadDisabled = isSandbox || webappBrandRemoved || !isCurrentWorkspaceManager
  45. const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
  46. const file = e.target.files?.[0]
  47. if (!file)
  48. return
  49. if (file.size > 5 * 1024 * 1024) {
  50. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerLimit', { size: 5 }) })
  51. return
  52. }
  53. imageUpload({
  54. file,
  55. onProgressCallback: (progress) => {
  56. setUploadProgress(progress)
  57. },
  58. onSuccessCallback: (res) => {
  59. setUploadProgress(100)
  60. setFileId(res.id)
  61. },
  62. onErrorCallback: () => {
  63. notify({ type: 'error', message: t('common.imageUploader.uploadFromComputerUploadError') })
  64. setUploadProgress(-1)
  65. },
  66. }, false, '/workspaces/custom-config/webapp-logo/upload')
  67. }
  68. const handleApply = async () => {
  69. await updateCurrentWorkspace({
  70. url: '/workspaces/custom-config',
  71. body: {
  72. remove_webapp_brand: webappBrandRemoved,
  73. replace_webapp_logo: fileId,
  74. },
  75. })
  76. mutateCurrentWorkspace()
  77. setFileId('')
  78. setImgKey(Date.now())
  79. }
  80. const handleRestore = async () => {
  81. await updateCurrentWorkspace({
  82. url: '/workspaces/custom-config',
  83. body: {
  84. remove_webapp_brand: false,
  85. replace_webapp_logo: '',
  86. },
  87. })
  88. mutateCurrentWorkspace()
  89. }
  90. const handleSwitch = async (checked: boolean) => {
  91. await updateCurrentWorkspace({
  92. url: '/workspaces/custom-config',
  93. body: {
  94. remove_webapp_brand: checked,
  95. },
  96. })
  97. mutateCurrentWorkspace()
  98. }
  99. const handleCancel = () => {
  100. setFileId('')
  101. setUploadProgress(0)
  102. }
  103. return (
  104. <div className='py-4'>
  105. <div className='flex items-center justify-between mb-2 p-4 rounded-xl bg-background-section-burn system-md-medium text-text-primary'>
  106. {t('custom.webapp.removeBrand')}
  107. <Switch
  108. size='l'
  109. defaultValue={webappBrandRemoved}
  110. disabled={isSandbox || !isCurrentWorkspaceManager}
  111. onChange={handleSwitch}
  112. />
  113. </div>
  114. <div className={cn('flex items-center justify-between h-14 px-4 rounded-xl bg-background-section-burn', webappBrandRemoved && 'opacity-30')}>
  115. <div>
  116. <div className='system-md-medium text-text-primary'>{t('custom.webapp.changeLogo')}</div>
  117. <div className='system-xs-regular text-text-tertiary'>{t('custom.webapp.changeLogoTip')}</div>
  118. </div>
  119. <div className='flex items-center'>
  120. {(uploadDisabled || (!webappLogo && !webappBrandRemoved)) && (
  121. <>
  122. <Button
  123. variant='ghost'
  124. disabled={uploadDisabled || (!webappLogo && !webappBrandRemoved)}
  125. onClick={handleRestore}
  126. >
  127. {t('custom.restore')}
  128. </Button>
  129. <div className='mx-2 h-5 w-[1px] bg-divider-regular'></div>
  130. </>
  131. )}
  132. {
  133. !uploading && (
  134. <Button
  135. className='relative mr-2'
  136. disabled={uploadDisabled}
  137. >
  138. <RiImageAddLine className='mr-1 w-4 h-4' />
  139. {
  140. (webappLogo || fileId)
  141. ? t('custom.change')
  142. : t('custom.upload')
  143. }
  144. <input
  145. className={cn('absolute block inset-0 opacity-0 text-[0] w-full', uploadDisabled ? 'cursor-not-allowed' : 'cursor-pointer')}
  146. onClick={e => (e.target as HTMLInputElement).value = ''}
  147. type='file'
  148. accept={ALLOW_FILE_EXTENSIONS.map(ext => `.${ext}`).join(',')}
  149. onChange={handleChange}
  150. disabled={uploadDisabled}
  151. />
  152. </Button>
  153. )
  154. }
  155. {
  156. uploading && (
  157. <Button
  158. className='relative mr-2'
  159. disabled={true}
  160. >
  161. <RiLoader2Line className='animate-spin mr-1 w-4 h-4' />
  162. {t('custom.uploading')}
  163. </Button>
  164. )
  165. }
  166. {
  167. fileId && (
  168. <>
  169. <Button
  170. className='mr-2'
  171. onClick={handleCancel}
  172. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  173. >
  174. {t('common.operation.cancel')}
  175. </Button>
  176. <Button
  177. variant='primary'
  178. className='mr-2'
  179. onClick={handleApply}
  180. disabled={webappBrandRemoved || !isCurrentWorkspaceManager}
  181. >
  182. {t('custom.apply')}
  183. </Button>
  184. </>
  185. )
  186. }
  187. </div>
  188. </div>
  189. {uploadProgress === -1 && (
  190. <div className='mt-2 text-xs text-[#D92D20]'>{t('custom.uploadedFail')}</div>
  191. )}
  192. <div className='mt-5 mb-2 flex items-center gap-2'>
  193. <div className='shrink-0 system-xs-medium-uppercase text-text-tertiary'>{t('appOverview.overview.appInfo.preview')}</div>
  194. <Divider bgStyle='gradient' className='grow' />
  195. </div>
  196. <div className='relative mb-2 flex items-center gap-3'>
  197. {/* chat card */}
  198. <div className='grow basis-1/2 h-[320px] flex bg-background-default-burn rounded-2xl border-[0.5px] border-components-panel-border-subtle overflow-hidden'>
  199. <div className='shrink-0 h-full w-[232px] p-1 pr-0 flex flex-col'>
  200. <div className='p-3 pr-2 flex items-center gap-3'>
  201. <div className={cn('w-8 h-8 inline-flex items-center justify-center rounded-lg border border-divider-regular', 'bg-components-icon-bg-blue-light-solid')}>
  202. <BubbleTextMod className='w-4 h-4 text-components-avatar-shape-fill-stop-100' />
  203. </div>
  204. <div className='grow system-md-semibold text-text-secondary'>Chatflow App</div>
  205. <div className='p-1.5'>
  206. <RiLayoutLeft2Line className='w-4 h-4 text-text-tertiary' />
  207. </div>
  208. </div>
  209. <div className='shrink-0 px-4 py-3'>
  210. <Button variant='secondary-accent' className='w-full justify-center'>
  211. <RiEditBoxLine className='w-4 h-4 mr-1' />
  212. <div className='p-1 opacity-20'>
  213. <div className='h-2 w-[94px] rounded-sm bg-text-accent-light-mode-only'></div>
  214. </div>
  215. </Button>
  216. </div>
  217. <div className='grow px-3 pt-5'>
  218. <div className='h-8 px-3 py-1 flex items-center'>
  219. <div className='w-14 h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  220. </div>
  221. <div className='h-8 px-3 py-1 flex items-center'>
  222. <div className='w-[168px] h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  223. </div>
  224. <div className='h-8 px-3 py-1 flex items-center'>
  225. <div className='w-[128px] h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  226. </div>
  227. </div>
  228. <div className='shrink-0 p-3 flex items-center justify-between'>
  229. <div className='p-1.5'>
  230. <RiEqualizer2Line className='w-4 h-4 text-text-tertiary' />
  231. </div>
  232. <div className='flex items-center gap-1.5'>
  233. {!webappBrandRemoved && (
  234. <>
  235. <div className='text-text-tertiary system-2xs-medium-uppercase'>POWERED BY</div>
  236. {webappLogo
  237. ? <img src={`${webappLogo}?hash=${imgKey}`} alt='logo' className='block w-auto h-5' />
  238. : <LogoSite className='!h-5' />
  239. }
  240. </>
  241. )}
  242. </div>
  243. </div>
  244. </div>
  245. <div className='grow flex flex-col justify-between w-[138px] p-2 pr-0'>
  246. <div className='grow pt-16 pl-[22px] pb-4 flex flex-col justify-between bg-chatbot-bg rounded-l-2xl border-[0.5px] border-r-0 border-components-panel-border-subtle'>
  247. <div className='w-[720px] px-4 py-3 bg-chat-bubble-bg rounded-2xl border border-divider-subtle'>
  248. <div className='mb-1 text-text-primary body-md-regular'>Hello! How can I assist you today?</div>
  249. <Button size='small'>
  250. <div className='w-[144px] h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  251. </Button>
  252. </div>
  253. <div className='w-[578px] h-[52px] flex items-center pl-3.5 rounded-xl bg-components-panel-bg-blur backdrop-blur-sm border border-components-chat-input-border shadow-md text-text-placeholder body-lg-regular'>Talk to Dify</div>
  254. </div>
  255. </div>
  256. </div>
  257. {/* workflow card */}
  258. <div className='grow basis-1/2 h-[320px] flex flex-col bg-background-default-burn rounded-2xl border-[0.5px] border-components-panel-border-subtle overflow-hidden'>
  259. <div className='w-full p-4 pb-0 border-b-[0.5px] border-divider-subtle'>
  260. <div className='mb-2 flex items-center gap-3'>
  261. <div className={cn('w-8 h-8 inline-flex items-center justify-center rounded-lg border border-divider-regular', 'bg-components-icon-bg-indigo-solid')}>
  262. <RiExchange2Fill className='w-4 h-4 text-components-avatar-shape-fill-stop-100' />
  263. </div>
  264. <div className='grow system-md-semibold text-text-secondary'>Workflow App</div>
  265. <div className='p-1.5'>
  266. <RiLayoutLeft2Line className='w-4 h-4 text-text-tertiary' />
  267. </div>
  268. </div>
  269. <div className='flex items-center gap-4'>
  270. <div className='shrink-0 h-10 flex items-center border-b-2 border-components-tab-active text-text-primary system-md-semibold-uppercase'>RUN ONCE</div>
  271. <div className='grow h-10 flex items-center border-b-2 border-transparent text-text-tertiary system-md-semibold-uppercase'>RUN BATCH</div>
  272. </div>
  273. </div>
  274. <div className='grow bg-components-panel-bg'>
  275. <div className='p-4 pb-1'>
  276. <div className='mb-1 py-2'>
  277. <div className='w-20 h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  278. </div>
  279. <div className='w-full h-16 rounded-lg bg-components-input-bg-normal '></div>
  280. </div>
  281. <div className='px-4 py-3 flex items-center justify-between'>
  282. <Button size='small'>
  283. <div className='w-10 h-2 rounded-sm bg-text-quaternary opacity-20'></div>
  284. </Button>
  285. <Button variant='primary' size='small' disabled>
  286. <RiPlayLargeLine className='mr-1 w-4 h-4' />
  287. <span>Execute</span>
  288. </Button>
  289. </div>
  290. </div>
  291. <div className='shrink-0 h-12 p-4 pt-3 flex items-center gap-1.5 bg-components-panel-bg'>
  292. {!webappBrandRemoved && (
  293. <>
  294. <div className='text-text-tertiary system-2xs-medium-uppercase'>POWERED BY</div>
  295. {webappLogo
  296. ? <img src={`${webappLogo}?hash=${imgKey}`} alt='logo' className='block w-auto h-5' />
  297. : <LogoSite className='!h-5' />
  298. }
  299. </>
  300. )}
  301. </div>
  302. </div>
  303. </div>
  304. </div>
  305. )
  306. }
  307. export default CustomWebAppBrand