Skip to content

Commit

Permalink
fix(viewer): fix user profile & reply showing
Browse files Browse the repository at this point in the history
build(deps): bump nextjs version to 13.5.3
  • Loading branch information
immccn123 committed Sep 29, 2023
1 parent d80a777 commit d7861b9
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 163 deletions.
4 changes: 2 additions & 2 deletions packages/viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"highlight.js": "^11.8.0",
"jsdom": "^22.1.0",
"katex": "^0.16.8",
"next": "^13.5.2",
"next": "^13.5.3",
"puppeteer": "^21.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -38,7 +38,7 @@
"eslint": "^8.49.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-next": "^13.5.2",
"eslint-config-next": "^13.5.3",
"eslint-import-resolver-typescript": "^3.6.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-react": "^7.33.2",
Expand Down
22 changes: 6 additions & 16 deletions packages/viewer/src/app/r/[rid]/get-reply-raw.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { notFound } from "next/navigation";
import prisma from "@/lib/prisma";
import { selectReply } from "@/lib/reply";

export default async (id: number) =>
(await prisma.reply.findUnique({
select: {
id: true,
author: true,
time: true,
content: true,
discussion: {
...selectReply.withLatestContent,
...selectReply.withTakedown,
...selectReply.withBasic,
postId: undefined,
post: {
select: {
id: true,
snapshots: {
Expand All @@ -18,17 +19,6 @@ export default async (id: number) =>
},
},
},
takedown: {
select: {
submitter: {
select: {
id: true,
username: true,
},
},
reason: true,
},
},
},
where: { id },
})) ?? notFound();
2 changes: 1 addition & 1 deletion packages/viewer/src/app/r/[rid]/image/template-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export default (
position: relative;
"
>
${reply.content}
${reply.snapshots[0].content}
</div>
</div>
<div
Expand Down
36 changes: 16 additions & 20 deletions packages/viewer/src/app/r/[rid]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ import "@/components/markdown.css";
import Content from "@/components/replies/Content";
import UserInfo from "@/components/UserInfo";
import UserAvatar from "@/components/UserAvatar";
import serializeReply from "@/lib/serialize-reply";
// import serializeReply from "@/lib/serialize-reply";
import getReplyRaw from "./get-reply-raw";
import stringifyTime from "@/lib/time";

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</>;

// return <>抱歉,本功能暂未完成 TAT</>;
const id = parseInt(params.rid, 10);
if (Number.isNaN(id)) notFound();
const replyRaw = await getReplyRaw(id);
const reply = {
...replyRaw,
...(await serializeReply(replyRaw.discussion.id, replyRaw)),
};
const reply = await getReplyRaw(id);
const pages = Math.ceil(
(await prisma.reply.count({
where: { id: { lte: id }, discussionId: reply.discussion.id },
where: { id: { lte: id }, postId: reply.post.id },
})) / REPLIES_PER_PAGE,
);
// return redirect(`/${discussionId}/${pages}#${params.rid}`);
Expand All @@ -37,44 +33,44 @@ export default async function Page({ params }: { params: { rid: string } }) {
<div className="rounded-4 shadow-bssb">
<div className="px-md-4x px-4 pt-md-3x pt-2x pb-md-4x pb-4 position-relative">
<Content
discussionAuthor={reply.discussion.snapshots[0].authorId}
content={reply.content}
usersMetioned={reply.usersMetioned}
discussionAuthor={reply.post.snapshots[0].authorId}
content={reply.snapshots[0].content}
// usersMetioned={reply.usersMetioned}
/>
<span
className="text-end text-body-tertiary d-block d-md-none"
style={{ fontSize: ".8rem" }}
>
{reply.time}
{stringifyTime(reply.time)}
</span>
</div>
</div>
<div
className="position-absolute"
style={{ bottom: "-1.6em", left: ".8em" }}
>
<UserAvatar user={reply.author} size={72} />
<UserAvatar user={reply.snapshots[0].author} size={72} />
</div>
<div
className="ps-6 position-absolute reply-meta-bottom"
style={{ left: 0, right: 0 }}
>
<div>
<UserInfo user={reply.author} />
<UserInfo user={reply.snapshots[0].author} />
<span
className="float-end text-body-tertiary d-none d-md-inline"
style={{ marginRight: ".8em" }}
>
{reply.time}
{stringifyTime(reply.time)}
</span>
</div>
<div className="fw-medium text-body-tertiary d-none d-md-block">
于帖子{" "}
<Link
className="text-decoration-none"
href={`/${reply.discussion.id}/${pages}#${params.rid}`}
href={`/${reply.post.id}/${pages}#${params.rid}`}
>
{reply.discussion.snapshots[0].title}
{reply.post.snapshots[0].title}
<span className="fw-normal">(第 {pages} 页)</span>
</Link>
</div>
Expand All @@ -85,9 +81,9 @@ export default async function Page({ params }: { params: { rid: string } }) {
于帖子{" "}
<Link
className="text-decoration-none"
href={`/${reply.discussion.id}/${pages}#${params.rid}`}
href={`/${reply.post.id}/${pages}#${params.rid}`}
>
{reply.discussion.snapshots[0].title}
{reply.post.snapshots[0].title}
<span className="fw-normal">(第 {pages} 页)</span>
</Link>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default function UserDiscussions({ uid }: { uid: string }) {
<Content
discussionAuthor={discussion.snapshots[0].author.id}
content={discussion.content}
usersMetioned={discussion.usersMetioned}
// usersMetioned={discussion.usersMetioned}
/>
</DiscussionEntry>
)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import useSWRInfinite from "swr/infinite";
import InfiniteScroll from "react-infinite-scroll-component";
import { User } from "@prisma/client";
import fetcher from "@/lib/fetcher";
import Spinner from "@/components/Spinner";
import Ostracon from "@/components/Ostracon";
import type { LatestUser } from "@/lib/user";

interface PageData {
data: { time: string; user: User; content: string }[];
data: { time: string; user: LatestUser; content: string }[];
nextCursor: string;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/viewer/src/app/user/[uid]/judgements/data/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { NextResponse, type NextRequest } from "next/server";
import prisma from "@/lib/prisma";
import { NUM_PER_PAGE } from "../../constants";
import { selectUser } from "@/lib/user";

export async function GET(
request: NextRequest,
{ params }: { params: { uid: string } },
{ params }: { params: { uid: string } }
) {
const uid = parseInt(params.uid, 10);
const cursor = request.nextUrl.searchParams.get("cursor");
const judgements = await prisma.judgement.findMany({
select: {
time: true,
user: true,
user: { select: selectUser.withLatest },
content: true,
},
where: {
Expand Down
2 changes: 2 additions & 0 deletions packages/viewer/src/app/user/[uid]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import UserInfo from "@/components/UserInfo";
import "@/components/markdown.css";
import TabNavigation from "./TabNavigation";
import UserStatistics from "./UserStatistics";
import { selectUser } from "@/lib/user";

export async function generateMetadata({
params,
Expand All @@ -28,6 +29,7 @@ export default async function Layout({
const user =
(await prisma.user.findUnique({
where: { id: parseInt(params.uid, 10) },
select: selectUser.withLatest,
})) ?? notFound();
return (
<div className="row px-2 px-md-0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@ import Link from "next/link";
import useSWRInfinite from "swr/infinite";
import InfiniteScroll from "react-infinite-scroll-component";
import type { PostWithLatestContent } from "@/lib/post";
import type { UserMetioned } from "@/lib/serialize-reply";
// import type { UserMetioned } from "@/lib/serialize-reply";
import UserAvatar from "@/components/UserAvatar";
import UserInfo from "@/components/UserInfo";
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";
import { ReplyWithLatestContentPostMeta } from "@/lib/reply";
import stringifyTime from "@/lib/time";

interface PageData {
data: (PostWithLatestContent & {
content: string;
time: string;
usersMetioned: UserMetioned[];
} & {
replies: {
content: string;
time: string;
usersMetioned: UserMetioned[];
id: number;
discussionId: number;
authorId: number;
}[];
replies: ReplyWithLatestContentPostMeta[];
})[];
nextCursor: number;
}
Expand All @@ -45,7 +36,7 @@ export default function UserParticipated({
: `/user/${uid}/participated/data${
pageIndex ? `?cursor=${previousPageData.nextCursor}` : ""
}`,
fetcher,
fetcher
);

return (
Expand Down Expand Up @@ -136,7 +127,7 @@ export default function UserParticipated({
</summary>
<Content
discussionAuthor={discussion.snapshots[0].author.id}
content={discussion.content}
content={discussion.snapshots[0].content}
// usersMetioned={discussion.usersMetioned}
/>
</details>
Expand Down Expand Up @@ -185,7 +176,7 @@ export default function UserParticipated({
<div className="rounded-4 shadow-bssb-sm my-3x">
<div className="timeline-connect bg-light-bssb rounded-top-4 px-4 py-2">
<UserInfo user={user} />
{reply.authorId ===
{reply.snapshots[0].author.id ===
discussion.snapshots[0].author.id ? (
<span
className="ms-1 badge position-relative bg-teal d-inline-block"
Expand All @@ -202,7 +193,7 @@ export default function UserParticipated({
)}
<span className="float-end text-body-tertiary">
<span className="d-none d-md-inline">
{reply.time}
{stringifyTime(reply.time)}
</span>
<Link
href={`/r/${reply.id}`}
Expand Down Expand Up @@ -234,14 +225,14 @@ export default function UserParticipated({
discussionAuthor={
discussion.snapshots[0].author.id
}
content={reply.content}
content={reply.snapshots[0].content}
// usersMetioned={reply.usersMetioned}
/>
<span
className="text-end text-body-tertiary d-block d-md-none"
style={{ fontSize: ".8rem" }}
>
{reply.time}
{stringifyTime(reply.time)}
</span>
</div>
</div>
Expand Down Expand Up @@ -288,12 +279,12 @@ export default function UserParticipated({
<path d="m2.165 15.803.02-.004c1.83-.363 2.948-.842 3.468-1.105A9.06 9.06 0 0 0 8 15c4.418 0 8-3.134 8-7s-3.582-7-8-7-8 3.134-8 7c0 1.76.743 3.37 1.97 4.6a10.437 10.437 0 0 1-.524 2.318l-.003.011a10.722 10.722 0 0 1-.244.637c-.079.186.074.394.273.362a21.673 21.673 0 0 0 .693-.125zm.8-3.108a1 1 0 0 0-.287-.801C1.618 10.83 1 9.468 1 8c0-3.192 3.004-6 7-6s7 2.808 7 6c0 3.193-3.004 6-7 6a8.06 8.06 0 0 1-2.088-.272 1 1 0 0 0-.711.074c-.387.196-1.24.57-2.634.893a10.97 10.97 0 0 0 .398-2z" />
</svg>{" "}
{discussion.replyCount}
<span className="float-end">{discussion.time}</span>
<span className="float-end">{stringifyTime(discussion.time)}</span>
</div>
</div>
</div>
</div>
)),
))
)}
</InfiniteScroll>
{isValidating && <Spinner />}
Expand Down
27 changes: 5 additions & 22 deletions packages/viewer/src/app/user/[uid]/participated/data/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse, type NextRequest } from "next/server";
import prisma from "@/lib/prisma";
import { getPost } from "@/lib/post";
import serializeReply from "@/lib/serialize-reply";
// import serializeReply from "@/lib/serialize-reply";
import { getReply } from "@/lib/reply";
import { NUM_PER_PAGE } from "../../constants";

Expand All @@ -11,7 +11,7 @@ export async function GET(
) {
const uid = parseInt(params.uid, 10);
const cursor = request.nextUrl.searchParams.get("cursor");
const discussions = await prisma.post
const posts = await prisma.post
.findMany({
select: {
...getPost.latestWithContent,
Expand All @@ -34,27 +34,10 @@ export async function GET(
orderBy: { id: "desc" },
take: NUM_PER_PAGE,
})
.then((d) =>
Promise.all(
d.map(async (discussion) => ({
...discussion,
...(await serializeReply(discussion.id, {
content: discussion.snapshots[0].content,
time: discussion.time,
})),
replies: await Promise.all(
discussion.replies.map(async (reply) => ({
...reply,
...(await serializeReply(discussion.id, reply)),
})),
),
})),
),
);
return NextResponse.json({
data: discussions,
nextCursor: discussions.length
? discussions[discussions.length - 1].id
data: posts,
nextCursor: posts.length
? posts[posts.length - 1].id
: null,
});
}
Loading

0 comments on commit d7861b9

Please sign in to comment.