import { getSetting } from '@woocommerce/settings'; import { __ } from '@wordpress/i18n'; import { useEffect, useState } from 'react'; import { getApmData } from './api'; import type { BlockMethodSettings, PaymentMethodComponentProps } from '../types/blocks'; const settings: BlockMethodSettings = getSetting('airwallex_main_data', {}); const title = settings?.title ?? __('Pay with cards and more', 'airwallex-online-payments-gateway'); const description = settings?.description ?? ''; const useLogos = () => { const [logos, setLogos] = useState>({}); const [maxInlineLogoCount, setMaxInlineLogoCount] = useState(0); useEffect(() => { getApmData().then(({ data }) => { setLogos(data?.active_logos ?? {}); setMaxInlineLogoCount(data?.max_number_of_logos || 0); }); }, []); return { logos, maxInlineLogoCount }; }; const LogoList = ({ logos }: { logos: Record }) => ( <> {Object.entries(logos).map(([name, src]) => ( {title} ))} ); // `components` is optional for compile-time JSX (the factory is // invoked as `` without props); WC blocks // injects `props.components.PaymentMethodLabel` at runtime via // cloneElement. type AirwallexLabelProps = Partial; const AirwallexLabelDropIn = (props: AirwallexLabelProps) => { const { PaymentMethodLabel } = props.components!; const { logos, maxInlineLogoCount } = useLogos(); return Object.keys(logos).length <= maxInlineLogoCount ? ( <> ) : ( ); } const AirwallexContentDropIn = () => { const { logos, maxInlineLogoCount } = useLogos(); return Object.keys(logos).length > maxInlineLogoCount ? ( <>
{description}
) : ( <>
{description}
); }; const canMakePayment = (): boolean => { return settings?.enabled ?? false; } export const airwallexDropInOption = { name: settings?.name ?? 'airwallex_main', label: , content: , edit: , canMakePayment: canMakePayment, ariaLabel: title, supports: { features: settings?.supports ?? [], } };