import { Temporal } from '@js-temporal/polyfill'; import { DESK_WINDOW_DAYS, type AdminEvent, type HomeEvent, type HomeShift, type AdminShift, type AdminShiftSummary, type AdminTemplate, type MoveTarget, type RosterPerson } from './admin/store.js'; import { spotsSentence } from './schedule.js'; import type { EventValues, ShiftValues, TemplateValues } from './admin/forms.js'; import { MAX_REMINDER_HOURS, MAX_RETENTION_MONTHS, type SettingsErrors, type SettingsForm } from './settings.js'; import type { AppConfig } from './config.js'; import { CONTACT_LIST_PAGE, contactImportSentence, contactSkipSentence, type Contact, type ContactImportSummary, type ContactManualErrors, type ContactManualValues, type ContactShift, } from './contacts.js'; import type { SignupErrors, SignupValues } from './signup-form.js'; import { PartySizeField } from './pages.js'; import { addLocalDays, formatInstantInZone, todayInZone } from './time.js'; import { ShiftTargetOptions } from './shift-options.js'; import type { AdminAccount, AdminErrors, AdminForm } from './admins.js'; import { assetHref } from './static-assets.js'; import { appPath } from './url.js'; export type WalkInState = { token: string; values: SignupValues; errors: SignupErrors; message?: string; contactId?: string; }; export type ConfirmPrompt = { action: string; title: string; body: string; detail?: string; note?: string; submit?: string; error?: string; fields?: Record; }; const SHIFT_DELETE_BODY = 'This deletes the shift. It does not send email.'; const SHIFT_PEOPLE_BODY = 'Remove everyone on this roster first.'; const SHIFT_WAIT_BODY = 'Cancel this shift, or wait until it has ended, before removing it.'; const SHIFT_WEEKLY_NOTE = 'This date came from a weekly template that is still active. Saving that template puts this date back. End that template first if the date should stay gone.'; const PERSON_DELETE_BODY = 'This deletes their name, email, phone, and notes from the roster. It does not send email.'; const PERSON_WAIT_BODY = 'Cancel this signup before removing it.'; const PERSON_CANCEL_BODY = 'Cancelling a signup emails them. Their old private link stops working, and the email includes a new one.'; const SHIFT_CANCEL_EMPTY = 'No one is signed up. Cancelling closes the shift, takes it off this list, and does not send email.'; const SHIFT_CANCEL_MANY = 'This closes the selected shifts and takes them off this list. A cancellation email is queued for each person who is signed up.'; const SHIFT_CANCEL_KEEP = 'A template will not add these dates again.'; const SHIFT_CANCEL_ENDED = 'Those shifts have ended.'; const SHIFT_CANCEL_PARTIAL = 'Shifts that have ended stay on the list.'; const SHIFT_ALREADY = 'This shift is already cancelled.'; const SHORT_WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] as const; const SHORT_MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] as const; const TEMPLATE_END_BODY = 'Ending it stops later weeks from being added. Shifts that have not ended are cancelled and leave this list, and a cancellation email is queued for each person signed up. Shifts that already ended are deleted, including the names and emails on those rosters. That part does not send email.'; const TEMPLATE_ENDED_BODY = 'This template has ended. The shifts it added are off this list.'; const TEMPLATE_DELETE_BODY = 'This deletes the template. Any shifts left are no longer tied to this weekly pattern. Cancelled shifts stay off this list.'; const TEMPLATE_WAIT_BODY = 'End this template before removing it.'; export function shiftRemovalPrompt( shift: { role: string; whenLabel?: string; signupCount: number; weeklyActive: boolean; cancelled: boolean; ended: boolean }, error?: string, ): Omit { const people = error === SHIFT_PEOPLE_BODY || (error !== SHIFT_WAIT_BODY && shift.signupCount > 0); const waiting = !people && (error === SHIFT_WAIT_BODY || (!shift.cancelled && !shift.ended)); const body = people ? SHIFT_PEOPLE_BODY : waiting ? SHIFT_WAIT_BODY : SHIFT_DELETE_BODY; return { title: `Remove ${shift.role}`, detail: shift.whenLabel?.replace(/, (\d{1,2}:\d{2})/, '\n$1'), body, note: people || waiting || !shift.weeklyActive ? undefined : SHIFT_WEEKLY_NOTE, submit: people || waiting ? undefined : 'Remove this shift', error: error && error !== body ? error : undefined, }; } export function signupRemovalPrompt(signup: { name: string | null; status: string }, error?: string): Omit { const blocked = error === PERSON_WAIT_BODY || signup.status !== 'cancelled'; const body = blocked ? PERSON_WAIT_BODY : PERSON_DELETE_BODY; return { title: `Remove ${signup.name ?? 'This roster record'}`, body, submit: blocked ? undefined : 'Remove this person', error: error && error !== body ? error : undefined, }; } export function signupCancelPrompt(signup: { name: string | null; status: string }): Omit { if (signup.status === 'cancelled') { return { title: `Cancel ${signup.name ?? 'this signup'}`, body: 'That signup was already cancelled.' }; } return { title: `Cancel ${signup.name ?? 'this signup'}`, body: PERSON_CANCEL_BODY, submit: 'Cancel signup', }; } export function locationChangePrompt(people: number, location: string): Omit { const place = location.trim(); const count = people === 1 ? '1 person' : `${people} people`; const body = people === 0 ? 'No one is signed up for a shift that has not ended. This does not send email.' : people === 1 ? 'This emails 1 person about the new location. Their signup link stays the same.' : `This emails ${people} people about the new location. Each person gets one email. Their signup links stay the same.`; return { title: 'Change the location', detail: count, note: place ? `New location: ${place}` : 'The address will be removed.', body, submit: people === 0 ? 'Save location' : 'Save and email', }; } export function shiftCancelPrompt(shift: { role: string; whenLabel?: string; cancelled: boolean; confirmed: number }): Omit { if (shift.cancelled) { return { title: `Cancel ${shift.role}`, detail: whenDetail(shift.whenLabel), body: SHIFT_ALREADY }; } const confirmed = shift.confirmed; return { title: `Cancel ${shift.role}`, detail: whenDetail(shift.whenLabel), body: confirmed === 0 ? SHIFT_CANCEL_EMPTY : `This closes the shift and takes it off this list. ${confirmed} confirmed ${confirmed === 1 ? 'signup' : 'signups'} will be marked cancelled, and a cancellation email is queued for each.`, submit: 'Cancel this shift', }; } export function eventRemovalPrompt( event: { title: string; published: boolean; shiftCount: number; templateCount: number; signupCount: number }, error?: string, ): Omit { const expected = eventDeleteBody(event); return { title: `Remove ${event.title}`, body: error && error !== expected ? error : expected, note: event.published ? 'This event is on the public list. Removing it takes that listing away.' : undefined, submit: error ? undefined : 'Remove this event', }; } export function templateEndPrompt(template: { role: string; weekday: number; active: boolean }): Omit { const title = `End ${weekdayName(template.weekday)} ${template.role}`; if (!template.active) return { title, body: TEMPLATE_ENDED_BODY }; return { title, body: TEMPLATE_END_BODY, submit: 'End this template' }; } export function contactRemovalPrompt(contact: { name: string; email: string }): Omit { return { title: `Remove ${contact.name}`, detail: contact.email, body: 'This deletes their name, email, and phone from the contact list. It does not cancel a signup and it does not send email.', submit: 'Remove this contact', }; } export function adminRemovalPrompt(admin: { name: string; email: string; self: boolean; last: boolean }): Omit { const title = admin.self ? 'Remove your access' : `Remove ${admin.name}`; if (admin.last) { return { title, detail: admin.email, body: 'Add another administrator before removing this one.' }; } if (admin.self) { return { title, detail: admin.email, body: 'This signs you out. Your sign-in links stop working. Someone else who still has access can add you again. It does not send email.', submit: 'Remove your access', }; } return { title, detail: admin.email, body: 'This removes their access. Their sign-in links stop working. It does not send email.', submit: 'Remove this administrator', }; } export function templateRemovalPrompt( template: { role: string; weekday: number; active: boolean }, error?: string, ): Omit { const title = `Remove ${weekdayName(template.weekday)} ${template.role}`; const blocked = template.active || error === TEMPLATE_WAIT_BODY; const body = blocked ? TEMPLATE_WAIT_BODY : TEMPLATE_DELETE_BODY; return { title, body, submit: blocked ? undefined : 'Remove this template', error: error && error !== body ? error : undefined, }; } function whenDetail(whenLabel?: string): string | undefined { return whenLabel?.replace(/, (\d{1,2}:\d{2})/, '\n$1'); } function Crumb(props: { items: { href?: string; label: string }[] }) { return ( ); } export function AdminHome(props: { config: AppConfig; name: string; events: HomeEvent[]; shifts: HomeShift[]; now: string; csrf: string; notice?: string; exportFrom?: string; exportTo?: string; exportError?: string; backupNote: string; backupState: 'saved' | 'waiting' | 'behind'; backupSavedToday: boolean; sessionEndsAt: string; newEvent?: { values: EventValues; error?: string }; }) { const draft = props.newEvent?.values ?? { title: '', slug: '', description: '', location: '', tz: props.config.defaultTimeZone, published: false, }; const clock = formatInstantInZone(props.now, props.config.defaultTimeZone); const sessionEnds = formatInstantInZone(props.sessionEndsAt, props.config.defaultTimeZone); const thisMonth = monthBounds(clock.date, 0); const lastMonth = monthBounds(clock.date, -1); const exportFrom = props.exportFrom || thisMonth.from; const exportTo = props.exportTo || thisMonth.to; const preset = exportFrom === thisMonth.from && exportTo === thisMonth.to ? 'this' : exportFrom === lastMonth.from && exportTo === lastMonth.to ? 'last' : 'custom'; const openShifts = props.shifts.filter((shift) => !shift.full); const openDates = new Set(openShifts.map((shift) => shift.date)); const dayShifts = props.shifts.filter((shift) => openDates.has(shift.date)); const fullOnly = props.shifts.filter((shift) => shift.full && !openDates.has(shift.date)); return ( <>

Coordinator

{clock.dateLabel}

Signed in as {props.name}. This sign-in ends on {sessionEnds.dateLabel}.

{props.backupSavedToday ? : }

Shifts that need volunteers

Next 2 weeks

{openShifts.length === 0 ?

No open shifts in the next 2 weeks.

: } {fullOnly.length > 0 ? : null}
{props.events.length === 0 ?

No events yet.

: }