You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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