Skip to content

Commit

Permalink
feat(viewer): redirect/link to legacy site
Browse files Browse the repository at this point in the history
- Notice that it is hard encoded in the code.
  • Loading branch information
immccn123 committed Oct 1, 2023
1 parent c7149d5 commit e5c73b0
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 45 deletions.
42 changes: 23 additions & 19 deletions packages/viewer/src/app/[id]/[page]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { notFound } from "next/navigation";
import { notFound, redirect } 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 Reply from "@/components/replies/Reply";
import { selectReply } from "@/lib/reply";
import { selectPost } from "@/lib/post";
import { checkExists } from "@/lib/utils";
import savedInLegacyList from "../saved-in-legacy.json";
// import serializeReply from "@/lib/serialize-reply";

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

Expand All @@ -17,29 +19,31 @@ export default async function Page({
const id = parseInt(params.id, 10);
const page = parseInt(params.page, 10);
if (Number.isNaN(page)) notFound();
const savedAtLegacy = checkExists(savedInLegacyList, id);
const {
snapshots,
replies,
_count: { replies: numReplies },
} = await prisma.post
.findUnique({
where: { id },
select: {
...selectPost.withLatestSnapshotMeta,
replies: {
select: {
...selectReply.withBasic,
...selectReply.withTakedown,
...selectReply.withLatestContent,
},
orderBy: { id: "asc" },
skip: (page - 1) * REPLIES_PER_PAGE,
take: REPLIES_PER_PAGE,
} = (await prisma.post.findUnique({
where: { id },
select: {
...selectPost.withLatestSnapshotMeta,
replies: {
select: {
...selectReply.withBasic,
...selectReply.withTakedown,
...selectReply.withLatestContent,
},
_count: { select: { replies: true } },
orderBy: { id: "asc" },
skip: (page - 1) * REPLIES_PER_PAGE,
take: REPLIES_PER_PAGE,
},
})
.then((post) => post ?? notFound());
_count: { select: { replies: true } },
},
})) ??
(savedAtLegacy
? redirect(`https://legacy.lglg.top/${id}/${page}`)
: notFound());
const numPages = Math.ceil(numReplies / REPLIES_PER_PAGE);
const { pagesLocalAttachedFront, pagesLocalAttachedBack, pagesLocal } =
paginate(numPages, page);
Expand Down
21 changes: 19 additions & 2 deletions packages/viewer/src/app/[id]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import prisma from "@/lib/prisma";
import { getPostUrl, getForumUrl } from "@/lib/luogu";
import stringifyTime from "@/lib/time";
Expand All @@ -8,6 +8,8 @@ import "@/components/markdown.css";
import UpdateButton from "@/components/UpdateButton";
import Reply from "@/components/replies/Reply";
import { selectPost } from "@/lib/post";
import { checkExists } from "@/lib/utils";
import savedInLegacyList from "./saved-in-legacy.json";

export async function generateMetadata({
params,
Expand All @@ -34,6 +36,7 @@ export default async function Page({
}: React.PropsWithChildren<{ params: { id: string } }>) {
const id = parseInt(params.id, 10);
if (Number.isNaN(id)) notFound();
const savedAtLegacy = checkExists(savedInLegacyList, id);
const {
replyCount,
time,
Expand All @@ -48,7 +51,8 @@ export default async function Page({
...selectPost.withTakedown,
_count: { select: { replies: true } },
},
})) ?? notFound();
})) ??
(savedAtLegacy ? redirect(`https://legacy.lglg.top/${id}`) : notFound());

if (takedown)
return (
Expand Down Expand Up @@ -111,6 +115,19 @@ export default async function Page({
>
查看原帖
</a>
{savedAtLegacy ? (
<a
className="btn btn-outline-info shadow-bssb-sm ms-2"
// style={{ marginLeft: ".875em" }}
href={`https://legacy.lglg.top/${id}`}
target="_blank"
rel="noopener noreferrer"
>
前往旧站
</a>
) : (
""
)}
<UpdateButton className="ms-2" target={params.id} key={params.id}>
更新帖子
</UpdateButton>
Expand Down
9 changes: 7 additions & 2 deletions packages/viewer/src/app/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { notFound } from "next/navigation";
import { notFound, redirect } from "next/navigation";
import prisma from "@/lib/prisma";
import paginate from "@/lib/pagination";
import InfiniteScrollReplies from "@/components/replies/InfiniteScrollReplies";
import { checkExists } from "@/lib/utils";
import savedInLegacyList from "./saved-in-legacy.json";

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

export default async function Page({ params }: { params: { id: string } }) {
const id = parseInt(params.id, 10);
const savedAtLegacy = checkExists(savedInLegacyList, id);

const {
snapshots: [{ authorId }],
_count: { replies },
Expand All @@ -21,7 +25,8 @@ export default async function Page({ params }: { params: { id: string } }) {
},
_count: { select: { replies: true } },
},
})) ?? notFound();
})) ??
(savedAtLegacy ? redirect(`https://legacy.lglg.top/${id}`) : notFound());
const numPages = Math.ceil(replies / REPLIES_PER_PAGE);
const { pagesLocalAttachedFront, pagesLocalAttachedBack, pagesLocal } =
paginate(numPages, 1);
Expand Down
1 change: 1 addition & 0 deletions packages/viewer/src/app/[id]/saved-in-legacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
13 changes: 5 additions & 8 deletions packages/viewer/src/app/r/[rid]/get-reply-raw.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { notFound } from "next/navigation";
import { selectPost } from "@/lib/post";
import prisma from "@/lib/prisma";
import { selectReply } from "@/lib/reply";

