mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-04 08:05:42 +00:00 
			
		
		
		
	refactor: rename site to frontend
This commit is contained in:
		
							
								
								
									
										42
									
								
								frontend/src/context/app-context.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								frontend/src/context/app-context.tsx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,42 @@
 | 
			
		||||
import { useQuery } from "@tanstack/react-query";
 | 
			
		||||
import React, { createContext, useContext } from "react";
 | 
			
		||||
import axios from "axios";
 | 
			
		||||
import { AppContextSchemaType } from "../schemas/app-context-schema";
 | 
			
		||||
 | 
			
		||||
const AppContext = createContext<AppContextSchemaType | null>(null);
 | 
			
		||||
 | 
			
		||||
export const AppContextProvider = ({
 | 
			
		||||
  children,
 | 
			
		||||
}: {
 | 
			
		||||
  children: React.ReactNode;
 | 
			
		||||
}) => {
 | 
			
		||||
  const {
 | 
			
		||||
    data: userContext,
 | 
			
		||||
    isLoading,
 | 
			
		||||
    error,
 | 
			
		||||
  } = useQuery({
 | 
			
		||||
    queryKey: ["appContext"],
 | 
			
		||||
    queryFn: async () => {
 | 
			
		||||
      const res = await axios.get("/api/app");
 | 
			
		||||
      return res.data;
 | 
			
		||||
    },
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  if (error && !isLoading) {
 | 
			
		||||
    throw error;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (
 | 
			
		||||
    <AppContext.Provider value={userContext}>{children}</AppContext.Provider>
 | 
			
		||||
  );
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const useAppContext = () => {
 | 
			
		||||
  const context = useContext(AppContext);
 | 
			
		||||
 | 
			
		||||
  if (context === null) {
 | 
			
		||||
    throw new Error("useAppContext must be used within an AppContextProvider");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return context;
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user