| import { Gridicon } from '@automattic/components'; |
| import { localize } from 'i18n-calypso'; |
| import { some } from 'lodash'; |
| import PropTypes from 'prop-types'; |
| import { Component } from 'react'; |
| import { resemblesUrl, withoutHttp, addSchemeIfMissing, urlToDomainAndPath } from 'calypso/lib/url'; |
| import ReaderFollowFeedIcon from 'calypso/reader/components/icons/follow-feed-icon'; |
| import ReaderFollowingFeedIcon from 'calypso/reader/components/icons/following-feed-icon'; |
| import FollowButton from 'calypso/reader/follow-button'; |
| import { commonExtensions } from 'calypso/state/reader/follows/selectors/get-reader-aliased-follow-feed-url'; |
| import './style.scss'; |
|
|
| class SearchFollowButton extends Component { |
| static propTypes = { |
| query: PropTypes.string, |
| feeds: PropTypes.array, |
| }; |
|
|
| |
| |
| |
| |
| |
| isPotentialFeedUrl = ( url ) => { |
| let isPotentialFeedUrl = false; |
| if ( resemblesUrl( url ) ) { |
| let parsedUrl; |
| try { |
| parsedUrl = new URL( url ); |
| } catch { |
| |
| } |
|
|
| |
| if ( parsedUrl === undefined ) { |
| try { |
| parsedUrl = new URL( 'http://' + url ); |
| } catch { |
| |
| } |
| } |
|
|
| if ( parsedUrl ) { |
| isPotentialFeedUrl = some( commonExtensions, ( ext ) => |
| parsedUrl.toString().includes( ext ) |
| ); |
| } |
| } |
| return isPotentialFeedUrl; |
| }; |
|
|
| render() { |
| const { query, translate, feeds } = this.props; |
|
|
| |
| if ( ! feeds && ! this.isPotentialFeedUrl( query ) ) { |
| return null; |
| } |
|
|
| |
| |
| |
| let feed; |
| if ( resemblesUrl( query ) ) { |
| feed = feeds?.find( ( f ) => f?.feed_URL?.includes( urlToDomainAndPath( query ) ) ); |
| } |
|
|
| |
| if ( ! feed ) { |
| return null; |
| } |
|
|
| |
| if ( feed.is_following === true ) { |
| return null; |
| } |
|
|
| let followTitle = withoutHttp( query ); |
| |
| if ( feed?.name?.length > 0 ) { |
| followTitle = feed.name; |
| } |
|
|
| let followUrl = null; |
| |
| if ( feed?.feed_URL ) { |
| followUrl = feed.feed_URL; |
| } |
| |
| if ( feed?.subscribe_URL ) { |
| followUrl = feed.subscribe_URL; |
| } |
|
|
| |
| if ( ! followUrl ) { |
| return null; |
| } |
|
|
| return ( |
| <div className="search-stream__url-follow"> |
| <p> |
| <Gridicon icon="info" size="16" /> |
| <strong>{ translate( 'Click below to add this site to your Reader feed:' ) }</strong> |
| </p> |
| <FollowButton |
| followLabel={ translate( 'Subscribe to %s', { |
| args: followTitle, |
| comment: '%s is the name of the site being subscribed to. For example: "Discover"', |
| } ) } |
| followingLabel={ translate( 'Subscribed to %s', { |
| args: followTitle, |
| comment: '%s is the name of the site being subscribed to. For example: "Discover"', |
| } ) } |
| siteUrl={ addSchemeIfMissing( followUrl, 'http' ) } |
| followIcon={ ReaderFollowFeedIcon( { iconSize: 20 } ) } |
| followingIcon={ ReaderFollowingFeedIcon( { iconSize: 20 } ) } |
| /> |
| </div> |
| ); |
| } |
| } |
|
|
| export default localize( SearchFollowButton ); |
|
|