/** * Funnelforms → Integrations, section Video meetings: video call types can get a meeting of their * own for each appointment. Google Meet and Microsoft Teams come with the connected Google and * Microsoft calendars; Zoom accounts are connected here (sign-in through the Funnelforms broker) * and can belong to a team member, whose appointments are then held in their Zoom. */ import React, { useEffect, useRef, useState } from 'react'; import { ApiError, fixUrl, type CalendarConnection, type CalendarsOverview, type ZoomAccount } from '../api'; import { waitForSignIn, type CalendarApi, type CalendarService } from './BookingTypes'; import { __, sprintf } from './i18n'; import { Button, Dialog, Notice } from './ui'; /** What the section needs of the API. */ export interface MeetingsApi extends CalendarApi { arrangeCalendar( ref: string, changes: { owner?: number } ): Promise; deleteConnection( ref: string ): Promise; } const message = ( e: unknown ): string => ( e instanceof Error ? e.message : String( e ) ); /** Marks of the three services, in their colours (styles in admin.css). */ function Mark( { service }: { service: 'zoom' | 'meet' | 'teams' } ) { return ( ); } export function MeetingsSection( { data, api, onChanged, onCalendar }: { data: CalendarsOverview; api: MeetingsApi; onChanged: ( next: CalendarsOverview | null, said: string ) => void; onCalendar: ( service: CalendarService ) => void } ) { const [ adding, setAdding ] = useState<{ again?: ZoomAccount } | null>( null ); const [ removing, setRemoving ] = useState( null ); const [ error, setError ] = useState( null ); const [ saving, setSaving ] = useState( null ); const zoom = data.zoom ?? []; const google = data.connections.filter( ( c ) => 'google' === c.type ); const microsoft = data.connections.filter( ( c ) => 'microsoft' === c.type ); const people = data.people; const owner = async ( account: ZoomAccount, value: number ): Promise => { setSaving( account.ref ); setError( null ); try { onChanged( await api.arrangeCalendar( account.ref, { owner: value } ), sprintf( __( '%s was saved.', 'funnelforms-pro' ), account.label ) ); } catch ( e ) { setError( message( e ) ); } finally { setSaving( null ); } }; const remove = async (): Promise => { if ( ! removing ) { return; } setError( null ); try { await api.deleteConnection( removing.ref ); const label = removing.label; setRemoving( null ); onChanged( null, sprintf( __( '%s was removed.', 'funnelforms-pro' ), label ) ); } catch ( e ) { setError( message( e ) ); } }; const calendarState = ( list: typeof google, service: 'google' | 'microsoft' ): React.ReactNode => list.length ? ( c.label ).join( ', ' ) }> { __( 'Ready', 'funnelforms-pro' ) } ) : ( ); return (

{ __( 'Video meetings', 'funnelforms-pro' ) }

{ __( 'Appointment types for video calls can create a meeting of their own for each booking. The link is in the confirmation, the reminders and the calendar entry, moves with the appointment and is removed when it is cancelled. Choose the service in the appointment type.', 'funnelforms-pro' ) }

{ error ? { error } : null }
  • Google Meet { __( 'Comes with your Google calendar, nothing else to connect.', 'funnelforms-pro' ) }
    { calendarState( google, 'google' ) }
  • Microsoft Teams { __( 'Comes with your Microsoft calendar. Needs Microsoft 365 for work or school with Teams.', 'funnelforms-pro' ) }
    { calendarState( microsoft, 'microsoft' ) }
  • Zoom { data.website.connected ? __( 'Sign in with your Zoom account. Team members can connect their own.', 'funnelforms-pro' ) : __( 'Needs the Funnelforms account', 'funnelforms-pro' ) }
{ zoom.length ? ( <>

{ __( 'Connected Zoom accounts', 'funnelforms-pro' ) }

    { zoom.map( ( account ) => (
  • { account.label } { account.usedBy.length ? sprintf( __( 'Used by: %s', 'funnelforms-pro' ), account.usedBy.map( ( t ) => t.title ).join( ', ' ) ) : __( 'Not used by an appointment type yet.', 'funnelforms-pro' ) }
    { 'auth_failed' === account.error ? { __( 'Access expired', 'funnelforms-pro' ) } : { __( 'Connected', 'funnelforms-pro' ) } }
    { 'auth_failed' === account.error ? ( ) : null }
  • ) ) }
) : null } { adding ? ( setAdding( null ) } onConnected={ ( connection ) => { setAdding( null ); onChanged( null, sprintf( __( '%s is connected.', 'funnelforms-pro' ), connection.label ) ); } } /> ) : null } { removing ? ( setRemoving( null ) } actions={ <> } >

{ sprintf( __( 'Remove %s from this website? Meetings already scheduled stay in Zoom.', 'funnelforms-pro' ), removing.label ) }

{ removing.usedBy.length ? { sprintf( __( 'These appointment types then need another Zoom account: %s.', 'funnelforms-pro' ), removing.usedBy.map( ( t ) => t.title ).join( ', ' ) ) } : null }
) : null }
); } /** Signs in to Zoom in a window, through the Funnelforms broker; again: an account whose access ran out. */ export function ZoomDialog( { api, again, onClose, onConnected }: { api: CalendarApi; again?: ZoomAccount; onClose: () => void; onConnected: ( connection: CalendarConnection ) => void } ) { const [ busy, setBusy ] = useState( false ); const [ error, setError ] = useState( null ); const [ accountUrl, setAccountUrl ] = useState( '' ); const [ link, setLink ] = useState( '' ); const attempt = useRef( 0 ); useEffect( () => () => { attempt.current++; }, [] ); // The window opens right on the click, before the address is known, so browsers allow it. const signIn = (): void => { const popup = window.open( '', 'ffx-calendar', 'popup,width=520,height=720' ); const mine = ++attempt.current; setBusy( true ); setError( null ); setAccountUrl( '' ); void ( async () => { try { const started = await api.startCalendarSignIn( 'zoom', again?.ref ); if ( popup && ! popup.closed ) { popup.location.href = started.url; } else { setLink( started.url ); } const result = await waitForSignIn( api, started.state, () => mine === attempt.current ); if ( ! result || mine !== attempt.current ) { return; } setBusy( false ); setLink( '' ); if ( ! result.ok || ! result.connection ) { setError( result.message ?? __( 'The sign-in did not work.', 'funnelforms-pro' ) ); setAccountUrl( fixUrl( result.url ) ); return; } onConnected( result.connection ); } catch ( e ) { popup?.close(); setError( message( e ) ); setAccountUrl( e instanceof ApiError ? fixUrl( e.data.url ) : '' ); setBusy( false ); } } )(); }; return ( } > { error ? ( { error } { accountUrl ? ( <> { ' ' } { __( 'Connect the website', 'funnelforms-pro' ) } ) : null } ) : null }

{ again ? sprintf( __( 'Sign in to %s again so Funnelforms can schedule meetings for your appointments.', 'funnelforms-pro' ), again.label ) : __( 'A window opens where you sign in to Zoom and allow Funnelforms to schedule, move and delete the meetings of your appointments. Funnelforms reads nothing else in your account.', 'funnelforms-pro' ) }

{ link ? ( { __( 'Open the sign-in', 'funnelforms-pro' ) } ) : null }

{ __( 'The access is stored encrypted on this website. Meetings are scheduled straight from here in your Zoom account.', 'funnelforms-pro' ) }

); }