TypeScript
feat: delete the selected event with Delete and undo with ⌘Z
Vipul Kumar9c785680f002
diff --git a/src/components/calendar/delete-event.ts b/src/components/calendar/delete-event.tsnew file mode 100644index 0000000..c150162--- /dev/null+++ b/src/components/calendar/delete-event.ts@@ -0,0 +1,25 @@+"use client";++import { toast } from "sonner";+import { useAppStore } from "@/stores/app-store";+import { useCalendarStore } from "@/stores/calendar-store";+import { openOverlay } from "@/stores/overlay-store";++/**+ * Deletes a calendar event from the keyboard or popover. Events that belong+ * to a leave request are managed through the request instead.+ */+export function deleteCalendarEvent(id: string) {+ const event = useAppStore.getState().events.find((e) => e.id === id);+ if (!event) return;+ useCalendarStore.getState().closePopover();+ if (event.requestId) {+ const requestId = event.requestId;+ toast("This event belongs to a leave request", {+ description: "Cancel or delete the request to remove it.",+ action: { label: "View request", onClick: () => openOverlay({ kind: "request-details", requestId }) },+ });+ return;+ }+ useAppStore.getState().deleteEvent(id);+}diff --git a/src/components/calendar/event-popover.tsx b/src/components/calendar/event-popover.tsxindex bbe7f77..0c213bc 100644--- a/src/components/calendar/event-popover.tsx+++ b/src/components/calendar/event-popover.tsx@@ -12,6 +12,7 @@ import { useIsMobile } from "@/hooks/use-media-query";import { useAppStore } from "@/stores/app-store";import { useCalendarStore } from "@/stores/calendar-store";import { openOverlay } from "@/stores/overlay-store";+import { deleteCalendarEvent } from "./delete-event";/** Details card for an event or holiday, anchored to whatever was clicked. */export function EventPopover() {@@ -21,7 +22,6 @@ export function EventPopover() {const holiday = useAppStore((s) => (target?.kind === "holiday" ? s.holidays.find((h) => h.id === target.id) : undefined));const request = useAppStore((s) => (event?.requestId ? s.requests.find((r) => r.id === event.requestId) : undefined));const timeFormat = useAppStore((s) => s.settings.timeFormat);- const deleteEvent = useAppStore((s) => s.deleteEvent);const duplicateEvent = useAppStore((s) => s.duplicateEvent);const isMobile = useIsMobile();@@ -123,15 +123,14 @@ export function EventPopover() {variant="ghost"size="icon-sm"aria-label="Delete"- onClick={() => {- deleteEvent(event.id);- close();- }}+ onClick={() => deleteCalendarEvent(event.id)}><Trash2Icon className="text-danger" /></Button></TooltipTrigger>- <TooltipContent>Delete</TooltipContent>+ <TooltipContent>+ Delete <kbd className="ml-1 font-sans opacity-70">⌫</kbd>+ </TooltipContent></Tooltip>)}</div>diff --git a/src/components/calendar/time-grid.tsx b/src/components/calendar/time-grid.tsxindex 0df3a5f..b69e91b 100644--- a/src/components/calendar/time-grid.tsx+++ b/src/components/calendar/time-grid.tsx@@ -137,6 +137,7 @@ export function TimeGrid({ days, strip }: { days: string[]; strip?: string[] })const focusEventId = useCalendarStore((s) => s.focusEventId);const clearFocus = useCalendarStore((s) => s.clearFocus);const openPopover = useCalendarStore((s) => s.openPopover);+ const selectedEventId = useCalendarStore((s) => (s.popover?.kind === "event" ? s.popover.id : null));const today = useToday();const now = useNow();@@ -459,6 +460,7 @@ export function TimeGrid({ days, strip }: { days: string[]; strip?: string[] })timeFormat={timeFormat}dragging={Boolean(drag && drag.type !== "create" && drag.eventId === event.id && drag.moved && !drag.locked)}highlighted={highlightId === event.id}+ selected={selectedEventId === event.id}onOpen={(el) => openPopover("event", event.id, el)}/>))}@@ -504,6 +506,7 @@ function EventBlock({timeFormat,dragging,highlighted,+ selected,onOpen,}: {event: CalEvent;@@ -512,6 +515,7 @@ function EventBlock({timeFormat: "24h" | "12h";dragging: boolean;highlighted: boolean;+ selected: boolean;onOpen: (el: HTMLElement) => void;}) {const height = minutesToPx(event.end - event.start) - 2;@@ -530,6 +534,7 @@ function EventBlock({e.preventDefault();onOpen(e.currentTarget);}+ // Delete / Backspace are handled globally (see keyboard-shortcuts).}}data-locked={locked || undefined}className={cn(@@ -538,6 +543,7 @@ function EventBlock({"hover-fine:brightness-[0.97] dark:hover-fine:brightness-110",locked ? "cursor-pointer" : "cursor-grab",dragging && "z-30 opacity-95 shadow-[0_8px_24px_-6px_rgba(15,15,15,0.28)]",+ selected && "ring-2 ring-brand/70 ring-offset-1 ring-offset-surface",highlighted && "animate-[event-pulse_1.4s_var(--ease-out)]",)}style={{diff --git a/src/components/shell/keyboard-shortcuts.tsx b/src/components/shell/keyboard-shortcuts.tsxindex 393e9b0..b023bbb 100644--- a/src/components/shell/keyboard-shortcuts.tsx+++ b/src/components/shell/keyboard-shortcuts.tsx@@ -2,12 +2,18 @@import { usePathname, useRouter } from "next/navigation";import { useEffect } from "react";+import { toast } from "sonner";+import { deleteCalendarEvent } from "@/components/calendar/delete-event";+import { redo, undo } from "@/stores/app-store";import { useToday } from "@/hooks/use-today";import { useCalendarStore } from "@/stores/calendar-store";import { openOverlay, useOverlayStore } from "@/stores/overlay-store";export const SHORTCUTS: { keys: string[]; label: string }[] = [{ keys: ["⌘", "K"], label: "Search" },+ { keys: ["⌘", "Z"], label: "Undo" },+ { keys: ["⇧", "⌘", "Z"], label: "Redo" },+ { keys: ["⌫"], label: "Delete selected event" },{ keys: ["N"], label: "New event" },{ keys: ["R"], label: "Request time-off" },{ keys: ["T"], label: "Go to today" },@@ -32,7 +38,39 @@ export function KeyboardShortcuts() {useEffect(() => {const onKey = (e: KeyboardEvent) => {- if (e.metaKey || e.ctrlKey || e.altKey || isTyping(e.target)) return;+ if (isTyping(e.target)) return; // text fields keep their native undo/delete++ // ⌘Z / Ctrl+Z undo · ⇧⌘Z / Ctrl+Shift+Z / Ctrl+Y redo+ const mod = e.metaKey || e.ctrlKey;+ const k = e.key.toLowerCase();+ if (mod && !e.altKey && (k === "z" || (k === "y" && e.ctrlKey))) {+ e.preventDefault();+ const isRedo = k === "y" || e.shiftKey;+ const label = isRedo ? redo() : undo();+ if (label) {+ toast(isRedo ? "Redone" : "Undone", {+ description: label,+ action: { label: isRedo ? "Undo" : "Redo", onClick: () => void (isRedo ? undo() : redo()) },+ });+ } else {+ toast(isRedo ? "Nothing to redo" : "Nothing to undo");+ }+ return;+ }++ // Delete / Backspace removes the selected event (open in the popover, or focused).+ if ((e.key === "Delete" || e.key === "Backspace") && !mod && !e.altKey) {+ const popover = useCalendarStore.getState().popover;+ const focused = (document.activeElement as HTMLElement | null)?.closest<HTMLElement>("[data-event-id]");+ const id = popover?.kind === "event" ? popover.id : focused?.dataset.eventId;+ if (id && useOverlayStore.getState().overlay.kind === "none") {+ e.preventDefault();+ deleteCalendarEvent(id);+ }+ return;+ }++ if (mod || e.altKey) return;if (useOverlayStore.getState().overlay.kind !== "none") return;if (document.querySelector("[role=dialog],[role=menu],[data-radix-popper-content-wrapper]")) return;