/** * WordPress dependencies */ import { useSelect } from '@safe-wordpress/data'; /** * External dependencies */ import clsx from 'clsx'; import { EMPTY_ARRAY } from '@nelio-content/constants'; import { store as NC_DATA } from '@nelio-content/data'; /** * Internal dependencies */ import './style.scss'; import { store as NC_EDIT_POST } from '../../../store'; import { useMemo } from '@safe-wordpress/element'; export const Progress = (): JSX.Element | null => { const { completed, total } = useProgress(); if ( ! total ) { return null; } return (
{ Math.round( ( completed * 100 ) / total ) }%
); }; // ===== // HOOKS // ===== const useProgress = () => { const tasks = useSelect( ( select ) => { const postId = select( NC_EDIT_POST ).getPostId(); return select( NC_DATA ).getTasksRelatedToPost( postId ) ?? EMPTY_ARRAY; }, [] ); return useMemo( () => { let completed = 0; for ( const t of tasks ) { if ( t.completed ) { completed++; } } return { completed, total: tasks.length, }; }, [ tasks ] ); };