Files
role-user/src/app/(app)/schedule/page.tsx
T
2026-06-02 14:46:39 +08:00

88 lines
2.9 KiB
TypeScript

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 { getShifts } from "@/lib/mobile-data";
import type { Route } from "next";
type ScheduleRange = "today" | "upcoming" | "all";
type SchedulePageProps = {
searchParams: Promise<{ range?: string | string[] }>;
};
const statusText = {
scheduled: "已排班",
cancelled: "已取消",
completed: "已完成"
} as const;
function firstSearchValue(value: string | string[] | undefined) {
return Array.isArray(value) ? value[0] : value;
}
function toDateString(date: Date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
}
function normalizeRange(value: string | string[] | undefined): ScheduleRange {
const range = firstSearchValue(value);
if (range === "upcoming" || range === "all") return range;
return "today";
}
function rangeFilter(range: ScheduleRange) {
const today = toDateString(new Date());
if (range === "today") return { startDate: today, endDate: today };
if (range === "upcoming") return { startDate: today };
return {};
}
function emptyCopy(range: ScheduleRange) {
if (range === "today") return "今天没有安排班次。";
if (range === "upcoming") return "当前没有未来班次。";
return "当前没有你的排班记录。";
}
export default async function SchedulePage({ searchParams }: SchedulePageProps) {
const range = normalizeRange((await searchParams).range);
const shifts = await getShifts(rangeFilter(range));
return (
<div className="page-stack">
<PageHeader title="排班" description="查看本周和未来班次,第一版仅支持查看。" />
<FilterNav
label="排班筛选"
options={[
{ label: "今日", href: "/schedule" as Route, active: range === "today" },
{ label: "未来", href: "/schedule?range=upcoming" as Route, active: range === "upcoming" },
{ label: "全部", href: "/schedule?range=all" as Route, active: range === "all" }
]}
/>
{shifts.length > 0 ? (
<section className="list-stack">
{shifts.map((shift) => (
<article className="list-card" key={shift.id}>
<div>
<h2>{shift.position}</h2>
<p>
{formatDateTime(shift.startAt)} - {formatDateTime(shift.endAt)}
</p>
</div>
<StatusPill tone={shift.status === "cancelled" ? "danger" : "success"}>
{statusText[shift.status]}
</StatusPill>
</article>
))}
</section>
) : (
<EmptyState title="暂无排班" description={emptyCopy(range)} />
)}
</div>
);
}