import { h, Component } from 'preact' import { connect } from 'react-redux' import actions from '../actions' const image_types = { 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'png': 'image/png', 'gif': 'image/gif', } const audio_types = { 'wav': 'audio/wav', 'mp3': 'audio/mp3', 'flac': 'audio/flac', 'aiff': 'audio/aiff', } const video_types = { 'mp4': 'video/mp4', } class FileViewer extends Component { state = { loading: false, stale: false, buffer: {} } fetch() { const { file, path } = this.props if (!file) return if (this.state.loading) { this.setState({ stale: true }) return } const fn = [path || file.path, file.name].join('/').replace('//','/') console.log('fetch file', fn) const { tool: module } = this.props.app this.setState({ buffer: null, loading: true }) actions.socket.read_file({ module, fn }).then(buffer => { console.log('fetched buffer') const { stale } = this.state this.setState({ buffer, loading: false, stale: false, }, () => { if (stale) { console.log('stale, fetching...') this.fetch() } }) }) } componentDidMount(){ this.fetch() } componentDidUpdate(nextProps){ if (this.props.file !== nextProps.file) { this.fetch() } } render() { const { file } = this.props if (!file) { return
} const { loading, buffer } = this.state if (loading) { return
Loading...
} const { error, name, path, date, size, buf, } = buffer if (error) { return
{error}
} if (!name) { return
} if (!buf) { return
File empty
} const ext = extension(name) let tag; if (ext in image_types) { tag = } else if (ext in audio_types) { tag =