// @flow import React from 'react'; import fs from 'fs'; 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: { fileType: string, filePath: string, downloadPath: string, }, }; class DocumentViewer extends React.PureComponent { constructor(props) { super(props); this.state = { error: null, content: null, loading: true, }; } componentDidMount() { const { source } = this.props; const stream = fs.createReadStream(source.downloadPath, 'utf8'); let data = ''; stream.on('data', chunk => { data += chunk; }); stream.on('end', () => { this.setState({ content: data, loading: false }); }); stream.on('error', error => { this.setState({ error }); }); } renderDocument() { let viewer = null; const { source, theme } = this.props; const { fileType, contentType } = source; const { content, error } = this.state; const isReady = content && !error; const markdownType = ['md', 'markdown']; if (isReady && markdownType.includes(fileType)) { // Render markdown viewer = ; } else if (isReady) { // Render plain text viewer = ; } return viewer; } render() { const { error, loading } = this.state; const loadingMessage = __('Rendering document.'); const errorMessage = __("Sorry looks like we can't load the document."); return (
{loading && !error && } {error && } {this.renderDocument()}
); } } export default DocumentViewer;