Instant env switch: invalidate all queries on environment change
Some checks failed
Deploy to Server / deploy (push) Failing after 37s
Some checks failed
Deploy to Server / deploy (push) Failing after 37s
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>
This commit is contained in:
@@ -3,6 +3,18 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useState, useEffect } from "react";
|
||||
import { syncStoredApiKey } from "@/lib/api";
|
||||
import { EnvironmentProvider, useEnvironment } from "@/lib/EnvironmentContext";
|
||||
|
||||
/** Invalidates all queries whenever the active environment changes. */
|
||||
function QueryInvalidatorOnEnvChange({ queryClient }: { queryClient: QueryClient }) {
|
||||
const { environment } = useEnvironment();
|
||||
useEffect(() => {
|
||||
if (environment != null) {
|
||||
void queryClient.invalidateQueries();
|
||||
}
|
||||
}, [environment, queryClient]);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function Providers({ children }: { children: React.ReactNode }) {
|
||||
useEffect(() => {
|
||||
@@ -17,7 +29,13 @@ export function Providers({ children }: { children: React.ReactNode }) {
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
<QueryClientProvider client={client}>
|
||||
<EnvironmentProvider>
|
||||
<QueryInvalidatorOnEnvChange queryClient={client} />
|
||||
{children}
|
||||
</EnvironmentProvider>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
|
||||
60
admin-ui/src/components/EnvironmentSwitcher.tsx
Normal file
60
admin-ui/src/components/EnvironmentSwitcher.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { fetchEnvironments } from "@/lib/api";
|
||||
import { useEnvironment } from "@/lib/EnvironmentContext";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
|
||||
export function EnvironmentSwitcher() {
|
||||
const t = useTranslations("EnvironmentSwitcher");
|
||||
const { environment, setEnvironment } = useEnvironment();
|
||||
const { data } = useQuery({
|
||||
queryKey: ["environments"],
|
||||
queryFn: fetchEnvironments,
|
||||
});
|
||||
|
||||
const environments = data?.environments ?? [];
|
||||
const defaultEnv = data?.default ?? null;
|
||||
|
||||
useEffect(() => {
|
||||
if (environments.length > 0 && !environment && defaultEnv) {
|
||||
setEnvironment(defaultEnv);
|
||||
}
|
||||
}, [environments.length, environment, defaultEnv, setEnvironment]);
|
||||
|
||||
if (environments.length === 0) return null;
|
||||
|
||||
const value = environment ?? defaultEnv ?? environments[0];
|
||||
|
||||
const handleChange = (next: string) => {
|
||||
setEnvironment(next);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="shrink-0 text-xs font-medium text-gray-500 uppercase tracking-wide">
|
||||
{t("label")}:
|
||||
</span>
|
||||
<Select value={value} onValueChange={handleChange}>
|
||||
<SelectTrigger className="h-8 w-full min-w-0 max-w-[140px] text-xs">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{environments.map((env) => (
|
||||
<SelectItem key={env} value={env} className="text-xs">
|
||||
{env}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
37
admin-ui/src/lib/EnvironmentContext.tsx
Normal file
37
admin-ui/src/lib/EnvironmentContext.tsx
Normal file
@@ -0,0 +1,37 @@
|
||||
"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);
|
||||
}
|
||||
Reference in New Issue
Block a user