import type { ReactNode } from 'react'; import { useEffect } from '@wordpress/element'; import { useQuery } from '@tanstack/react-query'; import { Link, useLocation } from 'react-router-dom'; import { AlertTriangle, Settings } from 'lucide-react'; import { getBootstrapSettings, getSettings, hasApiToken, } from '../lib/settings'; import { attachHoverPrefetch } from '../lib/prefetch-nav'; import Masthead from './masthead'; const items = [ { to: '/dashboard', label: 'Dashboard' }, { to: '/subscribers', label: 'Subscribers' }, { to: '/segments', label: 'Segments' }, { to: '/notifications', label: 'Campaigns' }, { to: '/automations', label: 'Automations' }, { to: '/analytics', label: 'Analytics' }, { to: '/reports', label: 'Reports' }, { to: '/settings', label: 'Settings' }, ]; export default function Shell( { children }: { children: ReactNode } ) { const location = useLocation(); const settingsQuery = useQuery( { queryKey: [ 'np-settings' ], queryFn: getSettings, initialData: getBootstrapSettings(), } ); const activePath = location.pathname === '/' ? '/dashboard' : location.pathname || window.location.hash.replace( /^#/, '' ); const isSetupRoute = activePath.startsWith( '/setup' ); useEffect( () => { attachHoverPrefetch(); }, [] ); const currentItem = items.find( ( item ) => activePath === item.to || activePath.startsWith( `${ item.to }/` ) ); useEffect( () => { const menu = document.getElementById( 'toplevel_page_notifypress' ); if ( ! menu ) { return; } menu.querySelectorAll( 'li' ).forEach( ( item ) => { item.classList.remove( 'current' ); } ); menu.querySelectorAll( 'a' ).forEach( ( link ) => { link.classList.remove( 'current' ); } ); const activeRoute = currentItem?.to || '/dashboard'; const activeLink = activeRoute === '/dashboard' ? menu.querySelector< HTMLAnchorElement >( '.wp-submenu li.wp-first-item > a' ) : Array.from( menu.querySelectorAll< HTMLAnchorElement >( '.wp-submenu a' ) ).find( ( link ) => { const hashRoute = link.hash.replace( /^#/, '' ); return ( hashRoute === activeRoute || hashRoute.startsWith( `${ activeRoute }/` ) ); } ); if ( activeLink ) { activeLink.classList.add( 'current' ); activeLink.closest( 'li' )?.classList.add( 'current' ); } menu.classList.add( 'wp-has-current-submenu', 'wp-menu-open' ); menu.classList.remove( 'wp-not-current-submenu' ); menu .querySelector( 'a.wp-has-submenu' ) ?.classList.add( 'wp-has-current-submenu' ); }, [ currentItem ] ); if ( isSetupRoute ) { return (
{ children }
); } const showMissingTokenNotice = Boolean( settingsQuery.data?.setupCompleted ) && ! hasApiToken( settingsQuery.data ); return (
{ showMissingTokenNotice ? (

API token required

Connect NotifyPress or add an API token in Settings to sync this site.

Open Settings
) : null }
{ children }
); }