mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 11:56:55 +08:00
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Poojan <poojan@infocusp.com> Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com> Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com> Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com> Co-authored-by: User <user@example.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com> Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: tda <95275462+tda1017@users.noreply.github.com> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com> Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com> Co-authored-by: 99 <wh2099@pm.me> Co-authored-by: Br1an <932039080@qq.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: lif <1835304752@qq.com> Co-authored-by: weiguang li <codingpunk@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: Stable Genius <stablegenius043@gmail.com> Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com> Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
163 lines
5.0 KiB
TypeScript
163 lines
5.0 KiB
TypeScript
'use client'
|
|
import type { ReactNode } from 'react'
|
|
import { noop } from 'es-toolkit/function'
|
|
import * as React from 'react'
|
|
import { useEffect, useState } from 'react'
|
|
import { createRoot } from 'react-dom/client'
|
|
import ActionButton from '@/app/components/base/action-button'
|
|
import { cn } from '@/utils/classnames'
|
|
import { ToastContext, useToastContext } from './context'
|
|
|
|
export type ToastHandle = {
|
|
clear?: VoidFunction
|
|
}
|
|
const Toast = ({
|
|
type = 'info',
|
|
size = 'md',
|
|
message,
|
|
children,
|
|
className,
|
|
customComponent,
|
|
}: IToastProps) => {
|
|
const { close } = useToastContext()
|
|
// sometimes message is react node array. Not handle it.
|
|
if (typeof message !== 'string')
|
|
return null
|
|
|
|
return (
|
|
<div className={cn(
|
|
className,
|
|
'fixed z-[9999] mx-8 my-4 w-[360px] grow overflow-hidden rounded-xl',
|
|
'border border-components-panel-border-subtle bg-components-panel-bg-blur shadow-sm',
|
|
'top-0',
|
|
'right-0',
|
|
size === 'md' ? 'p-3' : 'p-2',
|
|
className,
|
|
)}
|
|
>
|
|
<div className={cn(
|
|
'absolute inset-0 -z-10 opacity-40',
|
|
type === 'success' && 'bg-toast-success-bg',
|
|
type === 'warning' && 'bg-toast-warning-bg',
|
|
type === 'error' && 'bg-toast-error-bg',
|
|
type === 'info' && 'bg-toast-info-bg',
|
|
)}
|
|
/>
|
|
<div className={cn('flex', size === 'md' ? 'gap-1' : 'gap-0.5')}>
|
|
<div className={cn('flex items-center justify-center', size === 'md' ? 'p-0.5' : 'p-1')}>
|
|
{type === 'success' && <span className={cn('i-ri-checkbox-circle-fill', 'text-text-success', size === 'md' ? 'h-5 w-5' : 'h-4 w-4')} data-testid="toast-icon-success" aria-hidden="true" />}
|
|
{type === 'error' && <span className={cn('i-ri-error-warning-fill', 'text-text-destructive', size === 'md' ? 'h-5 w-5' : 'h-4 w-4')} data-testid="toast-icon-error" aria-hidden="true" />}
|
|
{type === 'warning' && <span className={cn('i-ri-alert-fill', 'text-text-warning-secondary', size === 'md' ? 'h-5 w-5' : 'h-4 w-4')} data-testid="toast-icon-warning" aria-hidden="true" />}
|
|
{type === 'info' && <span className={cn('i-ri-information-2-fill', 'text-text-accent', size === 'md' ? 'h-5 w-5' : 'h-4 w-4')} data-testid="toast-icon-info" aria-hidden="true" />}
|
|
</div>
|
|
<div className={cn('flex grow flex-col items-start gap-1 py-1', size === 'md' ? 'px-1' : 'px-0.5')}>
|
|
<div className="flex items-center gap-1">
|
|
<div className="text-text-primary system-sm-semibold [word-break:break-word]">{message}</div>
|
|
{customComponent}
|
|
</div>
|
|
{!!children && (
|
|
<div className="text-text-secondary system-xs-regular">
|
|
{children}
|
|
</div>
|
|
)}
|
|
</div>
|
|
{close
|
|
&& (
|
|
<ActionButton data-testid="toast-close-button" className="z-[1000]" onClick={close}>
|
|
<span className="i-ri-close-line h-4 w-4 shrink-0 text-text-tertiary" />
|
|
</ActionButton>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export const ToastProvider = ({
|
|
children,
|
|
}: {
|
|
children: ReactNode
|
|
}) => {
|
|
const placeholder: IToastProps = {
|
|
type: 'info',
|
|
message: 'Toast message',
|
|
duration: 6000,
|
|
}
|
|
const [params, setParams] = React.useState<IToastProps>(placeholder)
|
|
const defaultDuring = (params.type === 'success' || params.type === 'info') ? 3000 : 6000
|
|
const [mounted, setMounted] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (mounted) {
|
|
setTimeout(() => {
|
|
setMounted(false)
|
|
}, params.duration || defaultDuring)
|
|
}
|
|
}, [defaultDuring, mounted, params.duration])
|
|
|
|
return (
|
|
<ToastContext.Provider value={{
|
|
notify: (props) => {
|
|
setMounted(true)
|
|
setParams(props)
|
|
},
|
|
close: () => setMounted(false),
|
|
}}
|
|
>
|
|
{mounted && <Toast {...params} />}
|
|
{children}
|
|
</ToastContext.Provider>
|
|
)
|
|
}
|
|
|
|
Toast.notify = ({
|
|
type,
|
|
size = 'md',
|
|
message,
|
|
duration,
|
|
className,
|
|
customComponent,
|
|
onClose,
|
|
}: Pick<IToastProps, 'type' | 'size' | 'message' | 'duration' | 'className' | 'customComponent' | 'onClose'>): ToastHandle => {
|
|
const defaultDuring = (type === 'success' || type === 'info') ? 3000 : 6000
|
|
const toastHandler: ToastHandle = {}
|
|
|
|
if (typeof window === 'object') {
|
|
const holder = document.createElement('div')
|
|
const root = createRoot(holder)
|
|
|
|
toastHandler.clear = () => {
|
|
if (holder) {
|
|
root.unmount()
|
|
holder.remove()
|
|
}
|
|
onClose?.()
|
|
}
|
|
|
|
root.render(
|
|
<ToastContext.Provider value={{
|
|
notify: noop,
|
|
close: () => {
|
|
if (holder) {
|
|
root.unmount()
|
|
holder.remove()
|
|
}
|
|
onClose?.()
|
|
},
|
|
}}
|
|
>
|
|
<Toast type={type} size={size} message={message} duration={duration} className={className} customComponent={customComponent} />
|
|
</ToastContext.Provider>,
|
|
)
|
|
document.body.appendChild(holder)
|
|
const d = duration ?? defaultDuring
|
|
if (d > 0)
|
|
setTimeout(toastHandler.clear, d)
|
|
}
|
|
|
|
return toastHandler
|
|
}
|
|
|
|
export default Toast
|
|
|
|
export type { IToastProps } from './context'
|