From e97b2e54adf012c474c4d49a22bb992640a1de8f Mon Sep 17 00:00:00 2001 From: Nazim Kri Date: Fri, 30 Oct 2026 09:58:33 -0400 Subject: [PATCH] Country selector in the header, auto-detected and remembered MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/actions.ts | 19 ++++ app/loterie/[id]/page.tsx | 5 +- app/page.tsx | 103 +++++++----------- app/profil/page.tsx | 3 +- components/app-header.tsx | 13 ++- components/country-nav.tsx | 79 -------------- components/country-switcher.tsx | 58 ++++++++++ components/flag.tsx | 14 +++ lib/countries.ts | 16 +-- lib/country-pref.ts | 32 ++++++ lib/member-auth.ts | 2 +- .../migration.sql | 5 + prisma/schema.prisma | 5 +- public/flags/ca-qc.svg | 1 + public/flags/dz.svg | 1 + public/flags/es.svg | 1 + public/flags/fr.svg | 1 + public/flags/it.svg | 1 + public/flags/ma.svg | 1 + public/flags/pt.svg | 1 + public/flags/us.svg | 1 + 21 files changed, 200 insertions(+), 162 deletions(-) delete mode 100644 components/country-nav.tsx create mode 100644 components/country-switcher.tsx create mode 100644 components/flag.tsx create mode 100644 lib/country-pref.ts create mode 100644 prisma/migrations/20261030120000_favorite_country_nullable/migration.sql create mode 100644 public/flags/ca-qc.svg create mode 100644 public/flags/dz.svg create mode 100644 public/flags/es.svg create mode 100644 public/flags/fr.svg create mode 100644 public/flags/it.svg create mode 100644 public/flags/ma.svg create mode 100644 public/flags/pt.svg create mode 100644 public/flags/us.svg diff --git a/app/actions.ts b/app/actions.ts index f19afcd..ab3be1f 100644 --- a/app/actions.ts +++ b/app/actions.ts @@ -1,8 +1,27 @@ "use server" import { revalidatePath } from "next/cache" +import { cookies } from "next/headers" +import { prisma } from "@/lib/db" import { getCurrentMember } from "@/lib/member-auth" import { followPick, unfollowPick } from "@/lib/follows" +import { COUNTRY_COOKIE, isKnownCountry } from "@/lib/country-pref" + +export async function setCountryAction(code: string) { + if (!isKnownCountry(code)) return { ok: false as const } + const jar = await cookies() + jar.set(COUNTRY_COOKIE, code, { + httpOnly: true, + sameSite: "lax", + secure: process.env.NODE_ENV === "production", + maxAge: 365 * 24 * 3600, + path: "/", + }) + const member = await getCurrentMember() + if (member) await prisma.user.update({ where: { id: member.id }, data: { favoriteCountry: code } }) + revalidatePath("/", "layout") + return { ok: true as const } +} export async function toggleFollowAction(lotteryId: string, country: string, targetDrawDate: string, isFollowing: boolean) { const member = await getCurrentMember() diff --git a/app/loterie/[id]/page.tsx b/app/loterie/[id]/page.tsx index 732ba2d..31887a0 100644 --- a/app/loterie/[id]/page.tsx +++ b/app/loterie/[id]/page.tsx @@ -8,6 +8,7 @@ import { NumberFrequencyChart } from "@/components/number-frequency-chart" import { PerformanceChart } from "@/components/performance-chart" import { DrawTimeline } from "@/components/draw-timeline" import { Badge } from "@/components/ui/badge" +import { Flag } from "@/components/flag" export const instant = false @@ -35,8 +36,8 @@ export default async function LotteryStatsPage({ params }: { params: Promise<{ i

{entry.name}

- - {theme.flagEmoji} {theme.name} + + {theme.name}

diff --git a/app/page.tsx b/app/page.tsx index 51c4fd0..f699e46 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -3,34 +3,29 @@ import { COUNTRIES, countryTheme } from "@/lib/countries" import { getArmyFeed } from "@/lib/lokilab-feed" import { getCurrentMember } from "@/lib/member-auth" import { listFollowedPicks } from "@/lib/follows" -import { CountryNav, CountryNavMobile } from "@/components/country-nav" +import { resolveCountry } from "@/lib/country-pref" import { LotteryResultsPanel } from "@/components/lottery-results-panel" import { AppHeader } from "@/components/app-header" import { Hero } from "@/components/hero" +import { Flag } from "@/components/flag" import { Skeleton } from "@/components/ui/skeleton" export const instant = false -export default async function Home({ - searchParams, -}: { - searchParams: Promise<{ country?: string }> -}) { - const sp = await searchParams - const activeCode = sp.country && COUNTRIES.some((c) => c.code === sp.country) ? sp.country : "CA-QC" - +export default function Home() { return (

}> - +
) } -async function HomeContent({ activeCode }: { activeCode: string }) { +async function HomeContent() { const [feed, member] = await Promise.all([getArmyFeed(), getCurrentMember()]) + const activeCode = await resolveCountry(member) const theme = countryTheme(activeCode) const lotteries = feed.lotteries.filter((l) => l.country === activeCode) const totalWins = lotteries.reduce((sum, l) => sum + l.lifetime.wins, 0) @@ -51,54 +46,41 @@ async function HomeContent({ activeCode }: { activeCode: string }) { />
-
- +
+

+ + {theme.name} +

+

+ {lotteries.length} loterie{lotteries.length === 1 ? "" : "s"} suivie + {lotteries.length === 1 ? "" : "s"} · changez de pays en haut à droite +

-
- +
+ + + 0 ? `${totalWins} / ${totalTracked}` : "—"} label="Grilles gagnantes" /> +
-
-
-

{theme.name}

-

- {lotteries.length} loterie{lotteries.length === 1 ? "" : "s"} suivie - {lotteries.length === 1 ? "" : "s"} -

-
- -
- - - 0 ? `${totalWins} / ${totalTracked}` : "—"} - label="Grilles gagnantes" - /> -
- - {lotteries.length > 0 ? ( - - ) : ( -
- Aucune loterie suivie pour {theme.name} pour le moment. -
- )} - -

- Données fournies par LokiLab. Statistiques descriptives seulement — aucune garantie - de gain. -

+ {lotteries.length > 0 ? ( + + ) : ( +
+ Les loteries de {theme.name} arrivent bientôt dans l'univers de Loki.
-
+ )} + +

+ Données fournies par LokiLab. Statistiques descriptives seulement — aucune garantie de gain. +

) @@ -115,15 +97,10 @@ function Stat({ value, label }: { value: string; label: string }) { function HomeSkeleton() { return ( -
-
- -
- - - -
-
+
+ + +
) } diff --git a/app/profil/page.tsx b/app/profil/page.tsx index 3339a59..c9d9af6 100644 --- a/app/profil/page.tsx +++ b/app/profil/page.tsx @@ -6,6 +6,7 @@ import { countryTheme } from "@/lib/countries" import { AppHeader } from "@/components/app-header" import { NicknameForm } from "@/components/nickname-form" import { Badge } from "@/components/ui/badge" +import { Flag } from "@/components/flag" export const metadata: Metadata = { title: "Mon profil — LokiLoto" } export const instant = false @@ -47,7 +48,7 @@ export default async function ProfilePage() { return (
- {theme.flagEmoji} + {f.lotteryId} {f.targetDrawDate} diff --git a/components/app-header.tsx b/components/app-header.tsx index 8dcde7b..f79938b 100644 --- a/components/app-header.tsx +++ b/components/app-header.tsx @@ -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 +} export function AppHeader() { return ( @@ -24,7 +32,10 @@ export function AppHeader() { -
+
+ }> + + }> diff --git a/components/country-nav.tsx b/components/country-nav.tsx deleted file mode 100644 index 36b7a7c..0000000 --- a/components/country-nav.tsx +++ /dev/null @@ -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 ( - - ) -} - -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 ( - - ) -} diff --git a/components/country-switcher.tsx b/components/country-switcher.tsx new file mode 100644 index 0000000..b022fed --- /dev/null +++ b/components/country-switcher.tsx @@ -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 ( + + + + {current.name} + + + + Choisir un pays + + {COUNTRIES.map((c) => ( + choose(c.code)} className="gap-2.5"> + + {c.name} + {c.code === active && } + + ))} + + + ) +} diff --git a/components/flag.tsx b/components/flag.tsx new file mode 100644 index 0000000..aed7924 --- /dev/null +++ b/components/flag.tsx @@ -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 + + ) +} diff --git a/lib/countries.ts b/lib/countries.ts index 14a449b..54d7abd 100644 --- a/lib/countries.ts +++ b/lib/countries.ts @@ -10,7 +10,6 @@ export interface CountryTheme { code: string // ISO 3166-1 alpha-2, or a region code like 'CA-QC' name: string - flagEmoji: string flagPrimary: string flagSecondary: string accent: string // oklch(), CSS custom property value @@ -23,7 +22,6 @@ export const COUNTRIES: CountryTheme[] = [ { code: 'CA-QC', name: 'Québec', - flagEmoji: '⚜️', flagPrimary: '#1768C4', flagSecondary: '#183653', accent: 'oklch(0.62 0.16 255.5)', @@ -32,7 +30,6 @@ export const COUNTRIES: CountryTheme[] = [ { code: 'FR', name: 'France', - flagEmoji: '🇫🇷', flagPrimary: '#0055A4', flagSecondary: '#EF4135', accent: 'oklch(0.63 0.19 28.6)', @@ -40,8 +37,7 @@ export const COUNTRIES: CountryTheme[] = [ }, { code: 'US', - name: 'United States', - flagEmoji: '🇺🇸', + name: 'États-Unis', flagPrimary: '#B22234', flagSecondary: '#3C3B6E', accent: 'oklch(0.6 0.17 20.7)', @@ -49,8 +45,7 @@ export const COUNTRIES: CountryTheme[] = [ }, { code: 'ES', - name: 'España', - flagEmoji: '🇪🇸', + name: 'Espagne', flagPrimary: '#AA151B', flagSecondary: '#F1BF00', accent: 'oklch(0.76 0.16 89.6)', @@ -58,8 +53,7 @@ export const COUNTRIES: CountryTheme[] = [ }, { code: 'IT', - name: 'Italia', - flagEmoji: '🇮🇹', + name: 'Italie', flagPrimary: '#009246', flagSecondary: '#CE2B37', accent: 'oklch(0.65 0.15 151.1)', @@ -68,7 +62,6 @@ export const COUNTRIES: CountryTheme[] = [ { code: 'PT', name: 'Portugal', - flagEmoji: '🇵🇹', flagPrimary: '#046A38', flagSecondary: '#DA020E', accent: 'oklch(0.63 0.14 153.6)', @@ -77,7 +70,6 @@ export const COUNTRIES: CountryTheme[] = [ { code: 'DZ', name: 'Algérie', - flagEmoji: '🇩🇿', flagPrimary: '#006233', flagSecondary: '#D21034', accent: 'oklch(0.6 0.13 153.7)', @@ -86,7 +78,6 @@ export const COUNTRIES: CountryTheme[] = [ { code: 'MA', name: 'Maroc', - flagEmoji: '🇲🇦', flagPrimary: '#C1272D', flagSecondary: '#006233', accent: 'oklch(0.58 0.19 25)', @@ -99,7 +90,6 @@ export function countryTheme(code: string): CountryTheme { COUNTRIES.find((c) => c.code === code) ?? { code, name: code, - flagEmoji: '🏳️', flagPrimary: '#D7AE54', flagSecondary: '#242B36', accent: FALLBACK_ACCENT, diff --git a/lib/country-pref.ts b/lib/country-pref.ts new file mode 100644 index 0000000..23b67ff --- /dev/null +++ b/lib/country-pref.ts @@ -0,0 +1,32 @@ +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 { + 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) +} diff --git a/lib/member-auth.ts b/lib/member-auth.ts index 133a7e8..62d31ae 100644 --- a/lib/member-auth.ts +++ b/lib/member-auth.ts @@ -26,7 +26,7 @@ export interface CurrentMember { name: string avatarUrl: string | null publicNickname: string | null - favoriteCountry: string + favoriteCountry: string | null } export async function getCurrentMember(): Promise { diff --git a/prisma/migrations/20261030120000_favorite_country_nullable/migration.sql b/prisma/migrations/20261030120000_favorite_country_nullable/migration.sql new file mode 100644 index 0000000..908cf8e --- /dev/null +++ b/prisma/migrations/20261030120000_favorite_country_nullable/migration.sql @@ -0,0 +1,5 @@ +-- A member's country preference is only set when they actually pick one, +-- so geo-detection isn't overridden by a default nobody chose. +ALTER TABLE "User" ALTER COLUMN "favoriteCountry" DROP NOT NULL; +ALTER TABLE "User" ALTER COLUMN "favoriteCountry" DROP DEFAULT; +UPDATE "User" SET "favoriteCountry" = NULL WHERE "favoriteCountry" = 'CA-QC'; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 20fdb48..6c5569d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -23,8 +23,9 @@ model User { /// (leaderboard, follow counts). Must be set before a member appears /// publicly. publicNickname String? - /// Last country tab the member viewed, restored on next visit. - favoriteCountry String @default("CA-QC") + /// Country the member explicitly picked in the header selector — synced + /// across devices. Null until they pick one, so geo-detection applies. + favoriteCountry String? createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/public/flags/ca-qc.svg b/public/flags/ca-qc.svg new file mode 100644 index 0000000..2a475fb --- /dev/null +++ b/public/flags/ca-qc.svg @@ -0,0 +1 @@ + diff --git a/public/flags/dz.svg b/public/flags/dz.svg new file mode 100644 index 0000000..fe8bb71 --- /dev/null +++ b/public/flags/dz.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/es.svg b/public/flags/es.svg new file mode 100644 index 0000000..e143324 --- /dev/null +++ b/public/flags/es.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/fr.svg b/public/flags/fr.svg new file mode 100644 index 0000000..ec311d7 --- /dev/null +++ b/public/flags/fr.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/it.svg b/public/flags/it.svg new file mode 100644 index 0000000..2eb1289 --- /dev/null +++ b/public/flags/it.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/ma.svg b/public/flags/ma.svg new file mode 100644 index 0000000..c2f0600 --- /dev/null +++ b/public/flags/ma.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/pt.svg b/public/flags/pt.svg new file mode 100644 index 0000000..51d7c80 --- /dev/null +++ b/public/flags/pt.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/flags/us.svg b/public/flags/us.svg new file mode 100644 index 0000000..dc427e7 --- /dev/null +++ b/public/flags/us.svg @@ -0,0 +1 @@ + \ No newline at end of file