From 29126ce5c20cd99e520e3f4a26330761ebfb833c Mon Sep 17 00:00:00 2001 From: mortezaei Date: Thu, 20 Aug 2026 18:35:55 +0330 Subject: [PATCH] refactor: remove Flutter bridge location lookup to prioritize backend-based geolocation --- src/lib/geo-region.ts | 120 +++--------------------------------------- 1 file changed, 7 insertions(+), 113 deletions(-) diff --git a/src/lib/geo-region.ts b/src/lib/geo-region.ts index a8c982e..7948f3f 100644 --- a/src/lib/geo-region.ts +++ b/src/lib/geo-region.ts @@ -79,99 +79,6 @@ function resolvePhoneCodeFromCountryCode(isoCode?: string): string | undefined { return undefined; } -function requestLocationFromFlutterBridge(): Promise { - if (typeof window === "undefined") return Promise.resolve(null); - - const hasFlutterBridge = - typeof (window as any).HabibApp?.postMessage === "function" || - typeof (window as any).flutter_inappwebview?.callHandler === "function"; - - if (!hasFlutterBridge) { - return Promise.resolve(null); - } - - return new Promise((resolve) => { - let settled = false; - let timer: NodeJS.Timeout | null = null; - let unsubscribe: (() => void) | undefined; - - const cleanup = () => { - settled = true; - if (timer) clearTimeout(timer); - if (unsubscribe) unsubscribe(); - }; - - timer = setTimeout(() => { - if (!settled) { - cleanup(); - console.warn("[GEO_BRIDGE] Timeout waiting for Flutter get_location response"); - resolve(null); - } - }, 4000); - - const handleFlutterResponse: NonNullable = ( - event, - ) => { - if (event.action === "get_location") { - cleanup(); - if (event.success && event.data) { - const data = event.data as any; - console.log( - "[GEO_BRIDGE] 📥 Received from Flutter get_location:", - JSON.stringify(data), - ); - - const rawCountry = data.country || data.country_code || ""; - const isoCode = - data.country_code || - (rawCountry && rawCountry.trim().length === 2 - ? rawCountry.trim().toUpperCase() - : undefined); - const phoneCode = resolvePhoneCodeFromCountryCode(isoCode); - const countryName = - resolveCountryName(rawCountry || isoCode, "en") || rawCountry; - const countryFa = - resolveCountryName(rawCountry || isoCode, "fa") || countryName; - - console.log( - `[GEO_BRIDGE] 🌍 Resolved Country: FA=${countryFa} | EN=${countryName} | Code=${isoCode} | City=${data.city || "None"} | IP=${data.ip || "None"}`, - ); - - const region: UserGeoRegion = { - ip: data.ip, - city: data.city, - country: countryName, - countryCode: isoCode, - phoneCode: phoneCode || "+44", - }; - resolve(region); - } else { - resolve(null); - } - } - }; - - if (typeof window.addFlutterResponseListener === "function") { - unsubscribe = window.addFlutterResponseListener(handleFlutterResponse); - } - - try { - console.log("[GEO_BRIDGE] 🚀 Sending { action: 'get_location' } to Flutter..."); - if ((window as any).HabibApp?.postMessage) { - (window as any).HabibApp.postMessage( - JSON.stringify({ action: "get_location" }), - ); - } else if ((window as any).flutter_inappwebview?.callHandler) { - (window as any).flutter_inappwebview.callHandler("get_location"); - } - } catch (err) { - console.error("[GEO_BRIDGE] Failed to postMessage to Flutter:", err); - cleanup(); - resolve(null); - } - }); -} - export function getUserGeoRegion(force = false): Promise { if (!force) { const existing = getStoredUserGeoRegion(); @@ -189,18 +96,9 @@ export function getUserGeoRegion(force = false): Promise { geoRegionPromise = (async () => { try { - // 1. Primary: Flutter Native Bridge (uses phone's direct native IP) - const bridgeRegion = await requestLocationFromFlutterBridge(); - if (bridgeRegion && (bridgeRegion.country || bridgeRegion.city)) { - setStoredUserGeoRegion(bridgeRegion); - return bridgeRegion; - } - - // 2. Secondary: Direct HTTP Client fallback (if in browser or bridge fails) + // 1. Primary: Habib Backend User Region API (/account/auth/user/region/) try { - console.log( - "[GEO_AUTO_LOG] 🚀 Requesting /account/auth/user/region/ from backend...", - ); + console.log("[GEO_AUTO_LOG] 🚀 Requesting /account/auth/user/region/ from backend..."); const response = await http.get<{ ip?: string; country?: string; @@ -211,22 +109,18 @@ export function getUserGeoRegion(force = false): Promise { }); const data = response.data; - console.log( - "[GEO_AUTO_LOG] 📥 Backend Raw Response:", - JSON.stringify(data), - ); + console.log("[GEO_AUTO_LOG] 📥 Backend Raw Response:", JSON.stringify(data)); if (data && (data.country || data.country_code || data.city)) { - const rawCountry = data.country || data.country_code || ""; const isoCode = data.country_code || - (rawCountry && rawCountry.trim().length === 2 - ? rawCountry.trim().toUpperCase() + (data.country && data.country.trim().length === 2 + ? data.country.trim().toUpperCase() : undefined); const phoneCode = resolvePhoneCodeFromCountryCode(isoCode); const countryName = - resolveCountryName(rawCountry || isoCode, "en") || rawCountry; + resolveCountryName(data.country || isoCode, "en") || data.country; const countryFa = - resolveCountryName(rawCountry || isoCode, "fa") || countryName; + resolveCountryName(data.country || isoCode, "fa") || countryName; console.log( `[GEO_AUTO_LOG] 🌍 Resolved Country: FA=${countryFa} | EN=${countryName} | Code=${isoCode} | City=${data.city || "None"} | IP=${data.ip || "None"}`,