2025-03-09 20:22:48 +08:00
|
|
|
import {
|
|
|
|
|
createContext,
|
|
|
|
|
useState,
|
|
|
|
|
useContext,
|
|
|
|
|
type PropsWithChildren,
|
|
|
|
|
type Dispatch,
|
|
|
|
|
type SetStateAction,
|
|
|
|
|
} from 'react'
|
2025-03-10 12:21:08 +08:00
|
|
|
import { z } from 'zod'
|
2025-03-09 20:22:48 +08:00
|
|
|
|
2025-03-10 12:21:08 +08:00
|
|
|
export const uploadCategorySchema = z
|
|
|
|
|
.enum(['featured_image', 'ads', 'content', 'profile_picture'])
|
|
|
|
|
.optional()
|
|
|
|
|
|
2025-03-10 14:32:32 +08:00
|
|
|
export type TUpload = z.infer<typeof uploadCategorySchema>
|
2025-03-09 20:22:48 +08:00
|
|
|
|
|
|
|
|
type AdminContextProperties = {
|
|
|
|
|
isUploadOpen: TUpload
|
|
|
|
|
setIsUploadOpen: Dispatch<SetStateAction<TUpload>>
|
2025-03-10 12:21:08 +08:00
|
|
|
uploadedFile?: string
|
|
|
|
|
setUploadedFile: Dispatch<SetStateAction<string | undefined>>
|
2025-03-09 20:22:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AdminContext = createContext<AdminContextProperties | undefined>(
|
|
|
|
|
undefined,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const AdminProvider = ({ children }: PropsWithChildren) => {
|
|
|
|
|
const [isUploadOpen, setIsUploadOpen] = useState<TUpload>()
|
2025-03-10 12:21:08 +08:00
|
|
|
const [uploadedFile, setUploadedFile] = useState<string | undefined>()
|
2025-03-09 20:22:48 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AdminContext.Provider
|
|
|
|
|
value={{
|
|
|
|
|
isUploadOpen,
|
|
|
|
|
setIsUploadOpen,
|
2025-03-10 12:21:08 +08:00
|
|
|
uploadedFile,
|
|
|
|
|
setUploadedFile,
|
2025-03-09 20:22:48 +08:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</AdminContext.Provider>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useAdminContext = (): AdminContextProperties => {
|
|
|
|
|
const context = useContext(AdminContext)
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error('useAdminContext must be used within a AdminProvider')
|
|
|
|
|
}
|
|
|
|
|
return context
|
|
|
|
|
}
|