From c5fe88a911513588c8d0effd87a8ef51474c0d20 Mon Sep 17 00:00:00 2001 From: hyeonsu Date: Tue, 26 Aug 2025 18:33:04 +0900 Subject: [PATCH] =?UTF-8?q?=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=A7=80?= =?UTF-8?q?=EC=97=AD=20=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/contexts/MenuContext.tsx | 24 ++++++++++++++++++++++++ frontend/lib/api/client.ts | 26 +++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/frontend/contexts/MenuContext.tsx b/frontend/contexts/MenuContext.tsx index f1aa7bb1..99084c41 100644 --- a/frontend/contexts/MenuContext.tsx +++ b/frontend/contexts/MenuContext.tsx @@ -49,6 +49,30 @@ export function MenuProvider({ children }: { children: ReactNode }) { setLoading(true); console.log("=== MenuContext API 호출 시작 ==="); + // 사용자 로케일이 로드될 때까지 잠시 대기 + let retryCount = 0; + const maxRetries = 20; // 최대 2초 대기 (100ms * 20) + + while (retryCount < maxRetries) { + if (typeof window !== "undefined") { + const hasGlobalLang = !!(window as any).__GLOBAL_USER_LANG; + const hasStoredLang = !!localStorage.getItem("userLocale"); + + if (hasGlobalLang || hasStoredLang) { + console.log("✅ 사용자 로케일 로드 완료, 메뉴 API 호출 진행"); + break; + } + } + + console.log(`⏳ 사용자 로케일 로드 대기 중... (${retryCount + 1}/${maxRetries})`); + await new Promise((resolve) => setTimeout(resolve, 100)); + retryCount++; + } + + if (retryCount >= maxRetries) { + console.warn("⚠️ 사용자 로케일 로드 타임아웃, 기본값으로 진행"); + } + // 관리자 메뉴와 사용자 메뉴를 병렬로 로드 const [adminResponse, userResponse] = await Promise.all([menuApi.getAdminMenus(), menuApi.getUserMenus()]); diff --git a/frontend/lib/api/client.ts b/frontend/lib/api/client.ts index f3dea2bf..ada8a972 100644 --- a/frontend/lib/api/client.ts +++ b/frontend/lib/api/client.ts @@ -61,9 +61,29 @@ apiClient.interceptors.request.use( // 언어 정보를 쿼리 파라미터에 추가 (GET 요청 시에만) if (config.method?.toUpperCase() === "GET") { - // 전역 언어 상태에서 현재 언어 가져오기 (DB 값 그대로 사용) - const currentLang = typeof window !== "undefined" ? (window as any).__GLOBAL_USER_LANG || "KR" : "KR"; - console.log("🌐 API 요청 시 언어 정보:", currentLang); + // 우선순위: 전역 변수 > localStorage > 기본값 + let currentLang = "KR"; // 기본값 + + if (typeof window !== "undefined") { + // 1순위: 전역 변수에서 확인 + if ((window as any).__GLOBAL_USER_LANG) { + currentLang = (window as any).__GLOBAL_USER_LANG; + } + // 2순위: localStorage에서 확인 (새 창이나 페이지 새로고침 시) + else { + const storedLocale = localStorage.getItem("userLocale"); + if (storedLocale) { + currentLang = storedLocale; + } + } + } + + console.log("🌐 API 요청 시 언어 정보:", { + currentLang, + globalVar: (window as any).__GLOBAL_USER_LANG, + localStorage: typeof window !== "undefined" ? localStorage.getItem("userLocale") : null, + url: config.url, + }); if (config.params) { config.params.userLang = currentLang;