Feat: Add Note List & Detail Page

日记列表与详情页面,包括载入条和文章组件
This commit is contained in:
奇趣保罗 2023-11-06 01:55:58 +08:00
parent e2704ddb1d
commit db8a91e6ee
11 changed files with 271 additions and 3 deletions

View File

@ -0,0 +1,30 @@
.article {
line-height: 1.7;
> * {
margin-bottom: 1rem;
&:last-child {
margin-bottom: 0;
}
}
pre {
tab-size: 4;
padding: 1em;
color: #fff;
overflow: auto;
border-radius: .75rem;
background-color: #333;
}
ul {
line-height: 2;
list-style: disc;
margin-left: 1.25rem;
::marker {
color: rgb(244 114 182 / var(--tw-bg-opacity));
}
}
}

View File

@ -0,0 +1,15 @@
import { clsn } from "~/utils";
import styles from "./article.module.less";
interface ArticleProps {
className?: string;
html: string;
}
function Article({ className, html }: ArticleProps) {
return (
<article className={clsn(styles.article, className)} dangerouslySetInnerHTML={{ __html: html }} />
);
}
export default Article;

View File

@ -0,0 +1,9 @@
import { useNavigation } from "@remix-run/react";
export default function SpinnerBar() {
const navigation = useNavigation();
return navigation.state === "loading" ? (
<div className="spinner animate-spinner-bar h-1 z-20 fixed top-0 left-0 right-0 bg-orange-200" />
) : null;
}

View File

@ -0,0 +1,31 @@
import { Link, NavLink } from "@remix-run/react";
const navItems = [
{ name: "首页", to: "/" },
{ name: "日记", to: "/note" },
{ name: "相册", to: "/gallery" },
{ name: "关于我", to: "/about" },
];
const inheritCls = "inline-block py-2 px-5";
const activeCls = "inline-block py-2 px-5 bg-orange-200 text-pink-400 rounded-xl";
function Header() {
return (
<header className="fixed top-0 left-0 right-0 bg-pink-400 text-white z-10">
<nav className="py-3 px-2">
{navItems.map((item) => (
<NavLink
key={item.name}
to={item.to}
className={({ isActive }) => isActive ? activeCls : inheritCls}
>
{item.name}
</NavLink>
))}
</nav>
</header>
);
}
export default Header;

View File

@ -8,22 +8,28 @@ import {
Scripts,
ScrollRestoration,
} from "@remix-run/react";
import Header from "./components/layout/header";
import Spinner from "./components/common/spinner";
import "./index.css";
export const links: LinksFunction = () => [
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
{ rel: "stylesheet", href: "https://cdn-font.hyperos.mi.com/font/css?family=MiSans:100,200,300,400,450,500,600,650,700,900:Chinese_Simplify,Latin&display=swap" },
];
export default function App() {
return (
<html lang="en">
<html lang="zh-cmn-hans">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<body className="font-mi pt-16 bg-orange-50 text-neutral-600">
<Spinner />
<Header />
<Outlet />
<ScrollRestoration />
<LiveReload />

View File

@ -0,0 +1,33 @@
import { LoaderFunctionArgs, json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import Article from "~/components/common/article";
export async function loader({ params }: LoaderFunctionArgs) {
if (Number.isNaN(Number(params.year)) || Number.isNaN(Number(params.id))) {
throw json("Not Found", { status: 404 });
}
const note = await fetch(`https://paul.ren/api/note/get?id=${params.id}&year=${params.year}`).then((res) => res.json()) as API.Response<API.Note.INoteData>;
if (note.status === "Failed") {
throw json("Not Found", { status: 404 });
}
return json(note);
}
export default function Detail() {
const note = useLoaderData<typeof loader>();
return (
<main className="px-2 py-10 max-w-3xl mx-auto">
<section className="my-12">
<h1 className="text-center text-5xl/tight md:text-7xl/tight mb-4">{note.data.title}</h1>
<p className="text-center opacity-60">{note.data.date}</p>
</section>
<section className="p-5 bg-white rounded-xl border-b-4 border-b-cyan-200">
<Article html={note.data.content_html} />
</section>
</main>
);
};

View File

@ -0,0 +1,51 @@
import { useEffect } from "react";
import { Link, useLoaderData } from "@remix-run/react";
import { json, type MetaFunction } from "@remix-run/node";
export const meta: MetaFunction = () => {
return [
{ title: "日记" },
{ name: "description", content: "奇趣保罗的日常笔记" },
];
};
export async function loader() {
const note = await fetch("https://paul.ren/api/note").then((res) => res.json()) as API.Response<API.Note.INoteData[]>;
return json(note);
}
export default function Note() {
const note = useLoaderData<typeof loader>();
useEffect(() => {
console.log(note);
}, []);
return (
<main className="px-2 py-10 max-w-3xl mx-auto">
<section className="my-12">
<h1 className="text-center text-5xl/tight md:text-7xl/tight mb-4"></h1>
</section>
{note.data.map((item) => {
const year = item.date.substring(0, 4);
return (
<div
key={item.id}
className="p-5 bg-white rounded-xl mb-8 last:mb-0 border-4 border-transparent hover:border-pink-400 transition-colors border-b-4 border-b-cyan-200"
>
<h2 className="text-pink-400 text-2xl font-bold mb-4">{item.title}</h2>
<p className="mb-4">{item.except}</p>
<div className="flex items-end justify-between">
<p className="opacity-60">{item.date}</p>
<Link className="py-2 px-4 bg-cyan-400 hover:bg-pink-400 transition-colors text-white rounded-xl" to={`/note/${year}/${item.id}`}>
</Link>
</div>
</div>
);
})}
</main>
);
}

7
app/types/api.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
declare namespace API {
interface Response<D> {
status: "Success" | "Failed";
msg: string;
data: D;
}
}

69
app/types/api.note.d.ts vendored Normal file
View File

@ -0,0 +1,69 @@
declare namespace API {
namespace Note {
enum NoteType {
Private,
Friends,
Limited,
Public,
}
interface INoteMusic {
id: number;
type?: "netease";
title: string;
artist: string;
album: string;
cover: string;
}
interface INoteQuery {
page: number;
year?: number;
month?: number;
search?: string;
}
interface INoteDetailQuery {
id: number;
year?: number;
}
// 旧数据兼容
interface INotePhotoData {
year: number | string;
name: string;
type: string;
url: string;
}
interface INoteData {
id: number;
title: string;
content: string;
except: string;
content_html: string;
date: string;
mood: number;
weather: number;
status: number;
type: NoteType;
time_spent: number;
music?: INoteMusic;
starred: boolean;
unlocked?: boolean;
media: any[];
photo?: INotePhotoData[];
year?: string;
time: number;
likes: number;
created_at: string;
updated_at: string;
}
}
}

4
app/utils/index.ts Normal file
View File

@ -0,0 +1,4 @@
// Classnames
export const clsn = (...clsn: (string | undefined | null | false)[]) => {
return clsn.filter(item => item).join(" ");
}

View File

@ -5,7 +5,20 @@ export default {
"./app/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
extend: {
fontFamily: {
"mi": "MiSans",
},
animation: {
'spinner-bar': 'spinnerBar 6s linear infinite',
},
keyframes: {
spinnerBar: {
"0%": { width: "0%" },
"100%": { width: "100%" },
}
}
},
},
plugins: [],
}