CoastalCommitsPastes/client/app/(posts)/post/[id]/components/post-page/index.tsx

78 lines
1.7 KiB
TypeScript
Raw Normal View History

"use client"
2022-11-12 03:58:21 -05:00
import DocumentComponent from "./view-document"
import { useEffect, useState } from "react"
import { useRouter } from "next/navigation"
import PasswordModalPage from "./password-modal-wrapper"
import { File, PostWithFilesAndAuthor } from "@lib/server/prisma"
type Props = {
post: string | PostWithFilesAndAuthor
isProtected?: boolean
isAuthor?: boolean
}
const PostPage = ({ post: initialPost, isProtected, isAuthor }: Props) => {
const [post, setPost] = useState<PostWithFilesAndAuthor>(
typeof initialPost === "string" ? JSON.parse(initialPost) : initialPost
2022-04-09 20:48:19 -04:00
)
const router = useRouter()
2022-04-09 20:48:19 -04:00
useEffect(() => {
if (post.expiresAt) {
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 : "")
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
}
}, [isAuthor, post.expiresAt, router])
2022-04-09 20:48:19 -04:00
if (isProtected) {
return <PasswordModalPage setPost={setPost} postId={post.id} />
}
2022-04-09 20:48:19 -04:00
return (
<>
{post.files?.map(({ id, content, title, html }: File) => (
<DocumentComponent
key={id}
title={title}
initialTab={"preview"}
id={id}
content={content}
preview={html}
/>
))}
2022-12-04 04:31:51 -05:00
</>
2022-04-09 20:48:19 -04:00
)
}
2022-04-09 20:48:19 -04:00
export default PostPage