Files
rustycms/admin-ui/src/lib/EnvironmentContext.tsx
Peter Meier a1e0287e6d
Some checks failed
Deploy to Server / deploy (push) Failing after 37s
Instant env switch: invalidate all queries on environment change
EnvironmentContext holds current env as React state. On change,
QueryInvalidatorOnEnvChange calls queryClient.invalidateQueries() so
all content/list pages refetch immediately without manual reload.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 22:39:57 +01:00

38 lines
933 B
TypeScript

"use client";
import { createContext, useContext, useState, useEffect, type ReactNode } from "react";
import { getCurrentEnvironment, setCurrentEnvironment } from "@/lib/api";
type EnvironmentContextType = {
environment: string | null;
setEnvironment: (env: string) => void;
};
const EnvironmentContext = createContext<EnvironmentContextType>({
environment: null,
setEnvironment: () => {},
});
export function EnvironmentProvider({ children }: { children: ReactNode }) {
const [environment, setEnv] = useState<string | null>(null);
useEffect(() => {
setEnv(getCurrentEnvironment());
}, []);
function setEnvironment(env: string) {
setCurrentEnvironment(env);
setEnv(env);
}
return (
<EnvironmentContext.Provider value={{ environment, setEnvironment }}>
{children}
</EnvironmentContext.Provider>
);
}
export function useEnvironment() {
return useContext(EnvironmentContext);
}