From 90e2e1ba4697179702640cbd979a428597051fb8 Mon Sep 17 00:00:00 2001 From: Eshan Roy Date: Sun, 8 Dec 2024 14:52:03 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(=5Fidk):=20i=20wanted=20to?= =?UTF-8?q?=20show=20suggested=20mirror?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Download.tsx | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/pages/Download.tsx b/src/pages/Download.tsx index 18dd8f6..6d25644 100644 --- a/src/pages/Download.tsx +++ b/src/pages/Download.tsx @@ -3,7 +3,7 @@ import { Download as DownloadIcon, Monitor, Server, HardDrive, Smartphone, Code export function DownloadPage() { const [userLocation, setUserLocation] = useState(null); - const [userRegion, setUserRegion] = useState(null); // Added userRegion to store the region + const [userRegion, setUserRegion] = useState(null); // Detect user location using a public API useEffect(() => { @@ -16,7 +16,7 @@ export function DownloadPage() { } catch (error) { console.error('Failed to fetch user location:', error); setUserLocation(null); - setUserRegion(null); // Ensure region is null on error + setUserRegion(null); } } fetchLocation(); @@ -25,13 +25,30 @@ export function DownloadPage() { // Find the closest mirror based on the user's region const getSuggestedMirror = () => { if (!userRegion) return null; // If region is not detected, return null - - // Map userRegion to mirror data and suggest the closest region + + // Normalize userRegion to handle cases like "United States" vs. "USA" + const regionMap: { [key: string]: string } = { + "united states": "north america", + "canada": "north america", + "brazil": "south america", + "argentina": "south america", + "germany": "europe", + "france": "europe", + "india": "asia", + "japan": "asia", + "south africa": "africa", + "australia": "australia", + }; + + // Lowercase the userRegion for more lenient matching + const normalizedRegion = regionMap[userRegion.toLowerCase()] || userRegion.toLowerCase(); + + // Find the closest mirror based on the user's region return mirrorData.find((mirror) => - mirror.region.toLowerCase().includes(userRegion.toLowerCase()) + mirror.region.toLowerCase().includes(normalizedRegion) ); }; - + const suggestedMirror = getSuggestedMirror(); return ( @@ -229,11 +246,13 @@ function MirrorButton({ speed, host, url, + suggested = false, // Added 'suggested' prop to highlight the suggested mirror }: { region: string; speed: string; host: string; url: string; + suggested?: boolean; // Optional prop to check if this is the suggested mirror }) { const speedColor = { 'Very Fast': 'text-green-500', @@ -244,7 +263,7 @@ function MirrorButton({ return (
@@ -257,6 +276,11 @@ function MirrorButton({ + {suggested && ( +
+ Suggested Mirror +
+ )}
); @@ -323,15 +347,7 @@ const editionData = [ }, ]; -// const mirrorData = [ -// { -// region: 'North America', -// speed: 'Fast', -// host: 'MirrorHost USA', -// url: '#', -// }, -// // ... Add other mirrors similarly -// ]; +// Mirrors Data const mirrorData = [ { region: 'North America',