/** * Mailpuff's page: the frame Saddle used to draw, now Mailpuff's own. * * It reads the address (`tab`, `sub`, `view`, args), hands each screen the * same props Saddle did (`ui`, `kit`, `navigate`, `header`, `tab`, `sub`, * `view`, `args`), and draws the header bar (Create and Help, on every * page), the heading with the section's * page tabs (Settings: a side menu inside one panel), the notices, the * screen and the bottom slot a screen's bar lands in. `navigate` changes the address with pushState (Back works); * `header.drillIn( { title, back? } )` names the item an open view shows * and, optionally, where the heading's way back goes. * `header.narrow( { title, meta?, back } )` names what a section's own list * is narrowed to (a tag or a segment on Subscribers), so the heading reads * "Subscribers › Tutorials" with a short fact beside it; `narrow( null )` * clears it. The screen that sets it clears it when it goes. */ import { useCallback, useEffect, useMemo, useRef, useState, } from '@wordpress/element'; import { applyFilters } from '@wordpress/hooks'; import { BottomSlot, BottomSlotProvider, KitProvider, PageActionsProvider, PageActionsSlot, ui, } from '../kit'; import { useNewCampaign } from '../hooks/use-campaigns'; import { UiProvider } from '../runtime'; import Home from '../screens/Home'; import Campaigns from '../screens/Campaigns'; import { ContactsPage } from '../screens/Contacts'; import Help from '../screens/Help'; import Forms from '../screens/Forms'; import Welcome from '../screens/Welcome'; import { welcomeDue } from '../lib/welcome'; import { AutomaticSettings, BusinessSettings, EmailSettings, FieldsSettings, SpamSettings, McpSettings, PrivacySettings, SendingSettings, } from '../screens/Settings'; import { AppBar, PageHeading, PageTabs } from './chrome'; import { FieldGrid, SettingsFrame, SettingsPage, SettingsSection, } from './settings-layout'; import { useWpCommands } from './wp-commands'; import { useWpMenu } from './wp-menu'; import { locationUrl, MOVED, nextLocation, readLocation, sections, } from './nav'; import { Notices, SectionHeader } from './parts'; import SettingsForm from './SettingsForm'; const SCREENS = { home: { '': Home }, campaigns: { '': Campaigns }, contacts: { '': ContactsPage }, forms: { '': Forms }, help: { '': Help }, welcome: { '': Welcome }, settings: { sending: SendingSettings, business: BusinessSettings, fields: FieldsSettings, emails: EmailSettings, automatic: AutomaticSettings, spam: SpamSettings, privacy: PrivacySettings, mcp: McpSettings, }, }; // The frame's own parts a screen may need beyond `ui`: an add-on's settings // page is a SettingsPage like Mailpuff's, and its sub-pages are PageTabs. const kit = { FieldGrid, PageTabs, SectionHeader, SettingsForm, SettingsPage, SettingsSection, }; /** * The screens, with any an add-on (Mailpuff Pro) registers through the * `mailpuff.screens` filter: `{ [sectionId]: { [pageId or '']: Component } }`. * Read once, when the page starts. * * @return {Object} Section id => page id => component. */ function allScreens() { const screens = applyFilters( 'mailpuff.screens', SCREENS ); return screens && typeof screens === 'object' ? screens : SCREENS; } /** * Where the page opens: the address, except that Home opens the welcome * while first run is new or under way, and an old Groups or Segments * address opens Subscribers (the address says so too). * * @return {Object} Location. */ function firstLocation() { const loc = readLocation( window.location.search ); const asked = new URLSearchParams( window.location.search ).get( 'tab' ); if ( MOVED[ asked ] ) { window.history.replaceState( null, '', locationUrl( loc ) ); return loc; } if ( loc.tab === 'home' && ! loc.view && welcomeDue( window.mailpuffData?.onboarding ) ) { const next = { tab: 'welcome', sub: '', view: '', args: {} }; window.history.replaceState( null, '', locationUrl( next ) ); return next; } return loc; } function Frame() { const [ loc, setLoc ] = useState( firstLocation ); const [ drill, setDrill ] = useState( '' ); // What the section's list is narrowed to: `{ title, meta, back }`. const [ narrowed, setNarrowed ] = useState( null ); // Where the heading's way back goes, when the open view names one. const drillBack = useRef( null ); const screens = useMemo( allScreens, [] ); const current = useRef( loc ); current.current = loc; const navigate = useCallback( ( target ) => { const next = nextLocation( current.current, target ); window.history.pushState( null, '', locationUrl( next ) ); // Cleared here, not in an effect: a screen names its item in its own // effect, which runs before this component's. setDrill( '' ); drillBack.current = null; setLoc( next ); window.scrollTo( 0, 0 ); }, [] ); useEffect( () => { const onPop = () => { setDrill( '' ); drillBack.current = null; setLoc( readLocation( window.location.search ) ); }; window.addEventListener( 'popstate', onPop ); return () => window.removeEventListener( 'popstate', onPop ); }, [] ); // The sections, read once: add-ons register theirs before the app mounts. const all = useMemo( () => sections(), [] ); const creating = useNewCampaign(); useWpCommands( { sections: all, navigate, newCampaign: creating.start } ); // Where a page's own actions land (`PageActions`): beside the title, or // beside Home's greeting (`header.actions`). const actions = useMemo( () => , [] ); const header = useMemo( () => ( { drillIn: ( { title, back } = {} ) => { setDrill( title || '' ); drillBack.current = typeof back === 'function' ? back : null; }, narrow: ( named ) => setNarrowed( named?.title ? named : null ), actions, } ), [ actions ] ); const section = all.find( ( s ) => s.id === loc.tab ) || all[ 0 ]; const pages = screens[ section.id ] || {}; const Screen = pages[ loc.sub ] || pages[ '' ] || Home; const viewOpen = !! loc.view; // The welcome is a page of its own: no header bar, heading or notices. const focused = section.id === 'welcome'; // Home greets instead of naming itself. const titled = ( section.id !== 'home' && ! focused ) || viewOpen; // Settings lists its pages in a side menu inside one panel. const sideMenu = section.id === 'settings' && ! viewOpen; const screen = ( ); useWpMenu( section.id, navigate ); return (
{ ! focused && } { /* A div, not
: WordPress's #wpbody-content is the page's main region already. */ }
{ titled && ( drillBack.current ? drillBack.current() : navigate( { view: '', args: {} } ) } actions={ actions } /> ) } { ! viewOpen && ! sideMenu && section.pages.length > 0 && ( navigate( { sub } ) } /> ) } { ! focused && } { sideMenu ? ( navigate( { sub } ) } > { screen } ) : ( screen ) }
); } export default function App() { return ( ); }