mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
69 lines
3.2 KiB
TypeScript
69 lines
3.2 KiB
TypeScript
import { Button, buttonClasses } from "@usememos/ui";
|
|
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
|
import { addMonths } from "@/lib/calendar-utils";
|
|
import { collectionPathForLocation } from "@/router/routes";
|
|
import { useTranslate } from "@/utils/i18n";
|
|
import { CalendarLink } from "./CalendarLink";
|
|
import { MonthPicker } from "./MonthPicker";
|
|
import { buildCalendarPath, getMonthOfDate } from "./paths";
|
|
|
|
export interface CalendarHeaderProps {
|
|
month: string;
|
|
monthLabel: string;
|
|
/** `YYYY-MM-DD` */
|
|
today: string;
|
|
/** `YYYY-MM-DD` of the day being shown, whether the URL names it or the layout defaulted to it. */
|
|
activeDate?: string;
|
|
/** Whether the shown day can be dismissed: a panel or sheet can, the phone's inline list cannot. */
|
|
closable: boolean;
|
|
}
|
|
|
|
/**
|
|
* Where "Today" goes: from another month it returns to this month; within this month it opens
|
|
* today, and where the day can be dismissed a second press closes it again. On phones a day is
|
|
* always shown, so Today simply keeps today selected and reads as pressed while it is.
|
|
*/
|
|
export const getTodayPath = (month: string, activeDate: string | undefined, today: string, closable: boolean): string => {
|
|
const currentMonth = getMonthOfDate(today);
|
|
if (month !== currentMonth) return buildCalendarPath(currentMonth);
|
|
return closable && activeDate === today ? buildCalendarPath(currentMonth) : buildCalendarPath(currentMonth, today);
|
|
};
|
|
|
|
export const CalendarHeader = ({ month, monthLabel, today, activeDate, closable }: CalendarHeaderProps) => {
|
|
const t = useTranslate();
|
|
const navigate = useNavigate();
|
|
const { pathname, search } = useLocation();
|
|
const todayOpen = activeDate === today;
|
|
const iconControlClassName = buttonClasses({ variant: "ghost", size: "sm", iconOnly: true });
|
|
|
|
return (
|
|
// The title's text starts on the grid's text axis (border + cell padding); the month
|
|
// controls sit at the trailing edge so they never move as the title's width changes.
|
|
<header className="flex h-9 shrink-0 items-center ps-px">
|
|
<MonthPicker month={month} monthLabel={monthLabel} today={today} />
|
|
<div className="ms-auto flex items-center gap-0.5 pointer-coarse:gap-4">
|
|
<CalendarLink to={buildCalendarPath(addMonths(month, -1))} aria-label={t("common.previous-month")} className={iconControlClassName}>
|
|
<ChevronLeftIcon className="mu-icon rtl:rotate-180" />
|
|
</CalendarLink>
|
|
<CalendarLink to={buildCalendarPath(addMonths(month, 1))} aria-label={t("common.next-month")} className={iconControlClassName}>
|
|
<ChevronRightIcon className="mu-icon rtl:rotate-180" />
|
|
</CalendarLink>
|
|
{/* Today is a toggle, so it is a button that navigates; the search keeps the filter query.
|
|
Its pressed state is the quiet variant's accent fill, keyed off aria-pressed. */}
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
aria-pressed={todayOpen}
|
|
className="ms-1.5"
|
|
onClick={() =>
|
|
navigate({ pathname: collectionPathForLocation(getTodayPath(month, activeDate, today, closable), pathname), search })
|
|
}
|
|
>
|
|
{t("common.today")}
|
|
</Button>
|
|
</div>
|
|
</header>
|
|
);
|
|
};
|