/** * WordPress dependencies */ import { useSelect, useDispatch } from '@safe-wordpress/data'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { uniq, without } from 'lodash'; import { AuthorSearcher } from '@nelio-content/components'; import type { AuthorId } from '@nelio-content/types'; /** * Internal dependencies */ import { Follower } from './follower'; import { store as NC_EDIT_POST } from '../../store'; export const Notifications = (): JSX.Element => { const [ followers, setFollowers ] = useFollowers(); const authorId = useAuthorId(); const shouldAuthorFollow = useShouldAuthorFollow(); const addFollower = ( followerId: AuthorId ) => setFollowers( uniq( [ ...followers, followerId ] ) ); const removeFollower = ( followerId: AuthorId ) => setFollowers( without( followers, followerId ) ); return (
void ( id && addFollower( id ) ) } />
{ followers.map( ( userId ) => ( removeFollower( userId ) } /> ) ) }
); }; // ===== // HOOKS // ===== const useAuthorId = () => useSelect( ( select ) => select( NC_EDIT_POST ).getAuthorId(), [] ); const useShouldAuthorFollow = () => useSelect( ( select ) => select( NC_EDIT_POST ).shouldAuthorBeFollower(), [] ); const useFollowers = () => { const authorId = useAuthorId(); const followers = useSelect( ( select ) => select( NC_EDIT_POST ).getFollowers(), [] ); const { setFollowers } = useDispatch( NC_EDIT_POST ); const expectedFollowers = useShouldAuthorFollow() && authorId ? uniq( [ authorId, ...followers ] ) : followers; return [ expectedFollowers, setFollowers ] as const; };