Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 12x 12x 12x 42x 42x 42x 42x 42x 42x 42x 22x 22x 22x 21x 21x 21x 21x 21x 21x 21x 1x 1x 22x 42x 21x 42x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 42x 1x 42x 21x 21x 12x 7x 7x 7x | import {
createContext,
useContext,
useEffect,
useState,
useCallback,
type PropsWithChildren,
} from 'react'
import { useLocation, useParams } from 'react-router-dom'
import {
initWhiteLabeling,
loadConfigs,
loadAssets,
loadTheme,
type EventConfig,
} from '@fundraising/white-labeling'
import { VITE_API_URL } from '@core/lib/api'
import { syncLocales } from '@core/lib/i18n'
import { Loader2 } from 'lucide-react'
interface AppConfigContextType {
config: EventConfig
isLoading: boolean
error: Error | null
refreshConfig: () => Promise<void>
}
// Initial state as fallback (synchronous defaults)
const INITIAL_CONFIG: EventConfig = {
...loadConfigs(),
theme: {
assets: loadAssets(),
variables: loadTheme(), // loads default theme
},
}
const AppConfigContext = createContext<AppConfigContextType>({
config: INITIAL_CONFIG,
isLoading: true,
error: null,
refreshConfig: async () => {},
})
export const AppConfigProvider = ({
children,
slug: slugProp,
}: PropsWithChildren<{ slug?: string }>) => {
const params = useParams<{ slug: string }>()
const slug = slugProp || params.slug
const [config, setConfig] = useState<EventConfig>(INITIAL_CONFIG)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<Error | null>(null)
const location = useLocation()
const loadConfig = useCallback(
async ({ isInitial = true } = {}) => {
if (isInitial) setIsLoading(true)
try {
// 1. Initialize DB Store (Async fetch from API)
await initWhiteLabeling(VITE_API_URL, slug)
// 2. Load Domain Specific Configs (Sync read from store)
const baseConfig = loadConfigs()
const assets = loadAssets()
const themeVars = loadTheme(true) // Load and Apply CSS variables to :root
// 3. Compose final configuration object
const fullConfig: EventConfig = {
...baseConfig,
theme: {
assets,
variables: themeVars,
},
}
// 4. Apply Dynamic Locales
syncLocales()
setConfig(fullConfig)
setError(null)
} catch (err) {
console.error('Failed to initialize app config:', err)
setError(err instanceof Error ? err : new Error('Unknown error'))
} finally {
if (isInitial) setIsLoading(false)
}
},
[slug],
)
useEffect(() => {
loadConfig({ isInitial: true })
}, [slug, location.pathname, loadConfig]) // React to path changes to enforce correct config (event vs global)
// Apply Favicon
useEffect(() => {
const faviconUrl = config.theme?.assets?.favicon
if (faviconUrl) {
const linkCallback = (link: HTMLLinkElement) => {
link.href = faviconUrl
}
let link = document.querySelector("link[rel~='icon']") as HTMLLinkElement
Eif (!link) {
link = document.createElement('link')
link.rel = 'icon'
document.getElementsByTagName('head')[0].appendChild(link)
}
linkCallback(link)
}
}, [config.theme?.assets?.favicon])
const refreshConfig = async () => {
await loadConfig({ isInitial: false })
}
if (isLoading) {
return (
<div className="flex items-center justify-center h-screen w-screen bg-muted/20">
<Loader2 className="w-10 h-10 animate-spin text-primary opacity-50" />
</div>
)
}
return (
<AppConfigContext.Provider value={{ config, isLoading, error, refreshConfig }}>
{children}
</AppConfigContext.Provider>
)
}
export const useAppConfig = () => {
const context = useContext(AppConfigContext)
Iif (context === undefined) {
throw new Error('useAppConfig must be used within an AppConfigProvider')
}
return context
}
|