/** * WordPress dependencies */ import { useSelect, useDispatch } from '@safe-wordpress/data'; import { useMemo } from '@safe-wordpress/element'; /** * External dependencies */ import { filter } from 'lodash'; import { EMPTY_ARRAY } from '@nelio-content/constants'; import { store as NC_DATA } from '@nelio-content/data'; import { isUuid } from '@nelio-content/utils'; import type { AutoSelectedUuid, Maybe, SocialProfile, Uuid, } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_SOCIAL_EDITOR } from '../store'; export const useAvailableProfiles = (): ReadonlyArray< SocialProfile > => { const profiles = useSelect( ( select ) => select( NC_DATA ).getSocialProfiles() ?? EMPTY_ARRAY, [] ); const disabledProfiles = useSelect( ( select ) => select( NC_SOCIAL_EDITOR ).getDisabledProfileIds() ?? EMPTY_ARRAY, [] ); return useMemo( () => filter( profiles, ( { id } ) => ! disabledProfiles.includes( id ) ), [ profiles, disabledProfiles ] ); }; export const useSelectedProfiles = (): [ ReadonlyArray< Uuid > | readonly [ AutoSelectedUuid ], ( profileId: Uuid ) => void, ] => { const profiles = useSelect( ( select ) => select( NC_SOCIAL_EDITOR ).getSelectedSocialProfiles(), [] ); const manuallySelectedProfiles = profiles.filter( isUuid ); const { selectSocialProfile } = useDispatch( NC_SOCIAL_EDITOR ); const toggleProfile = ( profileId: Uuid ) => selectSocialProfile( profileId, ! manuallySelectedProfiles.includes( profileId ) ); return [ profiles, toggleProfile ]; }; export const useActiveProfile = (): Maybe< Uuid > => useSelect( ( select ) => select( NC_SOCIAL_EDITOR ).getActiveSocialProfile(), [] );