mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +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:
@@ -10,6 +10,7 @@ import type { RefreshCandidate, RefreshField } from './placeRefresh';
|
||||
type Translate = (key: string, params?: Record<string, string | number>) => string;
|
||||
|
||||
const LABEL: Record<RefreshField, string> = {
|
||||
description: 'places.formDescription',
|
||||
website: 'places.formWebsite',
|
||||
phone: 'places.formPhone',
|
||||
email: 'places.formEmail',
|
||||
@@ -100,7 +101,7 @@ export function PlaceRefreshPill({
|
||||
{t('places.refresh.now')}
|
||||
</p>
|
||||
{shown(c.field, c.current, locale, t).map((line) => (
|
||||
<p key={line} className="m-0 truncate">
|
||||
<p key={line} className={c.field === 'description' ? 'm-0 line-clamp-3' : 'm-0 truncate'}>
|
||||
{line}
|
||||
</p>
|
||||
))}
|
||||
@@ -110,7 +111,7 @@ export function PlaceRefreshPill({
|
||||
{t('places.refresh.new')}
|
||||
</p>
|
||||
{shown(c.field, c.next, locale, t).map((line) => (
|
||||
<p key={line} className="m-0 truncate">
|
||||
<p key={line} className={c.field === 'description' ? 'm-0 line-clamp-3' : 'm-0 truncate'}>
|
||||
{line}
|
||||
</p>
|
||||
))}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-PLANNER-REFRESH-001 to FE-PLANNER-REFRESH-005
|
||||
// FE-PLANNER-REFRESH-001 to FE-PLANNER-REFRESH-007
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { DEFAULT_FORM, type PlaceFormData } from './PlaceFormModal.helpers';
|
||||
import { refreshCandidates } from './placeRefresh';
|
||||
@@ -46,6 +46,24 @@ describe('refreshCandidates', () => {
|
||||
expect(refreshCandidates(form({ opening_hours: hours?.next }), { opening_periods: PERIODS })).toEqual([]);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-REFRESH-006: a description comes from the index object or the OSM summary', () => {
|
||||
const text = 'An Asian restaurant in Evershagen.';
|
||||
expect(refreshCandidates(form(), { description: { text, source: 'community' } })).toEqual([
|
||||
{ field: 'description', current: '', next: text, preselected: true },
|
||||
]);
|
||||
expect(refreshCandidates(form(), { summary: text, summary_source: 'community' })).toEqual([
|
||||
{ field: 'description', current: '', next: text, preselected: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-REFRESH-007: the same description under the editor markup is no difference', () => {
|
||||
const text = 'An Asian restaurant in Evershagen.';
|
||||
expect(refreshCandidates(form({ description: `<p>${text}</p>` }), { summary: text })).toEqual([]);
|
||||
expect(refreshCandidates(form({ description: 'Our favourite.' }), { summary: text })).toEqual([
|
||||
{ field: 'description', current: 'Our favourite.', next: text, preselected: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-REFRESH-005: what the lookup does not know is never offered, so nothing is emptied', () => {
|
||||
expect(refreshCandidates(form({ phone: '0381 1111', website: 'https://a.example' }), {})).toEqual([]);
|
||||
expect(refreshCandidates(form(), null)).toEqual([]);
|
||||
|
||||
@@ -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