slate/pages/_/slate.js

192 lines
6.0 KiB
JavaScript
Raw Normal View History

2020-07-27 12:50:25 +03:00
import * as React from "react";
import * as Constants from "~/common/constants";
2020-07-30 04:00:45 +03:00
import * as System from "~/components/system";
import * as Strings from "~/common/strings";
2020-10-05 00:30:28 +03:00
import * as Actions from "~/common/actions";
2020-07-27 12:50:25 +03:00
import { css } from "@emotion/react";
import { ProcessedText } from "~/components/system/components/Typography";
import { Alert } from "~/components/core/Alert";
2020-10-23 09:25:19 +03:00
import { ViewAllButton } from "~/components/core/ViewAll";
2020-07-27 12:50:25 +03:00
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
2020-10-25 22:09:21 +03:00
import WebsitePrototypeHeader from "~/components/core/WebsitePrototypeHeader";
2020-08-03 07:17:22 +03:00
import WebsitePrototypeFooter from "~/components/core/WebsitePrototypeFooter";
2020-10-05 00:30:28 +03:00
import { SlateLayout } from "~/components/core/SlateLayout";
2020-10-24 22:55:19 +03:00
import { SlateLayoutMobile } from "~/components/core/SlateLayoutMobile";
import SlateMediaObject from "~/components/core/SlateMediaObject";
2020-08-02 09:41:18 +03:00
2020-10-05 00:30:28 +03:00
const SIZE_LIMIT = 1000000; //NOTE(martina): 1mb limit for twitter preview images
const DEFAULT_IMAGE = "";
2020-07-27 12:50:25 +03:00
const STYLES_ROOT = css`
2020-08-03 07:17:22 +03:00
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
min-height: 100vh;
text-align: center;
font-size: 1rem;
background-color: ${Constants.system.white};
2020-08-03 07:17:22 +03:00
`;
const STYLES_SLATE = css`
padding: 0 64px 0 64px;
2020-10-22 01:54:01 +03:00
${"" /* max-width: 1660px; */}
2020-07-30 04:00:45 +03:00
display: block;
width: 100%;
2020-10-05 00:30:28 +03:00
margin: 48px auto 0 auto;
2020-08-03 07:17:22 +03:00
min-height: 10%;
height: 100%;
2020-07-27 12:50:25 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-08-03 07:17:22 +03:00
padding: 0 24px 0 24px;
2020-07-27 12:50:25 +03:00
}
`;
2020-07-31 12:47:37 +03:00
export const getServerSideProps = async (context) => {
return {
props: { ...context.query },
};
};
2020-07-27 12:50:25 +03:00
export default class SlatePage extends React.Component {
2020-07-31 12:47:37 +03:00
componentDidMount() {
if (!this.props.slate) {
return null;
}
let CIDMap = {};
2020-07-31 12:47:37 +03:00
System.dispatchCustomEvent({
name: "slate-global-create-carousel",
detail: {
carouselType: "slate",
slides: this.props.slate.data.objects.map((each, index) => {
// NOTE(jim):
// This is a hack to catch this undefined case I don't want to track down yet.
const url = each.url.replace("https://undefined", "https://");
const cid = Strings.getCIDFromIPFS(url);
CIDMap[cid] = index;
return {
cid,
id: each.id,
data: each,
2020-10-05 00:30:28 +03:00
isOwner: false,
2020-10-22 01:54:01 +03:00
component: <SlateMediaObject key={each.id} useImageFallback data={each} />,
};
2020-07-31 12:47:37 +03:00
}),
},
});
2020-09-14 06:14:23 +03:00
if (!Strings.isEmpty(this.props.cid)) {
const index = CIDMap[this.props.cid];
if (index || index === 0) {
System.dispatchCustomEvent({
name: "slate-global-open-carousel",
detail: {
index,
2020-10-05 00:30:28 +03:00
baseURL: `${this.props.creator.username}/${this.props.slate.slatename}`,
},
});
}
}
2020-07-31 12:47:37 +03:00
}
_handleSelect = (index) =>
System.dispatchCustomEvent({
name: "slate-global-open-carousel",
detail: {
index,
baseURL: `${this.props.creator.username}/${this.props.slate.slatename}`,
},
2020-07-31 12:47:37 +03:00
});
2020-10-05 00:30:28 +03:00
_handleSave = async (layouts) => {
await Actions.updateSlate({
id: this.props.slate.id,
layoutOnly: true,
data: { layouts },
});
};
2020-07-27 12:50:25 +03:00
render() {
2020-09-14 06:14:23 +03:00
let title = `${this.props.creator.username}/${this.props.slate.slatename}`;
2020-10-05 00:30:28 +03:00
let url = `https://slate.host/${this.props.creator.username}/${this.props.slate.slatename}`;
2020-09-14 08:02:33 +03:00
let headerURL = `https://slate.host/${this.props.creator.username}`;
2020-07-27 12:50:25 +03:00
2020-10-05 00:30:28 +03:00
let { objects, layouts, body, preview } = this.props.slate.data;
let image = preview;
if (!image) {
for (let i = 0; i < objects.length; i++) {
if (
objects[i].type &&
objects[i].type.startsWith("image/") &&
(!objects[i].size || objects[i].size < SIZE_LIMIT)
) {
image = objects[i].url.replace("https://undefined", "https://");
break;
}
}
2020-09-08 08:08:10 +03:00
}
2020-10-05 00:30:28 +03:00
if (!image) {
image = DEFAULT_IMAGE;
}
2020-07-30 04:00:45 +03:00
2020-09-14 06:14:23 +03:00
if (!Strings.isEmpty(this.props.cid)) {
let object = objects.find((each) => {
const url = each.url.replace("https://undefined", "https://");
const cid = Strings.getCIDFromIPFS(url);
return cid === this.props.cid;
});
if (object) {
title = !Strings.isEmpty(object.title) ? object.title : this.props.cid;
2020-10-22 01:54:01 +03:00
body = !Strings.isEmpty(object.body) ? Strings.elide(object.body) : `An object on ${url}`;
2020-09-14 06:14:23 +03:00
image = object.type.includes("image/") ? object.url : image;
url = `${url}/cid:${this.props.cid}`;
}
}
2020-10-05 00:30:28 +03:00
const headerTitle = `${this.props.creator.username} / ${this.props.slate.slatename}`;
2020-07-27 12:50:25 +03:00
return (
2020-10-22 01:54:01 +03:00
<WebsitePrototypeWrapper title={title} description={body} url={url} image={image}>
2020-07-27 12:50:25 +03:00
<div css={STYLES_ROOT}>
2020-10-25 22:09:21 +03:00
<WebsitePrototypeHeader />
<ViewAllButton fullText={this.props.slate.data.body} maxCharacter={208}>
<ProcessedText text={this.props.slate.data.body} />
</ViewAllButton>
2020-08-03 07:17:22 +03:00
<div css={STYLES_SLATE}>
2020-10-24 22:55:19 +03:00
{this.props.mobile ? (
<SlateLayoutMobile
isOwner={false}
items={objects}
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
onSelect={this._handleSelect}
/>
) : (
<SlateLayout
external
slateId={this.props.slate.id}
layout={layouts && layouts.ver === "2.0" ? layouts.layout : null}
onSaveLayout={this._handleSave}
isOwner={false}
fileNames={layouts && layouts.ver === "2.0" ? layouts.fileNames : false}
items={objects}
onSelect={this._handleSelect}
defaultLayout={layouts && layouts.ver === "2.0" ? layouts.defaultLayout : true}
/>
)}
2020-08-03 07:17:22 +03:00
</div>
2020-10-22 07:17:41 +03:00
<WebsitePrototypeFooter />
2020-07-27 12:50:25 +03:00
</div>
<System.GlobalCarousel external current={this.props.slate} viewer={this.props.creator} />
2020-10-05 00:30:28 +03:00
<System.GlobalModal />
2020-07-27 12:50:25 +03:00
</WebsitePrototypeWrapper>
);
}
}