diff --git a/package.json b/package.json index f815b7f4f..c3fcf01ae 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@mui/lab": "^7.0.0-beta.14", "@mui/material": "^7.1.2", "@mui/material-nextjs": "^7.1.1", - "@openstreetmap/id-tagging-schema": "^6.12.0", + "@openstreetmap/id-tagging-schema": "^7.0.1", "@sentry/nextjs": "^8.34.0", "@teritorio/openmaptiles-gl-language": "^1.5.4", "@turf/turf": "^7.2.0", diff --git a/src/components/SearchBox/options/preset.tsx b/src/components/SearchBox/options/preset.tsx index 023ce8e42..6ae1cc5f9 100644 --- a/src/components/SearchBox/options/preset.tsx +++ b/src/components/SearchBox/options/preset.tsx @@ -41,7 +41,7 @@ const getPresetsForSearch = async () => { tags, tagsAsOneString: tagsAsStrings.join(', '), texts: [ - ...getPresetTermsTranslation(presetKey).split(','), + ...getPresetTermsTranslation(presetKey), ...tagsAsStrings, presetKey, ], diff --git a/src/services/tagging/__tests__/idTaggingScheme.test.ts b/src/services/tagging/__tests__/idTaggingScheme.test.ts index 70f80238c..fdfd53ba3 100644 --- a/src/services/tagging/__tests__/idTaggingScheme.test.ts +++ b/src/services/tagging/__tests__/idTaggingScheme.test.ts @@ -134,22 +134,22 @@ describe('idTaggingScheme', () => { 'level', 'polling_station', 'wheelchair', - 'wikimedia_commons', - 'wikidata', - 'start_date', - 'short_name', - 'reg_name', - 'panoramax', - 'official_name', - 'note', - 'nat_name', - 'mapillary', - 'loc_name', - 'fixme', - 'ele_node', - 'description', 'alt_name', + 'description', + 'ele_node', + 'fixme', + 'loc_name', + 'mapillary', + 'nat_name', + 'note', + 'official_name', + 'panoramax', 'ref/linz/place_id-NZ', + 'reg_name', + 'short_name', + 'start_date', + 'wikidata', + 'wikimedia_commons', 'architect', ]); }); diff --git a/src/services/tagging/__tests__/translations.test.ts b/src/services/tagging/__tests__/translations.test.ts new file mode 100644 index 000000000..41ed68816 --- /dev/null +++ b/src/services/tagging/__tests__/translations.test.ts @@ -0,0 +1,39 @@ +import { intl } from '../../intl'; +import { + getPresetTermsTranslation, + mockSchemaTranslations, +} from '../translations'; + +intl.lang = 'en'; + +describe('getPresetTermsTranslation', () => { + it('returns terms as-is when already a string[] (current id-tagging-schema format)', () => { + mockSchemaTranslations({ + en: { presets: { presets: { shop: { terms: ['retailer', 'store'] } } } }, + }); + + expect(getPresetTermsTranslation('shop')).toEqual(['retailer', 'store']); + }); + + it('splits a comma-separated string (older id-tagging-schema format)', () => { + mockSchemaTranslations({ + en: { presets: { presets: { shop: { terms: 'retailer,store' } } } }, + }); + + expect(getPresetTermsTranslation('shop')).toEqual(['retailer', 'store']); + }); + + it('returns [] when the preset has no terms', () => { + mockSchemaTranslations({ + en: { presets: { presets: { shop: { name: 'Shop' } } } }, + }); + + expect(getPresetTermsTranslation('shop')).toEqual([]); + }); + + it('returns [] for an unknown preset key', () => { + mockSchemaTranslations({ en: { presets: { presets: {} } } }); + + expect(getPresetTermsTranslation('does-not-exist')).toEqual([]); + }); +}); diff --git a/src/services/tagging/translations.ts b/src/services/tagging/translations.ts index 3461960c5..5ccd1054c 100644 --- a/src/services/tagging/translations.ts +++ b/src/services/tagging/translations.ts @@ -9,13 +9,20 @@ import { getOurTranslations } from './ourPresets'; // https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema@6.1.0/dist/translations/en.min.json const cdnUrl = `https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema`; +// Pinned to the same major as the `@openstreetmap/id-tagging-schema` dependency +// in package.json (bump both together) - an unpinned `latest` silently pulled +// in a breaking change to the `terms` format once (see getPresetTermsTranslation). +const CDN_VERSION_RANGE = '7'; + // TODO download up-to-date or use node_module? let translations = {}; export const fetchSchemaTranslations = async () => { if (translations[intl.lang]) return; try { - const presetsPackage = await fetchJson(`${cdnUrl}/package.json`); + const presetsPackage = await fetchJson( + `${cdnUrl}@${CDN_VERSION_RANGE}/package.json`, + ); const { version } = presetsPackage; // this request is cached in browser @@ -43,8 +50,12 @@ export const mockSchemaTranslations = (mockTranslations) => { export const getPresetTranslation = (key: string): string => translations?.[intl.lang]?.presets?.presets?.[key]?.name ?? `[${key}]`; -export const getPresetTermsTranslation = (key: string) => - translations?.[intl.lang]?.presets?.presets?.[key]?.terms ?? ''; +export const getPresetTermsTranslation = (key: string): string[] => { + const terms = translations?.[intl.lang]?.presets?.presets?.[key]?.terms; + if (Array.isArray(terms)) return terms; + if (typeof terms === 'string') return terms ? terms.split(',') : []; + return []; +}; export const getAllTranslations = () => translations?.[intl.lang]; diff --git a/tsconfig.json b/tsconfig.json index cb770944d..5e9ef5706 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, + "allowArbitraryExtensions": true, "isolatedModules": true, "jsx": "preserve", "incremental": true diff --git a/yarn.lock b/yarn.lock index 02887e940..a22f2abf6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1515,10 +1515,10 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" -"@openstreetmap/id-tagging-schema@^6.12.0": - version "6.12.0" - resolved "https://registry.yarnpkg.com/@openstreetmap/id-tagging-schema/-/id-tagging-schema-6.12.0.tgz#963c0340850b4e40680a87344fea89e90b9a1a70" - integrity sha512-uAe0YyXv/QjQGhudc2FBmTg+TT+mks8H8p1FlN/hby9KKpvF9CwLU/ZLe3oDqK0jye6t3GlJ9HAyN/hxk7fTwQ== +"@openstreetmap/id-tagging-schema@^7.0.1": + version "7.0.1" + resolved "https://registry.yarnpkg.com/@openstreetmap/id-tagging-schema/-/id-tagging-schema-7.0.1.tgz#c383d34ba993ad7488f485cb8fd64e548e8022ce" + integrity sha512-y58T1905O3SJZlf3SLjhKkxxLsZAvYRNpmjE8zsag39lHh23f0IC20wNRDGrl9iUcBdD8zNs5Z/DxEDa3+Fbcg== "@opentelemetry/api-logs@0.53.0": version "0.53.0"