{"version":3,"file":"useNotificationState.cjs","sources":["../../../../src/lib/Notification/useNotificationState.tsx"],"sourcesContent":["import {useCallback, useEffect, useState} from 'react';\n\nimport {useNotificationContext} from './NotificationContext.ts';\nimport type {NotificationProps, NotificationState} from './NotificationReducer.ts';\nimport {Actions} from './NotificationReducer.ts';\nimport {useNotificationProps} from './useNotificationProps.ts';\nimport {useNotificationInstance} from './useNotificationInstance.ts';\n\nconst useSelector = <TResult,>(callback: (arg0: NotificationState) => TResult) => {\n    const {state} = useNotificationContext();\n    return callback(state);\n};\n\nconst createNotification = (props: NotificationProps) => {\n    return new window.Notification(props.title, {\n        icon: props.icon,\n        body: props.body,\n        requireInteraction: props.requireInteraction,\n    });\n};\n\nexport const useNotificationState = (id: string) => {\n    const {dispatch} = useNotificationContext();\n    const instance = useNotificationInstance(id);\n    const notificationProps = useNotificationProps(id);\n    const isOpen = useSelector((state: NotificationState) =>\n        state.open.some(notificationId => notificationId === id)\n    );\n    const [permission, setPermission] = useState<NotificationPermission>();\n\n    const handleSelfClose = useCallback(() => {\n        dispatch({\n            type: Actions.NOTIFICATION_CLOSE,\n            id,\n        });\n    }, [dispatch, id]);\n\n    useEffect(() => {\n        instance?.addEventListener('close', handleSelfClose);\n        return () => {\n            instance?.removeEventListener('close', handleSelfClose);\n        };\n    }, [handleSelfClose, instance]);\n\n    const openNotification = useCallback(() => {\n        const currentPermission = window?.Notification?.permission;\n\n        /* Partially supported in Firefox. The new notification will not re-appear */\n        if (isOpen) {\n            instance?.close();\n        }\n        if (currentPermission === 'granted') {\n            dispatch({\n                type: Actions.NOTIFICATION_OPEN,\n                id,\n                instance: createNotification(notificationProps),\n            });\n            setPermission(currentPermission);\n        } else if (currentPermission !== 'denied') {\n            window?.Notification?.requestPermission().then(nextPermission => {\n                setPermission(nextPermission);\n                if (nextPermission === 'granted') {\n                    dispatch({\n                        type: Actions.NOTIFICATION_OPEN,\n                        id,\n                        instance: createNotification(notificationProps),\n                    });\n                }\n            });\n        } else if (currentPermission === 'denied') {\n            setPermission(currentPermission);\n        }\n    }, [dispatch, id, instance, isOpen, notificationProps]);\n\n    const closeNotification = useCallback(() => {\n        instance?.close();\n        dispatch({type: Actions.NOTIFICATION_CLOSE, id});\n    }, [dispatch, id, instance]);\n\n    return {\n        /**\n         * Show a notification to the user.\n         * Warning! Will not work from a component scope. Needs to be run as an effect\n         * @example\n         * const {openNotificaton} = useNotificationState(id);\n         * const handleClick = useCallback(() => {\n         *     openNotification();\n         * }, [openNotification]);\n         */\n        openNotification,\n        /**\n         * Hide a notification.\n         * Warning! Will not work from a component scope. Needs to be run as an effect\n         * @example\n         * const {closeNotification} = useNotificationState(id);\n         * const handleClick = useCallback(() => {\n         *     closeNotification();\n         * }, [closeNotification]);\n         */\n        closeNotification,\n        /** Current visibility of notification */\n        isOpen,\n        /**\n         * Indicates the current permission granted by the user for the current origin to display web notifications\n         * @see https://developer.mozilla.org/en-US/docs/Web/API/Notification/permission_static\n         */\n        permission,\n    };\n};\n"],"names":["useSelector","callback","state","useNotificationContext","createNotification","props","useNotificationState","id","dispatch","instance","useNotificationInstance","notificationProps","useNotificationProps","isOpen","notificationId","permission","setPermission","useState","handleSelfClose","useCallback","Actions","useEffect","openNotification","currentPermission","nextPermission","closeNotification"],"mappings":"uRAQMA,EAAyBC,GAAmD,CAC9E,KAAM,CAAC,MAAAC,CAAA,EAASC,yBAAA,EAChB,OAAOF,EAASC,CAAK,CACzB,EAEME,EAAsBC,GACjB,IAAI,OAAO,aAAaA,EAAM,MAAO,CACxC,KAAMA,EAAM,KACZ,KAAMA,EAAM,KACZ,mBAAoBA,EAAM,kBAAA,CAC7B,EAGQC,EAAwBC,GAAe,CAChD,KAAM,CAAC,SAAAC,CAAA,EAAYL,yBAAA,EACbM,EAAWC,EAAAA,wBAAwBH,CAAE,EACrCI,EAAoBC,EAAAA,qBAAqBL,CAAE,EAC3CM,EAASb,EAAaE,GACxBA,EAAM,KAAK,KAAKY,GAAkBA,IAAmBP,CAAE,CAAA,EAEr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