'use client'; import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react'; type MobileChromeState = { title: string | null; primaryAction: ReactNode | null; showBackButton: boolean; }; type MobileChromeApi = MobileChromeState & { setChrome: (next: Partial) => void; }; const MobileChromeContext = createContext(null); export function MobileLayoutProvider({ children }: { children: ReactNode }) { const [state, setState] = useState({ title: null, primaryAction: null, showBackButton: false, }); const setChrome = useCallback((next: Partial) => { setState((prev) => ({ ...prev, ...next })); }, []); const value = useMemo(() => ({ ...state, setChrome }), [state, setChrome]); return {children}; } const NOOP_SET_CHROME = () => {}; const NOOP_CHROME: MobileChromeApi = { title: null, primaryAction: null, showBackButton: false, setChrome: NOOP_SET_CHROME, }; /** * Page-level hook to push a title / back-button / primary action into the * mobile topbar. Both the desktop and mobile shells render the same * children, so this hook MUST be safe to call from either tree. When the * provider is missing (desktop tree), it returns a no-op so callers don't * have to branch on shell type. */ export function useMobileChrome() { const ctx = useContext(MobileChromeContext); return ctx ?? NOOP_CHROME; }