slate/pages/_/slate.js

137 lines
3.8 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-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-07-27 12:50:25 +03:00
import WebsitePrototypeWrapper from "~/components/core/WebsitePrototypeWrapper";
2020-08-03 07:17:22 +03:00
import WebsitePrototypeHeaderGeneric from "~/components/core/WebsitePrototypeHeaderGeneric";
import WebsitePrototypeFooter from "~/components/core/WebsitePrototypeFooter";
import Slate, { generateLayout } from "~/components/core/Slate";
import SlateMediaObject from "~/components/core/SlateMediaObject";
2020-08-02 09:41:18 +03:00
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;
`;
const STYLES_SLATE = css`
padding: 0 88px 0 88px;
2020-08-22 20:30:03 +03:00
max-width: 1660px;
2020-07-30 04:00:45 +03:00
display: block;
width: 100%;
margin: 0 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 {
state = {
layouts: this.props.slate.data.layouts
? this.props.slate.data.layouts
: { lg: generateLayout(this.props.slate.data.objects) },
};
2020-07-31 12:47:37 +03:00
componentDidMount() {
if (!this.props.slate) {
return null;
}
2020-07-31 12:47:37 +03:00
System.dispatchCustomEvent({
name: "slate-global-create-carousel",
detail: {
slides: this.props.slate.data.objects.map((each) => {
// 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);
return {
cid,
id: each.id,
data: each,
editing: false,
component: (
<SlateMediaObject key={each.id} useImageFallback data={each} />
),
};
2020-07-31 12:47:37 +03:00
}),
},
});
}
_handleSelect = (index) =>
System.dispatchCustomEvent({
name: "slate-global-open-carousel",
detail: { index },
});
2020-07-27 12:50:25 +03:00
render() {
const title = `${this.props.creator.username}/${
this.props.slate.slatename
}`;
const url = `https://slate.host/${this.props.creator.username}`;
2020-08-22 08:19:11 +03:00
const description = this.props.slate.data.body;
2020-07-27 12:50:25 +03:00
2020-09-08 08:08:10 +03:00
const { objects } = this.props.slate.data;
// TODO(jim): Takes the first image found
// but we want this to be a user choice.
2020-07-30 04:00:45 +03:00
let image;
2020-09-08 08:08:10 +03:00
for (let i = 0; i < objects.length; i++) {
if (objects[i].type && objects[i].type.startsWith("image/")) {
image = objects[i].url;
break;
}
2020-09-08 08:08:10 +03:00
}
2020-07-30 04:00:45 +03:00
const headerTitle = `${this.props.creator.username} / ${
this.props.slate.slatename
}`;
2020-07-27 12:50:25 +03:00
return (
<WebsitePrototypeWrapper
title={title}
description={description}
url={url}
image={image}
>
2020-07-27 12:50:25 +03:00
<div css={STYLES_ROOT}>
<WebsitePrototypeHeaderGeneric href={url} title={headerTitle}>
<ProcessedText text={this.props.slate.data.body} />
</WebsitePrototypeHeaderGeneric>
<Alert style={{ top: 0, width: "100%" }} />
2020-08-03 07:17:22 +03:00
<div css={STYLES_SLATE}>
2020-08-22 08:19:11 +03:00
<Slate
editing={false}
layouts={this.state.layouts}
2020-09-08 08:08:10 +03:00
items={objects}
2020-08-22 08:19:11 +03:00
onSelect={this._handleSelect}
/>
2020-08-03 07:17:22 +03:00
</div>
2020-08-03 19:44:25 +03:00
<WebsitePrototypeFooter style={{ marginTop: 88 }} />
2020-07-27 12:50:25 +03:00
</div>
2020-07-31 12:47:37 +03:00
<System.GlobalCarousel />
2020-07-27 12:50:25 +03:00
</WebsitePrototypeWrapper>
);
}
}