import React, { createRef } from "react"; import { Redirect } from "react-router"; import { Share } from "../model/"; import { notify, basename, filetype, findParams, randomString } from "../helpers/"; import { Loader, Input, Button, Container, ErrorPage, Icon } from "../components/"; import { t } from "../locales/"; import "./error.scss"; import "./sharepage.scss"; export class SharePageComponent extends React.Component { constructor(props) { super(props); this.state = { path: null, key: null, error: null, loading: false, isUploader: false, }; this.$input = createRef(); } componentDidMount() { this._proofQuery(this.props.match.params.id).then(() => { if (this.$input.current) { this.$input.current.ref.focus(); } }); } submitProof(e, type, value) { e.preventDefault(); this.setState({ loading: true }); this._proofQuery(this.props.match.params.id, { type: type, value: value }); } _proofQuery(id, data = {}) { this.setState({ loading: true }); return Share.proof(id, data).then((res) => { if (this.$input.current) { this.$input.current.ref.value = ""; } const st = { key: res.key, path: res.path || null, share: res.id, loading: false, isUploader: res.can_read === false && res.can_write === false && res.can_upload === true, }; if (res.message) { notify.send(res.message, "info"); } else if (res.error) { st.error = res.error; window.setTimeout(() => this.setState({ error: null }), 500); } return new Promise((done) => { this.setState(st, () => done()); }); }).catch((err) => this.props.error(err)); } render() { const marginTop = () => { return { marginTop: `${parseInt(window.innerHeight / 3)}px`, }; }; const className = this.state.error ? `error rand-${randomString()}` : ""; if (this.state.path !== null) { if (!!findParams("next")) { const url = findParams("next"); if (url[0] === "/") { requestAnimationFrame(() => { window.location.href = new URL( `${url}?share=${this.props.match.params.id}`, location, ).toString(); }); return (
); } notify.send(t("You can't do that :)"), "error"); } else if (CONFIG["share_redirect"] !== "" && this.state.isUploader === false) { requestAnimationFrame(() => { window.location.href = CONFIG["share_redirect"].replace("{{path}}", this.state.path); }); return (
); } else if (filetype(this.state.path) === "directory") { return ( ); } else { return ( ); } } else if (this.state.key === null) { return (
); } else if (this.state.key === "code") { return (
this.submitProof(e, "code", this.$input.current.ref.value)} style={marginTop()}>
); } else if (this.state.key === "password") { return (
this.submitProof(e, "password", this.$input.current.ref.value)} style={marginTop()}>
); } else if (this.state.key === "email") { return (
this.submitProof(e, "email", this.$input.current.ref.value)} style={marginTop()}>
); } return (

{ t("Oops!") }

{ t("There is nothing in here") }

); } } export const SharePage = ErrorPage(SharePageComponent);