|
|
@@ -1,6 +1,5 @@
|
|
|
"use client";
|
|
|
|
|
|
-import { message } from "antd";
|
|
|
import type React from "react";
|
|
|
import { createContext, useContext, useEffect, useState } from "react";
|
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
|
@@ -10,7 +9,6 @@ import type { ConstItem, ConstItemsMap } from "@/types/api";
|
|
|
interface GlobalConstsContextValue {
|
|
|
items: ConstItemsMap;
|
|
|
isLoading: boolean;
|
|
|
- error: string | null;
|
|
|
reload: () => Promise<void>;
|
|
|
getGroup: (key: string) => ConstItem[];
|
|
|
}
|
|
|
@@ -22,27 +20,18 @@ const GlobalConstsContext = createContext<GlobalConstsContextValue | undefined>(
|
|
|
export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
|
|
|
children,
|
|
|
}) => {
|
|
|
- const { isLoading: authLoading } = useAuth();
|
|
|
+ const { isAuthenticated } = useAuth();
|
|
|
const [items, setItems] = useState<ConstItemsMap>({});
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
|
- const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
const fetchConsts = async () => {
|
|
|
setIsLoading(true);
|
|
|
- setError(null);
|
|
|
try {
|
|
|
const res = await getGlobalConsts();
|
|
|
+
|
|
|
setItems(res.items || {});
|
|
|
} catch (err) {
|
|
|
- const msg =
|
|
|
- err instanceof Error ? err.message : "Failed to load global consts";
|
|
|
- setError(msg);
|
|
|
- if (typeof window !== "undefined") {
|
|
|
- message.open({
|
|
|
- type: "error",
|
|
|
- content: msg,
|
|
|
- });
|
|
|
- }
|
|
|
+ console.error("Failed to load global consts:", err);
|
|
|
} finally {
|
|
|
setIsLoading(false);
|
|
|
}
|
|
|
@@ -50,11 +39,10 @@ export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
|
|
|
|
|
|
// biome-ignore lint/correctness/useExhaustiveDependencies: no need
|
|
|
useEffect(() => {
|
|
|
- // Wait for auth initialization before fetching, so token is ready
|
|
|
- if (!authLoading) {
|
|
|
- void fetchConsts();
|
|
|
+ if (isAuthenticated) {
|
|
|
+ fetchConsts();
|
|
|
}
|
|
|
- }, [authLoading]);
|
|
|
+ }, [isAuthenticated]);
|
|
|
|
|
|
const getGroup = (key: string): ConstItem[] => {
|
|
|
return items[key] || [];
|
|
|
@@ -63,7 +51,6 @@ export const GlobalConstsProvider: React.FC<{ children: React.ReactNode }> = ({
|
|
|
const value: GlobalConstsContextValue = {
|
|
|
items,
|
|
|
isLoading,
|
|
|
- error,
|
|
|
reload: fetchConsts,
|
|
|
getGroup,
|
|
|
};
|