// @flow import React from 'react'; import LoadingScreen from 'component/common/loading-screen'; import CodeViewer from 'component/viewers/codeViewer'; import MarkdownPreview from 'component/common/markdown-preview'; type Props = { theme: string, source: { stream: string => any, fileType: string, contentType: string, }, }; type State = { error: boolean, loading: boolean, content: ?string, }; class DocumentViewer extends React.PureComponent { constructor(props: Props) { super(props); this.state = { error: false, loading: true, content: null, }; } componentDidMount() { const { source } = this.props; if (source && source.stream) { const stream = source.stream('utf8'); let data = ''; stream.on('data', chunk => { data += chunk; }); stream.on('end', () => { this.setState({ content: data, loading: false }); }); stream.on('error', () => { this.setState({ error: true, loading: false }); }); } } renderDocument() { let viewer = null; const { content } = this.state; const { source, theme } = this.props; const { fileType, contentType } = source; const markdownType = ['md', 'markdown']; if (markdownType.includes(fileType)) { // Render markdown viewer = ; } else { // Render plain text viewer = ; } return viewer; } render() { const { error, loading, content } = this.state; const isReady = content && !error; const loadingMessage = __('Rendering document.'); const errorMessage = __("Sorry, looks like we can't load the document."); return (
{loading && !error && } {error && } {isReady && this.renderDocument()}
); } } export default DocumentViewer;