From f64113a756e44857fb56e353f60f1af7a5226d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Wed, 8 Nov 2023 02:04:52 +0800 Subject: [PATCH] Fix: Note List Page Styles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 样式调整,显示站点名称,获取日记首图,列表页参数获取 --- app/routes/note.$year.$id.tsx | 14 +++++++++--- app/routes/note._index.tsx | 41 ++++++++++++++++++++++------------- app/utils/index.ts | 7 ++++++ app/utils/note.ts | 11 ++++++++++ vite.config.ts | 1 + 5 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 app/utils/note.ts diff --git a/app/routes/note.$year.$id.tsx b/app/routes/note.$year.$id.tsx index ab16e7e..e9d73bd 100644 --- a/app/routes/note.$year.$id.tsx +++ b/app/routes/note.$year.$id.tsx @@ -1,6 +1,14 @@ -import { LoaderFunctionArgs, json } from "@remix-run/node"; +import { LoaderFunctionArgs, MetaFunction, json } from "@remix-run/node"; import { useLoaderData } from "@remix-run/react"; import Article from "~/components/common/article"; +import { siteTitle } from "~/utils"; + +export const meta: MetaFunction = ({ data }) => { + return [ + { title: data ? siteTitle(data.data.title) : "404" }, + { name: "description", content: data?.data.except }, + ]; +} export async function loader({ params }: LoaderFunctionArgs) { if (Number.isNaN(Number(params.year)) || Number.isNaN(Number(params.id))) { @@ -20,8 +28,8 @@ export default function Detail() { const note = useLoaderData(); return ( -
-
+
+

{note.data.title}

{note.data.date}

diff --git a/app/routes/note._index.tsx b/app/routes/note._index.tsx index bf85c07..656d482 100644 --- a/app/routes/note._index.tsx +++ b/app/routes/note._index.tsx @@ -1,16 +1,22 @@ import { useEffect } from "react"; import { Link, useLoaderData } from "@remix-run/react"; -import { json, type MetaFunction } from "@remix-run/node"; +import { json, LoaderFunctionArgs, type MetaFunction } from "@remix-run/node"; +import { clsn, siteTitle } from "~/utils"; +import { getFirstImage } from "~/utils/note"; export const meta: MetaFunction = () => { return [ - { title: "日记" }, + { title: siteTitle("日记") }, { name: "description", content: "奇趣保罗的日常笔记" }, ]; }; -export async function loader() { - const note = await fetch("https://paul.ren/api/note").then((res) => res.json()) as API.Response; +export async function loader({ request }: LoaderFunctionArgs) { + const url = new URL(request.url); + const year = url.searchParams.get("year") || new Date().getFullYear(); + const page = url.searchParams.get("page") || 1; + + const note = await fetch(`https://paul.ren/api/note/?page=${page}&year=${year}`).then((res) => res.json()) as API.Response; return json(note); } @@ -23,27 +29,32 @@ export default function Note() { }, []); return ( -
-
-

日记

+
+
+

日记

{note.data.map((item) => { const year = item.date.substring(0, 4); + const cover = getFirstImage(item); return ( -

{item.title}

-

{item.except}

-
+

{item.except}

+

{item.date}

- - 继续阅读 -
-
+ {cover && ( +
+ )} + ); })}
diff --git a/app/utils/index.ts b/app/utils/index.ts index 7a86661..ac16d19 100644 --- a/app/utils/index.ts +++ b/app/utils/index.ts @@ -1,3 +1,10 @@ +// 站点名称 +export const siteTitle = (title?: string) => { + const siteName = import.meta.env.APP_SITENAME; + + return title ? `${title} - ${siteName}` : siteName; +} + // Classnames export const clsn = (...clsn: (string | undefined | null | false)[]) => { return clsn.filter(item => item).join(" "); diff --git a/app/utils/note.ts b/app/utils/note.ts new file mode 100644 index 0000000..a97c460 --- /dev/null +++ b/app/utils/note.ts @@ -0,0 +1,11 @@ +// 获取第一张图片 +export const getFirstImage = (note: API.Note.INoteData) => { + if (note.media && note.media.length > 0) { + return note.media[0].url; + } + + // 遗留版本 + if (note.photo && note.photo.length > 0) { + return note.photo[0].url; + } +} diff --git a/vite.config.ts b/vite.config.ts index 177dd59..4a3e39b 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,4 +4,5 @@ import tsconfigPaths from "vite-tsconfig-paths"; export default defineConfig({ plugins: [remix(), tsconfigPaths()], + envPrefix: "APP_", });