38 lines
1001 B
TypeScript
38 lines
1001 B
TypeScript
'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)
|
|
}
|