mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
places: offer a newer description in the refresh pill
The pill compared website, phone, e-mail and hours only, so a description the community published never showed up on a saved place. It now reads the index description or the OSM summary, compares it as plain words against the editor's markup, and shows it clamped to three lines.
This commit is contained in:
@@ -3,7 +3,7 @@ import type { PlaceFormData } from './PlaceFormModal.helpers';
|
||||
import { cleanWeek, weekFromPeriods, writeWeek } from './placeHours';
|
||||
|
||||
/** The details a saved place can take over from a fresh lookup. */
|
||||
const REFRESH_FIELDS = ['website', 'phone', 'email', 'opening_hours'] as const;
|
||||
const REFRESH_FIELDS = ['description', 'website', 'phone', 'email', 'opening_hours'] as const;
|
||||
export type RefreshField = (typeof REFRESH_FIELDS)[number];
|
||||
|
||||
/** One field where the lookup says something else than the place holds now. */
|
||||
@@ -21,9 +21,22 @@ type Details = Record<string, unknown>;
|
||||
|
||||
const text = (value: unknown): string => (typeof value === 'string' ? value.trim() : '');
|
||||
|
||||
/** A description as plain words: the editor stores markup, the lookup sends text. */
|
||||
const plain = (value: string): string =>
|
||||
value
|
||||
.replace(/<[^>]*>/g, ' ')
|
||||
.replace(/[*_`~#>]/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
|
||||
/** The lookup's value of a field, written the way the form stores it. */
|
||||
function freshValue(field: RefreshField, details: Details): string {
|
||||
if (field === 'website') return normalizePlaceWebsite(text(details.website)) ?? '';
|
||||
// An index place carries its description as an object, an OSM place as the summary.
|
||||
if (field === 'description') {
|
||||
const own = details.description as { text?: unknown } | null | undefined;
|
||||
return text(own?.text) || text(details.summary);
|
||||
}
|
||||
if (field === 'opening_hours') {
|
||||
const week = weekFromPeriods(details.opening_periods as Parameters<typeof weekFromPeriods>[0]);
|
||||
return week ? writeWeek(cleanWeek(week)) : '';
|
||||
@@ -37,6 +50,7 @@ function sameValue(field: RefreshField, a: string, b: string): boolean {
|
||||
if (field === 'website')
|
||||
return (normalizePlaceWebsite(a) ?? a).replace(/\/$/, '').toLowerCase() === b.replace(/\/$/, '').toLowerCase();
|
||||
if (field === 'email') return a.toLowerCase() === b.toLowerCase();
|
||||
if (field === 'description') return plain(a) === plain(b);
|
||||
if (field === 'opening_hours') {
|
||||
const week = parsePlaceHours(a);
|
||||
return (week ? writeWeek(cleanWeek(week)) : a) === b;
|
||||
|
||||
Reference in New Issue
Block a user