/** * WordPress dependencies */ import { useSelect, useDispatch } from '@safe-wordpress/data'; import { useMemo } from '@safe-wordpress/element'; /** * External dependencies */ import { EMPTY_ARRAY } from '@nelio-content/constants'; import type { FSProduct } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_ACCOUNT } from './store'; import type { DialogName, LockReason } from './store/types'; export const useDialog = ( name: DialogName ): [ boolean, ( v: boolean ) => void ] => { const isVisible = useSelect( ( select ) => select( NC_ACCOUNT ).isDialogOpen( name ), [ name ] ); const { openDialog, closeDialog } = useDispatch( NC_ACCOUNT ); const setVisible = ( v: boolean ) => v ? openDialog( name ) : closeDialog(); return [ isVisible, setVisible ]; }; export const useIsLocked = ( reason?: LockReason ): boolean => useSelect( ( select ) => select( NC_ACCOUNT ).isLocked( reason ), [ reason ] ); export const useUpgradeableProducts = (): ReadonlyArray< FSProduct > => { const { plan, productId, products } = useSelect( ( select ) => { const account = select( NC_ACCOUNT ).getAccount(); return { plan: account.plan, productId: account.plan !== 'free' ? account.productId : undefined, products: select( NC_ACCOUNT ).getProducts() ?? EMPTY_ARRAY, }; }, [] ); return useMemo( () => { if ( plan === 'free' || ! productId ) { return EMPTY_ARRAY; } return products.filter( ( p ) => p.upgradeableFrom.includes( productId ) ); }, [ plan, productId, products ] ); };