Files
TREK/client/src/utils/dayLabel.ts
T
Maurice 4003769a5c fix(planner): keep the date whole and rename the title where it stands with the date first
With "date first in day headings" a long day title squeezed the date in
the day list to "Th…" (#2920); the name now gives way and the date stays
whole. The day panel and the mobile day sheet kept the rename pencil on
the heading, which then is the date, and the input replaced it (#2921);
pencil and input now go with the title line under the date.
2026-11-01 11:41:56 +01:00

52 lines
2.7 KiB
TypeScript

import type { Day } from '../types'
type Translate = (key: string, params?: Record<string, string | number>) => string
/**
* How a day is named wherever days are listed as a whole: its title, else its
* date as a short weekday and day of the month, else "Day n" by its position.
*
* The reorder dialog, the phone's day sheet and the delete question all name a
* day, and they have to name it the same way, or the question asks about a day
* the list beside it calls something else. The date is read as a local calendar
* date, and a value that does not parse falls through to the position.
*/
export function dayLabel(day: Pick<Day, 'title' | 'date'>, index: number, t: Translate, locale: string): string {
if (day.title) return day.title
return (day.date && dayDate(day.date, locale)) || t('dayplan.dayN', { n: index + 1 })
}
/**
* A calendar date the way the day list writes it: short weekday, day, month,
* and no year, which the trip around it already says. Anything that talks
* about the days in that list (the next day to add, a moved check-out, the new
* end of the trip) writes its dates this way too, so a date reads the same in
* the button as in the row it becomes. Null when the value does not parse.
*/
export function dayDate(iso: string, locale: string): string | null {
const date = new Date(`${iso.slice(0, 10)}T00:00:00`)
if (Number.isNaN(date.getTime())) return null
return date.toLocaleDateString(locale, { weekday: 'short', day: 'numeric', month: 'short' })
}
/**
* A day heading's two parts in reading order. The day's name ("Day 3" or its own
* title) leads, unless the traveller plans by the calendar and asked for the date
* first (#1953). A day without a date always leads with its name.
*
* `nameFirst` says which of the two is the name: the rename pencil and the title
* input belong with the name wherever it stands (#2921), and it is the name that
* gives way when the line is short, never the date (#2920).
*/
export function dayHeadingParts(name: string, date: string | null | undefined, dateFirst: boolean): { primary: string; secondary: string | null; nameFirst: boolean } {
if (dateFirst && date) return { primary: date, secondary: name, nameFirst: false }
return { primary: name, secondary: date || null, nameFirst: true }
}
/** On one line the name gives way when it runs long, the date stays whole: with the date first a long title squeezed it to "Th…" (#2920). */
export function headingFit({ nameFirst }: { nameFirst: boolean }): { primary: string; secondary: string } {
const gives = 'min-w-0 truncate'
const keeps = 'flex-none whitespace-nowrap'
return nameFirst ? { primary: gives, secondary: keeps } : { primary: keeps, secondary: gives }
}