All files / src/utils spaceAgenda.ts

100% Statements 31/31
100% Branches 12/12
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                              7x               12x 11x 11x     12x       8x 8x   2x       20x       4x 4x 4x 4x 12x 12x 10x 10x 10x 10x 6x 6x 6x   10x 10x 8x 8x   10x   4x    
// @implements UC-SPACE-019.1
/**
 * The agenda layout of an item list: its items, in the order the API sorted
 * them on their date, grouped by month and by day. See
 * docs/architecture/FLOW_SPACE.md "Item lists: agenda and load more".
 */
import type { DataItem } from '@/utils/spacePages'
 
export interface AgendaDay {
  key: string
  /** The day of the month, e.g. "21" */
  day: string
  /** The short weekday, e.g. "Sat" */
  weekday: string
  items: DataItem[]
}
export interface AgendaMonth {
  key: string
  /** e.g. "November 2026" */
  label: string
  days: AgendaDay[]
}
 
const DATE_ONLY = /^(\d{4})-(\d{2})-(\d{2})$/
 
/**
 * The day an item is placed on, in the reader's time zone: a date field's
 * value is that day wherever the reader is; a creation date is an instant,
 * shown on the reader's local day
 */
function agendaDate(value: string | undefined): Date | null {
  if (!value) return null
  const dateOnly = DATE_ONLY.exec(value)
  const date = dateOnly
    ? new Date(Number(dateOnly[1]), Number(dateOnly[2]) - 1, Number(dateOnly[3]))
    : new Date(value)
  return Number.isNaN(date.getTime()) ? null : date
}
 
function formatter(lang: string, options: Intl.DateTimeFormatOptions) {
  try {
    return new Intl.DateTimeFormat(lang, options)
  } catch {
    return new Intl.DateTimeFormat('en', options)
  }
}
 
const pad = (n: number) => String(n).padStart(2, '0')
 
/** Groups consecutive items of the same month, then of the same day */
export function agendaMonths(items: DataItem[], lang: string): AgendaMonth[] {
  const monthLabel = formatter(lang, { month: 'long', year: 'numeric' })
  const weekday = formatter(lang, { weekday: 'short' })
  const months: AgendaMonth[] = []
  for (const item of items) {
    const date = agendaDate(item.date)
    if (!date) continue
    const monthKey = `${date.getFullYear()}-${pad(date.getMonth() + 1)}`
    const dayKey = `${monthKey}-${pad(date.getDate())}`
    let month = months[months.length - 1]
    if (month?.key !== monthKey) {
      const label = monthLabel.format(date)
      month = { key: monthKey, label: label.charAt(0).toUpperCase() + label.slice(1), days: [] }
      months.push(month)
    }
    let day = month.days[month.days.length - 1]
    if (day?.key !== dayKey) {
      day = { key: dayKey, day: String(date.getDate()), weekday: weekday.format(date), items: [] }
      month.days.push(day)
    }
    day.items.push(item)
  }
  return months
}