Files
lokiloto/lib/country-pref.ts
Nazim KriandClaude Opus 5.5 e97b2e54ad 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>
2026-10-30 09:58:33 -04:00

33 lines
1.2 KiB
TypeScript

import 'server-only'
import { cookies, headers } from 'next/headers'
import { COUNTRIES } from './countries'
import type { CurrentMember } from './member-auth'
export const COUNTRY_COOKIE = 'lokiloto_country'
export const DEFAULT_COUNTRY = 'CA-QC'
function isKnown(code: string | null | undefined): code is string {
return !!code && COUNTRIES.some((c) => c.code === code)
}
/** Cloudflare's CF-IPCountry is ISO alpha-2; Canada maps to Québec, the only Canadian locale so far. */
function fromGeo(iso: string | null): string | null {
if (!iso) return null
const code = iso.toUpperCase() === 'CA' ? 'CA-QC' : iso.toUpperCase()
return isKnown(code) ? code : null
}
/** Saved choice (cookie, then member profile) beats geo-detection, which beats the default. */
export async function resolveCountry(member: CurrentMember | null): Promise<string> {
const jar = await cookies()
const saved = jar.get(COUNTRY_COOKIE)?.value
if (isKnown(saved)) return saved
if (isKnown(member?.favoriteCountry)) return member.favoriteCountry
const h = await headers()
return fromGeo(h.get('cf-ipcountry')) ?? DEFAULT_COUNTRY
}
export function isKnownCountry(code: string): boolean {
return isKnown(code)
}