2026-04-29 14:11:27 +02:00
|
|
|
'use client';
|
|
|
|
|
|
2026-05-01 15:46:32 +02:00
|
|
|
import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from 'react';
|
2026-04-29 14:11:27 +02:00
|
|
|
|
|
|
|
|
type MobileChromeState = {
|
|
|
|
|
title: string | null;
|
|
|
|
|
primaryAction: ReactNode | null;
|
|
|
|
|
showBackButton: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type MobileChromeApi = MobileChromeState & {
|
|
|
|
|
setChrome: (next: Partial<MobileChromeState>) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MobileChromeContext = createContext<MobileChromeApi | null>(null);
|
|
|
|
|
|
|
|
|
|
export function MobileLayoutProvider({ children }: { children: ReactNode }) {
|
|
|
|
|
const [state, setState] = useState<MobileChromeState>({
|
|
|
|
|
title: null,
|
|
|
|
|
primaryAction: null,
|
|
|
|
|
showBackButton: false,
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-01 15:46:32 +02:00
|
|
|
const setChrome = useCallback((next: Partial<MobileChromeState>) => {
|
|
|
|
|
setState((prev) => ({ ...prev, ...next }));
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const value = useMemo<MobileChromeApi>(() => ({ ...state, setChrome }), [state, setChrome]);
|
2026-04-29 14:11:27 +02:00
|
|
|
|
|
|
|
|
return <MobileChromeContext.Provider value={value}>{children}</MobileChromeContext.Provider>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 15:46:32 +02:00
|
|
|
const NOOP_SET_CHROME = () => {};
|
|
|
|
|
const NOOP_CHROME: MobileChromeApi = {
|
|
|
|
|
title: null,
|
|
|
|
|
primaryAction: null,
|
|
|
|
|
showBackButton: false,
|
|
|
|
|
setChrome: NOOP_SET_CHROME,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-29 14:11:27 +02:00
|
|
|
/**
|
|
|
|
|
* Page-level hook to push a title / back-button / primary action into the
|
2026-05-01 15:46:32 +02:00
|
|
|
* 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.
|
2026-04-29 14:11:27 +02:00
|
|
|
*/
|
|
|
|
|
export function useMobileChrome() {
|
|
|
|
|
const ctx = useContext(MobileChromeContext);
|
2026-05-01 15:46:32 +02:00
|
|
|
return ctx ?? NOOP_CHROME;
|
2026-04-29 14:11:27 +02:00
|
|
|
}
|