import { selectTab, TAB_IDS } from '../../../utils/tabSelector'; import { useEffect, useState } from '@wordpress/element'; import { useDispatch, useSelect } from '@wordpress/data'; import { STORE_NAME as TODOS_STORE_NAME } from '../../../data/todos'; const TodoSettingsBlock = ( { todosData, className = '', setActiveModal, onDismissTodo, } ) => { const [ dismissingIds, setDismissingIds ] = useState( new Set() ); const { completedTodos, dismissedTodos } = useSelect( ( select ) => ( { completedTodos: select( TODOS_STORE_NAME ).getCompletedTodos() || [], dismissedTodos: select( TODOS_STORE_NAME ).getDismissedTodos() || [], } ), [] ); const { completeOnClick } = useDispatch( TODOS_STORE_NAME ); 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 ); }; const handleClick = async ( todo ) => { 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.completeOnClick === true ) { await completeOnClick( todo.id ); } if ( todo.action.modal ) { setActiveModal( todo.action.modal ); } }; // Filter out dismissed todos for display and limit to 5. const visibleTodos = todosData .filter( ( todo ) => ! dismissedTodos.includes( todo.id ) ) .slice( 0, 5 ); return (
{ visibleTodos.map( ( todo ) => ( handleDismiss( todo.id, e ) } onClick={ () => handleClick( todo ) } /> ) ) }
); }; const TodoItem = ( { title, description, isCompleted, isDismissing, onClick, onDismiss, } ) => { return (
{ isCompleted && ( ) }
{ title }
{ description && (
{ description }
) }
); }; export default TodoSettingsBlock;