/** * External dependencies */ import { useDispatch, useSelect, select as wpDataSelect, } from '@wordpress/data'; import { Button, Card, CardHeader } from '@wordpress/components'; import { OPTIONS_STORE_NAME } from '@woocommerce/data'; import { EllipsisMenu } from '@woocommerce/components'; import { __ } from '@wordpress/i18n'; import { createContext, useContext } from '@wordpress/element'; import classNames from 'classnames'; /** * Internal dependencies */ import './dismissable-list.scss'; // using a context provider for the option name so that the option name prop doesn't need to be passed to the `DismissableListHeading` too const OptionNameContext = createContext( '' ); export const DismissableListHeading: React.FC< { onDismiss?: () => void; } > = ( { children, onDismiss = () => null } ) => { const { updateOptions } = useDispatch( OPTIONS_STORE_NAME ); const dismissOptionName = useContext( OptionNameContext ); const handleDismissClick = () => { onDismiss(); updateOptions( { [ dismissOptionName ]: 'yes', } ); }; return (
{ children }
(
) } />
); }; export const DismissableList: React.FC< { dismissOptionName: string; className?: string; } > = ( { children, className, dismissOptionName } ) => { const isVisible = useSelect( ( select: typeof wpDataSelect ) => { const { getOption, hasFinishedResolution } = select( OPTIONS_STORE_NAME ); const hasFinishedResolving = hasFinishedResolution( 'getOption', [ dismissOptionName, ] ); const isDismissed = getOption( dismissOptionName ) === 'yes'; return hasFinishedResolving && ! isDismissed; } ); if ( ! isVisible ) { return null; } return ( { children } ); };