mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
f99c7f5421
- index.css stays at its size: the map canvas colour moves to its own stylesheet. - The rating badge on map markers takes its amber and white from a class in ratingBadge.css instead of inline literals; the theme entry drops with it. - Strict mode: the identity a link joins is resolved without a widened union, the description of an entry's origins is string or undefined, and the map search rows are typed. - knip: PLACE_NAME_ATTR is module private, the linked-by enum names the values the service writes, and the unlink contract is its own described schema rather than a second name for the use schema. - The duplicates hook test reads toasts through a module mock instead of the window hook; react-hooks/refs is lowered to the count the run measured. - Tests follow the rename to Places and the list icon on an edit.
67 lines
3.0 KiB
TypeScript
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 : NaN;
|
|
const lng = typeof place.lng === 'number' ? place.lng : 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);
|
|
}
|