-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathindex.jsx
121 lines (102 loc) · 3.03 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/** @format */
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import classnames from 'classnames';
import url from 'url';
import { connect } from 'react-redux';
import { localize } from 'i18n-calypso';
import { noop, get } from 'lodash';
/**
* Internal dependencies
*/
import CompactCard from 'components/card/compact';
import photon from 'photon';
import { hasTouch } from 'lib/touch-detect';
import * as utils from 'state/posts/utils';
import TimeSince from 'components/time-since';
import { getEditorUrl } from 'state/selectors/get-editor-url';
/**
* Style dependencies
*/
import './style.scss';
class Draft extends Component {
static propTypes = {
post: PropTypes.object,
isPlaceholder: PropTypes.bool,
onTitleClick: PropTypes.func,
postImages: PropTypes.object,
selected: PropTypes.bool,
showAuthor: PropTypes.bool,
// via `localize`
translate: PropTypes.func.isRequired,
};
static defaultProps = {
onTitleClick: noop,
selected: false,
showAuthor: false,
};
render() {
const { post } = this.props;
let image = null;
let imageUrl, editPostURL;
if ( this.props.isPlaceholder ) {
return this.postPlaceholder();
}
if ( utils.userCan( 'edit_post', post ) ) {
editPostURL = this.props.editorUrl;
}
if ( this.props.postImages && this.props.postImages.canonical_image ) {
image = url.parse( this.props.postImages.canonical_image.uri, true );
imageUrl = '//' + image.hostname + image.pathname + '?w=680px';
}
if ( post && post.canonical_image ) {
image = url.parse( post.canonical_image.uri, true );
if ( image.hostname.indexOf( 'files.wordpress.com' ) > 0 ) {
imageUrl = '//' + image.hostname + image.pathname + '?w=680px';
} else {
imageUrl = photon( post.canonical_image.uri, { width: 680 } );
}
}
const classes = classnames( 'draft', `is-${ post.format }`, {
'has-image': !! image,
'is-placeholder': this.props.isPlaceholder,
'is-touch': hasTouch(),
'is-selected': this.props.selected,
} );
const title = post.title || (
<span className="draft__empty-text">{ this.props.translate( 'Untitled' ) }</span>
);
// Render each Post
return (
<CompactCard
className={ classes }
key={ post.ID }
href={ editPostURL }
onClick={ this.props.onTitleClick }
>
<h3 className="draft__title">{ title }</h3>
<TimeSince className="draft__time" date={ post.modified } />
{ image && this.renderImage( imageUrl ) }
</CompactCard>
);
}
renderImage( image ) {
const style = { backgroundImage: 'url(' + image + ')' };
return <div className="draft__featured-image" style={ style } />;
}
postPlaceholder() {
return (
/* eslint-disable jsx-a11y/heading-has-content */
<CompactCard className="draft is-placeholder">
<h3 className="draft__title" />
</CompactCard>
/* eslint-enable jsx-a11y/heading-has-content */
);
}
}
export default connect( ( state, { siteId, post } ) => ( {
editorUrl: getEditorUrl( state, siteId, get( post, 'ID' ) ),
} ) )( localize( Draft ) );