first project version mrDecor
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
'use client'
|
||||
|
||||
import React, { createContext, useContext, useMemo } from 'react'
|
||||
import type { SiteContent } from '@/lib/content'
|
||||
|
||||
const ContentContext = createContext<SiteContent | null>(null)
|
||||
|
||||
export function ContentProvider({
|
||||
initialContent,
|
||||
children,
|
||||
}: {
|
||||
initialContent: SiteContent
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const value = useMemo(() => initialContent, [initialContent])
|
||||
return (
|
||||
<ContentContext.Provider value={value}>
|
||||
{children}
|
||||
</ContentContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useContent(): SiteContent {
|
||||
const ctx = useContext(ContentContext)
|
||||
if (!ctx) {
|
||||
throw new Error('useContent must be used within ContentProvider')
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
/**
|
||||
* استفاده در کامپوننتهایی که خارج از ContentProvider هستند (مثلاً سرور).
|
||||
* در آن صورت از getContentSync() یا getContent() استفاده کنید.
|
||||
*/
|
||||
export function useContentOptional(): SiteContent | null {
|
||||
return useContext(ContentContext)
|
||||
}
|
||||
Reference in New Issue
Block a user