2022-11-12 02:59:33 -05:00
|
|
|
"use client"
|
|
|
|
|
2022-11-12 04:28:06 -05:00
|
|
|
import VisibilityBadge from "@components/badges/visibility-badge"
|
2022-11-12 03:58:21 -05:00
|
|
|
import DocumentComponent from "./view-document"
|
2022-04-09 20:48:19 -04:00
|
|
|
import styles from "./post-page.module.css"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-04-01 19:26:42 -04:00
|
|
|
import { useEffect, useState } from "react"
|
2022-11-12 03:58:21 -05:00
|
|
|
import FileDropdown from "app/(posts)/components/file-dropdown"
|
2022-11-12 04:28:06 -05:00
|
|
|
import ScrollToTop from "@components/scroll-to-top"
|
2022-11-12 02:59:33 -05:00
|
|
|
import { useRouter } from "next/navigation"
|
2022-11-12 04:28:06 -05:00
|
|
|
import ExpirationBadge from "@components/badges/expiration-badge"
|
|
|
|
import CreatedAgoBadge from "@components/badges/created-ago-badge"
|
2022-04-12 19:48:12 -04:00
|
|
|
import PasswordModalPage from "./password-modal-wrapper"
|
2022-11-12 04:28:06 -05:00
|
|
|
import VisibilityControl from "@components/badges/visibility-control"
|
2022-11-14 04:28:40 -05:00
|
|
|
import { File, PostWithFilesAndAuthor } from "@lib/server/prisma"
|
2022-11-16 05:16:56 -05:00
|
|
|
import ButtonGroup from "@components/button-group"
|
|
|
|
import Button from "@components/button"
|
2022-11-29 03:43:04 -05:00
|
|
|
import { Archive, ArrowUpCircle, Edit } from "react-feather"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
type Props = {
|
2022-11-14 04:28:40 -05:00
|
|
|
post: string | PostWithFilesAndAuthor
|
2022-04-12 19:48:12 -04:00
|
|
|
isProtected?: boolean
|
2022-11-12 02:59:33 -05:00
|
|
|
isAuthor?: boolean
|
2022-03-21 23:30:45 -04:00
|
|
|
}
|
|
|
|
|
2022-11-12 02:59:33 -05:00
|
|
|
const PostPage = ({ post: initialPost, isProtected, isAuthor }: Props) => {
|
2022-11-14 04:28:40 -05:00
|
|
|
const [post, setPost] = useState<PostWithFilesAndAuthor>(
|
|
|
|
typeof initialPost === "string" ? JSON.parse(initialPost) : initialPost
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
2022-11-14 04:28:40 -05:00
|
|
|
const [visibility, setVisibility] = useState<string>(post.visibility)
|
2022-04-12 19:48:12 -04:00
|
|
|
const router = useRouter()
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (post.expiresAt) {
|
2022-11-14 04:28:40 -05:00
|
|
|
if (new Date(post.expiresAt) < new Date()) {
|
|
|
|
if (!isAuthor) {
|
|
|
|
router.push("/expired")
|
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
const expirationDate = new Date(post.expiresAt ? post.expiresAt : "")
|
2022-11-14 04:28:40 -05:00
|
|
|
if (!isAuthor && expirationDate < new Date()) {
|
|
|
|
router.push("/expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
let interval: NodeJS.Timer | null = null
|
|
|
|
if (post.expiresAt) {
|
|
|
|
interval = setInterval(() => {
|
|
|
|
const expirationDate = new Date(
|
|
|
|
post.expiresAt ? post.expiresAt : ""
|
|
|
|
)
|
|
|
|
if (expirationDate < new Date()) {
|
|
|
|
if (!isAuthor) {
|
|
|
|
router.push("/expired")
|
|
|
|
}
|
|
|
|
clearInterval(interval!)
|
|
|
|
}
|
|
|
|
}, 4000)
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
if (interval) clearInterval(interval)
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-11-14 04:28:40 -05:00
|
|
|
}, [isAuthor, post.expiresAt, 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 = () => {
|
2022-11-12 02:59:33 -05:00
|
|
|
router.push(`/post/${post.parentId}`)
|
2022-04-14 20:18:47 -04:00
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const isAvailable = !isProtected && post.title
|
2022-04-12 19:48:12 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
2022-11-11 22:17:44 -05:00
|
|
|
<>
|
2022-11-12 04:28:06 -05:00
|
|
|
{!isAvailable && <PasswordModalPage setPost={setPost} postId={post.id} />}
|
2022-11-12 02:59:33 -05:00
|
|
|
<div className={styles.header}>
|
|
|
|
<span className={styles.buttons}>
|
2022-11-28 21:33:06 -05:00
|
|
|
<ButtonGroup verticalIfMobile>
|
2022-11-12 02:59:33 -05:00
|
|
|
<Button
|
2022-11-16 05:16:56 -05:00
|
|
|
iconLeft={<Edit />}
|
2022-11-12 02:59:33 -05:00
|
|
|
onClick={editACopy}
|
|
|
|
style={{ textTransform: "none" }}
|
2022-04-09 20:48:19 -04:00
|
|
|
>
|
2022-11-12 02:59:33 -05:00
|
|
|
Edit a Copy
|
|
|
|
</Button>
|
2022-11-12 04:28:06 -05:00
|
|
|
{post.parentId && (
|
2022-11-29 03:43:04 -05:00
|
|
|
<Button iconLeft={<ArrowUpCircle />} onClick={viewParentClick}>
|
2022-11-12 02:59:33 -05:00
|
|
|
View Parent
|
2022-04-09 20:48:19 -04:00
|
|
|
</Button>
|
2022-11-12 02:59:33 -05:00
|
|
|
)}
|
|
|
|
<Button
|
|
|
|
onClick={download}
|
2022-11-16 05:16:56 -05:00
|
|
|
iconLeft={<Archive />}
|
2022-11-12 02:59:33 -05:00
|
|
|
style={{ textTransform: "none" }}
|
|
|
|
>
|
|
|
|
Download as ZIP Archive
|
|
|
|
</Button>
|
2022-11-16 05:16:56 -05:00
|
|
|
<FileDropdown files={post.files || []} />
|
2022-11-12 02:59:33 -05:00
|
|
|
</ButtonGroup>
|
|
|
|
</span>
|
|
|
|
<span className={styles.title}>
|
2022-11-16 05:16:56 -05:00
|
|
|
<h3>
|
|
|
|
{post.title}{" "}
|
|
|
|
<span style={{ color: "var(--gray)" }}>
|
|
|
|
by {post.author?.displayName}
|
|
|
|
</span>
|
|
|
|
</h3>
|
2022-11-12 02:59:33 -05:00
|
|
|
<span className={styles.badges}>
|
|
|
|
<VisibilityBadge visibility={visibility} />
|
|
|
|
<CreatedAgoBadge createdAt={post.createdAt} />
|
|
|
|
<ExpirationBadge postExpirationDate={post.expiresAt} />
|
2022-04-09 20:48:19 -04:00
|
|
|
</span>
|
2022-11-12 02:59:33 -05:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{post.description && (
|
|
|
|
<div>
|
2022-11-14 02:02:31 -05:00
|
|
|
<p>{post.description}</p>
|
2022-04-09 20:48:19 -04:00
|
|
|
</div>
|
2022-11-12 02:59:33 -05:00
|
|
|
)}
|
|
|
|
{/* {post.files.length > 1 && <FileTree files={post.files} />} */}
|
2022-11-12 19:06:23 -05:00
|
|
|
{post.files?.map(({ id, content, title, html }: File) => (
|
2022-11-12 02:59:33 -05:00
|
|
|
<DocumentComponent
|
|
|
|
key={id}
|
|
|
|
title={title}
|
|
|
|
initialTab={"preview"}
|
|
|
|
id={id}
|
|
|
|
content={content}
|
2022-11-12 19:06:23 -05:00
|
|
|
preview={html}
|
2022-11-12 02:59:33 -05:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{isAuthor && (
|
|
|
|
<span className={styles.controls}>
|
|
|
|
<VisibilityControl
|
|
|
|
postId={post.id}
|
|
|
|
visibility={visibility}
|
|
|
|
setVisibility={setVisibility}
|
2022-04-09 20:48:19 -04:00
|
|
|
/>
|
2022-11-12 02:59:33 -05:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
<ScrollToTop />
|
2022-11-11 22:17:44 -05:00
|
|
|
</>
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
2022-03-21 23:30:45 -04:00
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
export default PostPage
|