mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
31859651d7
Every help screen checked against the v5 redesign. Tours is documented for the first time, as a screen of its own with twelve screens under it, and notifications get help of their own. All pictures are taken again on a realistic demo trip, with short loops where a still cannot show the motion. The English is plain and every language is written naturally rather than word for word. The switcher shows two levels at most, a screen's overview lists its sub-screens as cards above its guides, each wiki page is linked once, and the help sidebar is wider. Fixes found along the way: Save to Collection keeps a tour's route, a track dropped on the places column opens the Tours import, tour Share on desktop, the unsaved tour draft lives on the account, a route click no longer adds an end point, Trip places leaves the trip's tours out, Escape closes a stage's day menu, confirms before destructive deletes, DWD warnings sharing one id kept apart, a booking's date moves with it, and smaller fixes across notifications, bookings, the day plan, stays, Atlas, Collections, Vacay and the admin screens.
82 lines
3.3 KiB
TypeScript
82 lines
3.3 KiB
TypeScript
import { getIntlLanguage } from '@trek/shared'
|
|
|
|
export interface PlaceLocality {
|
|
/** The country's name in the reader's language, or as the address spells it. */
|
|
country: string | null
|
|
/** State, province or prefecture, as resolved from the position. */
|
|
region: string | null
|
|
}
|
|
|
|
interface LocatedPlace {
|
|
address?: string | null
|
|
country_code?: string | null
|
|
region_name?: string | null
|
|
}
|
|
|
|
const hasDigits = (part: string) => /\d/.test(part)
|
|
const hasLetters = (part: string) => /\p{L}/u.test(part)
|
|
|
|
const ADDRESS_ALIASES: Record<string, string> = { usa: 'US', 'u.s.a.': 'US', 'u.s.': 'US', uk: 'GB', 'u.k.': 'GB', uae: 'AE' }
|
|
const countryNameCache = new Map<string, Map<string, string>>()
|
|
|
|
/** Every country name (reader's language and English, lower case) mapped to its code. */
|
|
function countryNames(language: string): Map<string, string> {
|
|
const cached = countryNameCache.get(language)
|
|
if (cached) return cached
|
|
const names = new Map<string, string>()
|
|
try {
|
|
const own = new Intl.DisplayNames([getIntlLanguage(language)], { type: 'region', fallback: 'none' })
|
|
const english = new Intl.DisplayNames(['en'], { type: 'region', fallback: 'none' })
|
|
for (let i = 65; i <= 90; i++) {
|
|
for (let j = 65; j <= 90; j++) {
|
|
const code = String.fromCharCode(i, j)
|
|
const en = english.of(code)
|
|
if (en) names.set(en.toLowerCase(), code)
|
|
}
|
|
}
|
|
for (let i = 65; i <= 90; i++) {
|
|
for (let j = 65; j <= 90; j++) {
|
|
const code = String.fromCharCode(i, j)
|
|
const name = own.of(code)
|
|
if (name) names.set(name.toLowerCase(), code)
|
|
}
|
|
}
|
|
} catch { /* no Intl.DisplayNames: nothing is recognised, so no fallback country */ }
|
|
countryNameCache.set(language, names)
|
|
return names
|
|
}
|
|
|
|
/**
|
|
* The last part of a formatted address is the country, when it names one. A region or
|
|
* city in that spot ("Shibuya City, Tokyo") is not a country and stays out of the filter.
|
|
*/
|
|
function countryFromAddress(address: string | null | undefined, language: string): string | null {
|
|
const parts = (address ?? '').split(',').map(p => p.trim()).filter(Boolean)
|
|
if (parts.length < 2) return null
|
|
const last = parts[parts.length - 1]
|
|
if (hasDigits(last) || !hasLetters(last)) return null
|
|
const code = countryNames(language).get(last.toLowerCase()) ?? ADDRESS_ALIASES[last.toLowerCase()]
|
|
if (!code) return null
|
|
try { return new Intl.DisplayNames([getIntlLanguage(language)], { type: 'region' }).of(code) ?? last } catch { return last }
|
|
}
|
|
|
|
/**
|
|
* Where a place lies, for filtering the trip's places (#2537).
|
|
*
|
|
* Both halves come from the position the server resolved, which is exact: the country
|
|
* code, named in the reader's language, and the region. A city is not offered, since a
|
|
* formatted address cannot tell one apart reliably (it names the borough before Berlin
|
|
* and the county before Schönefeld). A place not resolved yet falls back to the country
|
|
* its address ends on.
|
|
*/
|
|
export function placeLocality(place: LocatedPlace, language: string): PlaceLocality {
|
|
let country: string | null = null
|
|
if (place.country_code) {
|
|
try { country = new Intl.DisplayNames([getIntlLanguage(language)], { type: 'region' }).of(place.country_code.toUpperCase()) ?? null } catch { country = null }
|
|
}
|
|
return {
|
|
country: country ?? countryFromAddress(place.address, language),
|
|
region: place.region_name || null,
|
|
}
|
|
}
|