client: remove get-post-path and usage of it, fix view parent btn on post page not working due to SSR

This commit is contained in:
Max Leiter 2022-04-14 17:18:47 -07:00
parent 683cad2a8d
commit 454ea303a6
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
6 changed files with 21 additions and 41 deletions

View file

@ -17,6 +17,7 @@ const Description = ({ onChange, description }: props) => {
label="Description" label="Description"
maxLength={256} maxLength={256}
width="100%" width="100%"
placeholder="A short description of your post"
/> />
</div> </div>
) )

View file

@ -2,9 +2,7 @@ import {
Button, Button,
useToasts, useToasts,
ButtonDropdown, ButtonDropdown,
Toggle,
Input, Input,
useClickAway
} from "@geist-ui/core" } from "@geist-ui/core"
import { useRouter } from "next/router" import { useRouter } from "next/router"
import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { useCallback, useEffect, useMemo, useRef, useState } from "react"
@ -19,7 +17,6 @@ import type {
Document as DocumentType Document as DocumentType
} from "@lib/types" } from "@lib/types"
import PasswordModal from "./password-modal" import PasswordModal from "./password-modal"
import getPostPath from "@lib/get-post-path"
import EditDocumentList from "@components/edit-document-list" import EditDocumentList from "@components/edit-document-list"
import { ChangeEvent } from "react" import { ChangeEvent } from "react"
import DatePicker from "react-datepicker" import DatePicker from "react-datepicker"
@ -99,7 +96,7 @@ const Post = ({
if (res.ok) { if (res.ok) {
const json = await res.json() const json = await res.json()
router.push(getPostPath(json.visibility, json.id)) router.push(`/post/${json.id}`)
} else { } else {
const json = await res.json() const json = await res.json()
setToast({ setToast({

View file

@ -1,6 +1,5 @@
import NextLink from "next/link" import NextLink from "next/link"
import VisibilityBadge from "../badges/visibility-badge" import VisibilityBadge from "../badges/visibility-badge"
import getPostPath from "@lib/get-post-path"
import { import {
Link, Link,
Text, Text,
@ -36,6 +35,10 @@ const ListItem = ({
router.push(`/new/from/${post.id}`) router.push(`/new/from/${post.id}`)
} }
const viewParentClick = () => {
router.push(`/post/${post.parent?.id}`)
}
return ( return (
<FadeIn> <FadeIn>
<li key={post.id}> <li key={post.id}>
@ -44,7 +47,8 @@ const ListItem = ({
<Text h3 className={styles.title}> <Text h3 className={styles.title}>
<NextLink <NextLink
passHref={true} passHref={true}
href={getPostPath(post.visibility, post.id)} href={`/post/[id]`}
as={`/post/${post.id}`}
> >
<Link color marginRight={"var(--gap)"}> <Link color marginRight={"var(--gap)"}>
{post.title} {post.title}
@ -57,14 +61,7 @@ const ListItem = ({
<Button <Button
auto auto
icon={<Parent />} icon={<Parent />}
onClick={() => onClick={viewParentClick}
router.push(
getPostPath(
post.parent!.visibility,
post.parent!.id
)
)
}
/> />
</Tooltip> </Tooltip>
)} )}
@ -102,9 +99,7 @@ const ListItem = ({
<div key={file.id}> <div key={file.id}>
<Link <Link
color color
href={`${getPostPath(post.visibility, post.id)}#${ href={`/post/${post.id}#${file.title}`}
file.title
}`}
> >
{file.title || "Untitled file"} {file.title || "Untitled file"}
</Link> </Link>

View file

@ -16,7 +16,6 @@ import { useRouter } from "next/router"
import ExpirationBadge from "@components/badges/expiration-badge" import ExpirationBadge from "@components/badges/expiration-badge"
import CreatedAgoBadge from "@components/badges/created-ago-badge" import CreatedAgoBadge from "@components/badges/created-ago-badge"
import Cookies from "js-cookie" import Cookies from "js-cookie"
import getPostPath from "@lib/get-post-path"
import PasswordModalPage from "./password-modal-wrapper" import PasswordModalPage from "./password-modal-wrapper"
import VisibilityControl from "@components/badges/visibility-control" import VisibilityControl from "@components/badges/visibility-control"
@ -86,6 +85,12 @@ const PostPage = ({ post: initialPost, isProtected }: Props) => {
router.push(`/new/from/${post.id}`) router.push(`/new/from/${post.id}`)
} }
const viewParentClick = () => {
router.push(
`/post/${post.parent!.id}`
)
}
if (isLoading) { if (isLoading) {
return <></> return <></>
} }
@ -122,11 +127,7 @@ const PostPage = ({ post: initialPost, isProtected }: Props) => {
<Button <Button
auto auto
icon={<Parent />} icon={<Parent />}
onClick={() => onClick={viewParentClick}
router.push(
getPostPath(post.parent!.visibility, post.parent!.id)
)
}
> >
View Parent View Parent
</Button> </Button>

View file

@ -1,16 +0,0 @@
import type { PostVisibility } from "./types"
export default function getPostPath(visibility: PostVisibility, id: string) {
switch (visibility) {
case "private":
// return `/post/private/${id}`
case "protected":
// return `/post/protected/${id}`
case "unlisted":
case "public":
return `/post/${id}`
default:
console.error(`Unknown visibility: ${visibility}`)
return `/post/${id}`
}
}

View file

@ -1,4 +1,4 @@
import type { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next" import type { GetServerSideProps } from "next"
import type { Post } from "@lib/types" import type { Post } from "@lib/types"
import PostPage from "@components/post-page" import PostPage from "@components/post-page"
@ -69,9 +69,11 @@ export const getServerSideProps: GetServerSideProps = async ({
} }
} }
return { return {
props: { props: {
post: json post: json,
key: params?.id
} }
} }
} }