import type { PlaceActivity, PlaceActivityCandidate, PlaceHistoryEntry } from '@trek/shared'; import type { TranslationFn } from '../../types'; import { dayDate } from './libraryDates'; /** The words for one changed detail: which detail, and the old and the new value as the reader reads them. */ export function historyLine( entry: PlaceHistoryEntry, t: TranslationFn ): { field: string; from: string | null; to: string | null } { const field = t(`placeHistory.field.${entry.field}`); // A picture or the opening hours say nothing as text: only that they changed. if (entry.field === 'image' || entry.field === 'opening_hours') return { field, from: null, to: null }; if (entry.field === 'status') { const word = (v: string | null) => (v ? t(`collections.status.${v}`) : null); return { field, from: word(entry.old_value), to: word(entry.new_value) }; } return { field, from: entry.old_value, to: entry.new_value }; } /** Who changed it: the person, TREK itself, or someone whose account is gone. */ export function historyEditor(entry: PlaceHistoryEntry, t: TranslationFn): string { if (entry.automatic) return t('placeHistory.automatic'); return entry.editor?.username ?? t('placeHistory.formerMember'); } /** When it changed, as a short date and time in the reader's language. */ export function historyWhen(entry: PlaceHistoryEntry, language: string): string { if (!entry.changed_at) return ''; const d = new Date(`${entry.changed_at.replace(' ', 'T')}${entry.changed_at.endsWith('Z') ? '' : 'Z'}`); if (Number.isNaN(d.getTime())) return ''; return new Intl.DateTimeFormat(language, { day: 'numeric', month: 'short', year: 'numeric', hour: '2-digit', minute: '2-digit', }).format(d); } /** The second line of a tour or booking: where it lives and when it is. */ export function activityLine( activity: PlaceActivity | PlaceActivityCandidate, language: string, t: TranslationFn ): string { const where = activity.origin.kind === 'trip' ? activity.origin.trip_title : activity.origin.collection_name; const day = activity.date ? dayDate(activity.date, language) : null; const when = day ?? (activity.day_number != null ? t('collections.library.day', { day: activity.day_number }) : null); const kind = activity.type === 'booking' ? t('placeActivities.booking') : t('placeActivities.tour'); return [kind, where, when].filter(Boolean).join(', '); } /** A position as the location badge shows it, five decimals, about a metre. */ export function coordsText(lat: number, lng: number): string { return `${lat.toFixed(5)}, ${lng.toFixed(5)}`; }