import React, { useReducer, useEffect } from "react"; import { useHistory } from "react-router-dom"; import Path from "path"; import "./viewerpage.scss"; import "./error.scss"; import { Files, Chromecast } from "../model/"; import { BreadCrumb, Bundle, NgIf, Loader, EventReceiver, LoggedInOnly, ErrorPage, } from "../components/"; import { opener, notify, objectGet } from "../helpers/"; import { FileDownloader, ImageViewer, PDFViewer, FormViewer } from "./viewerpage/"; const VideoPlayer = (props) => ( {(Comp) => } ); const IDE = (props) => ( {(Comp) => } ); const AudioPlayer = (props) => ( {(Comp) => } ); const Appframe = (props) => ( {(Comp) => } ); const EbookViewer = (props) => ( {(Comp) => } ) export function ViewerPageComponent({ error, subscribe, unsubscribe, match, location }) { const history = useHistory(); const currentUrl = history.location.pathname; const [state, setState] = useReducer((s, a) => { return { ...s, ...a }; }, { url: null, opener: null, content: null, needSaving: false, isSaving: false, loading: false, application_arguments: null, }); const path = currentUrl.replace("/view", "").replace(/%23/g, "#") + (location.hash || ""); const filename = Path.basename(currentUrl.replace("/view", "")) || "untitled.dat"; const save = (file) => { setState({ isSaving: true, needSaving: false }); return (new Promise((done, err) => { const reader = new FileReader(); reader.onload = () => done(reader.result); reader.readAsText(file); })).then((content) => { let oldContent = state.content; setState({ content: content }); return Files.save(path, file) .then(() => setState({ isSaving: false })) .catch((err) => { if (err && err.code === "CANCELLED") return; setState({ isSaving: false, needSaving: true, content: oldContent }); notify.send(err, "error"); }); }); } const needSaving = (bool) => { setState({ needSaving: bool }); return Promise.resolve(); }; useEffect(() => { const metadata = () => { const [app_opener, app_args] = opener(path); setState({ loading: true, needSaving: false, url: null, opener: null, application_arguments: null }); return Files.url(path).then((url) => { setState({ url: url, opener: app_opener, application_arguments: app_args, }); return app_opener; }).catch((_err) => error(_err)); }; const data_fetch = (app) => { if (app !== "editor" && app !== "form") { setState({ loading: false }); return null; } return Promise.all([ Files.cat(path), Files.options(path), ]).then((d) => { const [content, options] = d; setState({ content: content, loading: false, acl: options["allow"], }); }).catch((err) => { if (err.code !== "BINARY_FILE") { error(err); return; } setState({ loading: false, opener: "download", }); }); }; metadata().then(data_fetch); return history.listen(() => {}) }, [path]); return (
); } export const ViewerPage = ErrorPage(LoggedInOnly(EventReceiver(ViewerPageComponent)));