stacker.news/components/toast.js

146 lines
4.6 KiB
JavaScript
Raw Normal View History

import { useRouter } from 'next/router'
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'
import Button from 'react-bootstrap/Button'
import Toast from 'react-bootstrap/Toast'
import ToastBody from 'react-bootstrap/ToastBody'
import ToastContainer from 'react-bootstrap/ToastContainer'
import styles from './toast.module.css'
const ToastContext = createContext(() => {})
export const ToastProvider = ({ children }) => {
const router = useRouter()
const [toasts, setToasts] = useState([])
const toastId = useRef(0)
2024-02-01 13:18:42 +00:00
const dispatchToast = useCallback((toastConfig) => {
toastConfig = {
...toastConfig,
id: toastId.current++
}
setToasts(toasts => [...toasts, toastConfig])
2024-02-01 13:18:42 +00:00
return () => removeToast(toastConfig)
}, [])
2024-02-01 13:18:42 +00:00
const removeToast = useCallback(({ id, onCancel, tag }) => {
setToasts(toasts => toasts.filter(toast => {
if (tag && !onCancel) {
// if tag onCancel is not set, toast did show X for closing.
// if additionally tag is set, we close all toasts with same tag.
return toast.tag !== tag
}
return toast.id !== id
}))
}, [])
const toaster = useMemo(() => ({
2024-02-01 13:18:42 +00:00
success: (body, options) => {
const toast = {
body,
variant: 'success',
autohide: true,
2024-02-01 13:18:42 +00:00
delay: 5000,
2024-02-01 13:50:51 +00:00
tag: options?.tag || body,
2024-02-01 13:18:42 +00:00
...options
}
return dispatchToast(toast)
},
2024-02-01 13:18:42 +00:00
warning: (body, options) => {
const toast = {
body,
variant: 'warning',
autohide: true,
2024-02-01 13:18:42 +00:00
delay: 5000,
2024-02-01 13:50:51 +00:00
tag: options?.tag || body,
2024-02-01 13:18:42 +00:00
...options
}
return dispatchToast(toast)
},
2024-02-01 13:18:42 +00:00
danger: (body, options) => {
const toast = {
body,
variant: 'danger',
autohide: false,
2024-02-01 13:50:51 +00:00
tag: options?.tag || body,
2024-02-01 13:18:42 +00:00
...options
}
2024-02-01 13:18:42 +00:00
return dispatchToast(toast)
}
}), [dispatchToast, removeToast])
2024-02-01 13:18:42 +00:00
// Only clear toasts with no cancel function on page navigation
// since navigation should not interfere with being able to cancel an action.
useEffect(() => {
2024-02-01 13:18:42 +00:00
const handleRouteChangeStart = () => setToasts(toasts => toasts.filter(({ onCancel }) => onCancel), [])
router.events.on('routeChangeStart', handleRouteChangeStart)
return () => {
router.events.off('routeChangeStart', handleRouteChangeStart)
}
}, [router])
2024-02-01 13:18:42 +00:00
// this function merges toasts with the same tag into one toast.
// for example: 3x 'zap pending' -> '(3) zap pending'
const tagReducer = (toasts, toast) => {
const { tag } = toast
// has tag?
if (!tag) return [...toasts, toast]
// existing tag?
const idx = toasts.findIndex(toast => toast.tag === tag)
if (idx === -1) return [...toasts, toast]
// merge toasts with same tag
const prevToast = toasts[idx]
let { rawBody, body, amount } = prevToast
rawBody ??= body
amount = amount ? amount + 1 : 2
body = `(${amount}) ${rawBody}`
return [
...toasts.slice(0, idx),
{ ...toast, rawBody, amount, body },
...toasts.slice(idx + 1)
]
}
// only show toast with highest ID of each tag
const visibleToasts = toasts.reduce(tagReducer, [])
return (
<ToastContext.Provider value={toaster}>
<ToastContainer className={`pb-3 pe-3 ${styles.toastContainer}`} position='bottom-end' containerPosition='fixed'>
2024-02-01 13:18:42 +00:00
{visibleToasts.map(toast => {
const textStyle = toast.variant === 'warning' ? 'text-dark' : ''
return (
<Toast
key={toast.id} bg={toast.variant} show autohide={toast.autohide}
delay={toast.delay} className={`${styles.toast} ${styles[toast.variant]} ${textStyle}`} onClose={() => removeToast(toast.id)}
>
<ToastBody>
<div className='d-flex align-items-center'>
<div className='flex-grow-1'>{toast.body}</div>
<Button
variant={null}
className='p-0 ps-2'
aria-label='close'
onClick={() => {
toast.onCancel?.()
toast.onClose?.()
removeToast(toast)
}}
>{toast.onCancel ? <div className={`${styles.toastCancel} ${textStyle}`}>cancel</div> : <div className={`${styles.toastClose} ${textStyle}`}>X</div>}
</Button>
</div>
</ToastBody>
</Toast>
)
})}
</ToastContainer>
{children}
</ToastContext.Provider>
)
}
export const useToast = () => useContext(ToastContext)