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-04-09 20:48:19 -04:00
|
|
|
import DocumentComponent from "@components/view-document"
|
|
|
|
import styles from "./post-page.module.css"
|
|
|
|
import homeStyles from "@styles/Home.module.css"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-04-14 17:27:38 -04:00
|
|
|
import type { File, Post, PostVisibility } 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-04-09 20:48:19 -04:00
|
|
|
import Archive from "@geist-ui/icons/archive"
|
|
|
|
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-12 19:48:12 -04:00
|
|
|
import PasswordModalPage from "./password-modal-wrapper"
|
2022-04-14 17:27:38 -04:00
|
|
|
import VisibilityControl from "@components/badges/visibility-control"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
type Props = {
|
2022-04-09 20:48:19 -04:00
|
|
|
post: Post
|
2022-04-12 19:48:12 -04:00
|
|
|
isProtected?: boolean
|
2022-03-21 23:30:45 -04:00
|
|
|
}
|
|
|
|
|
2022-04-12 19:48:12 -04:00
|
|
|
const PostPage = ({ post: initialPost, isProtected }: Props) => {
|
|
|
|
const [post, setPost] = useState<Post>(initialPost)
|
2022-04-14 17:27:38 -04:00
|
|
|
const [visibility, setVisibility] = useState<PostVisibility>(post.visibility)
|
2022-04-09 20:48:19 -04:00
|
|
|
const [isExpired, setIsExpired] = useState(
|
|
|
|
post.expiresAt ? new Date(post.expiresAt) < new Date() : null
|
|
|
|
)
|
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
2022-04-14 17:27:38 -04:00
|
|
|
const [isOwner] = useState(
|
|
|
|
post.users ? post.users[0].id === Cookies.get("drift-userid") : false
|
|
|
|
)
|
2022-04-12 19:48:12 -04:00
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const isMobile = useMediaQuery("mobile")
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isOwner && isExpired) {
|
|
|
|
router.push("/expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
const expirationDate = new Date(post.expiresAt ? post.expiresAt : "")
|
|
|
|
if (!isOwner && expirationDate < new Date()) {
|
|
|
|
router.push("/expired")
|
|
|
|
} else {
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 17:27:38 -04:00
|
|
|
}, [isExpired, isOwner, post.expiresAt, post.users, router])
|
2022-04-09 20:48:19 -04:00
|
|
|
|
|
|
|
const download = async () => {
|
|
|
|
if (!post.files) return
|
|
|
|
const downloadZip = (await import("client-zip")).downloadZip
|
|
|
|
const blob = await 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
const editACopy = () => {
|
|
|
|
router.push(`/new/from/${post.id}`)
|
|
|
|
}
|
|
|
|
|
2022-04-14 20:18:47 -04:00
|
|
|
const viewParentClick = () => {
|
|
|
|
router.push(
|
|
|
|
`/post/${post.parent!.id}`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
if (isLoading) {
|
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2022-04-12 19:48:12 -04:00
|
|
|
const isAvailable = !isExpired && !isProtected && post.title
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
|
|
|
<Page width={"100%"}>
|
|
|
|
<PageSeo
|
|
|
|
title={`${post.title} - Drift`}
|
|
|
|
description={post.description}
|
|
|
|
isPrivate={false}
|
|
|
|
/>
|
2022-04-12 19:48:12 -04:00
|
|
|
{!isAvailable && <PasswordModalPage setPost={setPost} />}
|
2022-04-09 20:48:19 -04:00
|
|
|
<Page.Content className={homeStyles.main}>
|
|
|
|
<div className={styles.header}>
|
|
|
|
<span className={styles.buttons}>
|
|
|
|
<ButtonGroup
|
|
|
|
vertical={isMobile}
|
|
|
|
marginLeft={0}
|
|
|
|
marginRight={0}
|
|
|
|
marginTop={1}
|
|
|
|
marginBottom={1}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
icon={<Edit />}
|
|
|
|
onClick={editACopy}
|
|
|
|
style={{ textTransform: "none" }}
|
|
|
|
>
|
|
|
|
Edit a Copy
|
|
|
|
</Button>
|
|
|
|
{post.parent && (
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
icon={<Parent />}
|
2022-04-14 20:18:47 -04:00
|
|
|
onClick={viewParentClick}
|
2022-04-09 20:48:19 -04:00
|
|
|
>
|
|
|
|
View Parent
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
onClick={download}
|
|
|
|
icon={<Archive />}
|
|
|
|
style={{ textTransform: "none" }}
|
|
|
|
>
|
|
|
|
Download as ZIP Archive
|
|
|
|
</Button>
|
|
|
|
<FileDropdown isMobile={isMobile} files={post.files || []} />
|
|
|
|
</ButtonGroup>
|
|
|
|
</span>
|
|
|
|
<span className={styles.title}>
|
|
|
|
<Text h3>{post.title}</Text>
|
|
|
|
<span className={styles.badges}>
|
2022-04-14 17:27:38 -04:00
|
|
|
<VisibilityBadge visibility={visibility} />
|
2022-04-09 20:48:19 -04:00
|
|
|
<CreatedAgoBadge createdAt={post.createdAt} />
|
|
|
|
<ExpirationBadge postExpirationDate={post.expiresAt} />
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
2022-04-14 17:25:31 -04:00
|
|
|
{post.description && (
|
|
|
|
<div>
|
|
|
|
<Text p>{post.description}</Text>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-04-09 20:48:19 -04:00
|
|
|
{/* {post.files.length > 1 && <FileTree files={post.files} />} */}
|
|
|
|
{post.files?.map(({ id, content, title }: File) => (
|
|
|
|
<DocumentComponent
|
|
|
|
key={id}
|
|
|
|
title={title}
|
|
|
|
initialTab={"preview"}
|
|
|
|
id={id}
|
|
|
|
content={content}
|
|
|
|
/>
|
|
|
|
))}
|
2022-04-14 17:32:20 -04:00
|
|
|
{isOwner && (
|
|
|
|
<span className={styles.controls}>
|
|
|
|
<VisibilityControl
|
|
|
|
postId={post.id}
|
|
|
|
visibility={visibility}
|
|
|
|
setVisibility={setVisibility}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)}
|
2022-04-09 20:48:19 -04:00
|
|
|
<ScrollToTop />
|
|
|
|
</Page.Content>
|
|
|
|
</Page>
|
|
|
|
)
|
2022-03-21 23:30:45 -04:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export default PostPage
|