feat: site page parallel routes

This commit is contained in:
DIYgod
2023-06-27 00:06:39 +01:00
parent 00b0a17285
commit ba41f6b504
11 changed files with 217 additions and 53 deletions
+1
View File
@@ -10,6 +10,7 @@ generated/
test-results/
tsconfig.tsbuildinfo
.eslintcache
server.js
# IDE Specific
.idea
+1 -1
View File
@@ -13,7 +13,7 @@ export default async function HomeLayout({
}) {
return (
<>
<header className="py-5 fixed w-full top-0 bg-white z-10">
<header className="py-5 fixed w-full top-0 bg-white z-[1]">
<div className="max-w-screen-lg px-5 mx-auto flex justify-between items-center">
<UniLink
href="/"
@@ -0,0 +1,78 @@
import { notFound } from "next/navigation"
import { Hydrate, dehydrate } from "@tanstack/react-query"
import PageContent from "~/components/common/PageContent"
import PostModal from "~/components/home/PostModal"
import { PostFooter } from "~/components/site/PostFooter"
import PostMeta from "~/components/site/PostMeta"
import getQueryClient from "~/lib/query-client"
import { fetchGetPage } from "~/queries/page.server"
import { fetchGetSite } from "~/queries/site.server"
export default async function SiteModal({
params,
}: {
params: {
site: string
slug: string
}
}) {
const queryClient = getQueryClient()
const site = await fetchGetSite(params.site, queryClient)
const page = await fetchGetPage(
{
characterId: site?.characterId,
slug: params.slug,
useStat: true,
},
queryClient,
)
if (
!page ||
new Date(page!.metadata?.content?.date_published || "") > new Date()
) {
notFound()
}
const dehydratedState = dehydrate(queryClient)
return (
<PostModal>
<div>
<article>
<div>
{page?.metadata?.content?.tags?.includes("post") ? (
<h2 className="xlog-post-title text-4xl font-bold leading-tight">
{page.metadata?.content?.title}
</h2>
) : (
<h2 className="xlog-post-title text-xl font-bold page-title">
{page?.metadata?.content?.title}
</h2>
)}
{page?.metadata?.content?.tags?.includes("post") && (
<PostMeta page={page} site={site} />
)}
</div>
<PageContent
className="mt-10"
content={page?.metadata?.content?.content}
toc={false}
page={page}
site={site}
withActions={false}
/>
</article>
<Hydrate state={dehydratedState}>
{page?.metadata && (
<PostFooter page={page} site={site} fixHeight={true} />
)}
</Hydrate>
</div>
</PostModal>
)
}
+3
View File
@@ -0,0 +1,3 @@
export default function Default() {
return null
}
+3
View File
@@ -0,0 +1,3 @@
export default function Default() {
return null
}
+6 -1
View File
@@ -76,8 +76,10 @@ export const metadata: Metadata = {
export default function RootLayout({
children,
modal,
}: {
children: React.ReactNode
modal: React.ReactNode
}) {
const lang = useAcceptLang()
const colorScheme = getColorScheme()
@@ -95,7 +97,10 @@ export default function RootLayout({
return (
<html lang={lang} dir={dir(lang)} className={colorScheme}>
<body>
<Providers lang={lang}>{children}</Providers>
<Providers lang={lang}>
{modal}
{children}
</Providers>
<Toaster />
</body>
</html>
+12 -1
View File
@@ -1,5 +1,6 @@
"use client"
import { useState } from "react"
// TODO
import { Virtuoso } from "react-virtuoso"
@@ -13,9 +14,11 @@ import { useGetComments } from "~/queries/page"
const Comment = ({
page,
className,
fixHeight,
}: {
page?: ExpandedNote
className?: string
fixHeight?: boolean
}) => {
const comments = useGetComments({
characterId: page?.characterId,
@@ -27,6 +30,8 @@ const Comment = ({
(page) => (page?.list?.length ?? 0) > 0,
)
const [totalListHeight, setTotalListHeight] = useState(10)
return (
<div
className={cn("xlog-comment comment", className)}
@@ -52,7 +57,13 @@ const Comment = ({
<Virtuoso
className="xlog-comment-list"
useWindowScroll
style={{
height: fixHeight ? totalListHeight : undefined,
}}
useWindowScroll={!fixHeight}
totalListHeightChanged={(height) => {
setTotalListHeight(height + 71)
}}
data={data}
endReached={() => comments.hasNextPage && comments.fetchNextPage()}
components={{
+1 -2
View File
@@ -43,8 +43,7 @@ const PostCard = ({
return (
<Link
target="_blank"
href={`/api/redirection?characterId=${post.characterId}&noteId=${post.noteId}`}
href={`/site/${post.character?.handle}/${post.metadata?.content?.slug}`}
className={cn(
"xlog-post sm:hover:bg-hover transition-all p-4 ml-10 sm:rounded-xl flex flex-col sm:flex-row items-center hover:opacity-100 group",
simple && "opacity-90",
+25
View File
@@ -0,0 +1,25 @@
"use client"
import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"
import { Modal } from "~/components/ui/Modal"
export default function SiteModal({ children }: { children: JSX.Element }) {
const router = useRouter()
const [open, setOpen] = useState(false)
useEffect(() => {
setOpen(true)
}, [])
return (
<Modal
open={open}
panelClassName="max-w-[888px] py-12 px-20"
zIndex={9}
setOpen={() => router.back()}
>
{children}
</Modal>
)
}
+3 -1
View File
@@ -51,9 +51,11 @@ export const usePostFooterInView = () => {
export const PostFooter = ({
page,
site,
fixHeight,
}: {
page?: ExpandedNote
site?: ExpandedCharacter
fixHeight?: boolean
}) => {
const actionElRef = useRef<HTMLDivElement>(null)
const [isInView] = useInViewport(actionElRef)
@@ -87,7 +89,7 @@ export const PostFooter = ({
<ReactionShare noteId={page?.noteId} />
</div>
{inited || isInView ? (
<DynamicComment page={page} />
<DynamicComment page={page} fixHeight={fixHeight} />
) : (
<CommentSkeleton />
)}
+84 -47
View File
@@ -1,7 +1,8 @@
import clsx from "clsx"
import React, { forwardRef } from "react"
"use client"
import { Dialog } from "@headlessui/react"
import React, { Fragment, forwardRef } from "react"
import { Dialog, Transition } from "@headlessui/react"
import { cn } from "~/lib/utils"
@@ -13,56 +14,92 @@ interface ModalProps {
titleIcon?: React.ReactNode
size?: "md" | "lg" | "sm"
zIndex?: number
panelClassName?: string
}
export const Modal = forwardRef<HTMLDivElement, ModalProps>(
({ open, setOpen, children, title, titleIcon, size = "md", zIndex }, ref) => {
(
{
open,
setOpen,
children,
title,
titleIcon,
size = "md",
zIndex,
panelClassName,
},
ref,
) => {
return (
<Dialog
open={open}
onClose={() => setOpen(false)}
className={clsx("relative", zIndex ? `z-${zIndex}` : "z-10")}
>
{/* The backdrop, rendered as a fixed sibling to the panel container */}
<div className="fixed inset-0 bg-black/30 z-40" aria-hidden={true} />
{/* Full-screen container to center the panel */}
<div
className={cn(
`fixed inset-0 flex items-center justify-center p-4 z-40`,
)}
ref={ref}
<Transition appear show={open} as={Fragment}>
<Dialog
onClose={() => setOpen(false)}
className="relative"
style={{
zIndex: zIndex ? zIndex : 10,
}}
>
{/* The actual dialog panel */}
<Dialog.Panel
className={cn(
`mx-auto rounded-lg bg-white w-full shadow-modal max-h-full overflow-y-auto flex flex-col`,
size === "md"
? `max-w-md`
: size === "lg"
? `max-w-lg`
: `max-w-sm`,
)}
<Transition.Child
as={Fragment}
enter="ease-out duration-100"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-100"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
{title && (
<Dialog.Title className="text-lg border-b h-14 flex items-center px-5 space-x-2 py-4 relative">
{titleIcon && <span>{titleIcon}</span>}
<span className="truncate flex items-center w-full">
{title}
</span>
<span
className="absolute right-1 w-7 h-7 text-xl cursor-pointer bg-white flex items-center justify-center"
onClick={() => setOpen(false)}
>
<i className="icon-[mingcute--close-line] inline-block" />
</span>
</Dialog.Title>
)}
{children}
</Dialog.Panel>
</div>
</Dialog>
<div
className="fixed inset-0 bg-black bg-opacity-25 z-40"
aria-hidden={true}
/>
</Transition.Child>
<div
className="fixed inset-0 flex items-center justify-center p-8 z-40"
ref={ref}
>
<Transition.Child
as={Fragment}
enter="ease-out duration-100"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-100"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
{/* The actual dialog panel */}
<Dialog.Panel
className={cn(
`mx-auto rounded-lg bg-white w-full shadow-modal max-h-full overflow-y-auto flex flex-col`,
size === "md"
? `max-w-md`
: size === "lg"
? `max-w-lg`
: `max-w-sm`,
panelClassName,
)}
>
{title && (
<Dialog.Title className="text-lg border-b h-14 flex items-center px-5 space-x-2 py-4 relative">
{titleIcon && <span>{titleIcon}</span>}
<span className="truncate flex items-center w-full">
{title}
</span>
<span
className="absolute right-1 w-7 h-7 text-xl cursor-pointer bg-white flex items-center justify-center"
onClick={() => setOpen(false)}
>
<i className="icon-[mingcute--close-line] inline-block" />
</span>
</Dialog.Title>
)}
{children}
</Dialog.Panel>
</Transition.Child>
</div>
</Dialog>
</Transition>
)
},
)