/** * WordPress dependencies */ import { sprintf, _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { castArray, compact } from 'lodash'; import { SocialMessageTextPreview } from '@nelio-content/components'; import { dateI18n } from '@nelio-content/date'; /** * Internal dependencies */ import './style.scss'; import { MaybeSharedImage } from './maybe-shared-image'; import type { PreviewAttributes } from '../../../hooks'; export const RedditPreview = ( { activeProfile: { displayName }, image, schedule, textComputed, type, }: PreviewAttributes ): JSX.Element => { const imageItems = compact( [ ...castArray( image ?? [] ) ] ); return (
{ sprintf( /* translators: %1$s: Username. %2$s: Date. */ _x( 'Published by %1$s. %2$s', 'text (reddit)', 'nelio-content' ), displayName, dateI18n( _x( 'F j, Y', 'text (Reddit preview date)', 'nelio-content' ), schedule ) ) }
{ shortenLink( lastLink( textComputed ) ).replace( /^https?:\/\//, '' ) }
{ 'image' === type && !! imageItems.length ? ( { ) : ( ) }
); }; // ======= // HELPERS // ======= const shortenLink = ( link: string ) => { const result = link.replace( /^(https?:\/\/[^\/]+.{0,16}).*/, '$1' ); return result === link ? result : result + '…'; }; const lastLink = ( text: string ) => { const links = ` ${ text } `.match( /\s(https?:\/\/[^ ]*)\s/g ) || []; return links[ links.length - 1 ] || ''; };