import { h } from 'preact';
import PropTypes from 'prop-types';
import { useEffect } from 'preact/hooks';
import { ErrorList } from './ErrorList';
import { AccessibilitySuggestions } from './AccessibilitySuggestions';
function titleArea({
previewResponse,
articleState,
errors,
markdownLintErrors,
}) {
const tagArray = previewResponse.tags || articleState.tagList.split(', ');
let tags = '';
if (tagArray.length > 0 && tagArray[0].length > 0) {
tags = tagArray.map((tag) => {
return (
tag.length > 0 && (
#
{tag}
)
);
});
}
// The v2 editor stores its cover image in articleState.mainImage, while the v1 editor
// stores it as previewResponse.cover_image. When previewing, we handle both by
// defaulting to setting the cover image to the mainImage on the article (v2),
// and only using the cover image from the previewResponse if it exists (v1).
let coverImage = articleState.mainImage || '';
if (articleState.previewShowing) {
// In preview state, use the cover_image from previewResponse.
if (previewResponse.cover_image && previewResponse.cover_image.length > 0) {
coverImage = previewResponse.cover_image;
}
}
const previewTitle = previewResponse.title || articleState.title || '';
return (
);
}
const previewResponsePropTypes = PropTypes.shape({
processed_html: PropTypes.string.isRequired,
title: PropTypes.string,
tags: PropTypes.array,
cover_image: PropTypes.string,
});
export const Preview = ({
previewResponse,
articleState,
errors,
markdownLintErrors,
}) => {
useEffect(() => {
if (previewResponse.processed_html.includes('twitter-timeline')) {
attachTwitterTimelineScript();
}
}, [previewResponse]);
return (
{titleArea({
previewResponse,
articleState,
errors,
markdownLintErrors,
})}
);
};
function attachTwitterTimelineScript() {
const script = document.createElement('script');
script.src = 'https://platform.twitter.com/widgets.js';
script.async = true;
document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}
Preview.propTypes = {
previewResponse: previewResponsePropTypes.isRequired,
errors: PropTypes.object,
markdownLintErrors: PropTypes.arrayOf(PropTypes.object),
articleState: PropTypes.shape({
id: PropTypes.number,
title: PropTypes.string,
tagList: PropTypes.string,
description: PropTypes.string,
canonicalUrl: PropTypes.string,
series: PropTypes.string,
allSeries: PropTypes.arrayOf(PropTypes.string),
bodyMarkdown: PropTypes.string,
published: PropTypes.bool,
previewShowing: PropTypes.bool,
helpShowing: PropTypes.bool,
previewResponse: previewResponsePropTypes,
helpHTML: PropTypes.string,
submitting: PropTypes.bool,
editing: PropTypes.bool,
imageManagementShowing: PropTypes.bool,
moreConfigShowing: PropTypes.bool,
mainImage: PropTypes.string,
organization: PropTypes.shape({
name: PropTypes.string.isRequired,
bg_color_hex: PropTypes.string.isRequired,
text_color_hex: PropTypes.string.isRequired,
profile_image_90: PropTypes.string.isRequired,
}),
postUnderOrg: PropTypes.bool,
errors: PropTypes.any,
edited: PropTypes.bool,
version: PropTypes.string,
}).isRequired,
};
Preview.displayName = 'Preview';