/** * The template of a WhatsApp action: chosen from the templates of the WhatsApp Business account * with a preview, or written here and submitted to Meta for review. Without the ID of the business * account (or the route) the name and language are entered by hand, as before. */ import React, { useEffect, useState } from 'react'; import { ApiError, type NewWhatsAppTemplate, type WhatsAppTemplate } from '../api'; import { useEnv } from './context'; import { __, sprintf } from './i18n'; import { Button, Dialog, icons, Notice, Select, TextInput } from './ui'; /** Languages Meta offers for templates that forms are written in most. */ const LANGUAGES = [ 'de', 'en', 'en_US', 'en_GB', 'fr', 'it', 'es', 'nl', 'pl', 'pt_BR', 'pt_PT', 'tr', 'sv', 'da', 'cs', 'hu' ]; /** Number of placeholders {{1}} … {{n}} of a text; null when one is skipped or not a number. */ export function templatePlaceholders( body: string ): number | null { const numbers = new Set(); for ( const [ , inner ] of body.matchAll( /\{\{\s*([^}]*?)\s*\}\}/g ) ) { if ( ! /^[1-9]\d?$/.test( inner ?? '' ) ) { return null; } numbers.add( Number( inner ) ); } return 0 === numbers.size || Math.max( ...numbers ) === numbers.size ? numbers.size : null; } function languageName( code: string ): string { try { const name = new Intl.DisplayNames( undefined, { type: 'language' } ).of( code.replace( '_', '-' ) ); return name && name !== code ? `${ name } (${ code })` : code; } catch { return code; } } function statusName( status: string ): string { switch ( status ) { case 'APPROVED': return __( 'approved', 'funnelforms-pro' ); case 'PENDING': case 'IN_APPEAL': return __( 'in review', 'funnelforms-pro' ); case 'REJECTED': return __( 'rejected', 'funnelforms-pro' ); default: return __( 'paused or disabled', 'funnelforms-pro' ); } } type Loaded = { state: 'loading' } | { state: 'ready'; templates: WhatsAppTemplate[] } | { state: 'failed'; code: string; message: string }; export function WhatsAppTemplatePicker( { connectionRef, template, language, values, defaultLanguage, onTemplate, onLanguage, onChoose, }: { connectionRef: string; template: string; language: string; /** Texts of the parameters, for the preview. */ values: string[]; defaultLanguage: string; onTemplate: ( name: string ) => void; onLanguage: ( language: string ) => void; onChoose: ( template: WhatsAppTemplate ) => void; } ) { const { api, rights, readOnly } = useEnv(); const [ loaded, setLoaded ] = useState( { state: 'loading' } ); const [ round, setRound ] = useState( 0 ); /** auto: the list unless the action names a template the account lacks. */ const [ mode, setMode ] = useState<'auto' | 'list' | 'manual'>( 'auto' ); const [ creating, setCreating ] = useState( false ); const listable = '' !== connectionRef && connectionRef.startsWith( 'whatsapp:' ) && !! api.whatsAppTemplates; useEffect( () => { if ( ! listable || ! api.whatsAppTemplates ) { return; } let live = true; setLoaded( { state: 'loading' } ); api.whatsAppTemplates( connectionRef ).then( ( templates ) => live && setLoaded( { state: 'ready', templates } ), ( e: unknown ) => live && setLoaded( { state: 'failed', code: e instanceof ApiError ? e.code : 'error', message: e instanceof Error ? e.message : String( e ) } ) ); return () => { live = false; }; }, [ api, connectionRef, listable, round ] ); const templates = 'ready' === loaded.state ? loaded.templates : []; const current = templates.find( ( t ) => t.name === template && t.language === language ); const listed = listable && 'ready' === loaded.state && templates.length > 0 && ( 'list' === mode || ( 'auto' === mode && ( '' === template || !! current ) ) ); const reload = (): void => setRound( ( r ) => r + 1 ); const manualInputs = ( <> onTemplate( v.trim() ) } hint={ __( 'Name of a template Meta approved for your account.', 'funnelforms-pro' ) } /> onLanguage( v.trim() ) } /> ); return (
{ listed ? ( ( { value: l, label: languageName( l ) } ) ) } onChange={ ( v ) => set( { language: v } ) } /> label={ __( 'Kind', 'funnelforms-pro' ) } value={ draft.category } options={ [ { value: 'UTILITY', label: __( 'Service message (e.g. confirmation of the request)', 'funnelforms-pro' ) }, { value: 'MARKETING', label: __( 'Marketing (offers, news)', 'funnelforms-pro' ) }, ] } onChange={ ( v ) => set( { category: v } ) } hint={ __( 'Meta charges marketing messages higher and may change the kind on review.', 'funnelforms-pro' ) } /> set( { body: v.slice( 0, 1024 ) } ) } hint={ __( 'Placeholders {{1}}, {{2}} … take the texts the action sends, e.g. the name from the form. Put text before the first and after the last one.', 'funnelforms-pro' ) } /> { null === count ? { __( 'Number the placeholders {{1}}, {{2}} … without gaps.', 'funnelforms-pro' ) } : null } { examples.map( ( value, i ) => ( setDraft( ( d ) => { const next = Array.from( { length: count ?? 0 }, ( _, j ) => d.examples[ j ] ?? '' ); next[ i ] = v; return { ...d, examples: next }; } ) } /> ) ) } { examples.length ?

{ __( 'Meta reviews the message with these examples; they are not sent.', 'funnelforms-pro' ) }

: null } set( { footer: v.slice( 0, 60 ) } ) } hint={ __( 'Small grey line below the message, at most 60 characters.', 'funnelforms-pro' ) } />

{ __( 'Meta reviews new templates, usually within minutes. Messages are sent once the template is approved.', 'funnelforms-pro' ) }

); }