slate/pages/_/slate.js

370 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";
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";
2020-07-27 12:50:25 +03:00
import { css } from "@emotion/core";
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 =
"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;
background-color: ${Constants.system.white};
2020-08-03 07:17:22 +03:00
`;
2020-10-26 03:18:55 +03:00
const STYLES_SLATE_INTRO = css`
display: flex;
2020-11-01 21:40:03 +03:00
padding: 32px 64px;
2020-10-26 03:18:55 +03:00
align-items: baseline;
line-height: 1.3;
2020-10-27 00:14:10 +03:00
word-wrap: break-word;
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-11-01 21:40:03 +03:00
padding: 32px 24px;
2020-10-26 03:18:55 +03:00
display: block;
font-size: 14px;
}
`;
const STYLES_CREATOR = css`
flex-shrink: 0;
margin-right: 4px;
text-decoration: none;
2020-10-26 22:46:37 +03:00
color: ${Constants.system.black};
2020-10-26 03:18:55 +03:00
:hover {
color: ${Constants.system.brand};
}
:visited {
color: ${Constants.system.black};
}
`;
const STYLES_DESCTIPTION = css`
font-size: ${Constants.typescale.lvl1};
font-family: ${Constants.font.text};
width: 50%;
2020-10-27 00:14:10 +03:00
word-wrap: break-word;
2020-10-26 08:03:41 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
2020-11-01 21:40:03 +03:00
width: 100%;
2020-10-26 03:18:55 +03:00
margin-top: 4px;
}
`;
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-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_FLEX = css`
display: flex;
2020-10-26 22:46:37 +03:00
margin-bottom: 12px;
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
display: block;
}
`;
const STYLES_STATS = css`
font-size: ${Constants.typescale.lvl0};
2020-10-26 22:46:37 +03:00
margin-top: 24px;
2020-10-26 03:18:55 +03:00
display: flex;
2020-10-26 22:46:37 +03:00
line-height: 1.5;
2020-10-26 03:18:55 +03:00
`;
const STYLES_STAT = css`
margin-right: 48px;
2020-11-01 21:40:03 +03:00
width: 88px;
2020-10-26 03:18:55 +03:00
${"" /* border-left: 1px solid ${Constants.system.darkGray};
padding-left: 12px; */};
`;
const STYLES_BUTTONS = css`
display: flex;
2020-10-26 22:46:37 +03:00
width: 200px;
height: 36px;
2020-10-26 03:18:55 +03:00
border-radius: 4px;
border: 1px solid ${Constants.system.gray};
2020-10-26 22:46:37 +03:00
flex-shrink: 0;
2020-10-26 03:18:55 +03:00
@media (max-width: ${Constants.sizes.mobile}px) {
margin: 12px 0;
}
`;
const STYLES_BUTTON = css`
border-right: 1px solid ${Constants.system.gray};
2020-10-26 22:46:37 +03:00
padding: 8px 4px;
2020-10-26 03:18:55 +03:00
cursor: pointer;
width: 50%;
font-family: ${Constants.font.medium};
font-weight: 400;
font-size: 14px;
text-align: center;
text-decoration: none;
2020-10-26 08:07:50 +03:00
color: ${Constants.system.black};
2020-10-26 03:18:55 +03:00
:last-child {
border-right: none;
}
:hover {
background-color: ${Constants.system.gray};
transition: 200ms background-color linear;
}
:visited {
color: ${Constants.system.black};
}
`;
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%;
@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-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;
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.replace("https://undefined", "https://");
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.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}`;
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}>
<a css={STYLES_CREATOR} href={headerURL}>
{slateCreator}
</a>
<div css={STYLES_DESCTIPTION}>
<div css={STYLES_FLEX}>
<div css={STYLES_TITLE}>{slateTitle} </div>
<div css={STYLES_BUTTONS}>
<a css={STYLES_BUTTON} href={"http://slate.host/_"}>
Follow
</a>
<a css={STYLES_BUTTON} href={"http://slate.host/_"}>
Download
</a>
</div>
</div>
<ViewAllButton fullText={this.props.slate.data.body} maxCharacter={208}>
<ProcessedText text={this.props.slate.data.body} />
</ViewAllButton>
<div css={STYLES_STATS}>
2020-10-26 03:18:55 +03:00
<div css={STYLES_STAT}>
2020-11-01 21:44:49 +03:00
<div style={{ color: `${Constants.system.darkGray}` }}>Data</div>
2020-10-26 22:46:37 +03:00
<div style={{ fontFamily: `${Constants.font.medium}` }}>
{this.props.slate.data.objects.length}
</div>
2020-10-26 03:18:55 +03:00
</div>
<div css={STYLES_STAT}>
2020-11-01 21:44:49 +03:00
<div style={{ color: `${Constants.system.darkGray}` }}>Contributors</div>
2020-10-26 22:46:37 +03:00
<div style={{ fontFamily: `${Constants.font.medium}` }}>{contributorsCount}</div>
2020-10-26 03:18:55 +03:00
</div>
{/* <div css={STYLES_STAT}>
2020-11-01 21:44:49 +03:00
<div style={{ color: `${Constants.system.darkGray}` }}>Followers</div>
2020-10-26 22:46:37 +03:00
<div style={{ fontFamily: `${Constants.font.medium}` }}>0</div>
</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}
/>
)}
2020-08-03 07:17:22 +03:00
</div>
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-10-26 03:18:55 +03:00
<WebsitePrototypeFooter />
2020-07-27 12:50:25 +03:00
</WebsitePrototypeWrapper>
);
}
}