-
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import { createContext, useContext, useEffect, useState } from 'react'
|
||||
import api from '../services/api'
|
||||
import { CONTENT_DEFAULTS } from '../content/defaults'
|
||||
|
||||
const ContentContext = createContext({ blocks: {}, loaded: false })
|
||||
|
||||
/** Loads admin-editable content blocks once; sections fall back to the Figma copy. */
|
||||
export function ContentProvider({ children }) {
|
||||
const [state, setState] = useState({ blocks: {}, loaded: false })
|
||||
useEffect(() => {
|
||||
let alive = true
|
||||
api
|
||||
.get('/content')
|
||||
.then((r) => alive && setState({ blocks: r.data || {}, loaded: true }))
|
||||
.catch(() => alive && setState({ blocks: {}, loaded: true }))
|
||||
return () => {
|
||||
alive = false
|
||||
}
|
||||
}, [])
|
||||
return <ContentContext.Provider value={state}>{children}</ContentContext.Provider>
|
||||
}
|
||||
|
||||
// eslint-disable-next-line react-refresh/only-export-components
|
||||
export function useContent(key) {
|
||||
const { blocks } = useContext(ContentContext)
|
||||
const defaults = CONTENT_DEFAULTS[key] || {}
|
||||
const override = blocks[key]
|
||||
if (!override || typeof override !== 'object') return defaults
|
||||
// Shallow merge: any field the admin left empty keeps the design default.
|
||||
const merged = { ...defaults }
|
||||
for (const [k, v] of Object.entries(override)) {
|
||||
if (v === null || v === undefined || v === '') continue
|
||||
if (Array.isArray(v) && v.length === 0) continue
|
||||
merged[k] = v
|
||||
}
|
||||
return merged
|
||||
}
|
||||
Reference in New Issue
Block a user