/** * Funnelforms → Integrations: the overview of all services on top (IntegrationsHub), then the * calendars of the website and its team. Connect Google, Microsoft, Apple iCloud or another * CalDAV calendar, say whose calendar it is, which of its calendars gets the appointments and * which are checked for busy times; below them the video meetings (Zoom, Google Meet, Teams). CRM, newsletter and spreadsheet tools are connected in the * overview; webhooks and access for Zapier, Make and n8n keep the settings tab Interfaces, and the * page leads there. */ import React, { useCallback, useEffect, useState } from 'react'; import { localeTag } from '@funnelforms/form-core'; import { ApiError, fixUrl, type CalendarAccount, type CalendarChoice, type CalendarsOverview, type ConnectionTestResult, type FormsApi } from '../api'; import type { ConnectApi } from '../connect'; import type { HubApi } from '../hub'; import { CalendarDialog, type CalendarApi, type CalendarService } from './BookingTypes'; import { IntegrationsHub } from './IntegrationsHub'; import { MeetingsSection } from './Meetings'; import { __, sprintf } from './i18n'; import { Button, Dialog, Notice, Spinner } from './ui'; export interface IntegrationsConfig { root: string; nonce: string; userLocale?: string; appointmentsUrl?: string; interfacesUrl?: string; builderUrl?: string; } /** What the screen needs of the API; with hub and connect it shows the overview of all integrations on top. */ export type IntegrationsApi = CalendarApi & Required> & { hub?: HubApi; connect?: ConnectApi }; 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', plus: 'M5 12h14M12 5v14', shield: 'M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z', lock: 'M7 11V7a5 5 0 0 1 10 0v4M5 11h14a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2z', eye: 'M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12zM12 9a3 3 0 1 0 0 6 3 3 0 0 0 0-6z', unlink: 'M18.84 12.25 20.56 10.54a5 5 0 0 0-7.07-7.07l-1.72 1.71M5.17 11.75 3.45 13.46a5 5 0 0 0 7.07 7.07l1.71-1.71M8 2v3M2 8h3M16 22v-3M22 16h-3', code: 'm16 18 6-6-6-6M8 6l-6 6 6 6', chevron: 'm6 9 6 6 6-6', }; function Icon( { name }: { name: keyof typeof PATHS } ) { return ( ); } const message = ( e: unknown ): string => ( e instanceof Error ? e.message : String( e ) ); /** The services a calendar can come from, as tiles and as the name in a row. */ function services(): Array<{ id: CalendarService; name: string; hint: string; oauth: boolean }> { return [ { id: 'google', name: 'Google Calendar', hint: __( 'Google Workspace and Gmail', 'funnelforms-pro' ), oauth: true }, { id: 'microsoft', name: 'Microsoft Outlook', hint: __( 'Microsoft 365, Outlook.com and Exchange Online', 'funnelforms-pro' ), oauth: true }, { id: 'icloud', name: 'Apple iCloud', hint: __( 'With an app-specific password', 'funnelforms-pro' ), oauth: false }, { id: 'caldav', name: __( 'Other calendar (CalDAV)', 'funnelforms-pro' ), hint: __( 'Nextcloud, Fastmail, mailbox.org and more', 'funnelforms-pro' ), oauth: false }, ]; } /** Which service a connection is: CalDAV at caldav.icloud.com is iCloud. */ function serviceOf( account: CalendarAccount ): CalendarService { if ( 'caldav' !== account.type ) { return account.type; } return /icloud\.com/i.test( account.account ) ? 'icloud' : 'caldav'; } function Mark( { service }: { service: CalendarService } ) { const letters: Record = { google: 'G', microsoft: 'M', icloud: '', caldav: '' }; return ( ); } type TestState = { state: 'testing' } | { state: 'ok'; calendars: NonNullable } | { state: 'failed'; text: string }; export function IntegrationsScreen( { api, config }: { api: IntegrationsApi; config: IntegrationsConfig } ) { const [ data, setData ] = useState( null ); const [ error, setError ] = useState( null ); const [ adding, setAdding ] = useState<{ service: CalendarService; again?: CalendarAccount } | null>( null ); const [ removing, setRemoving ] = useState( null ); const [ removeError, setRemoveError ] = useState( null ); const [ tests, setTests ] = useState>( {} ); const [ said, say ] = useState( '' ); const reload = useCallback( async (): Promise => { setError( null ); try { setData( await api.calendarsOverview() ); } catch ( e ) { setError( message( e ) ); } }, [ api ] ); useEffect( () => { void reload(); }, [ reload ] ); const test = async ( ref: string ): Promise => { setTests( ( t ) => ( { ...t, [ ref ]: { state: 'testing' } } ) ); try { const result = await api.testConnection( ref, {} ); setTests( ( t ) => ( { ...t, [ ref ]: { state: 'ok', calendars: result?.calendars ?? [] } } ) ); } catch ( e ) { setTests( ( t ) => ( { ...t, [ ref ]: { state: 'failed', text: message( e ) } } ) ); } }; const remove = async (): Promise => { if ( ! removing ) { return; } setRemoveError( null ); try { await api.deleteConnection( removing.ref ); setRemoving( null ); say( sprintf( __( '%s was removed.', 'funnelforms-pro' ), removing.label ) ); await reload(); } catch ( e ) { setRemoveError( message( e ) ); } }; const list = services(); const blocked = ( s: { oauth: boolean } ): boolean => s.oauth && ! data?.website.connected; const date = ( iso: string ): string => { const d = new Date( iso ); return Number.isNaN( d.getTime() ) ? '' : d.toLocaleDateString( localeTag( config.userLocale ), { day: 'numeric', month: 'short', year: 'numeric' } ); }; // Class instances (WpRestAdapter) keep their methods only when passed as they are. const hubApi = api.hub && api.connect ? ( api as IntegrationsApi & { hub: HubApi; connect: ConnectApi } ) : null; // A calendar tile of the overview: the dialog, or the section that says what is missing first. const calendarFromHub = ( service: CalendarService ): void => { const s = list.find( ( x ) => x.id === service ); if ( data && s && ! blocked( s ) ) { setAdding( { service } ); } else { document.getElementById( 'ffx-calendars-title' )?.scrollIntoView( { behavior: 'smooth', block: 'start' } ); } }; return (
{ hubApi ? : null }

