Files
TREK/client/src/components/Collections/libraryDates.ts
T
Maurice bacc6a29a7 feat(places): review possible duplicates, and repair the client gates
All places offers the pairs that look like one place: both sides side by side
with what differs marked, "keep this one" to group them as one place of the
user's own (undo right after), and "not the same place" to set a pair aside for
good. Desktop dialog and phone sheet share usePlaceDuplicates; every locale has
the new strings.

What the last dev run flagged on the client:
- Tailwind's Prettier plugin had trimmed the leading space of conditional class
  names (col-filter-btn, col-lrow sel, col-split map-full, col-detail docked),
  so the filter row and the open row lost their styling; those classes are now
  joined so the formatter cannot glue them.
- The dock position lives in one helper for the lists and All places, which
  moves the measured-position markers there instead of adding new ones.
- usePlacesLibrary is split into filters, pages, detail and cover hooks that
  derive their loading state instead of setting it inside effects; the big
  sheets and views are cut into parts under the complexity limit, dynamic icons
  go through createElement, originName moves out of a component file.
- formatRange is guarded like in the packing export, the cover lookup skips
  places without a position.
- Tests follow the rename to Places, the category icon, the default list icon
  and the new trip collections store; react-hooks/refs is lowered to the count
  the run measured.
- The rating on a place is a round badge again, in the list and on the map.
2026-11-29 16:28:22 +01:00

57 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { PlacesLibraryTripDetail, PlacesLibraryTripOrigin } from '@trek/shared';
/**
* The dates of "All places" in the reader's language: a trip as one short range
* ("1 to 5 Dec" in the locale's own form), a planned day as its weekday and date ("Wed, 2 Dec"). Dates are
* calendar days without a time zone, read and written in UTC so no zone shifts them.
*/
const SHORT: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'short', timeZone: 'UTC' };
const asDate = (iso: string | null | undefined): Date | null => {
if (!iso) return null;
const d = new Date(`${iso.slice(0, 10)}T00:00:00Z`);
return Number.isNaN(d.getTime()) ? null : d;
};
export function tripRange(
start: string | null | undefined,
end: string | null | undefined,
language: string
): string | null {
const a = asDate(start);
const b = asDate(end);
if (a && b) {
// ES2020's lib does not know formatRange yet; every browser TREK runs in has it.
const range = new Intl.DateTimeFormat(language, SHORT) as Intl.DateTimeFormat & {
formatRange?: (from: Date, to: Date) => string;
};
if (range.formatRange) return range.formatRange(a, b);
return `${range.format(a)} – ${range.format(b)}`;
}
const one = a ?? b;
return one ? new Intl.DateTimeFormat(language, SHORT).format(one) : null;
}
export function dayDate(date: string | null | undefined, language: string): string | null {
const d = asDate(date);
return d ? new Intl.DateTimeFormat(language, { ...SHORT, weekday: 'short' }).format(d) : null;
}
/** What a trip origin says under its name: the planned day, or the trip's dates when it is not planned yet. */
export function tripLine(
origin: Pick<PlacesLibraryTripOrigin, 'trip_start' | 'trip_end'>,
detail: Pick<PlacesLibraryTripDetail, 'days'>,
language: string,
t: (key: string, params?: Record<string, string | number>) => string
): string | null {
const day = detail.days[0];
if (!day) return tripRange(origin.trip_start, origin.trip_end, language);
const when = [dayDate(day.date, language), day.time].filter(Boolean).join(', ');
const first = when
? t('collections.library.dayOn', { day: day.day_number, date: when })
: t('collections.library.day', { day: day.day_number });
return detail.days.length > 1
? `${first} ${t('collections.library.moreOrigins', { count: detail.days.length - 1 })}`
: first;
}