/* eslint-disable wpcalypso/jsx-classname-namespace */
import { Button, Card, Gridicon } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import FollowButton from 'calypso/blocks/follow-button/button';
import SitePlaceholder from 'calypso/blocks/site/placeholder';
import { useDispatch, useSelector } from 'calypso/state';
import { addReaderListSite, deleteReaderListSite } from 'calypso/state/reader/lists/actions';
import { getMatchingItem } from 'calypso/state/reader/lists/selectors';
import ItemRemoveDialog from './item-remove-dialog';
import { Item, List, Site, SiteError } from './types';
function isSiteError( site: Site | SiteError ): site is SiteError {
return 'errors' in site;
}
function SiteTitle( { site: { name, URL, feed_URL } }: { site: Site } ) {
return <>{ name || URL || feed_URL }>;
}
function renderSite( site: Site ) {
return (
);
}
function renderSiteError( err: SiteError ) {
return (
{ err.error_data.site_gone ? 'Site has been deleted' : 'Unknown error' }
);
}
/* eslint-disable wpcalypso/jsx-classname-namespace */
export default function SiteItem( props: {
hideIfInList?: boolean;
isFollowed?: boolean;
item: Item;
list: List;
owner: string;
hideFollowButton?: boolean;
} ) {
const { item, list, owner } = props;
const site = props.item.meta?.data?.site as Site | SiteError | undefined;
const dispatch = useDispatch();
const translate = useTranslate();
const isInList = !! useSelector( ( state ) =>
getMatchingItem( state, { siteId: props.item.site_ID, listId: props.list.ID } )
);
const isRecommendedBlogsList = list.slug === 'recommended-blogs';
const [ showDeleteConfirmation, setShowDeleteConfirmation ] = useState( false );
const addItem = () =>
item.site_ID && dispatch( addReaderListSite( list.ID, owner, list.slug, item.site_ID ) );
const deleteItem = ( shouldDelete: boolean ) => {
setShowDeleteConfirmation( false );
shouldDelete &&
item.site_ID &&
dispatch( deleteReaderListSite( list.ID, owner, list.slug, item.site_ID ) );
};
if ( isInList && props.hideIfInList ) {
return null;
}
if ( ! site ) {
// TODO: Add support for removing invalid site list item
return (
);
}
return (
{ isSiteError( site ) ? renderSiteError( site ) : renderSite( site ) }
{ props.isFollowed && ! props.hideFollowButton && (
) }
{ ! isInList ? (
) : (
) }
{ ! isSiteError( site ) && (
}
type="site"
visibility={ showDeleteConfirmation }
/>
) }
);
}