import { selectTab, TAB_IDS } from '../../../utils/tabSelector'; import { useEffect, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { STORE_NAME as TODOS_STORE_NAME } from '../../../data/todos'; const TodoSettingsBlock = ( { todosData, className = '', setActiveModal, setActiveHighlight, onDismissTodo, } ) => { const [ dismissingIds, setDismissingIds ] = useState( new Set() ); const { completedTodos, dismissedTodos } = useSelect( ( select ) => ( { completedTodos: select( TODOS_STORE_NAME ).getCompletedTodos() || [], dismissedTodos: select( TODOS_STORE_NAME ).getDismissedTodos() || [], } ), [] ); useEffect( () => { if ( dismissedTodos.length === 0 ) { setDismissingIds( new Set() ); } }, [ dismissedTodos ] ); if ( todosData.length === 0 ) { return null; } const handleDismiss = ( todoId, e ) => { e.preventDefault(); e.stopPropagation(); setDismissingIds( ( prev ) => new Set( [ ...prev, todoId ] ) ); setTimeout( () => { onDismissTodo( todoId ); }, 300 ); }; // Filter out dismissed todos for display const visibleTodos = todosData.filter( ( todo ) => ! dismissedTodos.includes( todo.id ) ); return (
{ visibleTodos.map( ( todo ) => ( handleDismiss( todo.id, e ) } onClick={ async () => { if ( todo.action.type === 'tab' ) { const tabId = TAB_IDS[ todo.action.tab.toUpperCase() ]; await selectTab( tabId, todo.action.section ); } else if ( todo.action.type === 'external' ) { window.open( todo.action.url, '_blank' ); } if ( todo.action.modal ) { setActiveModal( todo.action.modal ); } if ( todo.action.highlight ) { setActiveHighlight( todo.action.highlight ); } } } /> ) ) }
); }; const TodoItem = ( { title, description, isCompleted, isDismissing, onClick, onDismiss, } ) => { return (
{ isCompleted && ( ) }
{ title }
{ description && (
{ description }
) }
); }; export default TodoSettingsBlock;