2022-03-11 21:48:40 -05:00
|
|
|
import { Button, Page, Text } from "@geist-ui/core";
|
2022-03-07 23:42:44 -05:00
|
|
|
|
2022-03-07 01:16:08 -05:00
|
|
|
import { useRouter } from "next/router";
|
2022-03-06 19:46:59 -05:00
|
|
|
import Document from '../../components/document'
|
|
|
|
import Header from "../../components/header";
|
|
|
|
import VisibilityBadge from "../../components/visibility-badge";
|
2022-03-12 23:13:35 -05:00
|
|
|
import PageSeo from "components/page-seo";
|
2022-03-12 14:53:55 -05:00
|
|
|
import styles from './styles.module.css';
|
2022-03-15 22:49:41 -04:00
|
|
|
import cookie from "cookie";
|
2022-03-21 18:55:21 -04:00
|
|
|
import { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next";
|
2022-03-21 17:20:20 -04:00
|
|
|
import { PostVisibility, ThemeProps } from "@lib/types";
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
type File = {
|
|
|
|
id: string
|
|
|
|
title: string
|
|
|
|
content: string
|
|
|
|
}
|
2022-03-15 22:49:41 -04:00
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
type Files = File[]
|
2022-03-15 22:49:41 -04:00
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
export type PostProps = ThemeProps & {
|
|
|
|
post: {
|
|
|
|
id: string
|
|
|
|
title: string
|
|
|
|
description: string
|
|
|
|
visibility: PostVisibility
|
|
|
|
files: Files
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 22:49:41 -04:00
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
const Post = ({ post, theme, changeTheme }: PostProps) => {
|
2022-03-11 21:48:40 -05:00
|
|
|
const download = async () => {
|
|
|
|
const clientZip = require("client-zip")
|
|
|
|
|
|
|
|
const blob = await clientZip.downloadZip(post.files.map((file: any) => {
|
|
|
|
return {
|
|
|
|
name: file.title,
|
|
|
|
input: file.content,
|
|
|
|
lastModified: new Date(file.updatedAt)
|
|
|
|
}
|
|
|
|
})).blob()
|
|
|
|
const link = document.createElement("a")
|
|
|
|
link.href = URL.createObjectURL(blob)
|
|
|
|
link.download = `${post.title}.zip`
|
|
|
|
link.click()
|
|
|
|
link.remove()
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
2022-03-07 23:42:44 -05:00
|
|
|
<Page width={"100%"}>
|
2022-03-21 17:20:20 -04:00
|
|
|
<PageSeo
|
|
|
|
title={`${post.title} - Drift`}
|
|
|
|
description={post.description}
|
|
|
|
isPrivate={post.visibility !== 'public'}
|
|
|
|
/>
|
2022-03-12 23:13:35 -05:00
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
<Page.Header>
|
2022-03-07 19:42:47 -05:00
|
|
|
<Header theme={theme} changeTheme={changeTheme} />
|
2022-03-06 19:46:59 -05:00
|
|
|
</Page.Header>
|
2022-03-06 20:41:30 -05:00
|
|
|
<Page.Content width={"var(--main-content-width)"} margin="auto">
|
2022-03-20 23:45:37 -04:00
|
|
|
{/* {!isLoading && <PostFileExplorer files={post.files} />} */}
|
2022-03-21 17:20:20 -04:00
|
|
|
<div className={styles.header}>
|
|
|
|
<div className={styles.titleAndBadge}>
|
|
|
|
<Text h2>{post.title}</Text>
|
|
|
|
<span><VisibilityBadge visibility={post.visibility} /></span>
|
2022-03-11 21:48:40 -05:00
|
|
|
</div>
|
2022-03-21 17:20:20 -04:00
|
|
|
<Button auto onClick={download}>
|
|
|
|
Download as ZIP archive
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{post.files.map(({ id, content, title }: { id: any, content: string, title: string }) => (
|
|
|
|
<Document
|
|
|
|
key={id}
|
|
|
|
id={id}
|
|
|
|
content={content}
|
|
|
|
title={title}
|
|
|
|
editable={false}
|
|
|
|
initialTab={'preview'}
|
|
|
|
/>
|
|
|
|
))}
|
2022-03-06 19:46:59 -05:00
|
|
|
</Page.Content>
|
|
|
|
</Page >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:49:41 -04:00
|
|
|
export const getServerSideProps: GetServerSideProps = async (context) => {
|
2022-03-21 17:20:20 -04:00
|
|
|
const headers = context.req.headers
|
|
|
|
const host = headers.host
|
|
|
|
const driftToken = cookie.parse(headers.cookie || '')[`drift-token`]
|
|
|
|
let driftTheme = cookie.parse(headers.cookie || '')[`drift-theme`]
|
|
|
|
if (driftTheme !== "light" && driftTheme !== "dark") {
|
|
|
|
driftTheme = "light"
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:49:41 -04:00
|
|
|
if (context.query.id) {
|
2022-03-21 17:20:20 -04:00
|
|
|
const post = await fetch('http://' + host + `/server-api/posts/${context.query.id}`, {
|
2022-03-15 22:49:41 -04:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"Authorization": `Bearer ${driftToken}`
|
|
|
|
}
|
2022-03-21 17:20:20 -04:00
|
|
|
})
|
2022-03-19 20:15:17 -04:00
|
|
|
|
2022-03-21 17:20:20 -04:00
|
|
|
if (!post.ok || post.status !== 200) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: '/',
|
|
|
|
permanent: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 20:15:17 -04:00
|
|
|
try {
|
2022-03-21 17:20:20 -04:00
|
|
|
const json = await post.json();
|
|
|
|
const maxAge = 60 * 60 * 24 * 365;
|
|
|
|
context.res.setHeader(
|
|
|
|
'Cache-Control',
|
2022-03-21 17:54:36 -04:00
|
|
|
`${json.visibility === "public" ? "public" : "private"}, s-maxage=${maxAge}`
|
2022-03-21 17:20:20 -04:00
|
|
|
)
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
post: json
|
|
|
|
}
|
|
|
|
}
|
2022-03-19 20:15:17 -04:00
|
|
|
} catch (e) {
|
2022-03-21 17:20:20 -04:00
|
|
|
console.log(e)
|
2022-03-19 20:15:17 -04:00
|
|
|
}
|
2022-03-15 22:49:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
2022-03-21 17:20:20 -04:00
|
|
|
post: null
|
2022-03-15 22:49:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
export default Post
|
2022-03-21 18:55:21 -04:00
|
|
|
|