/** * External dependencies */ import { Fragment, useState, forwardRef, useImperativeHandle, useRef, } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Card, CardHeader, CardBody, CardDivider, Button, Icon, } from '@wordpress/components'; import { chevronUp, chevronDown } from '@wordpress/icons'; /** * Internal dependencies */ import { RecommendedChannel } from '~/marketing/data-multichannel/types'; import { CardHeaderTitle, CardHeaderDescription, SmartPluginCardBody, } from '~/marketing/components'; import { RegisteredChannel } from '~/marketing/types'; import { RegisteredChannelCardBody } from './RegisteredChannelCardBody'; import './Channels.scss'; type ChannelsProps = { registeredChannels: Array< RegisteredChannel >; recommendedChannels: Array< RecommendedChannel >; onInstalledAndActivated?: ( pluginSlug: string ) => void; }; export type ChannelsRef = { /** * Scroll into the "Add channels" section in the card. * The section will be expanded, and the "Add channels" button will be in focus. */ scrollIntoAddChannels: () => void; }; export const Channels = forwardRef< ChannelsRef, ChannelsProps >( ( { registeredChannels, recommendedChannels, onInstalledAndActivated }, ref ) => { const hasRegisteredChannels = registeredChannels.length >= 1; /** * State to collapse / expand the recommended channels. * Initial state is expanded if there are no registered channels in first page load. */ const [ expanded, setExpanded ] = useState( ! hasRegisteredChannels ); const addChannelsButtonRef = useRef< HTMLButtonElement >( null ); useImperativeHandle( ref, () => ( { scrollIntoAddChannels: () => { setExpanded( true ); addChannelsButtonRef.current?.focus(); addChannelsButtonRef.current?.scrollIntoView( { block: 'center', } ); }, } ), [] ); return ( { __( 'Channels', 'woocommerce' ) } { ! hasRegisteredChannels && ( { __( 'Start by adding a channel to your store', 'woocommerce' ) } ) } { /* Registered channels section. */ } { registeredChannels.map( ( el, idx ) => ( { idx !== registeredChannels.length - 1 && ( ) } ) ) } { /* Recommended channels section. */ } { recommendedChannels.length >= 1 && (
{ hasRegisteredChannels && ( <> ) } { expanded && recommendedChannels.map( ( el, idx ) => ( { idx !== recommendedChannels.length - 1 && ( ) } ) ) }
) }
); } );