/** * "Use my website's design": the colours and fonts the site's own theme and page builder are set * to (theme.json, the Customizer, Elementor, Astra, Kadence, GeneratePress, OceanWP, Divi, the * logo), read by the plugin without a request (Application\SiteDesign). The user sees each colour * with where it was found, adjusts what they like, checks the contrast and applies. When the * reading is unsure, nothing is chosen for them; the start page can be read instead. */ import React, { useState } from 'react'; import type { FormDocument } from '@funnelforms/form-schema'; import type { Branding, SiteDesign } from '../api'; import type { BrandingPicks } from '../themes'; import { BrandingChoices, firstPicks, KEEP, nothingPicked, useApplyBranding } from './BrandingImport'; import { useEnv } from './context'; import { __, sprintf } from './i18n'; import { Button, Dialog, Notice, Spinner } from './ui'; const brushIcon = ( ); type State = { state: 'closed' } | { state: 'loading' } | { state: 'failed'; message: string } | { state: 'found'; found: Branding; site: SiteDesign; page: boolean }; export function SiteDesignButton( { doc }: { doc: FormDocument } ) { const { api, readOnly } = useEnv(); const [ state, setState ] = useState( { state: 'closed' } ); const [ picks, setPicks ] = useState( KEEP ); const [ reading, setReading ] = useState( false ); const applying = useApplyBranding(); if ( ! api.siteDesign ) { return null; } const siteDesign = api.siteDesign.bind( api ); const open = async (): Promise => { setState( { state: 'loading' } ); try { const site = await siteDesign(); setState( { state: 'found', found: site, site, page: false } ); setPicks( firstPicks( site ) ); } catch ( e ) { setState( { state: 'failed', message: e instanceof Error ? e.message : String( e ) } ); } }; // The start page of the own site, when the theme names too little. const readPage = async ( site: SiteDesign ): Promise => { setReading( true ); try { const page = await api.branding( site.url, true ); setState( { state: 'found', found: page, site, page: true } ); setPicks( firstPicks( page ) ); } catch ( e ) { setState( { state: 'failed', message: e instanceof Error ? e.message : String( e ) } ); } setReading( false ); }; const close = (): void => setState( { state: 'closed' } ); const found = 'found' === state.state ? state.found : null; const empty = found && ! found.colors.length && ! found.fonts.length; return ( <> } > { 'loading' === state.state ? : null } { 'failed' === state.state ? { state.message } : null } { 'found' === state.state ? (

{ state.page ? sprintf( /* translators: %s: address of the start page */ __( 'Read from your start page, %s.', 'funnelforms-pro' ), state.found.url ) : state.site.sources.length ? sprintf( /* translators: %s: list of sources, e.g. Twenty Twenty-Five, Elementor */ __( 'Read from %s.', 'funnelforms-pro' ), state.site.sources.join( ', ' ) ) : __( 'Your theme names no colours or fonts.', 'funnelforms-pro' ) }

{ empty ? { __( 'No colours or fonts were found.', 'funnelforms-pro' ) } : } { ! state.page && 'high' !== state.found.confidence ? (

{ __( 'Your site can also load its start page once and read the colours that show there.', 'funnelforms-pro' ) }

) : null } { applying.error ? { applying.error } : null }
) : null }
); }