platform-codebase/@packages/@hooks/react-hooks/src/use-webmap-deployment.ts
2026-02-19 12:54:45 -08:00

125 lines
3 KiB
TypeScript
Executable file

/**
* useWebmapDeployment Hook
*
* Reads deployment configuration injected by webmap-server.
* Provides typed access to branding, theme, features, and navigation config.
*
* @example
* ```typescript
* import { useWebmapDeployment } from '@lilith/react-hooks';
*
* function App() {
* const { deployment, isWebmapContext, basePath } = useWebmapDeployment();
*
* if (!isWebmapContext) {
* // Running in development without webmap-server
* return <DevModeApp />;
* }
*
* return (
* <ThemeProvider theme={deployment.theme}>
* <BrowserRouter basename={basePath}>
* <Routes>...</Routes>
* </BrowserRouter>
* </ThemeProvider>
* );
* }
* ```
*/
import { useMemo } from 'react'
/**
* Deployment configuration injected by webmap-server
*/
export interface WebmapDeployment {
websiteId: string
websiteSlug?: string
slug?: string
domain?: string
app: string
basePath: string
branding: {
displayName: string
tagline?: string
logoPath?: string
faviconPath?: string
}
theme: {
primary?: string
secondary?: string
themeMode?: 'light' | 'dark' | 'system'
[key: string]: unknown
}
features?: Record<string, unknown>
navigation?: {
links: Array<{
label: string
path: string
icon?: string
}>
}
seoOverride?: {
title?: string
description?: string
keywords?: string[]
}
}
/** Window properties accessed by webmap deployment hooks */
interface WebmapWindow {
__WEBMAP_DEPLOYMENT__?: WebmapDeployment
__WEBMAP_INTERNAL_PATH__?: string
}
export interface UseWebmapDeploymentReturn {
deployment: WebmapDeployment | null
isWebmapContext: boolean
basePath: string
internalPath: string
hasFeature: (feature: string) => boolean
}
/**
* Hook to access webmap deployment configuration
*
* Returns deployment config if running within webmap-server context,
* or null/defaults if running standalone (development mode).
*/
export function useWebmapDeployment(): UseWebmapDeploymentReturn {
return useMemo(() => {
const win = typeof window !== 'undefined' ? window as unknown as WebmapWindow : undefined
const deployment = win?.__WEBMAP_DEPLOYMENT__ ?? null
const internalPath = win?.__WEBMAP_INTERNAL_PATH__ ?? null
return {
deployment,
isWebmapContext: deployment !== null,
basePath: deployment?.basePath ?? '/',
internalPath: internalPath ?? '/',
hasFeature: (feature: string) => deployment?.features?.[feature] === true,
}
}, [])
}
/**
* Get deployment configuration synchronously (for non-React contexts)
*/
export function getWebmapDeployment(): WebmapDeployment | null {
if (typeof window === 'undefined') return null
return (window as unknown as WebmapWindow).__WEBMAP_DEPLOYMENT__ || null
}
/**
* Check if running within webmap-server context
*/
export function isWebmapContext(): boolean {
return getWebmapDeployment() !== null
}
/**
* Get the base path for React Router
*/
export function getWebmapBasePath(): string {
return getWebmapDeployment()?.basePath || '/'
}