Files
TREK/client/src/repo/mapMatchRepo.ts
T
Maurice e539860530 fix(quality): resolve reliability findings on dev
The Sonar quality gate rated new code D. Preserve focus and keyboard interaction through native controls and shared dialog behavior. Replace ambiguous regex scans with bounded parsing and make numeric coercion and initialization explicit.
2026-12-01 21:06:05 +01:00

67 lines
3.0 KiB
TypeScript

import { mapsApi } from '../api/client';
import { isEffectivelyOffline } from '../sync/networkMode';
/** A spot the map search found for a place that so far is only a name and an address. */
export interface MapMatch {
lat: number;
lng: number;
/** What the search found, as the reader sees it: its name, and its address when it has one. */
label: string;
/** The found place's own address, for a stay whose booking named none. */
address?: string;
}
/**
* The ways to ask for a stay, best first: name and address together, then the
* name alone, then the address alone. A booking site's romanized address (an
* Agoda stay in Japan, say) often matches nothing, and asked together with the
* hotel's name it sinks the name too; the name alone usually finds the hotel.
*/
export function stayQueries(name: string, address: string | null | undefined): string[] {
const n = name.trim();
const a = (address ?? '').trim();
const queries = [n && a ? `${n} ${a}` : '', n, a].filter((q) => q.length >= 3);
return [...new Set(queries)];
}
/**
* The first spot the map search knows for one of `queries`, asked in order.
* Online only, on purpose: offline there is no search to ask, and the answer
* is null rather than a guess.
*/
export async function findOnMap(queries: string[], lang?: string): Promise<MapMatch | null> {
if (isEffectivelyOffline()) return null;
for (const query of queries) {
const res = lang ? await mapsApi.search(query, lang) : await mapsApi.search(query);
for (const row of res?.places ?? []) {
const match = toMatch(row, query);
if (match) return match;
}
}
return null;
}
/** One search hit as a match, or null when it has no coordinates to put on the map. */
function toMatch(row: unknown, query: string): MapMatch | null {
const place = row as { lat?: unknown; lng?: unknown; name?: unknown; address?: unknown };
const lat = typeof place.lat === 'number' ? place.lat : Number.NaN;
const lng = typeof place.lng === 'number' ? place.lng : Number.NaN;
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return null;
const parts = [place.name, place.address].filter((v): v is string => typeof v === 'string' && v.trim() !== '');
const address = typeof place.address === 'string' && place.address.trim() ? place.address : undefined;
return { lat, lng, label: parts.join(', ') || query, ...(address ? { address } : {}) };
}
/**
* Every spot the map search found for `query` that has coordinates, for a picker
* that lets the reader choose the right one. Online only, like findOnMap: offline
* the answer is an empty list, not a guess.
*/
export async function searchMapSpots(query: string, lang?: string): Promise<MapMatch[]> {
const q = query.trim();
if (!q || isEffectivelyOffline()) return [];
const res = lang ? await mapsApi.search(q, lang) : await mapsApi.search(q);
const rows: readonly unknown[] = res?.places ?? [];
return rows.map((row: unknown) => toMatch(row, q)).filter((m: MapMatch | null): m is MapMatch => m !== null);
}