slate/pages/_/slate.js

359 lines
11 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";
2020-11-12 21:14:35 +03:00
import * as SVG from "~/common/svg";
import * as Strings from "~/common/strings";
2020-10-05 00:30:28 +03:00
import * as Actions from "~/common/actions";
2020-11-11 04:44:21 +03:00
import * as Validations from "~/common/validations";
import * as Events from "~/common/custom-events";
2020-07-27 12:50:25 +03:00
2020-12-13 04:16:55 +03:00
import { ButtonSecondary } from "~/components/system/components/Buttons";
2020-11-30 08:24:22 +03:00
import { css } from "@emotion/react";
2020-10-23 09:25:19 +03:00
import { ViewAllButton } from "~/components/core/ViewAll";
import { SlateLayout } from "~/components/core/SlateLayout";
import { SlateLayoutMobile } from "~/components/core/SlateLayoutMobile";
import { GlobalModal } from "~/components/system/components/GlobalModal";
import { GlobalCarousel } from "~/components/system/components/GlobalCarousel";
2020-07-27 12:50:25 +03:00
import ProcessedText from "~/components/core/ProcessedText";
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";
import SlateMediaObject from "~/components/core/SlateMediaObject";
2020-11-12 21:14:35 +03:00
import CTATransition from "~/components/core/CTATransition";
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 =
"https://slate.textile.io/ipfs/bafkreiaow45dlq5xaydaeqocdxvffudibrzh2c6qandpqkb6t3ahbvh6re";
const DEFAULT_BOOK =
"https://slate.textile.io/ipfs/bafkreibk32sw7arspy5kw3p5gkuidfcwjbwqyjdktd5wkqqxahvkm2qlyi";
const DEFAULT_DATA =
"https://slate.textile.io/ipfs/bafkreid6bnjxz6fq2deuhehtxkcesjnjsa2itcdgyn754fddc7u72oks2m";
const DEFAULT_DOCUMENT =
"https://slate.textile.io/ipfs/bafkreiecdiepww52i5q3luvp4ki2n34o6z3qkjmbk7pfhx4q654a4wxeam";
const DEFAULT_VIDEO =
"https://slate.textile.io/ipfs/bafkreibesdtut4j5arclrxd2hmkfrv4js4cile7ajnndn3dcn5va6wzoaa";
const DEFAULT_AUDIO =
"https://slate.textile.io/ipfs/bafkreig2hijckpamesp4nawrhd6vlfvrtzt7yau5wad4mzpm3kie5omv4e";
2020-10-05 00:30:28 +03:00
2020-07-27 12:50:25 +03:00
const STYLES_ROOT = css`
2020-10-26 03:18:55 +03:00
display: block;
2020-08-03 07:17:22 +03:00
min-height: 100vh;
2020-12-11 05:59:17 +03:00
background-color: ${Constants.system.foreground};
2020-08-03 07:17:22 +03:00
`;
2020-10-26 03:18:55 +03:00
const STYLES_SLATE_INTRO = css`
width: 100%;
padding: 16px 32px 0px 32px;
overflow-wrap: break-word;
white-space: pre-wrap;
flex-shrink: 0;
display: block;
width: 50%;
2020-11-21 00:55:47 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
padding: 24px 24px 0px 24px;
width: 100%;
}
`;
const STYLES_TITLELINE = css`
2020-10-26 03:18:55 +03:00
display: flex;
2020-12-13 04:16:55 +03:00
align-items: flex-end;
2020-10-26 03:18:55 +03:00
line-height: 1.3;
2020-10-27 00:14:10 +03:00
word-wrap: break-word;
2020-11-21 00:55:47 +03:00
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
display: block;
font-size: 14px;
}
`;
const STYLES_CREATOR = css`
flex-shrink: 0;
margin-right: 4px;
text-decoration: none;
font-size: ${Constants.typescale.lvl3};
2020-10-26 22:46:37 +03:00
color: ${Constants.system.black};
font-family: ${Constants.font.medium};
2020-10-26 03:18:55 +03:00
:hover {
color: ${Constants.system.brand};
}
`;
const STYLES_TITLE = css`
font-size: ${Constants.typescale.lvl3};
font-family: ${Constants.font.medium};
font-weight: 400;
color: ${Constants.system.black};
2020-10-26 22:46:37 +03:00
width: auto;
2020-10-27 00:14:10 +03:00
max-width: 100%;
2020-10-26 22:46:37 +03:00
margin-right: 24px;
2020-10-27 00:14:10 +03:00
word-wrap: break-word;
2020-11-21 00:55:47 +03:00
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-11-01 21:40:03 +03:00
font-size: ${Constants.typescale.lvl3};
2020-10-26 03:18:55 +03:00
}
`;
const STYLES_DESCRIPTION = css`
font-size: ${Constants.typescale.lvl0};
color: ${Constants.system.darkGray};
width: 100%;
overflow-wrap: break-word;
white-space: pre-wrap;
margin-top: 16px;
2020-11-21 00:55:47 +03:00
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
margin-top: 24px;
2020-10-26 03:18:55 +03:00
}
`;
const STYLES_STATS = css`
font-size: ${Constants.typescale.lvl0};
margin: 16px 0;
2020-10-26 03:18:55 +03:00
display: flex;
width: 100%;
color: ${Constants.system.grayBlack};
2020-10-26 03:18:55 +03:00
`;
const STYLES_STAT = css`
margin-right: 16px;
flex-shrink: 0;
2020-10-26 03:18:55 +03:00
`;
2020-08-03 07:17:22 +03:00
const STYLES_SLATE = css`
padding: 0 32px;
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-11-21 00:55:47 +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 },
};
};
export const FileTypeDefaultPreview = () => {
if (props.type && props.type.startsWith("video/")) {
return DEFAULT_VIDEO;
}
if (props.type && props.type.startsWith("audio/")) {
return DEFAULT_AUDIO;
}
if (props.type && props.type.startsWith("application/epub")) {
return DEFAULT_BOOK;
}
if (props.type && props.type.startsWith("application/pdf")) {
return DEFAULT_DOCUMENT;
}
return DEFAULT_DATA;
};
2020-07-27 12:50:25 +03:00
export default class SlatePage extends React.Component {
2020-11-12 21:14:35 +03:00
state = {
visible: false,
};
2020-07-31 12:47:37 +03:00
componentDidMount() {
if (!this.props.slate) {
return null;
}
let CIDMap = {};
Events.dispatchCustomEvent({
2020-07-31 12:47:37 +03:00
name: "slate-global-create-carousel",
detail: {
carouselType: "slate",
slides: this.props.slate.data.objects.map((each, index) => {
const url = each.url;
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) {
Events.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) => {
Events.dispatchCustomEvent({
2020-07-31 12:47:37 +03:00
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-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;
2020-11-10 04:26:53 +03:00
let image;
if (Strings.isEmpty(this.props.cid)) {
image = preview;
if (Strings.isEmpty(image)) {
for (let i = 0; i < objects.length; i++) {
if (
objects[i].type &&
2020-11-11 04:44:21 +03:00
Validations.isPreviewableImage(objects[i].type) &&
2020-11-11 00:06:26 +03:00
objects[i].size &&
objects[i].size < SIZE_LIMIT
2020-11-10 04:26:53 +03:00
) {
image = objects[i].url;
2020-11-10 04:26:53 +03:00
break;
}
2020-10-05 00:30:28 +03:00
}
}
2020-11-10 04:26:53 +03:00
} else {
2020-09-14 06:14:23 +03:00
let object = objects.find((each) => {
const url = each.url;
2020-09-14 06:14:23 +03:00
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}`;
image = object.type.includes("image/") ? (
object.url
) : (
<FileTypeDefaultPreview type={object.type} />
);
2020-09-14 06:14:23 +03:00
url = `${url}/cid:${this.props.cid}`;
}
}
2020-11-10 04:26:53 +03:00
if (Strings.isEmpty(image)) {
image = DEFAULT_IMAGE;
}
2020-09-14 06:14:23 +03:00
2020-10-26 03:18:55 +03:00
const slateCreator = `${this.props.creator.username} / `;
const slateTitle = `${this.props.slate.slatename}`;
const counts = objects.reduce((counts, { ownerId }) => {
counts[ownerId] = (counts[ownerId] || 0) + 1;
return counts;
}, {});
const contributorsCount = Object.keys(counts).length;
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 />
2020-10-26 03:18:55 +03:00
<div css={STYLES_SLATE_INTRO}>
<div css={STYLES_TITLELINE}>
<a css={STYLES_CREATOR} href={`/${this.props.creator.username}`}>
{slateCreator}
</a>
<div css={STYLES_TITLE}>{slateTitle} </div>
2020-12-13 04:16:55 +03:00
<ButtonSecondary onClick={() => this.setState({ visible: true })}>
Follow
</ButtonSecondary>
</div>
<div css={STYLES_DESCRIPTION}>
2020-10-26 03:18:55 +03:00
<ViewAllButton fullText={this.props.slate.data.body} maxCharacter={208}>
<ProcessedText text={this.props.slate.data.body} />
</ViewAllButton>
</div>
<div css={STYLES_STATS}>
<div css={STYLES_STAT}>
<div style={{ fontFamily: `${Constants.font.medium}` }}>
{this.props.slate.data.objects.length}{" "}
<span style={{ color: `${Constants.system.darkGray}` }}>Data</span>
2020-10-26 03:18:55 +03:00
</div>
</div>
<div css={STYLES_STAT}>
<div style={{ fontFamily: `${Constants.font.medium}` }}>
{contributorsCount}{" "}
<span style={{ color: `${Constants.system.darkGray}` }}>Contributors</span>
2020-10-26 03:18:55 +03:00
</div>
</div>
2020-10-26 03:18:55 +03:00
</div>
</div>
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}
creator={this.props.creator}
slate={this.props.slate}
2020-10-24 22:55:19 +03:00
/>
)}
2020-08-03 07:17:22 +03:00
</div>
2020-07-27 12:50:25 +03:00
</div>
<GlobalCarousel external current={this.props.slate} mobile={this.props.mobile} />
<GlobalModal />
2020-11-13 01:27:50 +03:00
{this.state.visible && (
<div>
<CTATransition
2020-11-17 06:17:56 +03:00
onClose={() => this.setState({ visible: false })}
viewer={this.props.viewer}
open={this.state.visible}
2020-12-13 04:16:55 +03:00
redirectURL={`/_${Strings.createQueryParams({
2020-12-19 08:25:50 +03:00
scene: "NAV_SLATE",
2020-12-13 04:16:55 +03:00
user: this.props.creator.username,
slate: this.props.slate.slatename,
})}`}
/>
2020-11-13 01:27:50 +03:00
</div>
)}
2020-10-26 03:18:55 +03:00
<WebsitePrototypeFooter />
2020-07-27 12:50:25 +03:00
</WebsitePrototypeWrapper>
);
}
}