Fix crash on third-party details when country is GLOBAL

Intl.DisplayNames rejects the GLOBAL pseudo-region, so label
resolution now handles it explicitly before rendering the picker.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
This commit is contained in:
Sacha Al Himdani
2026-07-27 18:11:44 +02:00
committed by Bryan Frimin
parent 302175617f
commit 2ebf3c180f
3 changed files with 73 additions and 6 deletions

View File

@@ -0,0 +1,43 @@
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import { describe, it, expect } from "vitest";
import { getCountryName, getCountryOptions } from "./countries";
describe("getCountryName", () => {
it("returns a label for the GLOBAL pseudo-region", () => {
expect(getCountryName("en", "GLOBAL")).toBe("Global");
});
it("returns localized names for ISO country codes", () => {
expect(getCountryName("en", "US")).toBe("United States");
expect(getCountryName("en", "EU")).toBe("European Union");
});
});
describe("getCountryOptions", () => {
it("includes GLOBAL without throwing", () => {
const options = getCountryOptions("en");
expect(options.find(option => option.value === "GLOBAL")).toEqual({
value: "GLOBAL",
label: "Global",
});
});
});

View File

@@ -50,8 +50,22 @@ export const countries = [
export type CountryCode = typeof countries[number];
// Pseudo-regions accepted by our CountryCode enum but not by Intl.DisplayNames.
const pseudoRegionNames: Partial<Record<CountryCode, string>> = {
GLOBAL: "Global",
};
export function getCountryName(language: string, code: CountryCode): string {
return new Intl.DisplayNames(language, { type: "region" }).of(code) ?? code;
const pseudoName = pseudoRegionNames[code];
if (pseudoName) {
return pseudoName;
}
try {
return new Intl.DisplayNames(language, { type: "region" }).of(code) ?? code;
} catch {
return code;
}
}
export function getCountryOptions(lang: string) {