feat: support the redirection of the site page under the /site/ path

This commit is contained in:
DIYgod
2023-06-23 18:56:20 +01:00
parent 72630df35b
commit 0cd546e0a8
7 changed files with 69 additions and 30 deletions
+10 -4
View File
@@ -1,7 +1,7 @@
"use client"
import Link from "next/link"
import { useRouter } from "next/navigation"
import { usePathname, useRouter } from "next/navigation"
import { memo } from "react"
import reactStringReplace from "react-string-replace"
@@ -13,7 +13,7 @@ import { Button } from "~/components/ui/Button"
import { EmptyState } from "~/components/ui/EmptyState"
import { useIsClient } from "~/hooks/useClient"
import { useDate } from "~/hooks/useDate"
import { getSlugUrl } from "~/lib/helpers"
import { getSiteRelativeUrl } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { ExpandedNote } from "~/lib/types"
@@ -96,6 +96,7 @@ const PostItem = memo(
const isMounted = useIsClient()
const date = useDate()
const router = useRouter()
const pathname = usePathname()
return (
<FadeIn
@@ -106,7 +107,10 @@ const PostItem = memo(
>
<Link
className="flex flex-col sm:flex-row items-center group px-5 py-7 focus-visible:outline focus-visible:outline-accent"
href={getSlugUrl(`/${post.metadata?.content?.slug}`)}
href={getSiteRelativeUrl(
pathname,
`/${post.metadata?.content?.slug}`,
)}
suppressHydrationWarning
>
<PostCover
@@ -146,7 +150,9 @@ const PostItem = memo(
onClick={(e) => {
e.stopPropagation()
e.preventDefault()
router.push(`/tag/${tag}`)
router.push(
getSiteRelativeUrl(pathname, `/tag/${tag}`),
)
}}
>
#{tag}
+2 -7
View File
@@ -1,6 +1,7 @@
import { Time } from "~/components/common/Time"
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
import { EditButton } from "~/components/site/EditButton"
import PostTag from "~/components/site/PostTag"
import { UniLink } from "~/components/ui/UniLink"
import { CSB_SCAN } from "~/lib/env"
import { useTranslation } from "~/lib/i18n"
@@ -38,13 +39,7 @@ export default async function PostMeta({
{page.metadata?.content?.tags
?.filter((tag) => tag !== "post" && tag !== "page")
.map((tag) => (
<UniLink
className="hover:text-zinc-600"
key={tag}
href={`/tag/${tag}`}
>
<>#{tag}</>
</UniLink>
<PostTag key={tag} tag={tag} />
))}
</span>
</>
+20
View File
@@ -0,0 +1,20 @@
"use client"
import { usePathname } from "next/navigation"
import { getSiteRelativeUrl } from "~/lib/helpers"
import { UniLink } from "../ui/UniLink"
export default async function PostTag({ tag }: { tag: string }) {
const pathname = usePathname()
return (
<UniLink
className="hover:text-zinc-600"
href={getSiteRelativeUrl(pathname, `/tag/${tag}`)}
>
<>#{tag}</>
</UniLink>
)
}
+12 -3
View File
@@ -1,11 +1,12 @@
"use client"
import Link from "next/link"
import { useParams } from "next/navigation"
import { useParams, usePathname } from "next/navigation"
import { useMemo } from "react"
import { Button } from "~/components/ui/Button"
import { useDate } from "~/hooks/useDate"
import { getSiteRelativeUrl } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { ExpandedNote, PageVisibilityEnum } from "~/lib/types"
import { useGetPagesBySiteLite } from "~/queries/page"
@@ -19,6 +20,7 @@ export const SiteArchives = () => {
const date = useDate()
const { t } = useTranslation("site")
const { t: commonT } = useTranslation("common")
const pathname = usePathname()
const params = useParams()
if (params?.tag) {
params.tag = decodeURIComponent(params.tag as string)
@@ -96,7 +98,11 @@ export const SiteArchives = () => {
</h3>
<div className="pt-2">
{[...tags.keys()].map((tag) => (
<UniLink key={tag} href={`/tag/${tag}`} className="mr-6">
<UniLink
key={tag}
href={getSiteRelativeUrl(pathname, `/tag/${tag}`)}
className="mr-6"
>
<span className="align-middle">{tag}</span>
<span className="text-gray-400 text-sm ml-1 align-middle">
({tags.get(tag)})
@@ -119,7 +125,10 @@ export const SiteArchives = () => {
return (
<Link
key={post.transactionHash}
href={`/${post.metadata?.content?.slug}`}
href={getSiteRelativeUrl(
pathname,
`/${post.metadata?.content?.slug}`,
)}
className="flex justify-between items-center p-2 rounded-lg -mx-2 hover:bg-hover"
>
<span className="text-zinc-700">
+4
View File
@@ -2,6 +2,7 @@
import { usePathname } from "next/navigation"
import { getSiteRelativeUrl } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { cn } from "~/lib/utils"
@@ -17,6 +18,9 @@ export type HeaderLinkType = {
export const HeaderLink = ({ link }: { link: HeaderLinkType }) => {
const pathname = usePathname()
const { t } = useTranslation("site")
if (link.url) {
link.url = getSiteRelativeUrl(pathname, link.url)
}
const active = pathname === link.url
return (
+4 -1
View File
@@ -11,6 +11,7 @@ import { Image } from "~/components/ui/Image"
import { Modal } from "~/components/ui/Modal"
import { Tooltip } from "~/components/ui/Tooltip"
import { CSB_SCAN, CSB_XCHAR, CSB_XFEED } from "~/lib/env"
import { getSiteLink } from "~/lib/helpers"
import { useTranslation } from "~/lib/i18n/client"
import { Button } from "../ui/Button"
@@ -92,7 +93,9 @@ export const SiteHeaderMenu = ({
{
text: "Subscribe to RSS",
icon: <RssIcon className="w-full h-full text-[#ee832f]" />,
url: `/feed?format=xml`,
url: `${getSiteLink({
subdomain: handle || "",
})}/feed?format=xml`,
out: true,
},
{
+17 -15
View File
@@ -5,7 +5,6 @@ import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
import { IS_PROD, IS_VERCEL_PREVIEW } from "./constants"
import { OUR_DOMAIN } from "./env"
import { isServerSide } from "./utils"
export const getSiteLink = ({
domain,
@@ -28,20 +27,6 @@ export const getSiteLink = ({
return `${IS_PROD ? "https" : "http"}://${subdomain}.${OUR_DOMAIN}`
}
export const getSlugUrl = (slug: string) => {
if (!isServerSide() && IS_VERCEL_PREVIEW) {
const pathArr = new URL(location.href).pathname.split("/").filter(($) => $)
const indicatorIndex = pathArr.findIndex(($) => $ === "site")
if (-~indicatorIndex) {
const handle = pathArr[indicatorIndex + 1]
return `/site/${handle}${slug}`
}
}
return slug
}
export const getNoteSlug = (note: NoteEntity) => {
return encodeURIComponent(
note.metadata?.content?.attributes?.find(
@@ -85,3 +70,20 @@ export const getTwitterShareUrl = ({
),
)}`
}
export const getSiteRelativeUrl = (pathname: string, address: string) => {
if (!address.startsWith("/")) {
address = `/${address}`
}
const reg = /\/site\/[^/]*/
if (address.match(reg) || address.match(/^https?:\/\//)) {
return address
} else {
const match = pathname.match(reg)
if (match?.[0]) {
return `${match[0]}${address === "/" ? "" : address}`
} else {
return address
}
}
}