CoastalCommitsPastes/client/pages/post/[id].tsx

120 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Button, Page, Text } from "@geist-ui/core";
2022-03-07 23:42:44 -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";
import styles from './styles.module.css';
import type { GetStaticPaths, GetStaticProps } from "next";
import { PostVisibility, ThemeProps } from "@lib/types";
2022-03-06 19:46:59 -05:00
type File = {
id: string
title: string
content: string
}
2022-03-15 22:49:41 -04:00
type Files = File[]
2022-03-15 22:49:41 -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
const Post = ({ post, theme, changeTheme }: PostProps) => {
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%"}>
<PageSeo
title={`${post.title} - Drift`}
description={post.description}
isPrivate={false}
/>
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} />} */}
<div className={styles.header}>
<div className={styles.titleAndBadge}>
<Text h2>{post.title}</Text>
<span><VisibilityBadge visibility={post.visibility} /></span>
</div>
<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 >
)
}
export const getStaticPaths: GetStaticPaths = async () => {
const posts = await fetch(process.env.API_URL + `/posts/`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-secret-key": process.env.SECRET_KEY || "",
}
})
const json = await posts.json()
const filtered = json.filter((post: any) => post.visibility === "public" || post.visibility === "unlisted")
const paths = filtered.map((post: any) => ({
params: { id: post.id }
}))
return { paths, fallback: 'blocking' }
}
export const getStaticProps: GetStaticProps = async ({ params }) => {
const post = await fetch(process.env.API_URL + `/posts/${params?.id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-secret-key": process.env.SECRET_KEY || "",
}
})
2022-03-15 22:49:41 -04:00
return {
props: {
post: await post.json()
},
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