File size: 3,967 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Card } from '@automattic/components';
import debug from 'debug';
import PropTypes from 'prop-types';
import { Component } from 'react';
import DocumentHead from 'calypso/components/data/document-head';
import Main from 'calypso/components/main';
import SearchCard from 'calypso/components/search-card';
import DocService from './service';
import './style.scss';

/**
 * Constants
 */

const DEFAULT_FILES = [
	'docs/guide/index.md',
	'README.md',
	'docs/CONTRIBUTING.md',
	'docs/coding-guidelines.md',
	'docs/coding-guidelines/javascript.md',
	'docs/coding-guidelines/css.md',
	'docs/coding-guidelines/html.md',
	'docs/accessibility.md',
];

/**
 * Module variables
 */

const log = debug( 'calypso:devdocs' );

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

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

	static defaultProps = {
		term: '',
	};

	state = {
		term: this.props.term,
		results: [],
		defaultResults: [],
		inputValue: '',
		searching: false,
	};

	// load default files if not already cached
	loadDefaultFiles = () => {
		if ( this.state.defaultResults.length ) {
			return;
		}

		DocService.list( DEFAULT_FILES, ( err, results ) => {
			if ( ! err ) {
				this.setState( {
					defaultResults: results,
				} );
			}
		} );
	};

	componentDidMount() {
		const { term } = this.state;
		this.loadDefaultFiles();
		if ( ! term ) {
			return;
		}
		this.onSearchChange( this.state.term );
		this.onSearch( this.state.term );
	}

	componentDidUpdate( prevProps, prevState ) {
		if ( typeof this.props.onSearchChange === 'function' && prevState.term !== this.state.term ) {
			this.props.onSearchChange( this.state.term );
		}
	}

	notFound = () => {
		return (
			this.state.inputValue &&
			this.state.term &&
			! this.state.results.length &&
			! this.state.searching
		);
	};

	onSearchChange = ( term ) => {
		this.setState( {
			inputValue: term,
			term: term,
			searching: !! term,
		} );
	};

	onSearch = ( term ) => {
		if ( ! term ) {
			return;
		}
		DocService.search( term, ( err, results ) => {
			if ( err ) {
				log( 'search error: %o', err );
			}

			this.setState( {
				results,
				searching: false,
			} );
		} );
	};

	results = () => {
		if ( this.notFound() ) {
			return <p>Not Found</p>;
		}

		const searchResults = this.state.inputValue ? this.state.results : this.state.defaultResults;
		return searchResults.map( function ( result ) {
			let url = '/devdocs/' + result.path;

			if ( this.state.term ) {
				url += '?term=' + encodeURIComponent( this.state.term );
			}

			return (
				<Card compact className="devdocs__result" key={ result.path }>
					<header className="devdocs__result-header">
						<h1 className="devdocs__result-title">
							<a className="devdocs__result-link" href={ url }>
								{ result.title }
							</a>
						</h1>
						<h2 className="devdocs__result-path">{ result.path }</h2>
					</header>
					{ this.snippet( result ) }
				</Card>
			);
		}, this );
	};

	snippet = ( result ) => {
		// split around <mark> tags to avoid setting unescaped inner HTML
		const parts = result.snippet.split( /(<mark>.*?<\/mark>)/ );

		return (
			<div className="devdocs__result-snippet" key={ 'snippet' + result.path }>
				{ parts.map( function ( part, i ) {
					const markMatch = part.match( /<mark>(.*?)<\/mark>/ );
					if ( markMatch ) {
						return <mark key={ 'mark' + i }>{ markMatch[ 1 ] }</mark>;
					}
					return part;
				} ) }
			</div>
		);
	};

	render() {
		return (
			<Main className="devdocs devdocs__search">
				<DocumentHead title="Calypso Docs" />

				<SearchCard
					autoFocus // eslint-disable-line jsx-a11y/no-autofocus
					placeholder="Search documentation…"
					analyticsGroup="Docs"
					initialValue={ this.state.term }
					delaySearch
					onSearchChange={ this.onSearchChange }
					onSearch={ this.onSearch }
				/>
				<div className="devdocs__results">{ this.results() }</div>
			</Main>
		);
	}
}