{ __( 'Calendars', 'funnelforms-pro' ) }

{ __( 'Busy times in your calendars cannot be booked, and every booking shows up in the calendar right away. Assign a calendar to a team member, and it counts for their appointments.', 'funnelforms-pro' ) }

{ data && ! data.website.connected ? (

{ __( 'Google and Microsoft sign in through your Funnelforms account. Connect this website with it first; Apple iCloud and CalDAV work without.', 'funnelforms-pro' ) }{ ' ' } { __( 'Connect the website', 'funnelforms-pro' ) }

) : null }
{ list.map( ( s ) => ( ) ) }

{ __( 'Connected calendars', 'funnelforms-pro' ) }

{ error ? (

{ error }{ ' ' }

) : null } { ! error && ! data ? : null } { data && 0 === data.connections.length ? (

{ __( 'No calendar yet', 'funnelforms-pro' ) }

{ __( 'Choose your calendar above. Your appointment types then check it before anyone books.', 'funnelforms-pro' ) }

) : null } { data && data.connections.length ? (
    { data.connections.map( ( account ) => ( void test( account.ref ) } onAgain={ () => setAdding( { service: serviceOf( account ), again: account } ) } onRemove={ () => { setRemoveError( null ); setRemoving( account ); } } onChanged={ ( next, text ) => { setData( next ); say( text ); } } /> ) ) }
) : null }

{ said }

{ data ? ( setAdding( { service } ) } onChanged={ ( next, text ) => { say( text ); if ( next ) { setData( next ); } else { void reload(); } } } /> ) : null }

{ __( 'What Funnelforms does with your calendars', 'funnelforms-pro' ) }

  • { __( 'It reads only when a calendar is busy, for the days a visitor looks at. Titles and guests of your events are not stored.', 'funnelforms-pro' ) }
  • { __( 'It adds each booking to the main calendar, and changes or removes it when the appointment is moved or cancelled.', 'funnelforms-pro' ) }
  • { __( 'The access is stored encrypted on this website. Calendar data goes from here straight to your calendar; Funnelforms only helps with the sign-in.', 'funnelforms-pro' ) }
  • { __( 'Remove a calendar here, or withdraw the access in your Google or Microsoft account, at any time.', 'funnelforms-pro' ) }

{ __( 'More integrations', 'funnelforms-pro' ) }

