/** * WordPress dependencies */ import { Button, ToggleControl, Tooltip } from '@safe-wordpress/components'; import { useSelect } from '@safe-wordpress/data'; import { useMemo } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { values } from 'lodash'; import clsx from 'clsx'; import { SocialProfileIcon } from '@nelio-content/components'; import { EMPTY_ARRAY, EMPTY_OBJECT } from '@nelio-content/constants'; import { useIsSubscribed, store as NC_DATA } from '@nelio-content/data'; import type { AutomationGroupId, ProfileAutomationSettings, Uuid, } from '@nelio-content/types'; /** * Internal dependencies */ import { useAutomationGroup, useGroupName, useIsSaving, } from '~/nelio-content-pages/settings/automations/hooks'; import { store as NC_AUTOMATION_SETTINGS } from '~/nelio-content-pages/settings/automations/store'; export type HeaderProps = { readonly groupId: AutomationGroupId; readonly isActive: boolean; readonly isExpanded: boolean; readonly onToggle: () => void; readonly onToggleActivation: () => void; }; const AMOUNT_OF_PROFILES_IN_HEADER = 4; export const Header = ( { groupId, isActive, isExpanded, onToggle, onToggleActivation, }: HeaderProps ): JSX.Element => { const name = useGroupName( groupId ); const socialProfiles = useActiveProfiles( groupId ); const isSaving = useIsSaving(); return (
{ if ( e.key === 'Enter' || e.key === ' ' ) { e.preventDefault(); onToggle(); } } } >
{ if ( e.key === 'Enter' || e.key === ' ' ) { e.preventDefault(); onToggle(); } } } >
ev.stopPropagation() } role="button" tabIndex={ 0 } aria-disabled={ false } aria-pressed={ false } onKeyDown={ ( e ) => { if ( e.key === 'Enter' || e.key === ' ' ) { e.preventDefault(); e.stopPropagation(); } } } >
{ name }
{ socialProfiles.length <= AMOUNT_OF_PROFILES_IN_HEADER ? socialProfiles.map( ( { id } ) => ( ) ) : socialProfiles .slice( 0, AMOUNT_OF_PROFILES_IN_HEADER - 1 ) .map( ( { id } ) => ( ) ) } { socialProfiles.length > AMOUNT_OF_PROFILES_IN_HEADER && (
{ `+${ socialProfiles.length - AMOUNT_OF_PROFILES_IN_HEADER + 1 }` }
) }
); }; // ============ // HELPER VIEWS // ============ const NotificationIcon = ( { groupId, isActive, isExpanded, }: Pick< HeaderProps, 'groupId' | 'isActive' | 'isExpanded' > ): JSX.Element | null => { const isValidGroup = useIsValidGroup( groupId ); const areProfilesSelected = useAreProfilesSelected( groupId ); const hasRequiredPlan = useIsSubscribed( 'standard' ); const isErrorVisible = ( isActive || isExpanded ) && ( ! isValidGroup || ( ! isExpanded && ! areProfilesSelected ) ); if ( isErrorVisible ) { return (
); } if ( ! hasRequiredPlan && 'universal' !== groupId ) { return (
); } return null; }; // ===== // HOOKS // ===== const useActiveProfiles = ( groupId: AutomationGroupId ) => { const { profileSettings, profiles } = useSelect( ( select ) => { const group = select( NC_AUTOMATION_SETTINGS ).getAutomationGroup( groupId ); return { profileSettings: group?.profileSettings ?? ( EMPTY_OBJECT as Record< Uuid, ProfileAutomationSettings > ), profiles: select( NC_DATA ).getSocialProfiles() ?? EMPTY_ARRAY, }; }, [ groupId ] ); return useMemo( () => { const enabledIds = new Set( Object.values( profileSettings ) .filter( ( ps ) => ps.enabled ) .map( ( ps ) => ps.profileId ) ); if ( enabledIds.size === 0 ) { return EMPTY_ARRAY; } return profiles.filter( ( p ) => enabledIds.has( p.id ) ); }, [ profileSettings, profiles ] ); }; const useIsValidGroup = ( groupId: AutomationGroupId ) => useSelect( ( select ) => select( NC_AUTOMATION_SETTINGS ).isGroupValid( groupId ), [ groupId ] ); const useAreProfilesSelected = ( groupId: AutomationGroupId ) => { const group = useAutomationGroup( groupId ); return !! values( group?.profileSettings ).filter( ( ps ) => ps.enabled ) .length; };