Skip to content

Commit

Permalink
fix(viewer): post display interface
Browse files Browse the repository at this point in the history
- migrate: database init

Please notice that HUSKY is not passed yet. It cannot be built without errors. Not ready for production use yet.
  • Loading branch information
immccn123 committed Sep 29, 2023
1 parent 3858e9e commit d80a777
Show file tree
Hide file tree
Showing 23 changed files with 1,235 additions and 372 deletions.
Empty file removed notice.md
Empty file.
3 changes: 3 additions & 0 deletions packages/viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-infinite-scroll-component": "^6.1.0",
"react-markdown": "^9.0.0",
"rehype-katex": "^7.0.0",
"remark-math": "^6.0.0",
"socket.io-client": "^4.7.2",
"swr": "^2.2.2"
},
Expand Down
43 changes: 10 additions & 33 deletions packages/viewer/src/app/[id]/[page]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { notFound } from "next/navigation";
import prisma from "@/lib/prisma";
import paginate from "@/lib/pagination";
import PageButtons from "@/components/replies/PageButtons";
import serializeReply from "@/lib/serialize-reply";
// import serializeReply from "@/lib/serialize-reply";
import Reply from "@/components/replies/Reply";
import { selectReply } from "@/lib/reply";
import { selectPost } from "@/lib/post";

const REPLIES_PER_PAGE = parseInt(process.env.REPLIES_PER_PAGE ?? "10", 10);

