2022-03-21 23:30:45 -04:00
|
|
|
import PageSeo from "@components/page-seo"
|
2022-03-30 23:01:24 -04:00
|
|
|
import VisibilityBadge from "@components/badges/visibility-badge"
|
2022-03-23 00:18:26 -04:00
|
|
|
import DocumentComponent from '@components/view-document'
|
2022-03-21 23:30:45 -04:00
|
|
|
import styles from './post-page.module.css'
|
2022-03-22 23:06:15 -04:00
|
|
|
import homeStyles from '@styles/Home.module.css'
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-03-23 00:18:26 -04:00
|
|
|
import type { File, Post } from "@lib/types"
|
2022-04-01 19:26:42 -04:00
|
|
|
import { Page, Button, Text, ButtonGroup, useMediaQuery } from "@geist-ui/core"
|
|
|
|
import { useEffect, useState } from "react"
|
2022-03-25 16:01:46 -04:00
|
|
|
import Archive from '@geist-ui/icons/archive'
|
2022-04-02 01:55:27 -04:00
|
|
|
import Edit from '@geist-ui/icons/edit'
|
|
|
|
import Parent from '@geist-ui/icons/arrowUpCircle'
|
2022-03-25 16:01:46 -04:00
|
|
|
import FileDropdown from "@components/file-dropdown"
|
2022-03-25 16:29:49 -04:00
|
|
|
import ScrollToTop from "@components/scroll-to-top"
|
2022-03-30 23:01:24 -04:00
|
|
|
import { useRouter } from "next/router"
|
|
|
|
import ExpirationBadge from "@components/badges/expiration-badge"
|
|
|
|
import CreatedAgoBadge from "@components/badges/created-ago-badge"
|
|
|
|
import Cookies from "js-cookie"
|
2022-04-02 01:55:27 -04:00
|
|
|
import getPostPath from "@lib/get-post-path"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
type Props = {
|
2022-03-21 23:30:45 -04:00
|
|
|
post: Post
|
|
|
|
}
|
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
const PostPage = ({ post }: Props) => {
|
2022-03-30 23:01:24 -04:00
|
|
|
const router = useRouter()
|
2022-03-30 23:52:30 -04:00
|
|
|
|
|
|
|
const isMobile = useMediaQuery("mobile")
|
|
|
|
const [isExpired, setIsExpired] = useState(post.expiresAt ? new Date(post.expiresAt) < new Date() : null)
|
2022-03-31 02:27:09 -04:00
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
2022-03-30 23:52:30 -04:00
|
|
|
useEffect(() => {
|
2022-04-02 01:55:27 -04:00
|
|
|
if (isExpired) {
|
|
|
|
router.push("/expired")
|
|
|
|
}
|
2022-03-31 02:27:09 -04:00
|
|
|
const isOwner = post.users ? post.users[0].id === Cookies.get("drift-userid") : false
|
|
|
|
|
|
|
|
const expirationDate = new Date(post.expiresAt ? post.expiresAt : "")
|
|
|
|
if (!isOwner && expirationDate < new Date()) {
|
|
|
|
router.push("/expired")
|
|
|
|
} else {
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
|
2022-03-30 23:52:30 -04:00
|
|
|
let interval: NodeJS.Timer | null = null;
|
|
|
|
if (post.expiresAt) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const expirationDate = new Date(post.expiresAt ? post.expiresAt : "")
|
|
|
|
setIsExpired(expirationDate < new Date())
|
|
|
|
}, 4000)
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (interval) clearInterval(interval)
|
|
|
|
}
|
2022-04-02 01:55:27 -04:00
|
|
|
}, [isExpired, post.expiresAt, post.users, router])
|
2022-03-30 23:52:30 -04:00
|
|
|
|
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
const download = async () => {
|
2022-04-01 19:26:42 -04:00
|
|
|
if (!post.files) return
|
2022-03-21 23:43:50 -04:00
|
|
|
const downloadZip = (await import("client-zip")).downloadZip
|
2022-03-21 23:34:05 -04:00
|
|
|
const blob = await downloadZip(post.files.map((file: any) => {
|
2022-03-21 23:30:45 -04:00
|
|
|
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-24 22:32:24 -04:00
|
|
|
|
2022-04-02 01:55:27 -04:00
|
|
|
const editACopy = () => {
|
|
|
|
router.push(`/new/from/${post.id}`)
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:27:09 -04:00
|
|
|
if (isLoading) {
|
|
|
|
return <></>
|
|
|
|
}
|
2022-03-30 23:01:24 -04:00
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
return (
|
|
|
|
<Page width={"100%"}>
|
|
|
|
<PageSeo
|
|
|
|
title={`${post.title} - Drift`}
|
|
|
|
description={post.description}
|
|
|
|
isPrivate={false}
|
|
|
|
/>
|
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
<Page.Content className={homeStyles.main}>
|
2022-03-21 23:30:45 -04:00
|
|
|
<div className={styles.header}>
|
2022-03-25 16:01:46 -04:00
|
|
|
<span className={styles.buttons}>
|
2022-04-03 16:09:04 -04:00
|
|
|
<ButtonGroup vertical={isMobile} marginLeft={0} marginRight={0} marginTop={1} marginBottom={1}>
|
2022-04-02 01:55:27 -04:00
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
icon={<Edit />}
|
|
|
|
onClick={editACopy}
|
|
|
|
style={{ textTransform: 'none' }}>
|
|
|
|
Edit a Copy
|
2022-03-25 16:01:46 -04:00
|
|
|
</Button>
|
2022-04-02 01:55:27 -04:00
|
|
|
{post.parent && <Button
|
|
|
|
auto
|
|
|
|
icon={<Parent />}
|
|
|
|
onClick={() => router.push(getPostPath(post.parent!.visibility, post.parent!.id))}
|
|
|
|
>
|
|
|
|
View Parent
|
|
|
|
</Button>}
|
2022-04-03 16:09:04 -04:00
|
|
|
<Button auto onClick={download} icon={<Archive />} style={{ textTransform: 'none' }}>
|
|
|
|
Download as ZIP Archive
|
|
|
|
</Button>
|
2022-04-02 01:55:27 -04:00
|
|
|
<FileDropdown isMobile={isMobile} files={post.files || []} />
|
2022-03-25 16:01:46 -04:00
|
|
|
</ButtonGroup>
|
|
|
|
</span>
|
2022-04-03 16:09:04 -04:00
|
|
|
<span className={styles.title}>
|
|
|
|
<Text h3>{post.title}</Text>
|
|
|
|
<span className={styles.badges}>
|
|
|
|
<VisibilityBadge visibility={post.visibility} />
|
|
|
|
<CreatedAgoBadge createdAt={post.createdAt} />
|
|
|
|
<ExpirationBadge postExpirationDate={post.expiresAt} />
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
</div>
|
2022-03-25 16:01:46 -04:00
|
|
|
{/* {post.files.length > 1 && <FileTree files={post.files} />} */}
|
2022-04-01 19:26:42 -04:00
|
|
|
{post.files?.map(({ id, content, title }: File) => (
|
2022-03-21 23:30:45 -04:00
|
|
|
<DocumentComponent
|
|
|
|
key={id}
|
|
|
|
title={title}
|
|
|
|
initialTab={'preview'}
|
2022-03-23 00:18:26 -04:00
|
|
|
id={id}
|
|
|
|
content={content}
|
2022-03-21 23:30:45 -04:00
|
|
|
/>
|
|
|
|
))}
|
2022-03-25 16:29:49 -04:00
|
|
|
<ScrollToTop />
|
|
|
|
|
2022-03-21 23:30:45 -04:00
|
|
|
</Page.Content>
|
|
|
|
</Page >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PostPage
|