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

@@ -63,9 +63,17 @@ type CountriesFieldInputProps = {
};
function CountriesFieldInput(props: CountriesFieldInputProps) {
const { i18n } = useTranslation();
const { t, i18n } = useTranslation();
const [animateBadge, setAnimateBadge] = useState(false);
const countryLabel = (code: CountryCode) => {
// GLOBAL is not a valid Intl region code; use the translated label.
if (code === "GLOBAL") {
return t("country.GLOBAL");
}
return getCountryName(i18n.language, code);
};
const addCountry = (code: string) => {
setAnimateBadge(true);
props.onValueChange([...props.value, code]);
@@ -94,7 +102,7 @@ function CountriesFieldInput(props: CountriesFieldInputProps) {
&& "starting:opacity-0 starting:w-0 w-max transition-all duration-500 starting:bg-accent",
)}
>
{getCountryName(i18n.language, countryCode as CountryCode)}
{countryLabel(countryCode as CountryCode)}
<div className="w-0 overflow-hidden group-hover:w-4 duration-200">
<IconCrossLargeX size={12} />
</div>
@@ -110,6 +118,7 @@ function CountriesFieldInput(props: CountriesFieldInputProps) {
(c: CountryCode) => !props.value.includes(c),
)}
onAdd={addCountry}
countryLabel={countryLabel}
/>
)}
</div>
@@ -119,15 +128,16 @@ function CountriesFieldInput(props: CountriesFieldInputProps) {
type CountryInputProps = {
availableCountries: readonly CountryCode[];
onAdd: (code: string) => void;
countryLabel: (code: CountryCode) => string;
};
function CountryInput({ availableCountries, onAdd }: CountryInputProps) {
const { t, i18n } = useTranslation();
function CountryInput({ availableCountries, onAdd, countryLabel }: CountryInputProps) {
const { t } = useTranslation();
const [search, setSearch] = useState("");
const [isOpen, setIsOpen] = useState(false);
const countryOptions = availableCountries.map(code => ({
value: code,
label: getCountryName(i18n.language, code),
label: countryLabel(code),
}));
useEffect(() => {

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 {
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) {