mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
Initial push
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import React from 'react'
|
||||
import { useDispatch, useSelector } from 'react-redux'
|
||||
import { useSnackbar } from 'notistack'
|
||||
import { removeSnackbar } from 'store/actions'
|
||||
|
||||
let displayed = []
|
||||
|
||||
const useNotifier = () => {
|
||||
const dispatch = useDispatch()
|
||||
const notifier = useSelector((state) => state.notifier)
|
||||
const { notifications } = notifier
|
||||
|
||||
const { enqueueSnackbar, closeSnackbar } = useSnackbar()
|
||||
|
||||
const storeDisplayed = (id) => {
|
||||
displayed = [...displayed, id]
|
||||
}
|
||||
|
||||
const removeDisplayed = (id) => {
|
||||
displayed = [...displayed.filter((key) => id !== key)]
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
notifications.forEach(({ key, message, options = {}, dismissed = false }) => {
|
||||
if (dismissed) {
|
||||
// dismiss snackbar using notistack
|
||||
closeSnackbar(key)
|
||||
return
|
||||
}
|
||||
|
||||
// do nothing if snackbar is already displayed
|
||||
if (displayed.includes(key)) return
|
||||
|
||||
// display snackbar using notistack
|
||||
enqueueSnackbar(message, {
|
||||
key,
|
||||
...options,
|
||||
onClose: (event, reason, myKey) => {
|
||||
if (options.onClose) {
|
||||
options.onClose(event, reason, myKey)
|
||||
}
|
||||
},
|
||||
onExited: (event, myKey) => {
|
||||
// remove this snackbar from redux store
|
||||
dispatch(removeSnackbar(myKey))
|
||||
removeDisplayed(myKey)
|
||||
}
|
||||
})
|
||||
|
||||
// keep track of snackbars that we've displayed
|
||||
storeDisplayed(key)
|
||||
})
|
||||
}, [notifications, closeSnackbar, enqueueSnackbar, dispatch])
|
||||
}
|
||||
|
||||
export default useNotifier
|
||||
Reference in New Issue
Block a user