/** * WordPress dependencies */ import { Button, Dashicon, ExternalLink, Icon, } from '@safe-wordpress/components'; import { useDispatch, useSelect } from '@safe-wordpress/data'; import { useMemo } from '@safe-wordpress/element'; import { sprintf, _x } from '@safe-wordpress/i18n'; /** * Extenral dependencies */ import { useAuthorName, useIsUserYou } from '@nelio-content/data'; import type { EditorialReference, Url } from '@nelio-content/types'; /** * Internal dependencies */ import './style.scss'; import { DiscardButton } from './discard-button'; import { store as NC_EDIT_POST } from '../../../store'; export type LinkProps = { readonly link: Url; }; export const Link = ( { link }: LinkProps ): JSX.Element => { return (
); }; // ============ // HELPER VIEWS // ============ const ReferenceTitle = ( { link }: Pick< LinkProps, 'link' > ) => { const { title, url } = useReference( link ); if ( ! title ) { return (
{ url }
); } return ( <>
{ title }
{ url }
); }; const ExtraInformation = ( { link }: Pick< LinkProps, 'link' > ) => { const isLoading = useIsLoading( link ); const reference = useReference( link ); const { author, email, twitter } = reference; if ( isLoading ) { return (
{ _x( 'Loading…', 'text', 'nelio-content' ) }
); } if ( ! twitter && ! email ) { return (
{ author || _x( 'Unknown Author', 'text', 'nelio-content' ) }
); } return (
{ author || _x( 'Unknown Author', 'text', 'nelio-content' ) }
{ !! twitter && ( } /> ) }{ ' ' } { !! email && ( ) }
); }; const Advisor = ( { link }: LinkProps ) => { const ref = useReference( link ); const areYouTheAdvisor = useIsUserYou( ref?.suggestionAdvisorId ); const advisorName = useAuthorName( ref?.suggestionAdvisorId ); if ( ! ref ) { return (
{ _x( 'Suggested by someone', 'text', 'nelio-content' ) }
); } if ( ! ref.suggestionAdvisorId || areYouTheAdvisor ) { return (
{ _x( 'Suggested by you', 'text', 'nelio-content' ) }
); } return (
{ ! advisorName ? _x( 'Suggested by someone', 'text', 'nelio-content' ) : sprintf( /* translators: %s: Author name. */ _x( 'Suggested by %s', 'text', 'nelio-content' ), advisorName ) }
); }; const Actions = ( { link }: LinkProps ) => { const isLoading = useIsLoading( link ); const { openReferenceEditor } = useDispatch( NC_EDIT_POST ); const { discardReference } = useDispatch( NC_EDIT_POST ); if ( isLoading ) { return (
{ sprintf( '%1$s | %2$s', _x( 'Discard', 'command', 'nelio-content' ), _x( 'Edit', 'command', 'nelio-content' ) ) }
); } return (
discardReference( link ) } /> { ' | ' }
); }; // ===== // HOOKS // ===== const useReference = ( link: Url ): Omit< EditorialReference, 'id' > => { const stored = useSelect( ( select ) => select( NC_EDIT_POST ).getReferenceByUrl( link ), [ link ] ); return useMemo( () => { const twitter = stored?.twitter ?? ''; return { author: '', date: '', email: '', isExternal: true, isSuggestion: true, status: 'pending', title: '', ...stored, url: link, twitter: /^@/.test( twitter ) ? twitter.substring( 1 ) : twitter, }; }, [ stored, link ] ); }; const useIsLoading = ( link: Url ) => useSelect( ( select ) => select( NC_EDIT_POST ).isReferenceLoading( link ), [ link ] );