{ adding ? ( setAdding( null ) } onConnected={ ( calendar ) => { setAdding( null ); say( sprintf( __( '%s is connected.', 'funnelforms-pro' ), calendar.label ) ); setTests( ( t ) => Object.fromEntries( Object.entries( t ).filter( ( [ ref ] ) => ref !== calendar.ref ) ) ); void reload(); } } /> ) : null } { removing ? ( setRemoving( null ) } actions={ <> } > { removeError ? { removeError } : null }

{ sprintf( __( 'Remove %s from this website? Appointments already in the calendar stay there.', 'funnelforms-pro' ), removing.label ) }

{ removing.usedBy.length ? { sprintf( __( 'Appointment types that use this calendar lose it and need another calendar: %s.', 'funnelforms-pro' ), removing.usedBy.map( ( t ) => t.title ).join( ', ' ) ) } : null }
) : null }
); } /** One connected account: its state, whose it is, and its calendars once opened. */ function CalendarRow( { account, data, api, since, test, onTest, onAgain, onRemove, onChanged, }: { account: CalendarAccount; data: CalendarsOverview; api: IntegrationsApi; since: string; test?: TestState; onTest: () => void; onAgain: () => void; onRemove: () => void; onChanged: ( next: CalendarsOverview, said: string ) => void; } ) { const service = serviceOf( account ); const name = services().find( ( s ) => s.id === service )?.name ?? service; const oauth = 'caldav' !== account.type; const [ open, setOpen ] = useState( false ); const [ calendars, setCalendars ] = useState( null ); const [ loadError, setLoadError ] = useState( null ); const [ loadUrl, setLoadUrl ] = useState( '' ); const [ saving, setSaving ] = useState( false ); const [ problem, setProblem ] = useState( null ); const [ password, setPassword ] = useState( '' ); useEffect( () => { if ( ! open || calendars ) { return; } let live = true; setLoadError( null ); api.accountCalendars( account.ref ).then( ( found ) => live && setCalendars( found ), ( e: unknown ) => { if ( live ) { setLoadError( message( e ) ); setLoadUrl( e instanceof ApiError ? fixUrl( e.data.url ) : '' ); } } ); return () => { live = false; }; }, [ open, calendars, api, account.ref ] ); const arrange = async ( changes: { calendar?: string; busy?: string[]; owner?: number } ): Promise => { setSaving( true ); setProblem( null ); try { onChanged( await api.arrangeCalendar( account.ref, changes ), sprintf( __( '%s was saved.', 'funnelforms-pro' ), account.label ) ); } catch ( e ) { setProblem( message( e ) ); } setSaving( false ); }; // CalDAV: a new password when the old one stopped working; empty keeps the stored one. const changePassword = async (): Promise => { setSaving( true ); setProblem( null ); try { await api.updateConnection( account.ref, { password: password.trim() } ); setPassword( '' ); setCalendars( null ); onChanged( await api.calendarsOverview(), sprintf( __( '%s was saved.', 'funnelforms-pro' ), account.label ) ); } catch ( e ) { setProblem( message( e ) ); } setSaving( false ); }; let badge: React.ReactNode = { __( 'Connected', 'funnelforms-pro' ) }; if ( 'auth_failed' === account.error ) { badge = { __( 'Access expired', 'funnelforms-pro' ) }; } else if ( account.error ) { badge = { __( 'Not reachable lately', 'funnelforms-pro' ) }; } if ( 'testing' === test?.state ) { badge = { __( 'Testing…', 'funnelforms-pro' ) }; } else if ( 'ok' === test?.state ) { badge = { __( 'Works', 'funnelforms-pro' ) }; } else if ( 'failed' === test?.state ) { badge = { __( 'Not working', 'funnelforms-pro' ) }; } const owner = account.owner > 0 && ! data.people.some( ( p ) => p.id === account.owner ) ? [ { id: account.owner, name: account.ownerName || sprintf( __( 'User %d', 'funnelforms-pro' ), account.owner ) } ] : []; const people = [ ...data.people, ...owner ]; const writable = ( calendars ?? [] ).filter( ( c ) => c.writable ); const details = 'ffx-cal-details-' + account.ref.replace( /[^a-z0-9-]/gi, '-' ); return (
  • { account.label } { [ name, account.account, since ? sprintf( __( 'since %s', 'funnelforms-pro' ), since ) : '', account.removable ? '' : __( 'From Funnelforms 3', 'funnelforms-pro' ) ].filter( Boolean ).join( ' · ' ) }
    { badge }
    { oauth && ( 'auth_failed' === account.error || 'failed' === test?.state ) ? ( ) : null }
    { 'failed' === test?.state ? (

    { test.text }

    ) : null } { 'ok' === test?.state && test.calendars.length ? (
      { test.calendars.map( ( c ) => (
    • { sprintf( __( '%1$s: %2$d busy in the next two weeks', 'funnelforms-pro' ), c.name, c.busy ) } { c.free ? ' · ' + sprintf( __( '%d marked free, they do not block', 'funnelforms-pro' ), c.free ) : '' }
    • ) ) }
    ) : null }

    { account.usedBy.length ? sprintf( __( 'Counts for: %s', 'funnelforms-pro' ), account.usedBy.map( ( t ) => t.title ).join( ', ' ) ) : account.owner > 0 ? __( 'Counts for the appointment types that book this person, once there are some.', 'funnelforms-pro' ) : __( 'No appointment type uses it yet. Choose it in an appointment type under Calendars.', 'funnelforms-pro' ) }

    { ! oauth && account.removable ? (
    { e.preventDefault(); if ( password.trim() ) { void changePassword(); } } } > { __( 'New password', 'funnelforms-pro' ) } setPassword( e.target.value ) } /> { __( 'Empty keeps the current password.', 'funnelforms-pro' ) }
    ) : null }
    { open ? (
    { loadError ? (

    { sprintf( __( 'The calendars could not be read: %s', 'funnelforms-pro' ), loadError ) }{ ' ' } { loadUrl ? ( <> { __( 'Connect the website', 'funnelforms-pro' ) } { ' ' } ) : null }

    ) : null } { ! loadError && ! calendars ? : null } { calendars ? ( <>
    { __( 'Check for busy times', 'funnelforms-pro' ) } { calendars.map( ( c ) => ( ) ) } { __( 'Add shared or family calendars, so their events block times too.', 'funnelforms-pro' ) }
    ) : null }
    ) : null } { problem ? (

    { problem }

    ) : null }
  • ); }