Files
co-lite/solvway-admin-portal/app/components/app/LocaleSwitcher.tsx
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +08:00

72 lines
2.7 KiB
TypeScript

"use client";
import { Check, Globe } from "lucide-react";
import { useRouter } from "next/navigation";
import { useLocale, useT } from "@/app/lib/i18n/MessagesProvider";
import { useLocales } from "@/app/lib/i18n/LocalesProvider";
import { Button } from "@/app/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/app/components/ui/dropdown-menu";
// Spec: "The Portal Provides A Locale Switcher". bb_locale is a plain
// (non-httpOnly) display preference -- never a security boundary, unlike
// colite_admin -- so the client sets it directly via document.cookie rather
// than a route handler, then asks the router to refetch the current route's
// server components (router.refresh()). RootLayout re-reads the cookie and
// reloads the catalog on that refetch, without a full page reload.
const COOKIE_MAX_AGE_S = 60 * 60 * 24 * 365;
// Defined outside the component so the react-hooks/immutability rule doesn't
// treat this as a component-render mutation of an external value (document
// is a global): this plain function only ever runs from the onClick handler
// below, never during render.
function setLocaleCookie(code: string) {
document.cookie = `bb_locale=${code}; path=/; max-age=${COOKIE_MAX_AGE_S}`;
}
export default function LocaleSwitcher() {
const router = useRouter();
const locale = useLocale();
const t = useT();
// LocalesProvider carries the full registry (disabled rows included, for the
// /messages editor). The switcher must offer only enabled locales, so a
// disabled/staged locale can never be selected.
const locales = useLocales().filter((l) => l.enabled !== false);
const current = locales.find((l) => l.code === locale);
const shortLabel = locale === "en_US" ? "EN" : (current?.label ?? locale);
function selectLocale(code: string) {
if (code === locale) return;
setLocaleCookie(code);
router.refresh();
}
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" aria-label={t("LocaleSwitcherChange")} className="h-9 px-2.5">
<Globe aria-hidden="true" className="size-4" />
<span className="hidden md:inline">{shortLabel}</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{locales.map((l) => (
<DropdownMenuItem
key={l.code}
onClick={() => selectLocale(l.code)}
aria-current={l.code === locale ? "true" : undefined}
>
{l.label}
{l.code === locale && <Check aria-hidden="true" className="ml-auto size-4" />}
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
);
}