2018-07-26 07:18:35 +02:00
|
|
|
// @flow
|
|
|
|
|
2019-11-26 20:08:34 +01:00
|
|
|
import React from 'react';
|
2018-07-26 07:18:35 +02:00
|
|
|
import LoadingScreen from 'component/common/loading-screen';
|
|
|
|
import MarkdownPreview from 'component/common/markdown-preview';
|
2019-11-07 20:39:22 +01:00
|
|
|
import CodeViewer from 'component/viewers/codeViewer';
|
2020-04-01 20:43:50 +02:00
|
|
|
import * as RENDER_MODES from 'constants/file_render_modes';
|
2019-11-26 20:08:34 +01:00
|
|
|
import * as https from 'https';
|
2019-03-27 05:40:02 +01:00
|
|
|
|
2018-07-26 07:18:35 +02:00
|
|
|
type Props = {
|
2018-07-28 03:42:35 +02:00
|
|
|
theme: string,
|
2020-04-01 20:43:50 +02:00
|
|
|
renderMode: string,
|
2018-07-26 07:18:35 +02:00
|
|
|
source: {
|
2019-11-26 20:08:34 +01:00
|
|
|
file: (?string) => any,
|
|
|
|
stream: string,
|
2018-08-02 02:53:38 +02:00
|
|
|
contentType: string,
|
2018-07-26 07:18:35 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-15 02:27:09 +02:00
|
|
|
type State = {
|
|
|
|
error: boolean,
|
|
|
|
loading: boolean,
|
|
|
|
content: ?string,
|
|
|
|
};
|
|
|
|
|
|
|
|
class DocumentViewer extends React.PureComponent<Props, State> {
|
|
|
|
constructor(props: Props) {
|
2018-07-26 07:18:35 +02:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-10-15 02:27:09 +02:00
|
|
|
error: false,
|
2018-07-26 07:18:35 +02:00
|
|
|
loading: true,
|
2018-10-15 02:27:09 +02:00
|
|
|
content: null,
|
2018-07-26 07:18:35 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { source } = this.props;
|
2019-11-26 20:08:34 +01:00
|
|
|
// @if TARGET='app'
|
|
|
|
if (source && source.file) {
|
|
|
|
const stream = source.file('utf8');
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
let data = '';
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2021-03-11 18:08:11 +01:00
|
|
|
stream.on('data', (chunk) => {
|
2019-03-21 14:59:31 +01:00
|
|
|
data += chunk;
|
|
|
|
});
|
2018-07-26 07:18:35 +02:00
|
|
|
|
2019-03-21 14:59:31 +01:00
|
|
|
stream.on('end', () => {
|
|
|
|
this.setState({ content: data, loading: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('error', () => {
|
|
|
|
this.setState({ error: true, loading: false });
|
|
|
|
});
|
|
|
|
}
|
2019-11-26 20:08:34 +01:00
|
|
|
// @endif
|
|
|
|
// @if TARGET='web'
|
|
|
|
if (source && source.stream) {
|
2021-03-11 18:08:11 +01:00
|
|
|
https.get(source.stream, (response) => {
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
let data = '';
|
|
|
|
response.on('data', (chunk) => {
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
response.on('end', () => {
|
|
|
|
this.setState({ content: data, loading: false });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({ error: true, loading: false });
|
|
|
|
}
|
|
|
|
});
|
2019-11-26 20:08:34 +01:00
|
|
|
}
|
|
|
|
// @endif
|
2018-07-26 07:18:35 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 02:27:09 +02:00
|
|
|
renderDocument() {
|
|
|
|
const { content } = this.state;
|
2021-04-08 19:59:22 +02:00
|
|
|
const { source, theme, renderMode } = this.props;
|
2020-04-01 20:43:50 +02:00
|
|
|
const { contentType } = source;
|
2018-07-27 02:24:00 +02:00
|
|
|
|
2020-04-01 20:43:50 +02:00
|
|
|
return renderMode === RENDER_MODES.MARKDOWN ? (
|
2021-04-08 19:59:22 +02:00
|
|
|
<MarkdownPreview content={content} isMarkdownPost promptLinks />
|
2020-04-01 20:43:50 +02:00
|
|
|
) : (
|
|
|
|
<CodeViewer value={content} contentType={contentType} theme={theme} />
|
|
|
|
);
|
2018-07-26 07:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-08-02 02:53:38 +02:00
|
|
|
const { error, loading, content } = this.state;
|
|
|
|
const isReady = content && !error;
|
2018-07-28 03:54:06 +02:00
|
|
|
const errorMessage = __("Sorry, looks like we can't load the document.");
|
2018-07-26 07:18:35 +02:00
|
|
|
|
|
|
|
return (
|
2020-04-14 01:48:11 +02:00
|
|
|
<div className="file-viewer file-viewer--document">
|
2020-01-06 19:32:35 +01:00
|
|
|
{loading && !error && <div className="placeholder--text-document" />}
|
2018-08-02 02:53:38 +02:00
|
|
|
{error && <LoadingScreen status={errorMessage} spinner={!error} />}
|
2019-11-07 20:39:22 +01:00
|
|
|
{isReady && this.renderDocument()}
|
2018-07-26 07:18:35 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DocumentViewer;
|