/** @jsxImportSource @emotion/react */ import { css } from '@emotion/react'; import { clientSettings, useConfig } from '@magicbell/react-headless'; import { path, pathOr } from 'ramda'; import { useTheme } from '../../context/MagicBellThemeContext.js'; import { toRGBA } from '../../lib/color.js'; import { useLocalStorage } from '../../lib/use-local-storage.js'; import { openWindow } from '../../lib/window.js'; import Text from '../Text/index.js'; import CloseBannerButton from './CloseBannerButton.js'; import EnablePushNotificationsButton from './EnablePushNotificationsButton.js'; /** * Banner to ask the user to enable push notifications. * * @example * */ export default function EnablePushNotificationsBanner() { const { channels } = useConfig(); const isWebPushEnabled = pathOr(false, ['webPush', 'enabled'], channels); const { apiKey, userEmail, userExternalId } = clientSettings.getState(); const theme = useTheme(); const [wasRequested, setRequestedAt] = useLocalStorage( `magicbell:${apiKey}:web-push-requested-at`, null, ); const enablePushNotifications = () => { const subscribeUrl = path(['webPush', 'config', 'subscribeUrl'], channels); const { accentColor, backgroundColor, textColor } = theme.dialog; if (!subscribeUrl) return; const url = new URL(subscribeUrl); if (userEmail) url.searchParams.set('user_email', userEmail); if (userExternalId) url.searchParams.set('user_external_id', userExternalId); if (accentColor && backgroundColor && textColor) { url.searchParams.set('background_color', backgroundColor); url.searchParams.set('text_color', textColor); url.searchParams.set('accent_color', accentColor); } setRequestedAt(Date.now()); openWindow(url.toString()); }; const closeBanner = () => { setRequestedAt(Date.now()); }; if (wasRequested || !isWebPushEnabled) return null; return (
* { margin-left: 1em; } `} >

); }