File size: 1,254 Bytes
136f9cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import { createContext, useContext, useState, ReactNode } from "react";
import { LoadingScreen } from "@/components/ui/loading-screen";
interface LoadingContextType {
isLoading: boolean;
message: string;
startLoading: (message?: string) => void;
stopLoading: () => void;
}
const LoadingContext = createContext<LoadingContextType | undefined>(undefined);
interface LoadingProviderProps {
children: ReactNode;
}
export function LoadingProvider({ children }: LoadingProviderProps) {
const [isLoading, setIsLoading] = useState(false);
const [message, setMessage] = useState("Loading...");
const startLoading = (message = "Loading...") => {
setMessage(message);
setIsLoading(true);
};
const stopLoading = () => {
setIsLoading(false);
};
return (
<LoadingContext.Provider value={{ isLoading, message, startLoading, stopLoading }}>
{children}
{isLoading && <LoadingScreen message={message} />}
</LoadingContext.Provider>
);
}
// eslint-disable-next-line react-refresh/only-export-components
export function useLoading() {
const context = useContext(LoadingContext);
if (context === undefined) {
throw new Error("useLoading must be used within a LoadingProvider");
}
return context;
} |