File size: 2,571 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
import PropTypes from 'prop-types';
import { Component } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import highlight from 'calypso/lib/highlight';
import Error from './error';
import DocService from './service';

export default class extends Component {
	static displayName = 'SingleDocument';

	static propTypes = {
		path: PropTypes.string.isRequired,
		term: PropTypes.string,
		sectionId: PropTypes.string,
	};

	state = {
		body: '',
		error: null,
	};

	timeoutID = null;

	componentDidMount() {
		this.fetch();
	}

	componentDidUpdate( prevProps ) {
		if ( this.props.path !== prevProps.path ) {
			this.fetch();
		}
		if ( this.state.body ) {
			this.setBodyScrollPosition();
		}
	}

	componentWillUnmount() {
		this.clearLoadingMessage();
	}

	fetch = () => {
		this.setState( {
			body: '',
			error: null,
		} );
		this.delayLoadingMessage();
		DocService.fetch( this.props.path, ( error, body ) => {
			this.setState( { body, error } );
		} );
	};

	setBodyScrollPosition = () => {
		if ( this.props.sectionId ) {
			const sectionNode = document.getElementById( this.props.sectionId );

			if ( sectionNode ) {
				sectionNode.scrollIntoView();
			}
		}
	};

	delayLoadingMessage = () => {
		this.clearLoadingMessage();
		this.timeoutID = setTimeout( () => {
			if ( ! this.state.body ) {
				this.setState( {
					body: 'Loading…',
				} );
			}
		}, 1000 );
	};

	clearLoadingMessage = () => {
		if ( 'number' === typeof this.timeoutID ) {
			window.clearTimeout( this.timeoutID );
			this.timeoutID = null;
		}
	};

	renderBody() {
		const editURL = encodeURI(
			'https://github.com/Automattic/wp-calypso/edit/trunk/' + this.props.path
		);
		const { body, error } = this.state;

		if ( ! body || error ) {
			return null;
		}

		return (
			<div className="devdocs__body">
				<a
					className="devdocs__doc-edit-link"
					href={ editURL }
					target="_blank"
					rel="noopener noreferrer"
				>
					Improve this document on GitHub
				</a>
				<div
					className="devdocs__doc-content"
					// eslint-disable-next-line react/no-danger
					dangerouslySetInnerHTML={ { __html: highlight( this.props.term, body ) } }
				/>
			</div>
		);
	}

	render() {
		const { body, error } = this.state;
		const titleMatches = body && body.length && body.match( /<h1[^>]+>(.+)<\/h1>/ );
		const title = titleMatches && titleMatches[ 1 ];

		return (
			<div className="devdocs devdocs__doc">
				{ title ? <DocumentHead title={ title } /> : null }
				{ this.renderBody() }
				{ error && <Error /> }
			</div>
		);
	}
}