2022-11-12 02:59:33 -05:00
|
|
|
"use client"
|
|
|
|
|
2022-11-12 03:58:21 -05:00
|
|
|
import DocumentComponent from "./view-document"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-04-01 19:26:42 -04:00
|
|
|
import { useEffect, useState } from "react"
|
2022-11-12 02:59:33 -05:00
|
|
|
import { useRouter } from "next/navigation"
|
2022-04-12 19:48:12 -04:00
|
|
|
import PasswordModalPage from "./password-modal-wrapper"
|
2022-11-14 04:28:40 -05:00
|
|
|
import { File, PostWithFilesAndAuthor } from "@lib/server/prisma"
|
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-30 01:10:51 -05:00
|
|
|
|
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
|
|
|
|
2022-11-30 01:10:51 -05:00
|
|
|
if (isProtected) {
|
|
|
|
return <PasswordModalPage setPost={setPost} postId={post.id} />
|
|
|
|
}
|
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
2022-11-11 22:17:44 -05:00
|
|
|
<>
|
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
|
|
|
/>
|
|
|
|
))}
|
2022-12-04 04:31:51 -05:00
|
|
|
|
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
|