|
| 1 | +import {Modal} from 'react-overlays'; |
| 2 | +import React from 'react'; |
| 3 | +import {host} from '../index'; |
| 4 | +import styles from '../../../css/bootstrap.min.css'; |
| 5 | + |
| 6 | +const modalStyle = { |
| 7 | + position: 'fixed', |
| 8 | + zIndex: 10, |
| 9 | + top: 0, bottom: 0, left: 0, right: 0 |
| 10 | +}; |
| 11 | + |
| 12 | +const backdropStyle = { |
| 13 | + position: 'fixed', |
| 14 | + top: 0, bottom: 0, left: 0, right: 0, |
| 15 | + zIndex: 'auto', |
| 16 | + backgroundColor: '#000', |
| 17 | + opacity: 0.5 |
| 18 | +}; |
| 19 | + |
| 20 | +const dialogStyle = function () { |
| 21 | + return { |
| 22 | + position: 'absolute', |
| 23 | + width: '100%', |
| 24 | + top: '5vh', |
| 25 | + height: '95vh', |
| 26 | + border: '1px solid #e5e5e5', |
| 27 | + backgroundColor: 'white', |
| 28 | + boxShadow: '0 5px 15px rgba(0,0,0,.5)', |
| 29 | + padding: 20 |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +function getTweetHtml(json) { |
| 34 | + return ( |
| 35 | + <div style={{padding: '5px', borderRadius: '3px', border: '1px solid black', margin: '10px'}}> |
| 36 | + <a href={json.link} target="_blank"> |
| 37 | + <div style={{marginBottom: '5px'}}> |
| 38 | + <b>@{json['screen_name']}</b> |
| 39 | + </div> |
| 40 | + <div style={{overflowX: 'hidden'}}>{json['text']}</div> |
| 41 | + </a> |
| 42 | + </div> |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +export default class StreamOverlay extends React.Component { |
| 47 | + constructor (props) { |
| 48 | + super(props); |
| 49 | + this.state = {channel: this.props.channel, country: this.props.country, tweets: []}; |
| 50 | + this.close = this.close.bind(this); |
| 51 | + this.eventSource = null; |
| 52 | + }; |
| 53 | + |
| 54 | + close() { |
| 55 | + if (this.eventSource) { |
| 56 | + this.eventSource.close(); |
| 57 | + this.eventSource = null; |
| 58 | + } |
| 59 | + this.props.onClose(); |
| 60 | + } |
| 61 | + |
| 62 | + startEventSource(country) { |
| 63 | + let channel = 'twitter%2Fcountry%2F' + country; |
| 64 | + if (this.eventSource) { |
| 65 | + return; |
| 66 | + } |
| 67 | + this.eventSource = new EventSource(host + '/api/stream.json?channel=' + channel); |
| 68 | + this.eventSource.onmessage = (event) => { |
| 69 | + let json = JSON.parse(event.data); |
| 70 | + this.state.tweets.push(json); |
| 71 | + if (this.state.tweets.length > 250) { |
| 72 | + this.state.tweets.shift(); |
| 73 | + } |
| 74 | + this.setState(this.state); |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + render () { |
| 79 | + this.startEventSource(this.props.channel); |
| 80 | + return ( |
| 81 | + <div className={styles.container}> |
| 82 | + <Modal aria-labelledby='modal-label' |
| 83 | + style={modalStyle} |
| 84 | + backdropStyle={backdropStyle} |
| 85 | + show={true} |
| 86 | + onHide={this.close}> |
| 87 | + <div style={dialogStyle()}> |
| 88 | + <h2 id='modal-label'>Streaming Tweets from |
| 89 | + <span style={{background: '-webkit-linear-gradient(0deg, #D02010 2%, #FFFB20 98%)', |
| 90 | + 'WebkitTextFillColor': 'transparent', |
| 91 | + 'WebkitBackgroundClip': 'text', |
| 92 | + }}>{this.state.country}</span></h2> |
| 93 | + <div className={styles.container} style={{'height': '100%', 'overflowY': 'auto', |
| 94 | + 'overflowX': 'hidden', maxWidth: '100%'}}> |
| 95 | + {this.state.tweets.reverse().map(getTweetHtml)} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + </Modal> |
| 99 | + </div> |
| 100 | + ) |
| 101 | + } |
| 102 | +}; |
0 commit comments