update comicbook viewer

- convert to functional component
- use hooks to load stream
This commit is contained in:
Baltazar Gomez 2020-05-08 13:50:18 -05:00 committed by Sean Yesmunt
parent 872fa9363d
commit 1992e78c2e

View file

@ -1,13 +1,19 @@
// @flow // @flow
import * as React from 'react'; import * as React from 'react';
import Villain from 'villain-react'; import Villain from 'villain-react';
import useFileStream from 'effects/use-stream-file'
import LoadingScreen from 'component/common/loading-screen';
import 'villain-react/dist/style.css'; import 'villain-react/dist/style.css';
type Props = { type Props = {
source: {
file: (?string) => any,
stream: string,
},
theme: string, theme: string,
source: string,
}; };
let workerUrl = 'webworkers/worker-bundle.js'; let workerUrl = 'webworkers/worker-bundle.js';
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
@ -15,25 +21,31 @@ if (process.env.NODE_ENV !== 'production') {
workerUrl = `/${workerUrl}`; workerUrl = `/${workerUrl}`;
} }
class ComicBookViewer extends React.PureComponent<Props> { const ComicBookViewer = ( props: Props) => {
render() { const { source, theme } = props
const { source } = this.props || {}; const { stream, file } = source
// Archive source
const file = `file://${source}`; // @if TARGET='app'
const finalSource = useFileStream(file)
// @endif
// Villain options // Villain options
const opts = { const opts = {
theme: this.props.theme === 'dark' ? 'Dark' : 'Light', theme: theme === 'dark' ? 'Dark' : 'Light',
allowFullScreen: true, allowFullScreen: true,
autoHideControls: false, autoHideControls: false,
allowGlobalShortcuts: true, allowGlobalShortcuts: true,
}; };
const errorMessage = __("Sorry, looks like we can't load the archive.");
return ( return (
<div className="file-render__viewer file-render__viewer--comic"> <div className="file-render__viewer file-render__viewer--comic">
<Villain source={file} className={'comic-viewer'} options={opts} workerUrl={workerUrl} /> { loading && <LoadingScreen status={__('Loading')} />}
{ ready && <Villain source={finalSource.content} className={'comic-viewer'} options={opts} workerUrl={workerUrl} /> }
{ error && <LoadingScreen status={errorMessage} spinner={false} /> }
</div> </div>
); );
} }
}
export default ComicBookViewer; export default ComicBookViewer;