Country selector in the header, auto-detected and remembered

Replaces the left sidebar with a header dropdown (flag + name), like
1xbet's region picker. The shown country resolves as: saved choice
(cookie, then the member's profile so it follows them across devices),
else Cloudflare's CF-IPCountry (Canada maps to Québec), else Québec.
favoriteCountry becomes nullable so geo-detection isn't overridden by
a default nobody chose. Emoji flags render as letters on Windows, so
flags are now SVGs in /public/flags; country names are in French.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nazim Kri
2026-10-30 09:58:33 -04:00
co-authored by Claude Opus 5.5
parent 2eafbfd3d7
commit e97b2e54ad
21 changed files with 200 additions and 162 deletions
+12 -1
View File
@@ -2,7 +2,15 @@ import Image from "next/image"
import Link from "next/link"
import { Suspense } from "react"
import { UserMenu } from "@/components/user-menu"
import { CountrySwitcher } from "@/components/country-switcher"
import { Skeleton } from "@/components/ui/skeleton"
import { getCurrentMember } from "@/lib/member-auth"
import { resolveCountry } from "@/lib/country-pref"
async function CountrySlot() {
const member = await getCurrentMember()
return <CountrySwitcher active={await resolveCountry(member)} />
}
export function AppHeader() {
return (
@@ -24,7 +32,10 @@ export function AppHeader() {
</Link>
</nav>
<div className="ml-auto">
<div className="ml-auto flex items-center gap-3">
<Suspense fallback={<Skeleton className="h-9 w-28 rounded-md" />}>
<CountrySlot />
</Suspense>
<Suspense fallback={<Skeleton className="h-8 w-8 rounded-full" />}>
<UserMenu />
</Suspense>
-79
View File
@@ -1,79 +0,0 @@
"use client"
import Link from "next/link"
import { usePathname, useSearchParams } from "next/navigation"
import { COUNTRIES } from "@/lib/countries"
import { cn } from "@/lib/utils"
export function CountryNav({ active }: { active: string }) {
const pathname = usePathname()
const searchParams = useSearchParams()
function hrefFor(code: string) {
const params = new URLSearchParams(searchParams.toString())
params.set("country", code)
return `${pathname}?${params.toString()}`
}
return (
<nav aria-label="Sélection du pays" className="space-y-0.5">
{COUNTRIES.map((c) => {
const isActive = c.code === active
return (
<Link
key={c.code}
href={hrefFor(c.code)}
aria-current={isActive ? "page" : undefined}
className={cn(
"flex items-center gap-2.5 rounded-md border-l-[3px] py-2 pr-3 pl-2.5 text-[13px] font-medium transition-colors",
isActive
? "border-l-accent bg-panel-hover text-foreground"
: "border-l-transparent text-muted-foreground hover:bg-panel-hover/60 hover:text-foreground"
)}
>
<span aria-hidden className="w-5 shrink-0 text-center text-base leading-none">
{c.flagEmoji}
</span>
{c.name}
</Link>
)
})}
</nav>
)
}
export function CountryNavMobile({ active }: { active: string }) {
const pathname = usePathname()
const searchParams = useSearchParams()
function hrefFor(code: string) {
const params = new URLSearchParams(searchParams.toString())
params.set("country", code)
return `${pathname}?${params.toString()}`
}
return (
<nav
aria-label="Sélection du pays"
className="flex gap-1.5 overflow-x-auto pb-1 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
>
{COUNTRIES.map((c) => {
const isActive = c.code === active
return (
<Link
key={c.code}
href={hrefFor(c.code)}
aria-current={isActive ? "page" : undefined}
className={cn(
"flex shrink-0 items-center gap-1.5 rounded-full px-3 py-1.5 text-[13px] font-medium",
isActive ? "bg-accent-soft text-foreground" : "text-muted-foreground"
)}
>
<span aria-hidden>{c.flagEmoji}</span>
{c.name}
</Link>
)
})}
</nav>
)
}
+58
View File
@@ -0,0 +1,58 @@
"use client"
import { useTransition } from "react"
import { useRouter } from "next/navigation"
import { Check, ChevronDown } from "lucide-react"
import { COUNTRIES, countryTheme } from "@/lib/countries"
import { setCountryAction } from "@/app/actions"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { cn } from "@/lib/utils"
import { Flag } from "@/components/flag"
export function CountrySwitcher({ active }: { active: string }) {
const router = useRouter()
const [pending, startTransition] = useTransition()
const current = countryTheme(active)
function choose(code: string) {
if (code === active) return
startTransition(async () => {
await setCountryAction(code)
router.refresh()
})
}
return (
<DropdownMenu>
<DropdownMenuTrigger
aria-label={`Pays : ${current.name}. Changer de pays`}
className={cn(
"flex h-9 items-center gap-2 rounded-md border border-gold/30 bg-card/70 px-3 text-sm font-medium outline-none transition-colors hover:border-gold/60 focus-visible:ring-2 focus-visible:ring-ring",
pending && "opacity-60"
)}
>
<Flag code={current.code} />
<span className="hidden sm:inline">{current.name}</span>
<ChevronDown className="h-3.5 w-3.5 text-muted-foreground" />
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-56">
<DropdownMenuLabel className="text-xs text-muted-foreground">Choisir un pays</DropdownMenuLabel>
<DropdownMenuSeparator />
{COUNTRIES.map((c) => (
<DropdownMenuItem key={c.code} onSelect={() => choose(c.code)} className="gap-2.5">
<Flag code={c.code} />
<span className="flex-1">{c.name}</span>
{c.code === active && <Check className="h-4 w-4 text-gold" />}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
)
}
+14
View File
@@ -0,0 +1,14 @@
import { cn } from "@/lib/utils"
/** SVG flag from /public/flags — emoji flags don't render on Windows. */
export function Flag({ code, className }: { code: string; className?: string }) {
return (
// eslint-disable-next-line @next/next/no-img-element
<img
src={`/flags/${code.toLowerCase()}.svg`}
alt=""
aria-hidden="true"
className={cn("inline-block h-[14px] w-[21px] shrink-0 rounded-[2px] object-cover ring-1 ring-black/20", className)}
/>
)
}