From e6146db781ac0b96a3c26a38183dd358d92557e4 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Thu, 22 Feb 2018 23:36:37 -0800 Subject: [PATCH] added OpenGraphTag component --- helpers/renderFullPage.js | 1 + react/components/OpenGraphTags/index.jsx | 21 ++++++ react/components/OpenGraphTags/view.jsx | 83 ++++++++++++++++++++++ react/components/ShowAssetDetails/view.jsx | 2 + react/components/ShowAssetLite/view.jsx | 2 + 5 files changed, 109 insertions(+) create mode 100644 react/components/OpenGraphTags/index.jsx create mode 100644 react/components/OpenGraphTags/view.jsx diff --git a/helpers/renderFullPage.js b/helpers/renderFullPage.js index 4c5089ad..4786da58 100644 --- a/helpers/renderFullPage.js +++ b/helpers/renderFullPage.js @@ -9,6 +9,7 @@ module.exports = (helmet, html, preloadedState) => { ${helmet.title.toString()} + ${helmet.meta.toString()} ${helmet.link.toString()} diff --git a/react/components/OpenGraphTags/index.jsx b/react/components/OpenGraphTags/index.jsx new file mode 100644 index 00000000..0af0073c --- /dev/null +++ b/react/components/OpenGraphTags/index.jsx @@ -0,0 +1,21 @@ +import { connect } from 'react-redux'; +import View from './view'; + +const mapStateToProps = ({ show }) => { + // select request info + const requestId = show.request.id; + // select asset info + let asset; + const request = show.requestList[requestId] || null; + const assetList = show.assetList; + if (request && assetList) { + const assetKey = request.key; // note: just store this in the request + asset = assetList[assetKey] || null; + }; + // return props + return { + asset, + }; +}; + +export default connect(mapStateToProps, null)(View); diff --git a/react/components/OpenGraphTags/view.jsx b/react/components/OpenGraphTags/view.jsx new file mode 100644 index 00000000..85a53cfe --- /dev/null +++ b/react/components/OpenGraphTags/view.jsx @@ -0,0 +1,83 @@ +import React from 'react'; +import Helmet from 'react-helmet'; + +const { site: { title, host }, claim: { defaultThumbnail, defaultDescription } } = require('../../../config/speechConfig.js'); + +const determineOgThumbnailContentType = (thumbnail) => { + if (thumbnail) { + const fileExt = thumbnail.substring(thumbnail.lastIndexOf('.')); + switch (fileExt) { + case 'jpeg': + case 'jpg': + return 'image/jpeg'; + case 'png': + return 'image/png'; + case 'gif': + return 'image/gif'; + case 'mp4': + return 'video/mp4'; + default: + return 'image/jpeg'; + } + } + return ''; +}; + +class OpenGraphTags extends React.Component { + render () { + const { claimData } = this.props.asset; + const { contentType } = claimData; + const embedUrl = `${host}/${claimData.claimId}/${claimData.name}`; + const showUrl = `${host}/${claimData.claimId}/${claimData.name}`; + const source = `${host}/${claimData.claimId}/${claimData.name}.${claimData.fileExt}`; + const ogTitle = claimData.title || claimData.name; + const ogDescription = claimData.description || defaultDescription; + const ogThumbnailContentType = determineOgThumbnailContentType(claimData.thumbnail); + const ogThumbnail = claimData.thumbnail || defaultThumbnail; + return ( +
+ + {/* basic open graph tags */} + + + + + + + {/* basic twitter tags */} + + + { contentType === 'video/mp4' || contentType === 'video/webm' ? ( + + {/* video open graph tags */} + + + + + + + {/* video twitter tags */} + + + + + + + + + ) : ( + + {/* image open graph tags */} + + + + {/* image twitter tags */} + + + )}; +
+ ); + } +}; + +export default OpenGraphTags; diff --git a/react/components/ShowAssetDetails/view.jsx b/react/components/ShowAssetDetails/view.jsx index 3ee78911..af3e168a 100644 --- a/react/components/ShowAssetDetails/view.jsx +++ b/react/components/ShowAssetDetails/view.jsx @@ -1,5 +1,6 @@ import React from 'react'; import Helmet from 'react-helmet'; +import OpenGraphTags from 'components/OpenGraphTags'; import NavBar from 'containers/NavBar'; import ErrorPage from 'components/ErrorPage'; import AssetTitle from 'components/AssetTitle'; @@ -26,6 +27,7 @@ class ShowAssetDetails extends React.Component { )} +
diff --git a/react/components/ShowAssetLite/view.jsx b/react/components/ShowAssetLite/view.jsx index e919b35d..25b0220a 100644 --- a/react/components/ShowAssetLite/view.jsx +++ b/react/components/ShowAssetLite/view.jsx @@ -1,5 +1,6 @@ import React from 'react'; import Helmet from 'react-helmet'; +import OpenGraphTags from 'components/OpenGraphTags'; import { Link } from 'react-router-dom'; import AssetDisplay from 'components/AssetDisplay'; @@ -22,6 +23,7 @@ class ShowLite extends React.Component { ) : ( )} +