client: redirect on page expiration if not author
This commit is contained in:
parent
76e7bb8013
commit
88d14a40b1
2 changed files with 29 additions and 17 deletions
|
@ -30,10 +30,11 @@ const ExpirationBadge = ({
|
|||
}, [expirationDate])
|
||||
|
||||
const isExpired = useMemo(() => {
|
||||
return expirationDate && new Date(expirationDate) < new Date()
|
||||
}, [expirationDate])
|
||||
return timeUntilString && timeUntilString === "in 0 seconds"
|
||||
}, [timeUntilString])
|
||||
|
||||
useEffect(() => {
|
||||
// check if expired every
|
||||
if (isExpired) {
|
||||
if (onExpires) {
|
||||
onExpires();
|
||||
|
|
|
@ -23,6 +23,32 @@ type Props = {
|
|||
|
||||
const PostPage = ({ post }: Props) => {
|
||||
const router = useRouter()
|
||||
|
||||
const isMobile = useMediaQuery("mobile")
|
||||
const [isExpired, setIsExpired] = useState(post.expiresAt ? new Date(post.expiresAt) < new Date() : null)
|
||||
|
||||
useEffect(() => {
|
||||
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)
|
||||
}
|
||||
}, [post.expiresAt])
|
||||
|
||||
const onExpires = useCallback(() => {
|
||||
const isOwner = post.users ? post.users[0].id === Cookies.get("drift-userid") : false
|
||||
|
||||
if (isExpired && !isOwner) {
|
||||
router.push("/expired")
|
||||
return <></>
|
||||
}
|
||||
}, [isExpired, post.users, router])
|
||||
|
||||
const download = async () => {
|
||||
const downloadZip = (await import("client-zip")).downloadZip
|
||||
const blob = await downloadZip(post.files.map((file: any) => {
|
||||
|
@ -40,21 +66,6 @@ const PostPage = ({ post }: Props) => {
|
|||
}
|
||||
|
||||
|
||||
const isMobile = useMediaQuery("mobile")
|
||||
|
||||
const isExpired = useMemo(() => {
|
||||
return post.expiresAt && new Date(post.expiresAt) < new Date()
|
||||
}, [post.expiresAt])
|
||||
|
||||
const onExpires = useCallback(() => {
|
||||
const isOwner = post.users ? post.users[0].id === Cookies.get("drift-userid") : false
|
||||
|
||||
if (isExpired && !isOwner) {
|
||||
router.push("/expired")
|
||||
return <></>
|
||||
}
|
||||
}, [isExpired, post.users, router])
|
||||
|
||||
return (
|
||||
<Page width={"100%"}>
|
||||
<PageSeo
|
||||
|
|
Loading…
Reference in a new issue