|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { createContext, useContext, useState } from "react" |
|
4
|
|
|
5
|
export type PageLoadingContextType = { |
|
6
|
isLoading: boolean |
|
7
|
setIsLoading: React.Dispatch<React.SetStateAction<boolean>> |
|
8
|
} |
|
9
|
|
|
10
|
const PageLoadingContext = createContext<PageLoadingContextType | null>(null) |
|
11
|
|
|
12
|
export type PageLoadingProviderProps = { |
|
13
|
children?: React.ReactNode |
|
14
|
} |
|
15
|
|
|
16
|
export const PageLoadingProvider = ({ children }: PageLoadingProviderProps) => { |
|
17
|
const [isLoading, setIsLoading] = useState(true) |
|
18
|
|
|
19
|
return ( |
|
20
|
<PageLoadingContext.Provider |
|
21
|
value={{ |
|
22
|
isLoading, |
|
23
|
setIsLoading, |
|
24
|
}} |
|
25
|
> |
|
26
|
{children} |
|
27
|
</PageLoadingContext.Provider> |
|
28
|
) |
|
29
|
} |
|
30
|
|
|
31
|
export const usePageLoading = (): PageLoadingContextType => { |
|
32
|
const context = useContext(PageLoadingContext) |
|
33
|
|
|
34
|
if (!context) { |
|
35
|
throw new Error("usePageLoading must be used inside a PageLoadingProvider") |
|
36
|
} |
|
37
|
|
|
38
|
return context |
|
39
|
} |