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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use client'
  2. import type { ReactNode } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { createRoot } from 'react-dom/client'
  5. import {
  6. RiAlertFill,
  7. RiCheckboxCircleFill,
  8. RiCloseLine,
  9. RiErrorWarningFill,
  10. RiInformation2Fill,
  11. } from '@remixicon/react'
  12. import { createContext, useContext } from 'use-context-selector'
  13. import ActionButton from '@/app/components/base/action-button'
  14. import classNames from '@/utils/classnames'
  15. import { noop } from 'lodash-es'
  16. export type IToastProps = {
  17. type?: 'success' | 'error' | 'warning' | 'info'
  18. size?: 'md' | 'sm'
  19. duration?: number
  20. message: string
  21. children?: ReactNode
  22. onClose?: () => void
  23. className?: string
  24. customComponent?: ReactNode
  25. }
  26. type IToastContext = {
  27. notify: (props: IToastProps) => void
  28. close: () => void
  29. }
  30. export type ToastHandle = {
  31. clear?: VoidFunction
  32. }
  33. export const ToastContext = createContext<IToastContext>({} as IToastContext)
  34. export const useToastContext = () => useContext(ToastContext)
  35. const Toast = ({
  36. type = 'info',
  37. size = 'md',
  38. message,
  39. children,
  40. className,
  41. customComponent,
  42. }: IToastProps) => {
  43. const { close } = useToastContext()
  44. // sometimes message is react node array. Not handle it.
  45. if (typeof message !== 'string')
  46. return null
  47. return <div className={classNames(
  48. className,
  49. 'fixed z-[9999] mx-8 my-4 w-[360px] grow overflow-hidden rounded-xl',
  50. size === 'md' ? 'p-3' : 'p-2',
  51. 'border border-components-panel-border-subtle bg-components-panel-bg-blur shadow-sm',
  52. 'top-0',
  53. 'right-0',
  54. )}>
  55. <div className={`absolute inset-0 -z-10 opacity-40 ${(type === 'success' && 'bg-toast-success-bg')
  56. || (type === 'warning' && 'bg-toast-warning-bg')
  57. || (type === 'error' && 'bg-toast-error-bg')
  58. || (type === 'info' && 'bg-toast-info-bg')
  59. }`}
  60. />
  61. <div className={`flex ${size === 'md' ? 'gap-1' : 'gap-0.5'}`}>
  62. <div className={`flex items-center justify-center ${size === 'md' ? 'p-0.5' : 'p-1'}`}>
  63. {type === 'success' && <RiCheckboxCircleFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-success`} aria-hidden="true" />}
  64. {type === 'error' && <RiErrorWarningFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-destructive`} aria-hidden="true" />}
  65. {type === 'warning' && <RiAlertFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-warning-secondary`} aria-hidden="true" />}
  66. {type === 'info' && <RiInformation2Fill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-accent`} aria-hidden="true" />}
  67. </div>
  68. <div className={`flex py-1 ${size === 'md' ? 'px-1' : 'px-0.5'} grow flex-col items-start gap-1`}>
  69. <div className='flex items-center gap-1'>
  70. <div className='system-sm-semibold text-text-primary [word-break:break-word]'>{message}</div>
  71. {customComponent}
  72. </div>
  73. {children && <div className='system-xs-regular text-text-secondary'>
  74. {children}
  75. </div>
  76. }
  77. </div>
  78. {close
  79. && (<ActionButton className='z-[1000]' onClick={close}>
  80. <RiCloseLine className='h-4 w-4 shrink-0 text-text-tertiary' />
  81. </ActionButton>)
  82. }
  83. </div>
  84. </div>
  85. }
  86. export const ToastProvider = ({
  87. children,
  88. }: {
  89. children: ReactNode
  90. }) => {
  91. const placeholder: IToastProps = {
  92. type: 'info',
  93. message: 'Toast message',
  94. duration: 6000,
  95. }
  96. const [params, setParams] = React.useState<IToastProps>(placeholder)
  97. const defaultDuring = (params.type === 'success' || params.type === 'info') ? 3000 : 6000
  98. const [mounted, setMounted] = useState(false)
  99. useEffect(() => {
  100. if (mounted) {
  101. setTimeout(() => {
  102. setMounted(false)
  103. }, params.duration || defaultDuring)
  104. }
  105. }, [defaultDuring, mounted, params.duration])
  106. return <ToastContext.Provider value={{
  107. notify: (props) => {
  108. setMounted(true)
  109. setParams(props)
  110. },
  111. close: () => setMounted(false),
  112. }}>
  113. {mounted && <Toast {...params} />}
  114. {children}
  115. </ToastContext.Provider>
  116. }
  117. Toast.notify = ({
  118. type,
  119. size = 'md',
  120. message,
  121. duration,
  122. className,
  123. customComponent,
  124. onClose,
  125. }: Pick<IToastProps, 'type' | 'size' | 'message' | 'duration' | 'className' | 'customComponent' | 'onClose'>): ToastHandle => {
  126. const defaultDuring = (type === 'success' || type === 'info') ? 3000 : 6000
  127. const toastHandler: ToastHandle = {}
  128. if (typeof window === 'object') {
  129. const holder = document.createElement('div')
  130. const root = createRoot(holder)
  131. toastHandler.clear = () => {
  132. if (holder) {
  133. root.unmount()
  134. holder.remove()
  135. }
  136. onClose?.()
  137. }
  138. root.render(
  139. <ToastContext.Provider value={{
  140. notify: noop,
  141. close: () => {
  142. if (holder) {
  143. root.unmount()
  144. holder.remove()
  145. }
  146. onClose?.()
  147. },
  148. }}>
  149. <Toast type={type} size={size} message={message} duration={duration} className={className} customComponent={customComponent} />
  150. </ToastContext.Provider>,
  151. )
  152. document.body.appendChild(holder)
  153. const d = duration ?? defaultDuring
  154. if (d > 0)
  155. setTimeout(toastHandler.clear, d)
  156. }
  157. return toastHandler
  158. }
  159. export default Toast