93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import Link from "next/link";
|
|
import type { Route } from "next";
|
|
|
|
import { EmptyState } from "@/components/empty-state";
|
|
import { FilterNav } from "@/components/filter-nav";
|
|
import { PageHeader } from "@/components/page-header";
|
|
import { StatusPill } from "@/components/status-pill";
|
|
import { formatDateTime } from "@/lib/format";
|
|
import { getAnnouncements } from "@/lib/mobile-data";
|
|
|
|
type AnnouncementFilter = "all" | "unread" | "read";
|
|
|
|
type AnnouncementsPageProps = {
|
|
searchParams: Promise<{ read?: string | string[] }>;
|
|
};
|
|
|
|
function firstSearchValue(value: string | string[] | undefined) {
|
|
return Array.isArray(value) ? value[0] : value;
|
|
}
|
|
|
|
function normalizeFilter(value: string | string[] | undefined): AnnouncementFilter {
|
|
const read = firstSearchValue(value);
|
|
if (read === "unread" || read === "read") return read;
|
|
return "all";
|
|
}
|
|
|
|
function emptyCopy(filter: AnnouncementFilter) {
|
|
if (filter === "unread") {
|
|
return {
|
|
title: "暂无未读公告",
|
|
description: "当前没有需要你阅读的新公告。"
|
|
};
|
|
}
|
|
|
|
if (filter === "read") {
|
|
return {
|
|
title: "暂无已读公告",
|
|
description: "你阅读过的公告会保留在这里,方便回看。"
|
|
};
|
|
}
|
|
|
|
return {
|
|
title: "暂无公告",
|
|
description: "当前没有面向你的门店公告。"
|
|
};
|
|
}
|
|
|
|
export default async function AnnouncementsPage({ searchParams }: AnnouncementsPageProps) {
|
|
const filter = normalizeFilter((await searchParams).read);
|
|
const allAnnouncements = await getAnnouncements();
|
|
const announcements = allAnnouncements.filter((item) => {
|
|
if (filter === "unread") return !item.read;
|
|
if (filter === "read") return item.read;
|
|
return true;
|
|
});
|
|
const empty = emptyCopy(filter);
|
|
|
|
return (
|
|
<div className="page-stack">
|
|
<PageHeader title="公告" description="接收管理员或店长发布给你的门店通知。" />
|
|
<FilterNav
|
|
label="公告筛选"
|
|
options={[
|
|
{ label: "全部", href: "/announcements" as Route, active: filter === "all" },
|
|
{ label: "未读", href: "/announcements?read=unread" as Route, active: filter === "unread" },
|
|
{ label: "已读", href: "/announcements?read=read" as Route, active: filter === "read" }
|
|
]}
|
|
/>
|
|
{announcements.length > 0 ? (
|
|
<section className="list-stack">
|
|
{announcements.map((item) => {
|
|
const href = `/announcements/${item.id}` as Route;
|
|
|
|
return (
|
|
<Link className="list-card" href={href} key={item.id}>
|
|
<div>
|
|
<h2>{item.title}</h2>
|
|
<p>{item.summary || formatDateTime(item.publishedAt)}</p>
|
|
</div>
|
|
<StatusPill tone={item.level === "normal" ? "default" : "warning"}>
|
|
{item.read ? "已读" : "未读"}
|
|
</StatusPill>
|
|
</Link>
|
|
);
|
|
})}
|
|
</section>
|
|
) : (
|
|
<EmptyState title={empty.title} description={empty.description} />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|