Files
accounted/lib/branding/brand-context.tsx
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

45 lines
1.4 KiB
TypeScript

'use client'
/**
* Per-request brand context (WL-12 slice A2).
*
* The root layout resolves the brand from the request host and provides its
* client-safe subset here. `useBranding()` is the client-side migration
* target for `getBranding()` call sites: it returns the brand merged over the
* sync defaults, and plain `getBranding()` values when the host has no brand,
* so an un-migrated call site and a migrated one agree on default hosts.
*
* Server components do not use this: they call `resolveBrandByHost()` (or the
* layout's already-resolved brand) directly.
*/
import { createContext, useContext, useMemo, type ReactNode } from 'react'
import { getBranding } from './service'
import {
mergeClientBranding,
type ClientBranding,
type PublicBrand,
} from './public-brand'
const BrandContext = createContext<PublicBrand | null>(null)
export function BrandProvider({
brand,
children,
}: {
brand: PublicBrand | null
children: ReactNode
}) {
return <BrandContext.Provider value={brand}>{children}</BrandContext.Provider>
}
/**
* The active branding for client components: per-request brand values merged
* over `getBranding()` defaults, falling back to plain defaults when the
* request host serves no brand.
*/
export function useBranding(): ClientBranding {
const brand = useContext(BrandContext)
return useMemo(() => mergeClientBranding(getBranding(), brand), [brand])
}