Feat: 新增评论组件,细化通知组件样式,引入环境变量,替换图标组件
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
import { useState } from "react";
|
||||
import { useParams } from "react-router";
|
||||
|
||||
import Article from "~/components/common/article";
|
||||
import LightBox, { useLightBox } from "~/components/biz/gallery/image-box";
|
||||
import Comments from "~/components/shared/comment";
|
||||
import { CupFill, ShareFill, ThumbUpFill } from "~/components/ui/icons";
|
||||
import { useHydrated } from "~/hooks/use-hydrated";
|
||||
import { siteTitle } from "~/utils";
|
||||
import { getImageThumbUrl } from "~/utils/media";
|
||||
|
||||
import type { Route } from "./+types/note-detail";
|
||||
|
||||
const { APP_APIURL } = import.meta.env;
|
||||
|
||||
export function meta({ loaderData }: Route.MetaArgs) {
|
||||
return [
|
||||
{ title: loaderData ? siteTitle(loaderData.data.title) : "404" },
|
||||
@@ -19,7 +26,7 @@ export async function loader({ params }: Route.LoaderArgs) {
|
||||
}
|
||||
|
||||
const note = (await fetch(
|
||||
`https://paul.ren/api/note/get?id=${params.id}&year=${params.year}`
|
||||
`${APP_APIURL}/api/note/get?id=${params.id}&year=${params.year}`,
|
||||
).then((res) => res.json())) as API.Response<API.Note.INoteData>;
|
||||
|
||||
if (note.status === "Failed") {
|
||||
@@ -31,9 +38,22 @@ export async function loader({ params }: Route.LoaderArgs) {
|
||||
|
||||
export default function NoteDetail({ loaderData }: Route.ComponentProps) {
|
||||
const note = loaderData;
|
||||
const params = useParams();
|
||||
const hydrated = useHydrated();
|
||||
const { ref: mediaLightBoxRef, open: onShowMediaLightBox } = useLightBox();
|
||||
|
||||
const [likes, setLikes] = useState(note.data.likes);
|
||||
|
||||
const mediaList = note.data.media?.length
|
||||
? note.data.media.map((item) => ({
|
||||
...item,
|
||||
thumb_url: item.thumb_url || getImageThumbUrl(item.url),
|
||||
}))
|
||||
: [];
|
||||
|
||||
const hasMedia = mediaList.length > 0;
|
||||
const commentRef = `note-${params.year}-${params.id}`;
|
||||
|
||||
const onLike = () => {
|
||||
setLikes((prevLike) => prevLike + 1);
|
||||
};
|
||||
@@ -65,8 +85,38 @@ export default function NoteDetail({ loaderData }: Route.ComponentProps) {
|
||||
</section>
|
||||
<section className="p-5 md:p-12 bg-white rounded-xl border-b-4 border-b-cyan-200 mb-16">
|
||||
<Article html={note.data.content_html} />
|
||||
{hasMedia && (
|
||||
<div className="mt-8 flex flex-col gap-4">
|
||||
{mediaList.map((media, index) => {
|
||||
const isVideo = media.type === "mp4" || media.url.includes("mp4");
|
||||
|
||||
if (isVideo) {
|
||||
return (
|
||||
<video
|
||||
key={media.id || media.url}
|
||||
src={media.url}
|
||||
controls
|
||||
className="w-full rounded-xl cursor-pointer"
|
||||
onClick={() => onShowMediaLightBox(index)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<img
|
||||
key={media.id || media.url}
|
||||
src={getImageThumbUrl(media.url)}
|
||||
alt={media.title}
|
||||
loading="lazy"
|
||||
className="w-full rounded-xl cursor-pointer hover:opacity-90 transition-opacity"
|
||||
onClick={() => onShowMediaLightBox(index)}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
<section className="flex gap-16 items-center justify-center text-base md:text-lg">
|
||||
<section className="flex gap-16 items-center justify-center text-base md:text-lg mb-16">
|
||||
<button onClick={onLike}>
|
||||
<ThumbUpFill className="inline-block h-8 w-8 mr-2" />
|
||||
{likes}
|
||||
@@ -80,6 +130,10 @@ export default function NoteDetail({ loaderData }: Route.ComponentProps) {
|
||||
打赏
|
||||
</button>
|
||||
</section>
|
||||
|
||||
<Comments refs={commentRef} />
|
||||
|
||||
{hydrated && <LightBox ref={mediaLightBoxRef} list={mediaList} />}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user