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

139 lines
4.4 KiB
TypeScript
Raw Normal View History

import { Button, Page, Text } from "@geist-ui/core";
2022-03-09 20:11:37 -05:00
import Skeleton from 'react-loading-skeleton';
2022-03-07 23:42:44 -05:00
2022-03-07 01:16:08 -05:00
import { useRouter } from "next/router";
2022-03-09 20:11:37 -05:00
import { useEffect, useState } from "react";
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-15 22:49:41 -04:00
import { PostProps } from "../_app";
2022-03-12 23:13:35 -05:00
import PageSeo from "components/page-seo";
import styles from './styles.module.css';
2022-03-15 15:15:54 -04:00
import Cookies from "js-cookie";
2022-03-15 22:49:41 -04:00
import cookie from "cookie";
import { GetServerSideProps } from "next";
2022-03-06 19:46:59 -05:00
2022-03-15 22:49:41 -04:00
const Post = ({renderedPost, theme, changeTheme}: PostProps) => {
const [post, setPost] = useState(renderedPost);
2022-03-07 01:16:08 -05:00
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string>()
const router = useRouter();
useEffect(() => {
async function fetchPost() {
setIsLoading(true);
2022-03-15 22:49:41 -04:00
2022-03-16 19:21:22 -04:00
if (renderedPost) {
2022-03-15 22:49:41 -04:00
setPost(renderedPost)
2022-03-07 01:16:08 -05:00
setIsLoading(false)
2022-03-16 18:54:04 -04:00
2022-03-15 22:49:41 -04:00
return;
}
2022-03-16 19:21:22 -04:00
if (!Cookies.get('drift-token')) {
router.push('/signin');
2022-03-16 18:54:04 -04:00
} else {
setError('Something went wrong fetching the post');
2022-03-07 01:16:08 -05:00
}
}
fetchPost()
}, [router, router.query.id])
2022-03-06 19:46:59 -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-12 23:13:35 -05:00
{!isLoading && (
<PageSeo
title={`${post.title} - Drift`}
description={post.description}
isPrivate={post.visibility === 'private'}
/>
)}
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-07 01:16:08 -05:00
{error && <Text type="error">{error}</Text>}
2022-03-09 20:11:37 -05:00
{!error && isLoading && <><Text h2><Skeleton width={400} /></Text>
<Document skeleton={true} />
</>}
{!isLoading && post && <>
<div className={styles.header}>
2022-03-20 23:45:37 -04:00
<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>
2022-03-07 01:16:08 -05:00
{post.files.map(({ id, content, title }: { id: any, content: string, title: string }) => (
2022-03-06 19:46:59 -05:00
<Document
key={id}
id={id}
2022-03-06 19:46:59 -05:00
content={content}
title={title}
editable={false}
2022-03-07 01:16:08 -05:00
initialTab={'preview'}
2022-03-06 19:46:59 -05:00
/>
2022-03-07 01:16:08 -05:00
))}
2022-03-09 20:11:37 -05:00
</>}
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) => {
const headers = context.req.headers;
const host = headers.host;
const driftToken = cookie.parse(headers.cookie || '')[`drift-token`];
let post;
if (context.query.id) {
post = await fetch('http://' + host + `/server-api/posts/${context.query.id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${driftToken}`
}
});
try {
post = await post.json();
} catch (e) {
console.log(e);
post = null;
}
2022-03-15 22:49:41 -04:00
}
return {
props: {
renderedPost: post
}
}
}
2022-03-06 19:46:59 -05:00
export default Post