TypeScript
update: show three days on phones instead of a scrolling week
Vipul Kumar03689b2a902c
diff --git a/src/components/calendar/calendar-header.tsx b/src/components/calendar/calendar-header.tsxindex eb2d93e..6eb7b7e 100644--- a/src/components/calendar/calendar-header.tsx+++ b/src/components/calendar/calendar-header.tsx@@ -27,8 +27,10 @@ const VIEWS: { id: CalendarView; label: string; left: number; width: number; key{ id: "month", label: "Month", left: 101, width: 61, key: "M" },];-function useRangeLabel() {- const { view, date } = useCalendarStore();+function useRangeLabel(visible: string[]) {+ const view = useCalendarStore((s) => s.view);+ const date = useCalendarStore((s) => s.date);+ const compact = useCalendarStore((s) => s.compact);const weekStartsOn = useAppStore((s) => s.settings.weekStartsOn);const year = fromKey(date).getFullYear();const title = `${monthName(date)}, ${year}`;@@ -41,18 +43,24 @@ function useRangeLabel() {// ISO weeks start on Monday; for Sunday-first weeks use that Monday.const week = isoWeek(weekStartsOn === 0 ? days[1] : days[0]);if (view === "day") return { title, subtitle: `Week ${isoWeek(date)} · ${formatWeekday(date)}, ${formatShort(date)}` };+ if (compact) {+ const first = visible[0];+ const last = visible[visible.length - 1];+ return { title: `${monthName(first)}, ${fromKey(first).getFullYear()}`, subtitle: `Week ${isoWeek(first)} · ${formatRange(first, last)}` };+ }return { title, subtitle: `Week ${week} · ${formatRange(days[0], days[6])}` };}-export function CalendarHeader() {+export function CalendarHeader({ days }: { days: string[] }) {const view = useCalendarStore((s) => s.view);const setView = useCalendarStore((s) => s.setView);const setDate = useCalendarStore((s) => s.setDate);const step = useCalendarStore((s) => s.step);const today = useToday();- const { title, subtitle } = useRangeLabel();+ const compact = useCalendarStore((s) => s.compact);+ const { title, subtitle } = useRangeLabel(days);const active = VIEWS.find((v) => v.id === view)!;- const unit = view === "day" ? "day" : view === "week" ? "week" : "month";+ const unit = view === "day" ? "day" : view === "week" ? (compact ? "3 days" : "week") : "month";const activeIndex = VIEWS.indexOf(active);@@ -129,7 +137,14 @@ export function CalendarHeader() {view === v.id ? "text-fg" : "text-fg-2 hover-fine:text-fg",)}>- {v.label}+ {v.id === "week" ? (+ <>+ <span className="md:hidden">3 days</span>+ <span className="max-md:hidden">{v.label}</span>+ </>+ ) : (+ v.label+ )}</button>))}</div>diff --git a/src/components/calendar/calendar.tsx b/src/components/calendar/calendar.tsxindex f8e0b54..5f769c4 100644--- a/src/components/calendar/calendar.tsx+++ b/src/components/calendar/calendar.tsx@@ -1,30 +1,35 @@"use client";import { useEffect, useMemo } from "react";-import { isWeekend, weekDays } from "@/lib/dates";+import { addDays, isWeekend, weekDays } from "@/lib/dates";import { useIsMobile } from "@/hooks/use-media-query";import { useAppStore } from "@/stores/app-store";-import { useCalendarStore } from "@/stores/calendar-store";+import { COMPACT_WEEK_DAYS, useCalendarStore } from "@/stores/calendar-store";import { CalendarHeader } from "./calendar-header";import { EventPopover } from "./event-popover";import { MonthView } from "./month-view";import { TimeGrid } from "./time-grid";-// Phones open on the day view once per session (a 7-column week is cramped-// there); after that the user's choice sticks.-let mobileViewApplied = false;+/** `count` consecutive days from `start`, optionally skipping weekends. */+function daysFrom(start: string, count: number, showWeekends: boolean): string[] {+ const out: string[] = [];+ for (let d = start; out.length < count; d = addDays(d, 1)) {+ if (showWeekends || !isWeekend(d)) out.push(d);+ }+ return out;+}export function Calendar() {const view = useCalendarStore((s) => s.view);const date = useCalendarStore((s) => s.date);+ const compact = useCalendarStore((s) => s.compact);const weekStartsOn = useAppStore((s) => s.settings.weekStartsOn);const showWeekends = useAppStore((s) => s.settings.showWeekends);const isMobile = useIsMobile();+ // Phones: "Week" is a 3-day window and the arrows step 3 days at a time.useEffect(() => {- if (!isMobile || mobileViewApplied) return;- mobileViewApplied = true;- if (useCalendarStore.getState().view === "week") useCalendarStore.getState().setView("day");+ useCalendarStore.getState().setCompact(isMobile);}, [isMobile]);const week = useMemo(() => {@@ -32,14 +37,17 @@ export function Calendar() {return showWeekends ? all : all.filter((d) => !isWeekend(d));}, [date, weekStartsOn, showWeekends]);- const days = view === "day" ? [date] : week;+ const days = useMemo(() => {+ if (view === "day") return [date];+ return compact ? daysFrom(date, COMPACT_WEEK_DAYS, showWeekends) : week;+ }, [view, date, compact, showWeekends, week]);return (<sectionaria-label="Calendar"className="flex h-full min-w-0 flex-1 flex-col overflow-hidden bg-surface [--axis:44px] md:[--axis:70px]">- <CalendarHeader />+ <CalendarHeader days={days} />{view === "month" ? <MonthView /> : <TimeGrid key={view} days={days} strip={view === "day" ? week : undefined} />}<EventPopover /></section>diff --git a/src/components/calendar/time-grid.tsx b/src/components/calendar/time-grid.tsxindex 043eb9f..0df3a5f 100644--- a/src/components/calendar/time-grid.tsx+++ b/src/components/calendar/time-grid.tsx@@ -48,6 +48,7 @@ export function DayHeaders({ days }: { days: string[] }) {const selected = useCalendarStore((s) => s.date);const setDate = useCalendarStore((s) => s.setDate);const setView = useCalendarStore((s) => s.setView);+ const compact = useCalendarStore((s) => s.compact);const today = useToday();return (@@ -60,7 +61,12 @@ export function DayHeaders({ days }: { days: string[] }) {<buttonkey={d}type="button"- onClick={() => (isSelected ? setView("day") : setDate(d))}+ onClick={() => {+ if (compact || isSelected) {+ setDate(d);+ setView("day");+ } else setDate(d);+ }}aria-label={`${fromKey(d).toDateString()}${isSelected ? ", selected — open day view" : ""}`}aria-pressed={isSelected}className="group flex h-full min-w-0 flex-1 items-center gap-2 overflow-hidden border-l border-hairline px-3 text-left outline-none focus-visible:bg-hover max-md:flex-col max-md:justify-center max-md:gap-1 max-md:px-0"@@ -313,7 +319,7 @@ export function TimeGrid({ days, strip }: { days: string[]; strip?: string[] })if (t.lifted) return false; // a lifted event finishes like a mouse drag belowconst dx = e.clientX - t.x;const dy = e.clientY - t.y;- if (days.length === 1 && Math.abs(dx) > SWIPE_DISTANCE && Math.abs(dy) < Math.abs(dx) * 0.6 && e.timeStamp - t.t < 600) {+ if (days.length <= 3 && Math.abs(dx) > SWIPE_DISTANCE && Math.abs(dy) < Math.abs(dx) * 0.6 && e.timeStamp - t.t < 600) {step(dx < 0 ? 1 : -1);} else if (Math.hypot(dx, dy) < 8) {if (t.eventEl) {@@ -423,8 +429,8 @@ export function TimeGrid({ days, strip }: { days: string[]; strip?: string[] })ref={bodyRef}className={cn("relative flex h-full min-w-0 flex-1 select-none",- // Day view keeps vertical-only panning so a sideways flick changes day.- days.length === 1 ? "touch-pan-y" : "touch-manipulation",+ // Day and 3-day views pan vertically only, so a sideways flick changes the period.+ days.length <= 3 ? "touch-pan-y" : "touch-manipulation",drag?.moved && drag.type === "move" && !drag.locked && "cursor-grabbing",)}onPointerDown={onPointerDown}diff --git a/src/stores/calendar-store.ts b/src/stores/calendar-store.tsindex 1872228..972a66c 100644--- a/src/stores/calendar-store.ts+++ b/src/stores/calendar-store.ts@@ -6,6 +6,9 @@ import { DESIGN_DATE } from "@/lib/seed";export type CalendarView = "day" | "week" | "month";+/** Phones show the week view as three days starting at the focused day. */+export const COMPACT_WEEK_DAYS = 3;+/** What the details popover is showing and where it's anchored. */export type PopoverTarget = {kind: "event" | "holiday";@@ -20,6 +23,9 @@ type CalendarStore = {/** Asks the grid to scroll a specific event into view. */focusEventId: string | null;popover: PopoverTarget | null;+ /** True on phones: "week" renders as a 3-day window and steps by 3 days. */+ compact: boolean;+ setCompact: (compact: boolean) => void;setView: (view: CalendarView) => void;setDate: (date: string) => void;step: (dir: 1 | -1) => void;@@ -34,11 +40,14 @@ export const useCalendarStore = create<CalendarStore>()((set, get) => ({date: DESIGN_DATE,focusEventId: null,popover: null,+ compact: false,+ setCompact: (compact) => set({ compact }),setView: (view) => set({ view, popover: null }),setDate: (date) => set({ date, popover: null }),step: (dir) => {- const { view, date } = get();- const next = view === "day" ? addDays(date, dir) : view === "week" ? addDays(date, 7 * dir) : addMonths(date, dir);+ const { view, date, compact } = get();+ const weekStep = compact ? COMPACT_WEEK_DAYS : 7;+ const next = view === "day" ? addDays(date, dir) : view === "week" ? addDays(date, weekStep * dir) : addMonths(date, dir);set({ date: next, popover: null });},focusEvent: (id, date) => set({ focusEventId: id, date }),