slate/scenes/SceneSlate.js

562 lines
17 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
import * as Validations from "~/common/validations";
2020-08-07 09:33:44 +03:00
import * as Constants from "~/common/constants";
import * as SVG from "~/common/svg";
import * as Strings from "~/common/strings";
import * as Utilities from "~/common/utilities";
2021-03-03 11:48:59 +03:00
import * as UserBehaviors from "~/common/user-behaviors";
import * as Events from "~/common/custom-events";
2021-05-06 03:08:14 +03:00
import { Link } from "~/components/core/Link";
import { LoaderSpinner } from "~/components/system/components/Loaders";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
2020-10-05 00:30:28 +03:00
import { SlateLayout } from "~/components/core/SlateLayout";
import { SlateLayoutMobile } from "~/components/core/SlateLayoutMobile";
2020-11-11 04:44:21 +03:00
import { FileTypeGroup } from "~/components/core/FileTypeIcon";
2020-12-11 05:59:17 +03:00
import { ButtonPrimary, ButtonSecondary } from "~/components/system/components/Buttons";
import { GlobalCarousel } from "~/components/system/components/GlobalCarousel";
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
import ProcessedText from "~/components/core/ProcessedText";
import ScenePage from "~/components/core/ScenePage";
2020-08-22 07:25:34 +03:00
import ScenePageHeader from "~/components/core/ScenePageHeader";
import SquareButtonGray from "~/components/core/SquareButtonGray";
import EmptyState from "~/components/core/EmptyState";
2020-09-04 01:42:08 +03:00
const STYLES_LOADER = css`
display: flex;
align-items: center;
justify-content: center;
height: 90vh;
width: 100%;
`;
2020-11-17 06:17:56 +03:00
const STYLES_COPY_INPUT = css`
pointer-events: none;
position: absolute;
opacity: 0;
`;
const STYLES_USERNAME = css`
cursor: pointer;
:hover {
2021-07-07 22:14:51 +03:00
color: ${Constants.system.blue};
}
`;
2020-10-01 03:41:53 +03:00
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 = {
loading: true,
notFound: false,
accessDenied: false,
};
2021-05-06 03:08:14 +03:00
// componentDidMount = async () => {
// if (this.props.data) {
// this.openCarouselToItem();
// }
// };
// componentDidUpdate = async (prevProps) => {
// // if (!this.props.data?.objects && !this.state.notFound) {
// // await this.fetchSlate();
// // } else
// if (this.props.data !== prevProps.data || this.props.page.params !== prevProps.page.params) {
// this.openCarouselToItem();
// }
// };
// fetchSlate = async () => {
// const { username, slatename, slateId } = this.props.page;
// if (!this.props.data && (!username || !slatename)) {
// this.setState({ notFound: true });
// return;
// }
// let id = slateId || this.props.data?.id;
// //NOTE(martina): look for the slate in the user's slates
// let slate;
// if (this.props.viewer) {
// if (id) {
// for (let s of this.props.viewer.slates) {
// if (id && 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 (id) {
// query = { id };
// }
// let response;
// if (query) {
// response = await Actions.getSerializedSlate(query);
// }
// if (response?.decorator == "SLATE_PRIVATE_ACCESS_DENIED") {
// this.setState({ accessDenied: true, loading: false });
// return;
// }
// if (Events.hasError(response)) {
// this.setState({ notFound: true, loading: false });
// return;
// }
// slate = response.slate;
// window.history.replaceState(
// { ...window.history.state, data: slate },
// "Slate",
// `/${slate.user.username}/${slate.slatename}`
// );
// }
// this.props.onUpdateData(slate, () => {
// this.setState({ loading: false });
// this.openCarouselToItem();
// });
// };
// openCarouselToItem = () => {
// if (!this.props.data?.objects?.length || !this.props.page?.params) {
// return;
// }
// let objects = this.props.data.objects;
// const { cid, fileId, index } = this.props.page.params;
// if (Strings.isEmpty(cid) && Strings.isEmpty(fileId) && typeof index === "undefined") {
// return;
// }
// let foundIndex = -1;
// if (index) {
// foundIndex = index;
// } else if (cid) {
// foundIndex = objects.findIndex((object) => object.cid === cid);
// } else if (fileId) {
// foundIndex = objects.findIndex((object) => object.id === fileId);
// }
// if (typeof foundIndex !== "undefined" && foundIndex !== -1) {
// Events.dispatchCustomEvent({
// name: "slate-global-open-carousel",
// detail: { index: foundIndex },
// });
// }
// };
render() {
const slate = this.props.data;
if (!slate) {
return (
<WebsitePrototypeWrapper
title={`${this.props.page.pageTitle} • Slate`}
url={`${Constants.hostname}${this.props.page.pathname}`}
>
<ScenePage>
<EmptyState>
<SVG.Layers height="24px" style={{ marginBottom: 24 }} />
<div>We were unable to locate that collection</div>
</EmptyState>
</ScenePage>
</WebsitePrototypeWrapper>
);
2021-05-06 03:08:14 +03:00
} else {
let title, description, file, image;
let name = slate.data.name;
if (this.props.page.params?.cid) {
file = slate.objects.find((file) => file.cid === this.props.page.params.cid);
}
if (file) {
title = `${file.data.name || file.filename}`;
description = file.data.body
? file.data.body
: `View ${title}, a file in the collection ${name} on Slate`;
image = Utilities.getImageUrlIfExists(file, Constants.linkPreviewSizeLimit);
} else {
if (slate.data.body) {
description = `${name}. ${slate.data.body}`;
} else {
description = `View the collection ${name} on Slate`;
}
title = `${name} • Slate`;
image = slate.data.preview;
const objects = slate.objects;
if (!image && objects) {
for (let i = 0; i < objects.length; i++) {
if (
objects[i].data.type &&
Validations.isPreviewableImage(objects[i].data.type) &&
objects[i].data.size &&
objects[i].data.size < Constants.linkPreviewSizeLimit
) {
image = Strings.getURLfromCID(objects[i].cid);
break;
}
}
}
}
return (
<WebsitePrototypeWrapper
description={description}
title={title}
url={`${Constants.hostname}${this.props.page.pathname}`}
image={image}
>
<SlatePage {...this.props} key={slate.id} data={slate} />
</WebsitePrototypeWrapper>
);
}
}
}
class SlatePage extends React.Component {
2020-11-17 06:17:56 +03:00
_copy = null;
2020-09-03 09:00:02 +03:00
_timeout = null;
_remoteLock = false;
state = {
2021-05-06 03:08:14 +03:00
...(this.props.data, this.props.viewer),
2020-10-05 00:30:28 +03:00
editing: false,
2021-05-06 03:08:14 +03:00
isSubscribed: this.props.viewer
? this.props.viewer.subscriptions.some((subscription) => {
return subscription.id === this.props.data.id;
})
: false,
2021-07-17 01:51:01 +03:00
index: -1,
};
2021-02-19 16:22:04 +03:00
componentDidMount() {
const {
page: { cid },
} = this.props;
/* NOTE(daniel): If user was redirected to this page, the cid of the slate object will exist in the page props.
We'll use the cid to open the global carousel */
if (Strings.isEmpty(cid)) {
return;
}
2021-05-06 03:08:14 +03:00
const index = this.props.data.objects.findIndex((object) => object.cid === cid);
// if (index !== -1) {
// Events.dispatchCustomEvent({
// name: "slate-global-open-carousel",
// detail: { index },
// });
// }
if (index === -1) {
Events.dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
message:
"The requested file could not be found. It may have been removed from the slate or deleted",
},
},
});
}
2021-02-19 16:22:04 +03:00
}
componentDidUpdate(prevProps) {
2021-05-06 03:08:14 +03:00
if (this.props.viewer && this.props.viewer.subscriptions !== prevProps.viewer.subscriptions) {
this.setState({
isSubscribed: this.props.viewer.subscriptions.some((subscription) => {
2021-05-06 03:08:14 +03:00
return subscription.id === this.props.data.id;
}),
2020-09-03 09:00:02 +03:00
});
}
}
_handleSubscribe = () => {
2021-05-06 03:08:14 +03:00
if (!this.props.viewer) {
Events.dispatchCustomEvent({ name: "slate-global-open-cta", detail: {} });
return;
}
this.setState({ isSubscribed: !this.state.isSubscribed }, () => {
Actions.createSubscription({
2021-05-06 03:08:14 +03:00
slateId: this.props.data.id,
});
2020-09-03 02:17:31 +03:00
});
};
2020-10-05 00:30:28 +03:00
_handleSaveLayout = async (layouts, autoSave) => {
const response = await Actions.updateSlateLayout({
2021-05-06 03:08:14 +03:00
id: this.props.data.id,
layouts,
});
2020-10-05 00:30:28 +03:00
if (!autoSave) {
Events.hasError(response);
}
};
_handleSavePreview = async (preview) => {
2021-05-06 03:08:14 +03:00
if (!this.props.viewer) {
return;
}
let updateObject = { id: this.props.data.id, data: { preview } };
let slates = this.props.viewer.slates;
2021-05-06 03:08:14 +03:00
let slateId = this.props.data.id;
for (let slate of slates) {
if (slate.id === slateId) {
slate.data.preview = preview;
break;
}
}
2021-05-06 03:08:14 +03:00
this.props.onAction({ type: "UPDATE_VIEWER", viewer: { slates } });
const response = await Actions.updateSlate(updateObject);
Events.hasError(response);
};
2021-05-06 03:08:14 +03:00
// _handleSelect = (index) =>
// Events.dispatchCustomEvent({
// name: "slate-global-open-carousel",
// detail: { index },
// });
2020-07-27 11:33:39 +03:00
2020-09-08 07:04:24 +03:00
_handleAdd = async () => {
2021-05-06 03:08:14 +03:00
if (!this.props.viewer) {
Events.dispatchCustomEvent({ name: "slate-global-open-cta", detail: {} });
return;
}
2020-09-08 07:04:24 +03:00
await this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_ADD_FILE_TO_BUCKET",
2021-05-06 03:08:14 +03:00
data: this.props.data,
});
2020-10-05 00:30:28 +03:00
};
_handleShowSettings = () => {
return this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_SINGLE_SLATE_SETTINGS",
2021-05-06 03:08:14 +03:00
data: this.props.data,
});
};
2020-07-27 11:33:39 +03:00
2020-11-17 06:17:56 +03:00
_handleCopy = (e, value) => {
e.stopPropagation();
e.preventDefault();
this.setState({ copyValue: value, copying: true }, () => {
this._copy.select();
document.execCommand("copy");
});
setTimeout(() => {
this.setState({ copying: false });
}, 1000);
};
_handleDownload = () => {
2021-05-06 03:08:14 +03:00
if (!this.props.viewer) {
Events.dispatchCustomEvent({ name: "slate-global-open-cta", detail: {} });
return;
}
const slateName = this.props.data.data.name;
const slateFiles = this.props.data.objects;
2021-03-05 10:32:08 +03:00
UserBehaviors.compressAndDownloadFiles({
files: slateFiles,
name: `${slateName}.zip`,
});
2021-03-03 11:48:59 +03:00
};
render() {
2021-05-06 03:08:14 +03:00
const { user, data } = this.props.data;
2020-10-05 00:30:28 +03:00
const { body = "", preview } = data;
2021-05-06 03:08:14 +03:00
let objects = this.props.data.objects;
let layouts = this.props.data.data.layouts;
const isPublic = this.props.data.isPublic;
const isOwner = this.props.viewer ? this.props.data.ownerId === this.props.viewer.id : false;
2021-03-08 12:32:29 +03:00
const tags = data.tags;
2020-09-08 01:47:54 +03:00
let actions = isOwner ? (
2020-10-05 00:30:28 +03:00
<span>
<SquareButtonGray onClick={this._handleDownload} style={{ marginRight: 16 }}>
<SVG.Download height="16px" />
</SquareButtonGray>
<SquareButtonGray onClick={this._handleAdd} style={{ marginRight: 16 }}>
2020-10-01 03:41:53 +03:00
<SVG.Plus height="16px" />
</SquareButtonGray>
<SquareButtonGray onClick={this._handleShowSettings}>
2020-10-01 03:41:53 +03:00
<SVG.Settings height="16px" />
</SquareButtonGray>
2020-10-01 03:41:53 +03:00
</span>
) : (
<div style={{ display: `flex` }}>
<SquareButtonGray onClick={this._handleDownload} style={{ marginRight: 16 }}>
<SVG.Download height="16px" />
</SquareButtonGray>
<div onClick={this._handleSubscribe}>
{this.state.isSubscribed ? (
<ButtonSecondary>Unsubscribe</ButtonSecondary>
) : (
<ButtonPrimary>Subscribe</ButtonPrimary>
)}
</div>
2020-10-01 03:41:53 +03:00
</div>
);
2020-07-27 11:33:39 +03:00
return (
2020-11-20 00:20:50 +03:00
<ScenePage>
2020-08-22 07:25:34 +03:00
<ScenePageHeader
2020-10-05 00:30:28 +03:00
wide
title={
user && !isOwner ? (
<span>
2021-05-06 03:08:14 +03:00
<Link href={`/$/user/${user.id}`} onAction={this.props.onAction}>
<span
// onClick={() =>
// this.props.onAction({
// type: "NAVIGATE",
// value: "NAV_PROFILE",
// shallow: true,
// data: user,
// })
// }
css={STYLES_USERNAME}
>
{user.username}
</span>{" "}
</Link>
2020-09-05 07:45:50 +03:00
/ {data.name}
{isOwner && !isPublic && (
2021-03-12 02:33:44 +03:00
<SVG.SecurityLock
height="24px"
2021-07-07 22:58:14 +03:00
style={{ marginLeft: 16, color: Constants.system.grayLight2 }}
2021-03-12 02:33:44 +03:00
/>
)}
</span>
) : (
<span>
{data.name}
{isOwner && !isPublic && (
2021-03-12 02:33:44 +03:00
<SVG.SecurityLock
height="24px"
2021-07-07 22:58:14 +03:00
style={{ marginLeft: 16, color: Constants.system.grayLight2 }}
2021-03-12 02:33:44 +03:00
/>
)}
</span>
)
}
2020-10-01 03:51:25 +03:00
actions={<span css={STYLES_MOBILE_HIDDEN}>{actions}</span>}
2021-03-08 12:32:29 +03:00
tags={tags}
>
2020-12-11 05:59:17 +03:00
{body}
2020-08-22 07:25:34 +03:00
</ScenePageHeader>
2020-10-01 03:41:53 +03:00
<span css={STYLES_MOBILE_ONLY}>{actions}</span>
2020-09-14 11:21:06 +03:00
{objects && objects.length ? (
<>
<GlobalCarousel
carouselType="SLATE"
viewer={this.props.viewer}
objects={objects}
2021-05-06 03:08:14 +03:00
data={this.props.data}
onAction={this.props.onAction}
isMobile={this.props.isMobile}
2021-05-06 03:08:14 +03:00
params={this.props.page.params}
isOwner={isOwner}
external={this.props.external}
2021-07-17 01:51:01 +03:00
index={this.state.index}
onChange={(index) => this.setState({ index })}
/>
{this.props.isMobile ? (
<SlateLayoutMobile
isOwner={isOwner}
2020-10-05 00:30:28 +03:00
items={objects}
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
2020-10-05 00:30:28 +03:00
onSelect={this._handleSelect}
/>
) : (
<div style={{ marginTop: isOwner ? 24 : 48 }}>
<SlateLayout
2021-05-06 03:08:14 +03:00
page={this.props.page}
external={this.props.external}
key={this.props.data.id}
data={this.props.data}
viewer={this.props.viewer}
2021-05-06 03:08:14 +03:00
slateId={this.props.data.id}
layout={layouts && layouts.ver === "2.0" ? layouts.layout || [] : null}
onSaveLayout={this._handleSaveLayout}
isOwner={isOwner}
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
preview={preview}
onSavePreview={this._handleSavePreview}
items={objects}
onSelect={this._handleSelect}
defaultLayout={layouts && layouts.ver === "2.0" ? layouts.defaultLayout : true}
onAction={this.props.onAction}
/>
</div>
)}
</>
) : isOwner ? (
2020-11-20 05:19:04 +03:00
<div>
<EmptyState>
2020-11-11 04:44:21 +03:00
<FileTypeGroup />
<div style={{ marginTop: 24 }}>
Drag and drop files to add them to this collection
</div>
</EmptyState>
</div>
2020-10-05 00:30:28 +03:00
) : (
2020-11-20 05:19:04 +03:00
<div>
2020-10-05 00:30:28 +03:00
<EmptyState>There's nothing here :)</EmptyState>
</div>
)}
2020-11-17 06:17:56 +03:00
<input
ref={(c) => {
this._copy = c;
}}
readOnly
value={this.state.copyValue}
css={STYLES_COPY_INPUT}
/>
2020-07-27 11:33:39 +03:00
</ScenePage>
);
}
}