export default async (id: number) =>
(await prisma.reply.findUnique({
prisma.reply.findUnique({
select: {
...selectReply.withLatestContent,
...selectReply.withTakedown,
Expand All @@ -12,13 +12,10 @@ export default async (id: number) =>
post: {
select: {
id: true,
snapshots: {
select: { title: true, authorId: true },
orderBy: { time: "desc" },
take: 1,
},
...selectPost.withLatestSnapshotMeta,
...selectPost.withTakedown,
},
},
},
where: { id },
})) ?? notFound();
});
53 changes: 46 additions & 7 deletions packages/viewer/src/app/r/[rid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
// eslint-disable
// Not impl yet
import { notFound } from "next/navigation";
import "@/components/markdown.css";
import { notFound, redirect } from "next/navigation";
import { checkExists } from "@/lib/utils";
import Link from "next/link";
import prisma from "@/lib/prisma";
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 stringifyTime from "@/lib/time";
import getReplyRaw from "./get-reply-raw";
import savedInLegacyList from "../saved-in-legacy.json";

// import serializeReply from "@/lib/serialize-reply";

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 reply = await getReplyRaw(id);
if (!reply) {
if (checkExists(savedInLegacyList, id))
redirect(`https://legacy.lglg.top/r/${id}`);
else notFound();
}
const pages = Math.ceil(
(await prisma.reply.count({
where: { id: { lte: id }, postId: reply.post.id },
})) / REPLIES_PER_PAGE,
);
// return redirect(`/${discussionId}/${pages}#${params.rid}`);

if (reply.post.takedown) {
return (
<>
<p>{reply.post.takedown.reason}</p>
<p>
由于回复的帖子由 <UserInfo user={reply.post.takedown.submitter} />{" "}
申请删除,本回复无法查看。
</p>
</>
);
}

if (reply.takedown) {
return (
<>
<p>{reply.takedown.reason}</p>
<p>
<UserInfo user={reply.takedown.submitter} /> 申请删除。
</p>
</>
);
}

return (
<div className="row px-2 px-md-0">
<div className="col-xl-9 col-lg-10 col-md-11 col-12 mt-3 mb-3x mx-auto">
Expand Down Expand Up @@ -57,6 +85,17 @@ export default async function Page({ params }: { params: { rid: string } }) {
>
<div>
<UserInfo user={reply.snapshots[0].author} />
{reply.snapshots[0].author.id ===
reply.post.snapshots[0].author.id ? (
<span
className="ms-1 badge position-relative bg-teal d-inline-block"
style={{ top: "-.15em", left: ".08em", marginRight: ".08em" }}
>
楼主
</span>
) : (
""
)}
<span
className="float-end text-body-tertiary d-none d-md-inline"
style={{ marginRight: ".8em" }}
Expand Down
1 change: 1 addition & 0 deletions packages/viewer/src/app/r/saved-in-legacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
15 changes: 8 additions & 7 deletions packages/viewer/src/components/replies/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ export default function Content({
<div
className="markdown text-break overflow-x-auto overflow-y-hidden"
ref={contentRef}
/>
<Markdown
rehypePlugins={[rehypeKatex]}
remarkPlugins={[remarkMath, [gfm, { singleTilde: false }]]}
skipHtml
>
{content}
</Markdown>
<Markdown
rehypePlugins={[rehypeKatex]}
remarkPlugins={[remarkMath, [gfm, { singleTilde: false }]]}
skipHtml
>
{content}
</Markdown>
</div>
{/* {usersMetioned.map((user) => (
<div
ref={(el) => {
Expand Down
37 changes: 37 additions & 0 deletions packages/viewer/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 检查元素是否出现在给定的数组中。
*
* @param arr **单调递增**的数组
* @param target 需要查找的目标
* @returns 目标是否在 arr 中
*/
export function checkExists(arr: number[], target: number) {
let left = 0;
let right = arr.length - 1;

while (left <= right) {
const mid = Math.floor((left + right) / 2);

if (arr[mid] === target) {
return true; // 找到了目标元素,返回true
}
if (arr[mid] < target) {
left = mid + 1; // 目标在右侧
} else {
right = mid - 1; // 目标在左侧
}
}

return false;
}

/**
* 检查元素是否未出现在给定的数组中。
*
* @param arr **单调递增**的数组
* @param target 需要查找的目标
* @returns 目标是否没有出现在 arr 中
*/
export function checkNonExists(arr: number[], target: number) {
return !checkExists(arr, target);
}

0 comments on commit e5c73b0

Please sign in to comment.