feat: app context

This commit is contained in:
Stavros
2025-05-09 16:49:54 +03:00
parent 0880152b48
commit 31a7b0ff06
14 changed files with 158 additions and 37 deletions

View File

@@ -0,0 +1,37 @@
import { AppContextSchema } from "@/schemas/app-context-schema";
import { createContext, useContext } from "react";
import { useQuery } from "@tanstack/react-query";
import axios from "axios";
const AppContext = createContext<AppContextSchema | null>(null);
export const AppContextProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const { isPending, isError, data, error } = useQuery({
queryKey: ["status"],
queryFn: () => axios.get("/api/app").then((res) => res.data),
});
if (isPending) {
return;
}
if (isError) {
throw error;
}
return <AppContext.Provider value={data}>{children}</AppContext.Provider>;
};
export const useAppContext = () => {
const context = useContext(AppContext);
if (!context) {
throw new Error("useAppContext must be used within an AppContextProvider");
}
return context;
};