/** * The appointments page (Funnelforms → Appointments): what is booked as an agenda by day or as a * week or month calendar, with filters and key figures; each appointment opens its details, where * the team moves it to another free time, cancels it with a message, or marks how it went. Users * who edit forms also set up the appointment types and word the visitor's mails of each here. * * The page sits in the frame of the other Funnelforms pages (admin.css); this screen uses its * cards, buttons and badges plus the appointments block at the end of admin.css. */ import React, { useCallback, useEffect, useId, useMemo, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { localeTag } from '@funnelforms/form-core'; import { TypesTab } from './AppointmentTypes'; import { addDays, addressOf, AppointmentsClient, byDay, counts, dayOf, figures, filtered, insertAt, lanes, MAIL_KINDS, minutesOf, rangeOf, startOfMonth, stateFromAddress, stateOf, step, zoneName, zoneOf, type AppointmentItem, type AppointmentsConfig, type AppointmentState, type Filter, type FreeTimes, type MailKind, type Mails, type MailSettings, type PageState, type StatusFilter, type View, } from '../appointments'; import { __, sprintf } from './i18n'; /* Formatting ---------------------------------------------------------------------------- */ interface Formats { locale: string; zone: string | undefined; time: ( iso: string ) => string; /** "Monday, 20 October". */ day: ( day: string ) => string; /** "Mon". */ weekdayShort: ( day: string ) => string; /** "09:00" of an hour of the day. */ hour: ( hour: number ) => string; /** "Monday, 20 October 2026, 10:00". */ long: ( iso: string ) => string; range: ( from: string, to: string, view: View ) => string; } function localeOf( userLocale: string | undefined ): string { return localeTag( userLocale ) ?? 'en-US'; } /** A day (Y-m-d) as a Date at noon UTC, formatted in UTC: the same day everywhere. */ function noon( day: string ): Date { return new Date( day + 'T12:00:00Z' ); } function makeFormats( locale: string, zone: string | undefined ): Formats { const time = new Intl.DateTimeFormat( locale, { timeZone: zone, hour: '2-digit', minute: '2-digit' } ); const day = new Intl.DateTimeFormat( locale, { timeZone: 'UTC', weekday: 'long', day: 'numeric', month: 'long' } ); const short = new Intl.DateTimeFormat( locale, { timeZone: 'UTC', weekday: 'short' } ); const long = new Intl.DateTimeFormat( locale, { timeZone: zone, weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' } ); const month = new Intl.DateTimeFormat( locale, { timeZone: 'UTC', month: 'long', year: 'numeric' } ); const span = new Intl.DateTimeFormat( locale, { timeZone: 'UTC', day: 'numeric', month: 'long', year: 'numeric' } ); const hour = new Intl.DateTimeFormat( locale, { timeZone: 'UTC', hour: '2-digit', minute: '2-digit' } ); return { locale, zone, time: ( iso ) => time.format( new Date( iso ) ), day: ( d ) => day.format( noon( d ) ), weekdayShort: ( d ) => short.format( noon( d ) ), hour: ( h ) => hour.format( Date.UTC( 2026, 0, 1, h ) ), long: ( iso ) => long.format( new Date( iso ) ) + ', ' + time.format( new Date( iso ) ), range: ( from, to, view ) => { if ( 'month' === view ) { return month.format( noon( from ) ); } const ranged = span as Intl.DateTimeFormat & { formatRange?: ( a: Date, b: Date ) => string }; return ranged.formatRange ? ranged.formatRange( noon( from ), noon( to ) ) : span.format( noon( from ) ) + ' – ' + span.format( noon( to ) ); }, }; } function duration( minutes: number ): string { if ( minutes >= 1440 && 0 === minutes % 1440 ) { return 1440 === minutes ? __( '1 day', 'funnelforms-pro' ) : sprintf( /* translators: %d: number of days */ __( '%d days', 'funnelforms-pro' ), minutes / 1440 ); } const h = Math.floor( minutes / 60 ); const m = minutes % 60; return [ h ? sprintf( __( '%d h', 'funnelforms-pro' ), h ) : '', m || ! h ? sprintf( __( '%d min', 'funnelforms-pro' ), m ) : '' ].filter( Boolean ).join( ' ' ); } function appointmentsCount( n: number ): string { return 1 === n ? __( '1 appointment', 'funnelforms-pro' ) : sprintf( __( '%d appointments', 'funnelforms-pro' ), n ); } /** Who booked: the name, else the e-mail address. */ function visitorName( a: AppointmentItem ): string { return a.visitor.name || a.visitor.email || sprintf( __( 'Entry #%d', 'funnelforms-pro' ), a.entryId ); } export function stateLabel( state: AppointmentState ): string { return { upcoming: __( 'Upcoming', 'funnelforms-pro' ), open: __( 'To mark', 'funnelforms-pro' ), done: __( 'Done', 'funnelforms-pro' ), noshow: __( 'No-show', 'funnelforms-pro' ), cancelled: __( 'Cancelled', 'funnelforms-pro' ), }[ state ]; } const TONES: Record = { upcoming: 'info', open: 'warning', done: 'success', noshow: 'danger', cancelled: '' }; function StateBadge( { state }: { state: AppointmentState } ) { return { stateLabel( state ) }; } /* Icons (Lucide, ISC licence) ---------------------------------------------------------- */ const PATHS = { calendar: 'M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z', clock: 'M12 6v6l4 2M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20z', check: 'M20 6 9 17l-5-5', alert: 'M12 8v4M12 16h.01M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20z', userX: 'M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2M9 3a4 4 0 1 0 0 8 4 4 0 0 0 0-8zM17 8l5 5M22 8l-5 5', left: 'm15 18-6-6 6-6', right: 'm9 18 6-6-6-6', list: 'M3 12h.01M3 18h.01M3 6h.01M8 12h13M8 18h13M8 6h13', week: 'M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2zM10 10v12M14 10v12', month: 'M8 2v4M16 2v4M3 10h18M5 4h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2zM3 16h18M9 10v12M15 10v12', move: 'M21 7.5V6a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h3.5M16 2v4M8 2v4M3 10h5M17.5 17.5 16 16.3V14M22 16a6 6 0 1 1-12 0 6 6 0 0 1 12 0z', x: 'M18 6 6 18M6 6l12 12', external: 'M15 3h6v6M10 14 21 3M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6', mail: 'm22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7M4 4h16a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2z', undo: 'M9 14 4 9l5-5M4 9h10.5a5.5 5.5 0 0 1 0 11H11', }; function Icon( { name }: { name: keyof typeof PATHS } ) { return ( ); } /* The screen ---------------------------------------------------------------------------- */ export interface AppointmentsScreenProps { config: AppointmentsConfig; /** The API; the page's own without it. */ client?: AppointmentsClient; /** Now, for tests. */ now?: () => number; } export function AppointmentsScreen( { config, client: given, now: clock = Date.now }: AppointmentsScreenProps ) { const client = useMemo( () => given ?? new AppointmentsClient( config.root, config.nonce ), [ given, config.root, config.nonce ] ); const zone = useMemo( () => zoneOf( config.timezone ), [ config.timezone ] ); const f = useMemo( () => makeFormats( localeOf( config.userLocale ), zone ), [ config.userLocale, zone ] ); const [ page, setPage ] = useState( () => stateFromAddress( window.location.search ) ); const today = dayOf( clock(), zone ); const day = page.day || today; const range = useMemo( () => rangeOf( page.view, day, config.weekStart ), [ page.view, day, config.weekStart ] ); const [ items, setItems ] = useState( null ); const [ recent, setRecent ] = useState( null ); const [ error, setError ] = useState( null ); const [ version, setVersion ] = useState( 0 ); const [ open, setOpen ] = useState( null ); const [ said, say ] = useState( '' ); const go = useCallback( ( changes: Partial ) => { setPage( ( p ) => { const next = { ...p, ...changes }; const url = addressOf( next, window.location.href ); if ( url !== window.location.href ) { window.history.replaceState( null, '', url ); } return next; } ); }, [] ); const setFilter = ( changes: Partial ): void => go( { filter: { ...page.filter, ...changes } } ); // The appointments of the days shown. useEffect( () => { let live = true; setError( null ); client.list( range.from, range.to ).then( ( list ) => live && setItems( list ), ( e: unknown ) => { if ( live ) { setItems( [] ); setError( e instanceof Error ? e.message : String( e ) ); } } ); return () => { live = false; }; }, [ client, range.from, range.to, version ] ); // The last 30 and the next 7 days, for the key figures. useEffect( () => { let live = true; client.list( addDays( today, -30 ), addDays( today, 7 ) ).then( ( list ) => live && setRecent( list ), () => live && setRecent( [] ) ); return () => { live = false; }; }, [ client, today, version ] ); const changed = ( updated: AppointmentItem, message: string ): void => { setItems( ( list ) => list && list.map( ( a ) => ( a.id === updated.id ? updated : a ) ) ); setOpen( ( current ) => ( current && current.id === updated.id ? updated : current ) ); say( message ); setVersion( ( v ) => v + 1 ); }; const now = clock(); const shown = items ? filtered( items, page.filter, config.user, now ) : []; const numbers = items ? counts( items, page.filter, config.user, now ) : null; const target = typeof document === 'undefined' ? null : document.getElementById( 'funnelforms-appointments-actions' ); const views = go( { view } ) } />; return (
{ config.canEditTypes ? ( ) : null } { 'types' === page.tab && config.canEditTypes ? : null } { 'mails' === page.tab && config.canEditTypes ? : null } { 'calendar' !== page.tab && config.canEditTypes ? null : ( <> { target && 'calendar' === page.tab ? createPortal( views, target ) : null } go( { view: 'list', day: first, filter: { ...page.filter, status: 'open' } } ) } />

{ f.range( 'month' === page.view ? startOfMonth( day ) : range.from, range.to, page.view ) }

{ target ? null : views }
{ error ? (

{ error }

) : null } { null === items ? (

{ __( 'Loading appointments…', 'funnelforms-pro' ) }

) : 'list' === page.view ? ( ) : 'week' === page.view ? ( ) : ( go( { view: 'list', day: d } ) } /> ) }
) } { open ? setOpen( null ) } /> : null }

{ said }

); } function ViewSwitch( { view, onChange }: { view: View; onChange: ( view: View ) => void } ) { const labels: Record = { list: __( 'List', 'funnelforms-pro' ), week: __( 'Week', 'funnelforms-pro' ), month: __( 'Month', 'funnelforms-pro' ) }; return (
{ ( [ 'list', 'week', 'month' ] as const ).map( ( v ) => ( ) ) }
); } /* Key figures --------------------------------------------------------------------------- */ function KeyFigures( { items, zone, now, f, onOpenList }: { items: AppointmentItem[] | null; zone: string | undefined; now: number; f: Formats; onOpenList: ( first: string ) => void } ) { const n = items ? figures( items, zone, now ) : null; const value = ( v: number | undefined ): string => ( undefined === v ? '–' : v.toLocaleString( f.locale ) ); const hours = n ? Math.round( n.weekMinutes / 6 ) / 10 : 0; return (
{ n?.nextToday ? sprintf( __( 'Next at %1$s: %2$s', 'funnelforms-pro' ), f.time( n.nextToday.start ), visitorName( n.nextToday ) ) : n && n.today ? __( 'None left for today', 'funnelforms-pro' ) : __( 'Nothing booked for today', 'funnelforms-pro' ) } { sprintf( __( '%s hours booked', 'funnelforms-pro' ), hours.toLocaleString( f.locale ) ) } { n && n.firstOpen ? ( ) : ( __( 'Past appointments of the last 30 days', 'funnelforms-pro' ) ) } { n && n.past ? sprintf( __( '%1$s %% of %2$d past appointments, last 30 days', 'funnelforms-pro' ), Math.round( ( 100 * n.noshow ) / n.past ).toLocaleString( f.locale ), n.past ) : __( 'Last 30 days', 'funnelforms-pro' ) }
); } function Kpi( { icon, label, value, tone = '', children }: { icon: keyof typeof PATHS; label: string; value: string; tone?: string; children: React.ReactNode } ) { return (
{ label } { value } { children }
); } /* Filters --------------------------------------------------------------------------------- */ function Filters( { config, filter, numbers, onChange }: { config: AppointmentsConfig; filter: Filter; numbers: Record | null; onChange: ( changes: Partial ) => void } ) { const id = useId(); const statuses: Array<[ StatusFilter, string ]> = [ [ 'active', __( 'Not cancelled', 'funnelforms-pro' ) ], [ 'upcoming', __( 'Upcoming', 'funnelforms-pro' ) ], [ 'open', __( 'To mark', 'funnelforms-pro' ) ], [ 'done', __( 'Done', 'funnelforms-pro' ) ], [ 'noshow', __( 'No-show', 'funnelforms-pro' ) ], [ 'cancelled', __( 'Cancelled', 'funnelforms-pro' ) ], [ 'all', __( 'All', 'funnelforms-pro' ) ], ]; return (
); } /* Views ----------------------------------------------------------------------------------- */ interface ViewProps { items: AppointmentItem[]; days: string[]; today: string; f: Formats; now: number; onOpen: ( a: AppointmentItem ) => void; } function label( a: AppointmentItem, f: Formats, now: number ): string { return [ f.long( a.start ), visitorName( a ), a.type?.title ?? '', stateLabel( stateOf( a, now ) ) ].filter( Boolean ).join( ', ' ); } function Empty( { children }: { children: React.ReactNode } ) { return (

{ __( 'No appointments in these days', 'funnelforms-pro' ) }

{ children }

); } function Agenda( { items, days, today, f, now, onOpen }: ViewProps ) { const zone = f.zone; const grouped = byDay( items, zone ); const shown = days.filter( ( d ) => grouped.has( d ) ); if ( ! shown.length ) { return { __( 'Go to later days, or change the filters.', 'funnelforms-pro' ) }; } return (
    { shown.map( ( d ) => { const list = grouped.get( d ) ?? []; return (
  1. { f.day( d ) } { d === today ? { __( 'Today', 'funnelforms-pro' ) } : null } { appointmentsCount( list.length ) }

      { list.map( ( a ) => ( ) ) }
  2. ); } ) }
); } function AgendaRow( { a, f, now, onOpen }: { a: AppointmentItem; f: Formats; now: number; onOpen: ( a: AppointmentItem ) => void } ) { const state = stateOf( a, now ); const contact = [ a.visitor.email, a.visitor.phone ].filter( Boolean ).join( ' · ' ); const what = [ a.type?.title ?? __( 'Deleted appointment type', 'funnelforms-pro' ), a.form?.title ?? '' ].filter( Boolean ).join( ' · ' ); return (
  • { f.time( a.start ) } { f.time( a.end ) } { what } { a.people.length ? a.people.map( ( p ) => p.name ).join( ', ' ) : duration( a.duration ) }
  • ); } /** Hours shown at least in the week. */ const FIRST_HOUR = 8; const LAST_HOUR = 18; /** Height of an hour in the week, in px. */ const HOUR = 56; function Week( { items, days, today, f, now, onOpen }: ViewProps ) { const zone = f.zone; const grouped = byDay( items, zone ); let first = FIRST_HOUR; let last = LAST_HOUR; for ( const a of items ) { const start = minutesOf( a.start, zone ); const end = dayOf( a.end, zone ) === dayOf( a.start, zone ) ? minutesOf( a.end, zone ) : 1440; first = Math.min( first, Math.floor( start / 60 ) ); last = Math.max( last, Math.ceil( end / 60 ) ); } const hours = Array.from( { length: last - first }, ( _, i ) => first + i ); const nowDay = dayOf( now, zone ); const nowMinutes = minutesOf( now, zone ); return (
    { days.map( ( d ) => { const list = grouped.get( d ) ?? []; const placed = lanes( list ); return (

    { f.day( d ) + ': ' + appointmentsCount( list.length ) }

    { d === nowDay && nowMinutes >= first * 60 && nowMinutes <= last * 60 ?
    ); } ) }
    ); } /** Appointments listed in a day of the month before "+ n more". */ const MONTH_CHIPS = 3; function Month( { items, days, month, today, f, now, onOpen, onDay }: ViewProps & { month: string; onDay: ( day: string ) => void } ) { const grouped = byDay( items, f.zone ); return (
    { days.map( ( d ) => { const list = grouped.get( d ) ?? []; return (

    { f.day( d ) + ': ' + appointmentsCount( list.length ) }

    { list.slice( 0, MONTH_CHIPS ).map( ( a ) => ( ) ) } { list.length > MONTH_CHIPS ? ( ) : null }
    ); } ) }
    ); } /* The appointment ------------------------------------------------------------------------- */ type Mode = 'details' | 'move' | 'cancel'; function AppointmentDialog( { appointment: a, client, config, f, now, onChanged, onClose }: { appointment: AppointmentItem; client: AppointmentsClient; config: AppointmentsConfig; f: Formats; now: () => number; onChanged: ( a: AppointmentItem, message: string ) => void; onClose: () => void } ) { const ref = useRef( null ); const titleId = useId(); const [ mode, setMode ] = useState( 'details' ); const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState( null ); const [ done, setDone ] = useState( null ); const [ message, setMessage ] = useState( '' ); // The free time chosen while moving, with how it reads. const [ chosen, setChosen ] = useState<{ start: string; label: string } | null>( null ); const state = stateOf( a, now() ); const who = visitorName( a ); useEffect( () => { const dialog = ref.current; if ( dialog && ! dialog.open ) { dialog.showModal?.(); } }, [] ); const run = async ( action: () => Promise, success: string ): Promise => { setBusy( true ); setError( null ); try { const updated = await action(); setMode( 'details' ); setMessage( '' ); setChosen( null ); setDone( success ); onChanged( updated, success ); } catch ( e ) { setError( e instanceof Error ? e.message : String( e ) ); } setBusy( false ); }; const switchTo = ( next: Mode ): void => { setMode( next ); setError( null ); setDone( null ); setChosen( null ); }; const entryUrl = config.entriesUrl + ( config.entriesUrl.includes( '?' ) ? '&' : '?' ) + 'entry=' + a.entryId; return (

    { 'move' === mode ? __( 'Choose a new time', 'funnelforms-pro' ) : 'cancel' === mode ? __( 'Cancel the appointment?', 'funnelforms-pro' ) : sprintf( __( '%1$s with %2$s', 'funnelforms-pro' ), a.type?.title ?? __( 'Appointment', 'funnelforms-pro' ), who ) }

    { f.long( a.start ) } – { dayOf( a.start, f.zone ) === dayOf( a.end, f.zone ) ? f.time( a.end ) : f.long( a.end ) } · { duration( a.duration ) }

    { done ? (

    { done }

    ) : null } { error ? (

    { error }

    ) : null } { 'details' === mode ?
    : null } { 'move' === mode ? setChosen( { start, label } ) } message={ message } onMessage={ setMessage } /> : null } { 'cancel' === mode ? ( <>

    { sprintf( __( '%s and the people booked get a cancellation by e-mail. The time is free again.', 'funnelforms-pro' ), who ) }

    ) : null }
    { 'details' === mode ? ( <> { __( 'Open entry', 'funnelforms-pro' ) } { a.changeable ? ( <> ) : null } { a.markable && ! a.outcome ? ( <> ) : null } { a.markable && a.outcome ? ( ) : null } ) : null } { 'move' === mode ? ( <> { chosen ? sprintf( __( 'New time: %s', 'funnelforms-pro' ), chosen.label ) : '' } ) : null } { 'cancel' === mode ? ( <> ) : null }
    ); } function Details( { a, f, entryUrl }: { a: AppointmentItem; f: Formats; entryUrl: string } ) { const kind = a.type ? { personal: __( 'Place', 'funnelforms-pro' ), call: __( 'Phone', 'funnelforms-pro' ), video: __( 'Video call', 'funnelforms-pro' ) }[ a.type.kind ] : ''; const changed = a.changedBy ? 'cancelled' === a.status ? { visitor: __( 'Cancelled by the visitor', 'funnelforms-pro' ), team: sprintf( __( 'Cancelled by %s', 'funnelforms-pro' ), a.changedBy.name || __( 'the team', 'funnelforms-pro' ) ), system: __( 'Cancelled automatically', 'funnelforms-pro' ) }[ a.changedBy.kind ] : { visitor: __( 'Moved by the visitor', 'funnelforms-pro' ), team: sprintf( __( 'Moved by %s', 'funnelforms-pro' ), a.changedBy.name || __( 'the team', 'funnelforms-pro' ) ), system: '' }[ a.changedBy.kind ] : ''; const zone = f.zone && a.timezone && ! [ f.zone, 'UTC' ].includes( a.timezone ) && a.timezone !== f.zone ? a.timezone : ''; return (
    { __( 'Contact', 'funnelforms-pro' ) }
    { a.visitor.name || '–' } { a.visitor.email ? ( <>
    { a.visitor.email } ) : null } { a.visitor.phone ? ( <>
    { a.visitor.phone } ) : null }
    { __( 'Form', 'funnelforms-pro' ) }
    { a.form?.title ?? '–' } · { sprintf( __( 'Entry #%d', 'funnelforms-pro' ), a.entryId ) }
    { __( 'Appointment type', 'funnelforms-pro' ) }
    { a.type?.title ?? __( 'Deleted appointment type', 'funnelforms-pro' ) }
    { a.type?.location ? ( <>
    { kind }
    { 'video' === a.type.kind ? { a.type.location } : a.type.location }
    ) : null }
    { __( 'Booked with', 'funnelforms-pro' ) }
    { a.people.length ? a.people.map( ( p ) => p.name ).join( ', ' ) : __( 'Your website (no one in particular)', 'funnelforms-pro' ) }
    { zone ? ( <>
    { __( 'Time zone of the type', 'funnelforms-pro' ) }
    { zoneName( zone, f.locale ) }
    ) : null } { changed ? ( <>
    { __( 'Changed', 'funnelforms-pro' ) }
    { changed }
    ) : null } { a.message ? ( <>
    { __( 'Message to the visitor', 'funnelforms-pro' ) }
    { a.message }
    ) : null }
    ); } function MessageField( { value, onChange }: { value: string; onChange: ( value: string ) => void } ) { const id = useId(); return (