Expand All @@ -16,35 +18,19 @@ export default async function Page({
const page = parseInt(params.page, 10);
if (Number.isNaN(page)) notFound();
const {
snapshots: [{ authorId }],
snapshots,
replies,
_count: { replies: numReplies },
} = await prisma.post
.findUnique({
where: { id },
select: {
snapshots: {
select: { authorId: true },
orderBy: { time: "desc" },
take: 1,
},
...selectPost.withLatestSnapshotMeta,
replies: {
select: {
id: true,
author: true,
time: true,
content: true,
takedown: {
select: {
submitter: {
select: {
id: true,
username: true,
},
},
reason: true,
},
},
...selectReply.withBasic,
...selectReply.withTakedown,
...selectReply.withLatestContent,
},
orderBy: { id: "asc" },
skip: (page - 1) * REPLIES_PER_PAGE,
Expand All @@ -53,24 +39,15 @@ export default async function Page({
_count: { select: { replies: true } },
},
})
.then((discussion) => discussion ?? notFound())
.then(async (discussion) => ({
...discussion,
replies: await Promise.all(
discussion.replies.map(async (reply) => ({
...reply,
...(await serializeReply(id, reply)),
})),
),
}));
.then((post) => post ?? notFound())
const numPages = Math.ceil(numReplies / REPLIES_PER_PAGE);
const { pagesLocalAttachedFront, pagesLocalAttachedBack, pagesLocal } =
paginate(numPages, page);

return (
<>
{replies.map((reply) => (
<Reply discussion={{ id, authorId }} reply={reply} key={reply.id} />
<Reply post={{ id, authorId: snapshots[0].author.id }} reply={reply} key={reply.id} />
))}
{numPages > 1 && (
<div className="bg-body rounded-4 shadow-bssb my-4s px-4 py-3 py-md-4 text-center">
Expand Down
63 changes: 23 additions & 40 deletions packages/viewer/src/app/[id]/context/[user]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { NextRequest, NextResponse } from "next/server";
import { selectReply } from "@/lib/reply";
import prisma from "@/lib/prisma";
import serializeReply from "@/lib/serialize-reply";
// import serializeReply from "@/lib/serialize-reply";

export async function GET(
request: NextRequest,
{ params }: { params: { id: string; user: string } },
{ params }: { params: { id: string; user: string } }
) {
const discussionId = parseInt(params.id, 10);
const postId = parseInt(params.id, 10);
const authorId = parseInt(params.user, 10);
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const replyId = parseInt(request.nextUrl.searchParams.get("reply")!, 10);
Expand All @@ -15,54 +16,36 @@ export async function GET(
const reply = await (offset <= 0
? prisma.reply.findFirst({
select: {
id: true,
author: true,
time: true,
content: true,
discussionId: true,
takedown: {
select: {
submitter: {
select: {
id: true,
username: true,
},
},
reason: true,
},
...selectReply.withBasic,
...selectReply.withTakedown,
...selectReply.withLatestContent,
},
where: {
postId,
snapshots: {
some: { authorId },
},
id: { lt: replyId },
},
where: { discussionId, authorId, id: { lt: replyId } },
orderBy: { id: "desc" },
skip: -offset,
})
: prisma.reply.findFirst({
select: {
id: true,
author: true,
time: true,
content: true,
discussionId: true,
takedown: {
select: {
submitter: {
select: {
id: true,
username: true,
},
},
reason: true,
},
...selectReply.withBasic,
...selectReply.withTakedown,
...selectReply.withLatestContent,
},
where: {
postId,
snapshots: {
some: { authorId },
},
id: { gt: replyId },
},
where: { discussionId, authorId, id: { gt: replyId } },
orderBy: { id: "asc" },
skip: offset - 1,
}));
if (reply)
return NextResponse.json({
...reply,
...(await serializeReply(discussionId, reply)),
});
if (reply) return NextResponse.json({ reply });
return NextResponse.json({});
}
45 changes: 17 additions & 28 deletions packages/viewer/src/app/[id]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import prisma from "@/lib/prisma";
import { getDiscussionUrl, getForumName, getForumUrl } from "@/lib/luogu";
import { getPostUrl, getForumUrl } from "@/lib/luogu";
import stringifyTime from "@/lib/time";
import UserInfo from "@/components/UserInfo";
import "@/components/markdown.css";
import UpdateButton from "@/components/UpdateButton";
import serializeReply from "@/lib/serialize-reply";
import Reply from "@/components/replies/Reply";
import { selectPost } from "@/lib/post";

export async function generateMetadata({
params,
Expand All @@ -16,16 +16,14 @@ export async function generateMetadata({
}): Promise<Metadata> {
const id = parseInt(params.id, 10);
if (Number.isNaN(id)) notFound();
const snapshot = await prisma.snapshot.findFirst({
select: { title: true, discussion: { select: { takedown: true } } },
const snapshot = await prisma.postSnapshot.findFirst({
select: { title: true, post: { select: { takedown: true } } },
orderBy: { time: "desc" },
where: { discussionId: id },
where: { postId: id },
});
return {
title: `${
snapshot && !snapshot.discussion.takedown
? `「${snapshot.title}」`
: "404"
snapshot && !snapshot.post.takedown ? `「${snapshot.title}」` : "404"
} - 洛谷帖子保存站`,
};
}
Expand All @@ -42,23 +40,12 @@ export default async function Page({
snapshots: [{ title, forum, author, content, until: updatedAt }],
_count: { replies },
takedown,
} = (await prisma.discussion.findUnique({
} = (await prisma.post.findUnique({
where: { id },
select: {
time: true,
replyCount: true,
snapshots: {
select: {
until: true,
title: true,
forum: true,
author: true,
content: true,
},
orderBy: { time: "desc" },
take: 1,
},
takedown: { select: { reason: true, submitter: true } },
...selectPost.withBasic,
...selectPost.withLatestContent,
...selectPost.withTakedown,
_count: { select: { replies: true } },
},
})) ?? notFound();
Expand Down Expand Up @@ -87,7 +74,7 @@ export default async function Page({
target="_blank"
rel="noopener noreferrer"
>
{getForumName(forum)}
{forum.name}
</a>
</li>
<li className="d-flex justify-content-between lh-lg">
Expand Down Expand Up @@ -118,7 +105,7 @@ export default async function Page({
<div className="mt-2 mb-1">
<a
className="btn btn-outline-secondary shadow-bssb-sm"
href={getDiscussionUrl(parseInt(params.id, 10))}
href={getPostUrl(parseInt(params.id, 10))}
target="_blank"
rel="noopener noreferrer"
>
Expand All @@ -135,10 +122,12 @@ export default async function Page({
{title}
</div>
<Reply
discussion={{ id, authorId: author.id }}
post={{ id, authorId: author.id }}
reply={{
author,
...(await serializeReply(id, { content, time })),
time,
id: -1,
postId: id,
snapshots: [{ content, time, author }],
}}
/>
{children}
Expand Down
9 changes: 2 additions & 7 deletions packages/viewer/src/app/[id]/replies/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import serializeReply from "@/lib/serialize-reply";
// import serializeReply from "@/lib/serialize-reply";

import { selectReply } from "@/lib/reply";

Expand All @@ -24,12 +24,7 @@ export async function GET(
take: parseInt(limit ?? "10", 10),
});
return NextResponse.json({
data: await Promise.all(
replies.map(async (reply) => ({
...reply,
...(await serializeReply(id, reply)),
})),
),
replies,
nextCursor: replies.length ? replies[replies.length - 1].id : null,
});
}
4 changes: 4 additions & 0 deletions packages/viewer/src/app/r/[rid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable
// Not impl yet
import { notFound } from "next/navigation";
import Link from "next/link";
import prisma from "@/lib/prisma";
Expand All @@ -13,6 +15,8 @@ export const metadata = { title: "金玉良言 - 洛谷帖子保存站" };
const REPLIES_PER_PAGE = parseInt(process.env.REPLIES_PER_PAGE ?? "10", 10);

export default async function Page({ params }: { params: { rid: string } }) {
return <>抱歉,本功能暂未完成 TAT</>;

const id = parseInt(params.rid, 10);
if (Number.isNaN(id)) notFound();
const replyRaw = await getReplyRaw(id);
Expand Down
2 changes: 2 additions & 0 deletions packages/viewer/src/app/user/[uid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { notFound } from "next/navigation";
import prisma from "@/lib/prisma";
import UserParticipated from "./participated/UserParticipated";
import { selectUser } from "@/lib/user";

export default async function Page({ params }: { params: { uid: string } }) {
const user =
(await prisma.user.findUnique({
where: { id: parseInt(params.uid, 10) },
select: selectUser.withLatest,
})) ?? notFound();
return <UserParticipated uid={params.uid} user={user} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import Link from "next/link";
import useSWRInfinite from "swr/infinite";
import InfiniteScroll from "react-infinite-scroll-component";
import type { User } from "@prisma/client";
import type { PostWithLatestContent } from "@/lib/post";
import type { UserMetioned } from "@/lib/serialize-reply";
import UserAvatar from "@/components/UserAvatar";
Expand All @@ -12,6 +11,7 @@ import Content from "@/components/replies/Content";
import fetcher from "@/lib/fetcher";
import Spinner from "@/components/Spinner";
import { NUM_MAX_REPLIES_SHOWED_DEFAULT } from "../constants";
import { LatestUser } from "@/lib/user";

interface PageData {
data: (PostWithLatestContent & {
Expand All @@ -36,7 +36,7 @@ export default function UserParticipated({
user,
}: {
uid: string;
user: User;
user: LatestUser;
}) {
const { data, size, setSize, isValidating } = useSWRInfinite<PageData>(
(pageIndex: number, previousPageData: PageData) =>
Expand Down Expand Up @@ -137,7 +137,7 @@ export default function UserParticipated({
<Content
discussionAuthor={discussion.snapshots[0].author.id}
content={discussion.content}
usersMetioned={discussion.usersMetioned}
// usersMetioned={discussion.usersMetioned}
/>
</details>
) : undefined}
Expand Down Expand Up @@ -235,7 +235,7 @@ export default function UserParticipated({
discussion.snapshots[0].author.id
}
content={reply.content}
usersMetioned={reply.usersMetioned}
// usersMetioned={reply.usersMetioned}
/>
<span
className="text-end text-body-tertiary d-block d-md-none"
Expand Down
2 changes: 1 addition & 1 deletion packages/viewer/src/app/user/[uid]/replies/UserReplies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function UserReplies({ uid }: { uid: string }) {
<div key={reply.id}>
<Reply
reply={reply}
discussion={{
post={{
id: reply.discussion.id,
authorId: reply.discussion.snapshots[0].authorId,
}}
Expand Down
Loading

0 comments on commit d80a777

Please sign in to comment.