| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | import type { ReactNode } from 'react' | 
					
						
							|  |  |  | import React, { useEffect, useState } from 'react' | 
					
						
							|  |  |  | import { createRoot } from 'react-dom/client' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   RiAlertFill, | 
					
						
							|  |  |  |   RiCheckboxCircleFill, | 
					
						
							|  |  |  |   RiCloseLine, | 
					
						
							|  |  |  |   RiErrorWarningFill, | 
					
						
							|  |  |  |   RiInformation2Fill, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | import { createContext, useContext } from 'use-context-selector' | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  | import ActionButton from '@/app/components/base/action-button' | 
					
						
							| 
									
										
										
										
											2024-07-09 15:05:40 +08:00
										 |  |  | import classNames from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type IToastProps = { | 
					
						
							|  |  |  |   type?: 'success' | 'error' | 'warning' | 'info' | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   size?: 'md' | 'sm' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   duration?: number | 
					
						
							|  |  |  |   message: string | 
					
						
							|  |  |  |   children?: ReactNode | 
					
						
							|  |  |  |   onClose?: () => void | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   className?: string | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |   customComponent?: ReactNode | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | type IToastContext = { | 
					
						
							|  |  |  |   notify: (props: IToastProps) => void | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   close: () => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const ToastContext = createContext<IToastContext>({} as IToastContext) | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | export const useToastContext = () => useContext(ToastContext) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | const Toast = ({ | 
					
						
							|  |  |  |   type = 'info', | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   size = 'md', | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   message, | 
					
						
							|  |  |  |   children, | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   className, | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |   customComponent, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | }: IToastProps) => { | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   const { close } = useToastContext() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   // sometimes message is react node array. Not handle it.
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |   if (typeof message !== 'string') | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     return null | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return <div className={classNames( | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |     className, | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |     'fixed w-[360px] rounded-xl my-4 mx-8 flex-grow z-[9999] overflow-hidden', | 
					
						
							|  |  |  |     size === 'md' ? 'p-3' : 'p-2', | 
					
						
							|  |  |  |     'border border-components-panel-border-subtle bg-components-panel-bg-blur shadow-sm', | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     'top-0', | 
					
						
							|  |  |  |     'right-0', | 
					
						
							|  |  |  |   )}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |     <div className={`absolute inset-0 -z-10 opacity-40 ${ | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |       (type === 'success' && 'bg-toast-success-bg') | 
					
						
							|  |  |  |       || (type === 'warning' && 'bg-toast-warning-bg') | 
					
						
							|  |  |  |       || (type === 'error' && 'bg-toast-error-bg') | 
					
						
							|  |  |  |       || (type === 'info' && 'bg-toast-info-bg') | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |     }`}
 | 
					
						
							|  |  |  |     /> | 
					
						
							|  |  |  |     <div className={`flex ${size === 'md' ? 'gap-1' : 'gap-0.5'}`}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className={`flex items-center justify-center ${size === 'md' ? 'p-0.5' : 'p-1'}`}> | 
					
						
							|  |  |  |         {type === 'success' && <RiCheckboxCircleFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-success`} aria-hidden="true" />} | 
					
						
							|  |  |  |         {type === 'error' && <RiErrorWarningFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-destructive`} aria-hidden="true" />} | 
					
						
							|  |  |  |         {type === 'warning' && <RiAlertFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-warning-secondary`} aria-hidden="true" />} | 
					
						
							|  |  |  |         {type === 'info' && <RiInformation2Fill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-accent`} aria-hidden="true" />} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |       <div className={`flex py-1 ${size === 'md' ? 'px-1' : 'px-0.5'} grow flex-col items-start gap-1`}> | 
					
						
							|  |  |  |         <div className='flex items-center gap-1'> | 
					
						
							| 
									
										
										
										
											2025-04-24 11:58:44 +08:00
										 |  |  |           <div className='system-sm-semibold text-text-primary [word-break:break-word]'>{message}</div> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           {customComponent} | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         {children && <div className='system-xs-regular text-text-secondary'> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           {children} | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |       {close | 
					
						
							|  |  |  |         && (<ActionButton className='z-[1000]' onClick={close}> | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <RiCloseLine className='h-4 w-4 shrink-0 text-text-tertiary' /> | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |         </ActionButton>) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const ToastProvider = ({ | 
					
						
							|  |  |  |   children, | 
					
						
							|  |  |  | }: { | 
					
						
							|  |  |  |   children: ReactNode | 
					
						
							|  |  |  | }) => { | 
					
						
							|  |  |  |   const placeholder: IToastProps = { | 
					
						
							|  |  |  |     type: 'info', | 
					
						
							|  |  |  |     message: 'Toast message', | 
					
						
							| 
									
										
										
										
											2024-07-17 20:24:31 +08:00
										 |  |  |     duration: 6000, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   const [params, setParams] = React.useState<IToastProps>(placeholder) | 
					
						
							| 
									
										
										
										
											2024-07-17 21:19:04 +08:00
										 |  |  |   const defaultDuring = (params.type === 'success' || params.type === 'info') ? 3000 : 6000 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const [mounted, setMounted] = useState(false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (mounted) { | 
					
						
							|  |  |  |       setTimeout(() => { | 
					
						
							|  |  |  |         setMounted(false) | 
					
						
							|  |  |  |       }, params.duration || defaultDuring) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-07-17 21:19:04 +08:00
										 |  |  |   }, [defaultDuring, mounted, params.duration]) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return <ToastContext.Provider value={{ | 
					
						
							|  |  |  |     notify: (props) => { | 
					
						
							|  |  |  |       setMounted(true) | 
					
						
							|  |  |  |       setParams(props) | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |     close: () => setMounted(false), | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }}> | 
					
						
							|  |  |  |     {mounted && <Toast {...params} />} | 
					
						
							|  |  |  |     {children} | 
					
						
							|  |  |  |   </ToastContext.Provider> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Toast.notify = ({ | 
					
						
							|  |  |  |   type, | 
					
						
							| 
									
										
										
										
											2024-11-22 15:05:04 +08:00
										 |  |  |   size = 'md', | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   message, | 
					
						
							|  |  |  |   duration, | 
					
						
							| 
									
										
										
										
											2023-12-03 22:10:16 +08:00
										 |  |  |   className, | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |   customComponent, | 
					
						
							| 
									
										
										
										
											2025-02-10 10:44:20 +08:00
										 |  |  |   onClose, | 
					
						
							|  |  |  | }: Pick<IToastProps, 'type' | 'size' | 'message' | 'duration' | 'className' | 'customComponent' | 'onClose'>) => { | 
					
						
							| 
									
										
										
										
											2024-07-17 21:19:04 +08:00
										 |  |  |   const defaultDuring = (type === 'success' || type === 'info') ? 3000 : 6000 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   if (typeof window === 'object') { | 
					
						
							|  |  |  |     const holder = document.createElement('div') | 
					
						
							|  |  |  |     const root = createRoot(holder) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |     root.render( | 
					
						
							|  |  |  |       <ToastContext.Provider value={{ | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |         notify: noop, | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |         close: () => { | 
					
						
							|  |  |  |           if (holder) { | 
					
						
							|  |  |  |             root.unmount() | 
					
						
							|  |  |  |             holder.remove() | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2025-02-10 10:44:20 +08:00
										 |  |  |           onClose?.() | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }}> | 
					
						
							| 
									
										
										
										
											2024-12-26 12:01:51 +08:00
										 |  |  |         <Toast type={type} size={size} message={message} duration={duration} className={className} customComponent={customComponent} /> | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |       </ToastContext.Provider>, | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     document.body.appendChild(holder) | 
					
						
							|  |  |  |     setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |       if (holder) { | 
					
						
							|  |  |  |         root.unmount() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         holder.remove() | 
					
						
							| 
									
										
										
										
											2024-12-09 17:55:06 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-02-10 10:44:20 +08:00
										 |  |  |       onClose?.() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     }, duration || defaultDuring) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default Toast |