import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
import * as Window from "~/common/window";
import * as Constants from "~/common/constants";
import * as SVG from "~/common/svg";
import * as Strings from "~/common/strings";
import * as Events from "~/common/custom-events";
import { LoaderSpinner } from "~/components/system/components/Loaders";
import { css } from "@emotion/react";
import { SlateLayout } from "~/components/core/SlateLayout";
import { SlateLayoutMobile } from "~/components/core/SlateLayoutMobile";
import { FileTypeGroup } from "~/components/core/FileTypeIcon";
import { ButtonPrimary, ButtonSecondary } from "~/components/system/components/Buttons";
import ProcessedText from "~/components/core/ProcessedText";
import ScenePage from "~/components/core/ScenePage";
import ScenePageHeader from "~/components/core/ScenePageHeader";
import CircleButtonGray from "~/components/core/CircleButtonGray";
import EmptyState from "~/components/core/EmptyState";
const STYLES_LOADER = css`
display: flex;
align-items: center;
justify-content: center;
height: 90vh;
width: 100%;
`;
const STYLES_COPY_INPUT = css`
pointer-events: none;
position: absolute;
opacity: 0;
`;
const STYLES_USERNAME = css`
cursor: pointer;
:hover {
color: ${Constants.system.brand};
}
`;
const STYLES_MOBILE_HIDDEN = css`
@media (max-width: ${Constants.sizes.mobile}px) {
display: none;
}
`;
const STYLES_MOBILE_ONLY = css`
@media (min-width: ${Constants.sizes.mobile}px) {
display: none;
}
`;
export default class SceneSlate extends React.Component {
state = {
notFound: false,
accessDenied: false,
};
componentDidMount = async () => {
await this.fetchSlate();
};
componentDidUpdate = async (prevProps) => {
if (this.props.data?.id && prevProps.data?.id && this.props.data.id !== prevProps.data.id) {
await this.fetchSlate();
}
};
fetchSlate = async () => {
const { user: username, slate: slatename, cid } = this.props.page;
if (!this.props.data && (!username || !slatename)) {
this.setState({ notFound: true });
return;
}
//NOTE(martina): look for the slate in the user's slates
let slate;
if (this.props.data?.id) {
for (let s of this.props.viewer.slates) {
if (this.props.data.id && this.props.data.id === s.id) {
slate = s;
break;
}
}
} else if (slatename && username === this.props.viewer.username) {
for (let s of this.props.viewer.slates) {
if (username && slatename === s.slatename) {
slate = s;
break;
}
}
if (!slate) {
Events.dispatchMessage({ message: "We're having trouble fetching that slate right now." });
this.setState({ notFound: true });
return;
}
}
if (slate) {
window.history.replaceState(
{ ...window.history.state, data: slate },
"Slate",
`/${this.props.viewer.username}/${slate.slatename}`
);
}
if (!slate) {
let query;
if (username && slatename) {
query = { username, slatename };
} else if (this.props.data && this.props.data.id) {
query = { id: this.props.data.id };
}
let response;
if (query) {
response = await Actions.getSerializedSlate(query);
}
if (response?.decorator == "SLATE_PRIVATE_ACCESS_DENIED") {
this.setState({ accessDenied: true });
return;
}
if (Events.hasError(response)) {
this.setState({ notFound: true });
return;
}
slate = response.data;
window.history.replaceState(
{ ...window.history.state, data: slate },
"Slate",
`/${response.data.user.username}/${response.data.slatename}`
);
}
this.props.onUpdateData({ data: slate });
let index = -1;
let page = this.props.page;
if (page?.fileId || page?.cid || page?.index || !Strings.isEmpty(cid)) {
if (page?.index) {
index = page.index;
} else {
for (let i = 0; i < slate.data.objects.length; i++) {
let obj = slate.data.objects[i];
if (
(obj.cid && (obj.cid === cid || obj.cid === page?.cid)) ||
(obj.id && obj.id === page?.fileId)
) {
index = i;
break;
}
}
}
}
if (index !== -1) {
Events.dispatchCustomEvent({
name: "slate-global-open-carousel",
detail: { index },
});
}
};
render() {
if (this.state.notFound || this.state.accessDenied) {
return (