slate/scenes/SceneSlate.js

481 lines
14 KiB
JavaScript
Raw Normal View History

import * as React from "react";
import * as System from "~/components/system";
import * as Actions from "~/common/actions";
2020-08-07 09:33:44 +03:00
import * as Constants from "~/common/constants";
2020-10-01 03:41:53 +03:00
import * as Validations from "~/common/validations";
import * as SVG from "~/common/svg";
import * as Strings from "~/common/strings";
2020-09-03 10:03:32 +03:00
import * as Window from "~/common/window";
import { css } from "@emotion/core";
2020-09-03 00:08:32 +03:00
import { ProcessedText } from "~/components/system/components/Typography";
2020-10-23 21:38:30 +03:00
import { ButtonPrimary, ButtonSecondary } from "~/components/system/components/Buttons";
import { dispatchCustomEvent } from "~/common/custom-events";
2020-10-05 00:30:28 +03:00
import { SlateLayout } from "~/components/core/SlateLayout";
import { SlateLayoutMobile } from "~/components/core/SlateLayoutMobile";
import ScenePage from "~/components/core/ScenePage";
2020-08-22 07:25:34 +03:00
import ScenePageHeader from "~/components/core/ScenePageHeader";
import CircleButtonGray from "~/components/core/CircleButtonGray";
import EmptyState from "~/components/core/EmptyState";
2020-09-04 01:42:08 +03:00
const STYLES_ICONS = css`
display: flex;
flex-direction: row;
justify-content: center;
`;
const STYLES_USERNAME = css`
cursor: pointer;
:hover {
color: ${Constants.system.brand};
}
`;
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;
}
`;
let isMounted = false;
export default class SceneSlate extends React.Component {
2020-09-03 09:00:02 +03:00
_timeout = null;
_remoteLock = false;
state = {
2020-10-05 00:30:28 +03:00
...(this.props.current, this.props.viewer),
loading: false,
saving: "IDLE",
2020-10-05 00:30:28 +03:00
isOwner: this.props.current.data.ownerId === this.props.viewer.id,
editing: false,
};
2020-09-03 09:00:02 +03:00
// NOTE(jim):
// The purpose of this is to update the Scene appropriately when
// it changes but isn't mounted.
2020-10-05 00:30:28 +03:00
async componentDidUpdate(prevProps) {
2020-09-03 09:00:02 +03:00
if (prevProps.current.id !== this.props.current.id) {
2020-10-05 00:30:28 +03:00
await this.setState({
2020-09-03 09:00:02 +03:00
loading: false,
saving: "IDLE",
2020-10-05 00:30:28 +03:00
isOwner: this.props.current.data.ownerId === this.props.viewer.id,
2020-09-03 09:00:02 +03:00
});
}
}
componentDidMount() {
if (isMounted) {
return false;
}
isMounted = true;
2020-10-23 21:38:30 +03:00
window.addEventListener("remote-delete-object", this._handleRemoteDeleteObject);
window.addEventListener("remote-object-update", this._handleRemoteEditObject);
2020-10-05 00:30:28 +03:00
if (this.state.isOwner) {
let changed = false;
let objects = [...this.props.current.data.objects];
for (let obj of objects) {
if (!obj.size) {
let matches = this.props.viewer.library[0].children.filter((file) => {
return file.id === obj.id;
});
if (matches.length) {
obj.size = matches[0].size;
changed = true;
}
}
}
if (changed) {
this._handleSave(null, objects, null, true);
}
}
}
2020-09-03 09:00:02 +03:00
componentWillUnmount() {
isMounted = false;
2020-10-23 21:38:30 +03:00
window.removeEventListener("remote-delete-object", this._handleRemoteDeleteObject);
window.removeEventListener("remote-object-update", this._handleRemoteEditObject);
2020-09-03 09:00:02 +03:00
}
2020-08-22 08:19:11 +03:00
2020-09-15 00:29:41 +03:00
_handleFollow = () => {
2020-10-31 02:12:20 +03:00
Actions.createSubscription({
slateId: this.props.current.id,
2020-09-03 02:17:31 +03:00
});
};
2020-10-05 00:30:28 +03:00
_handleSaveLayout = async (layouts, autoSave) => {
await this._handleSave(null, null, layouts, autoSave);
2020-08-22 12:32:40 +03:00
};
2020-10-05 00:30:28 +03:00
_handleSave = async (e, objects, layouts, autoSave = false, preview) => {
2020-09-03 09:00:02 +03:00
this.setState({ loading: true, saving: "SAVING" });
2020-10-05 00:30:28 +03:00
let layoutOnly = layouts && !objects;
let data = {};
if (objects) {
data.objects = objects;
}
if (layouts) {
data.layouts = layouts;
}
if (preview) {
data.preview = preview;
}
const response = await Actions.updateSlate({
id: this.props.current.id,
2020-10-05 00:30:28 +03:00
layoutOnly,
autoSave,
data,
});
2020-10-05 00:30:28 +03:00
if (!autoSave) {
if (!response) {
this.setState({ loading: false, saving: "ERROR" });
System.dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
2020-10-23 21:38:30 +03:00
message: "We're having trouble connecting right now. Please try again later",
2020-10-05 00:30:28 +03:00
},
2020-09-12 01:25:33 +03:00
},
2020-10-05 00:30:28 +03:00
});
}
2020-10-05 00:30:28 +03:00
if (response.error) {
this.setState({ loading: false, saving: "ERROR" });
System.dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
}
}
2020-09-03 09:00:02 +03:00
this.setState({
saving: "SAVED",
});
};
2020-10-05 00:30:28 +03:00
_handleRemoteEditObject = async ({ detail }) => {
2020-09-03 10:10:09 +03:00
const { object } = detail;
2020-10-05 00:30:28 +03:00
console.log(object);
2020-09-03 10:10:09 +03:00
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { saving: true },
});
2020-10-05 00:30:28 +03:00
const objects = [...this.props.current.data.objects];
for (let i = 0; i < objects.length; i++) {
if (objects[i].id === object.id) {
objects[i] = object;
break;
}
}
await this._handleSave(null, objects);
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { saving: false },
});
};
2020-10-05 00:30:28 +03:00
_handleDeleteFiles = async (cids) => {
const message = `Are you sure you want to delete these files? They will be deleted from your slates as well`;
if (!window.confirm(message)) {
return;
}
2020-09-03 10:03:32 +03:00
2020-10-05 00:30:28 +03:00
const response = await Actions.deleteBucketItems({ cids });
if (!response) {
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
2020-10-23 21:38:30 +03:00
message: "We're having trouble connecting right now. Please try again later",
2020-10-05 00:30:28 +03:00
},
},
});
return;
}
if (response.error) {
dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
return;
}
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message: "Files successfully deleted!", status: "INFO" },
},
});
};
_handleRemoteDeleteObject = async ({ detail }) => {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: true },
});
2020-08-07 05:17:49 +03:00
2020-10-05 00:30:28 +03:00
let objects = this.props.current.data.objects.filter((obj) => {
return !detail.ids.includes(obj.id);
});
const response = await Actions.updateSlate({
2020-09-03 10:03:32 +03:00
id: this.props.current.id,
data: {
objects,
},
2020-08-07 05:17:49 +03:00
});
if (!response) {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
2020-09-12 01:25:33 +03:00
System.dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
2020-10-23 21:38:30 +03:00
message: "We're having trouble connecting right now. Please try again later",
2020-09-12 01:25:33 +03:00
},
},
});
2020-10-05 00:30:28 +03:00
return;
2020-08-07 05:17:49 +03:00
}
if (response.error) {
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
2020-09-12 01:25:33 +03:00
System.dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
2020-10-05 00:30:28 +03:00
return;
2020-08-07 05:17:49 +03:00
}
System.dispatchCustomEvent({
name: "state-global-carousel-loading",
detail: { loading: false },
});
};
_handleSelect = (index) =>
System.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 () => {
await this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_ADD_FILE_TO_BUCKET",
data: this.props.current,
});
2020-10-05 00:30:28 +03:00
};
_handleSaveCopy = async (items) => {
this.setState({ loading: true });
let response = await Actions.addCIDToData({ items });
if (!response) {
this.setState({ loading: false, saving: "ERROR" });
System.dispatchCustomEvent({
name: "create-alert",
detail: {
alert: {
2020-10-23 21:38:30 +03:00
message: "We're having trouble connecting right now. Please try again later",
2020-10-05 00:30:28 +03:00
},
},
});
return;
}
if (response.error) {
this.setState({ loading: false, saving: "ERROR" });
System.dispatchCustomEvent({
name: "create-alert",
detail: { alert: { decorator: response.decorator } },
});
return;
}
2020-10-31 02:12:20 +03:00
let message = Strings.formatAsUploadMessage(response.data.added, response.data.skipped);
dispatchCustomEvent({
name: "create-alert",
detail: {
alert: { message, status: !response.data.added ? null : "INFO" },
},
});
2020-10-05 00:30:28 +03:00
this.setState({ loading: false, saving: "SAVED" });
};
2020-07-27 11:33:39 +03:00
_handleShowSettings = () => {
return this.props.onAction({
type: "SIDEBAR",
value: "SIDEBAR_SINGLE_SLATE_SETTINGS",
data: this.props.current,
});
};
2020-07-27 11:33:39 +03:00
render() {
2020-10-05 00:30:28 +03:00
const { user, data } = this.props.current;
const { body = "", preview } = data;
let objects = this.props.current.data.objects;
let layouts = this.props.current.data.layouts;
const isPublic = data.public;
2020-09-08 01:47:54 +03:00
2020-09-04 01:42:08 +03:00
let following = !!this.props.viewer.subscriptions.filter((subscription) => {
return subscription.target_slate_id === this.props.current.id;
}).length;
2020-10-05 00:30:28 +03:00
let actions = this.state.isOwner ? (
<span>
2020-10-01 03:41:53 +03:00
<CircleButtonGray onClick={this._handleAdd} style={{ marginRight: 16 }}>
<SVG.Plus height="16px" />
</CircleButtonGray>
{isPublic ? (
2020-10-05 00:30:28 +03:00
<a
href={
user
? `/${user.username}/${this.props.current.slatename}`
: this.state.isOwner
? `/${this.props.viewer.username}/${this.props.current.slatename}`
: ""
}
target="_blank"
2020-10-01 03:41:53 +03:00
>
2020-10-05 00:30:28 +03:00
<CircleButtonGray style={{ marginRight: 16 }}>
2020-10-30 23:09:11 +03:00
<SVG.ExternalLink height="16px" />
2020-10-05 00:30:28 +03:00
</CircleButtonGray>
</a>
2020-10-01 03:41:53 +03:00
) : null}
<CircleButtonGray onClick={this._handleShowSettings}>
<SVG.Settings height="16px" />
</CircleButtonGray>
</span>
) : (
<div onClick={this._handleFollow}>
{following ? (
2020-10-31 02:12:20 +03:00
<ButtonSecondary transparent style={{ minWidth: 120, paddingLeft: 0 }}>
2020-10-01 03:41:53 +03:00
Unfollow
</ButtonSecondary>
) : (
2020-10-31 02:12:20 +03:00
<ButtonPrimary transparent style={{ minWidth: 120, paddingLeft: 0 }}>
2020-10-01 03:41:53 +03:00
Follow
</ButtonPrimary>
)}
</div>
);
2020-07-27 11:33:39 +03:00
return (
2020-10-05 00:30:28 +03:00
<ScenePage contentstyle={{ maxWidth: "1660px" }}>
2020-08-22 07:25:34 +03:00
<ScenePageHeader
2020-10-05 00:30:28 +03:00
wide
title={
2020-09-04 01:42:08 +03:00
user ? (
<span>
<span
onClick={() =>
this.props.onAction({
type: "NAVIGATE",
value: this.props.sceneId,
2020-09-04 01:42:08 +03:00
scene: "PUBLIC_PROFILE",
data: user,
})
}
css={STYLES_USERNAME}
>
2020-09-04 01:42:08 +03:00
{user.username}
</span>{" "}
2020-09-05 07:45:50 +03:00
/ {data.name}
</span>
) : (
2020-09-05 07:45:50 +03:00
data.name
)
}
2020-10-01 03:51:25 +03:00
actions={<span css={STYLES_MOBILE_HIDDEN}>{actions}</span>}
>
2020-10-05 00:30:28 +03:00
<span
style={{
color: Constants.system.darkGray,
fontFamily: Constants.font.medium,
}}
>
<ProcessedText text={body} />
</span>
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 ? (
2020-10-05 00:30:28 +03:00
this.props.mobile ? (
<SlateLayoutMobile
isOwner={this.state.isOwner}
items={objects}
2020-10-23 21:38:30 +03:00
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
onSelect={this._handleSelect}
/>
2020-10-05 00:30:28 +03:00
) : (
<div style={{ marginTop: this.state.isOwner ? 24 : 48 }}>
<SlateLayout
link={
user
? `${window.location.hostname}${
window.location.port ? ":" + window.location.port : ""
}/${user.username}/${this.props.current.slatename}`
: this.state.isOwner
? `${window.location.hostname}${
window.location.port ? ":" + window.location.port : ""
2020-10-23 21:38:30 +03:00
}/${this.props.viewer.username}/${this.props.current.slatename}`
2020-10-05 00:30:28 +03:00
: ""
}
viewer={this.props.viewer}
slateId={this.props.current.id}
2020-10-23 21:38:30 +03:00
layout={layouts && layouts.ver === "2.0" ? layouts.layout || [] : null}
2020-10-05 00:30:28 +03:00
onSaveLayout={this._handleSaveLayout}
isOwner={this.state.isOwner}
2020-10-23 21:38:30 +03:00
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
2020-10-05 00:30:28 +03:00
preview={preview}
2020-10-23 21:38:30 +03:00
onSavePreview={(preview) => this._handleSave(null, null, null, false, preview)}
2020-10-05 00:30:28 +03:00
items={objects}
onSelect={this._handleSelect}
2020-10-23 21:38:30 +03:00
defaultLayout={layouts && layouts.ver === "2.0" ? layouts.defaultLayout : true}
2020-10-05 00:30:28 +03:00
onAction={this.props.onAction}
onRemoveFromSlate={this._handleRemoteDeleteObject}
onDeleteFiles={this._handleDeleteFiles}
onSaveCopy={this._handleSaveCopy}
/>
</div>
)
) : this.state.isOwner ? (
<div style={{ padding: "24px" }}>
<EmptyState>
<div css={STYLES_ICONS}>
<SVG.Sound height="24px" style={{ margin: "0 16px" }} />
<SVG.Document height="24px" style={{ margin: "0 16px" }} />
<SVG.Image height="24px" style={{ margin: "0 16px" }} />
<SVG.Book height="24px" style={{ margin: "0 16px" }} />
<SVG.Video height="24px" style={{ margin: "0 16px" }} />
</div>
2020-10-23 21:38:30 +03:00
<div style={{ marginTop: 24 }}>Drag and drop files to add them to this slate</div>
</EmptyState>
</div>
2020-10-05 00:30:28 +03:00
) : (
<div style={{ padding: "24px" }}>
<EmptyState>There's nothing here :)</EmptyState>
</div>
)}
2020-07-27 11:33:39 +03:00
</ScenePage>
);
}
}