diff --git a/package.json b/package.json index 8f50d62..3912929 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "react-redux": "^5.0.4", "react-refresh": "^0.8.3", "react-tabs": "3.0.0", + "react-twitter-embed": "^4.0.4", "redux": "^3.6.0", "redux-thunk": "^2.2.0", "reselect": "^3.0.1", diff --git a/src/common/utilities.js b/src/common/utilities.js index bfc3035..bdcb50a 100644 --- a/src/common/utilities.js +++ b/src/common/utilities.js @@ -323,6 +323,13 @@ export function typeForPath(path) { case /\.(pdf)$/.test(path): type = "Document"; break; + case /.+(twitter\.com).+/.test(path): + type = "Tweet"; + break; + case /.+(t\.me).+/.test(path): + type = "Telegram"; + break; + default: type = "Unknown"; break; diff --git a/src/components/controls/Card.js b/src/components/controls/Card.js index 93f1407..58d2b18 100644 --- a/src/components/controls/Card.js +++ b/src/components/controls/Card.js @@ -58,14 +58,6 @@ export const generateCardLayout = { }, ], ...event.sources.flatMap((source, idx) => [ - [ - { - kind: "text", - title: `Source ${idx}`, - value: source.description || ``, - scaleFont: 1.1, - }, - ], source.paths.map((p) => ({ kind: "media", title: "Media", diff --git a/src/components/controls/CardStack.js b/src/components/controls/CardStack.js index a6d5ad0..c5082af 100644 --- a/src/components/controls/CardStack.js +++ b/src/components/controls/CardStack.js @@ -67,7 +67,6 @@ class CardStack extends React.Component { return events.map((event, idx) => { const thisRef = React.createRef(); this.refs[idx] = thisRef; - console.log(event); const content = generateTemplate({ event, @@ -78,7 +77,6 @@ class CardStack extends React.Component { return ( { ); + + case "Telegram": + return ( +
+ +
+ ); + + case "Tweet": + const tweetIdRegex = + /https?:\/\/twitter.com\/[0-9a-zA-Z_]{1,20}\/status\/([0-9]*)/; + const match = tweetIdRegex.exec(src); + const tweetId = match[1]; + + return ( +
+ +
+ ); default: return null; } diff --git a/src/components/controls/atoms/TelegramEmbed.js b/src/components/controls/atoms/TelegramEmbed.js new file mode 100644 index 0000000..deaca13 --- /dev/null +++ b/src/components/controls/atoms/TelegramEmbed.js @@ -0,0 +1,105 @@ +/* + * Adapted from https://github.com/cudr/react-telegram-embed + */ +import React, { Component } from "react"; + +const styles = { + width: "100%", + frameBorder: "0", + scrolling: "no", + border: "none", + overflow: "hidden", +}; + +const containerStyles = { + marginLeft: -60, +}; + +/** + * Simple Component for Telegram embedding + * @extends Component + */ + +class TelegramEmbed extends Component { + constructor(props) { + super(props); + + this.state = { + src: this.props.src, + id: "", + height: "80px", + }; + this.messageHandler = this.messageHandler.bind(this); + this.urlObj = document.createElement("a"); + } + + componentDidMount() { + window.addEventListener("message", this.messageHandler); + + this.iFrame.addEventListener("load", () => { + this.checkFrame(this.state.id); + }); + } + + componentWillUnmount() { + window.removeEventListener("message", this.messageHandler); + } + + messageHandler({ data, source }) { + if ( + !data || + typeof data !== "string" || + source !== this.iFrame.contentWindow + ) { + return; + } + + const action = JSON.parse(data); + + if (action.event === "resize" && action.height) { + this.setState({ + height: action.height + "px", + }); + } + } + + checkFrame(id) { + this.iFrame.contentWindow.postMessage( + JSON.stringify({ event: "visible", frame: id }), + "*" + ); + } + + componentWillReceiveProps({ src }) { + if (this.state.src !== src) { + this.urlObj.href = src; + const id = `telegram-post${this.urlObj.pathname.replace( + /[^a-z0-9_]/gi, + "-" + )}`; + + this.setState({ src, id }, () => this.checkFrame(id)); + } + } + + render() { + const { src, height } = this.state; + const { container } = this.props; + + return ( +
+