diff --git a/src/app/e/[code]/g/[token]/page.tsx b/src/app/e/[code]/g/[token]/page.tsx index d82a311..4de20d7 100644 --- a/src/app/e/[code]/g/[token]/page.tsx +++ b/src/app/e/[code]/g/[token]/page.tsx @@ -1,9 +1,12 @@ import type { Metadata } from "next"; import Link from "next/link"; import { notFound } from "next/navigation"; +import { CancelledEventNotice } from "@/components/CancelledEventNotice"; +import { EventUnavailable } from "@/components/EventUnavailable"; import { RsvpForm } from "@/components/RsvpForm"; import { getDb } from "@/db/client"; import type { RsvpStatus } from "@/domain/headcount"; +import { eventState } from "@/lib/event-state"; import { formatWhen } from "@/lib/format"; import { NotFoundError } from "@/services/errors"; import { getGuestByToken, getPublicEvent } from "@/services/rsvp"; @@ -40,6 +43,24 @@ export default async function GuestEditPage({ params }: PageProps<"/e/[code]/g/[ throw error; } + const state = eventState(event); + if (state === "deleted") return ; + const cancelled = state === "cancelled"; + + const personalNote = ( +

+ This is your personal link — don't share it.{" "} + {cancelled ? null : ( + + Not you? Answer as someone else + + )} +

+ ); + return (
-
- {CONFIRMATION[guest.status]} - - Need to change something? Update your answer below. - -
-

- This is your personal link — don't share it.{" "} - - Not you? Answer as someone else - -

+ {cancelled ? ( + + ) : ( +
+ {CONFIRMATION[guest.status]} + + Need to change something? Update your answer below. + +
+ )} + {personalNote} } /> diff --git a/src/app/e/[code]/page.tsx b/src/app/e/[code]/page.tsx index b9cfaa0..2df16d1 100644 --- a/src/app/e/[code]/page.tsx +++ b/src/app/e/[code]/page.tsx @@ -1,8 +1,11 @@ import type { Metadata } from "next"; import { cookies } from "next/headers"; import { notFound, redirect } from "next/navigation"; +import { CancelledEventNotice } from "@/components/CancelledEventNotice"; +import { EventUnavailable } from "@/components/EventUnavailable"; import { RsvpForm } from "@/components/RsvpForm"; import { getDb } from "@/db/client"; +import { eventState } from "@/lib/event-state"; import { formatWhen } from "@/lib/format"; import { guestCookieName } from "@/lib/guest-cookie"; import { eventPreview, wantsBlankForm } from "@/lib/guest-page"; @@ -22,7 +25,8 @@ export async function generateMetadata({ params }: PageProps<"/e/[code]">): Prom if (error instanceof NotFoundError) return null; throw error; }); - const { title, description } = eventPreview(event); + // A deleted event must not keep advertising its title in link previews. + const { title, description } = eventPreview(event && !event.deletedAt ? event : null); return { title, description, openGraph: { title, description } }; } @@ -39,6 +43,10 @@ export default async function GuestPage({ params, searchParams }: PageProps<"/e/ throw error; } + const state = eventState(event); + if (state === "deleted") return ; + const cancelled = state === "cancelled"; + // A returning guest goes straight to their edit page. A missing, invalid or // stale cookie is ignored. `?new=1` ("Not you?") skips this and shows a blank form. const token = blank ? undefined : (await cookies()).get(guestCookieName(code))?.value; @@ -71,29 +79,33 @@ export default async function GuestPage({ params, searchParams }: PageProps<"/e/ goingCount: event.goingCount, }} action={submitGuestRsvp.bind(null, code)} + readOnly={cancelled} + notice={cancelled ? : undefined} /> - + + )}
); } diff --git a/src/app/host/actions.test.ts b/src/app/host/actions.test.ts new file mode 100644 index 0000000..6487c67 --- /dev/null +++ b/src/app/host/actions.test.ts @@ -0,0 +1,66 @@ +// @vitest-environment node +import { beforeEach, describe, expect, it, vi } from "vitest"; +import type { Db } from "@/db/client"; +import { createTestDb } from "@/db/test-db"; +import { createEvent, deleteEvent, getHostEvent } from "@/services/events"; + +const session = vi.hoisted(() => ({ hostId: "host-1" })); +const testDb = vi.hoisted(() => ({ db: null as unknown })); + +vi.mock("@/server/auth", () => ({ + requireHost: async () => ({ id: session.hostId, name: "Host", email: "h@example.com" }), +})); +vi.mock("@/db/client", () => ({ getDb: async () => testDb.db })); +vi.mock("next/cache", () => ({ revalidatePath: vi.fn() })); +vi.mock("next/navigation", () => ({ + notFound: () => { + throw new Error("NEXT_NOT_FOUND"); + }, + redirect: (url: string) => { + throw new Error(`NEXT_REDIRECT:${url}`); + }, +})); + +import { restoreEventAction } from "./actions"; + +let db: Db; +let eventId: string; + +beforeEach(async () => { + session.hostId = "host-1"; + db = await createTestDb(); + testDb.db = db; + ({ id: eventId } = await createEvent(db, "host-1", { + title: "Cookout", + startsAt: new Date("2026-07-04T18:00:00Z"), + location: "Backyard", + hostName: "Will", + })); +}); + +describe("restoreEventAction", () => { + it("restores the host's deleted event and opens it", async () => { + await deleteEvent(db, "host-1", eventId); + + await expect(restoreEventAction(eventId)).rejects.toThrow( + `NEXT_REDIRECT:/host/events/${eventId}`, + ); + + const event = await getHostEvent(db, "host-1", eventId); + expect(event.deletedAt).toBeNull(); + expect(event.title).toBe("Cookout"); + }); + + it("a different signed-in host gets a 404 and the event stays deleted", async () => { + await deleteEvent(db, "host-1", eventId); + session.hostId = "host-2"; + + await expect(restoreEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + await expect(getHostEvent(db, "host-1", eventId)).rejects.toThrow(); + }); + + it("is a 404 for a live event or an unknown id", async () => { + await expect(restoreEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + await expect(restoreEventAction("missing")).rejects.toThrow("NEXT_NOT_FOUND"); + }); +}); diff --git a/src/app/host/actions.ts b/src/app/host/actions.ts new file mode 100644 index 0000000..d1383e6 --- /dev/null +++ b/src/app/host/actions.ts @@ -0,0 +1,26 @@ +"use server"; + +import { revalidatePath } from "next/cache"; +import { notFound, redirect } from "next/navigation"; +import { getDb } from "@/db/client"; +import { requireHost } from "@/server/auth"; +import { NotFoundError } from "@/services/errors"; +import { restoreEvent } from "@/services/events"; + +// The host comes from the session; the event id arrives through `bind` and is +// therefore untrusted. restoreEvent re-checks that the event is this host's and +// deleted before it comes back. + +/** Undo on the dashboard's "Deleted …" notice: restores the event, then opens it. */ +export async function restoreEventAction(eventId: string): Promise { + const host = await requireHost(); + try { + await restoreEvent(await getDb(), host.id, eventId); + } catch (error) { + if (error instanceof NotFoundError) notFound(); + throw error; + } + revalidatePath("/host"); + revalidatePath(`/host/events/${encodeURIComponent(eventId)}`, "layout"); + redirect(`/host/events/${encodeURIComponent(eventId)}`); +} diff --git a/src/app/host/events/[id]/edit/actions.test.ts b/src/app/host/events/[id]/edit/actions.test.ts index 0ff6340..5dfe560 100644 --- a/src/app/host/events/[id]/edit/actions.test.ts +++ b/src/app/host/events/[id]/edit/actions.test.ts @@ -1,8 +1,15 @@ // @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; import type { Db } from "@/db/client"; +import { events } from "@/db/schema"; import { createTestDb } from "@/db/test-db"; -import { createEvent, getHostEvent } from "@/services/events"; +import { eq } from "drizzle-orm"; +import { + cancelEvent, + createEvent, + findDeletedHostEvent, + getHostEvent, +} from "@/services/events"; const session = vi.hoisted(() => ({ hostId: "host-1" })); const testDb = vi.hoisted(() => ({ db: null as unknown })); @@ -21,7 +28,12 @@ vi.mock("next/navigation", () => ({ }, })); -import { deleteEventAction, updateEventAction } from "./actions"; +import { + cancelEventAction, + deleteEventAction, + uncancelEventAction, + updateEventAction, +} from "./actions"; let db: Db; let eventId: string; @@ -77,11 +89,12 @@ describe("updateEventAction", () => { }); describe("deleteEventAction", () => { - it("deletes the event and redirects to the dashboard with its title", async () => { + it("moves the event to the bin and redirects to the dashboard with its id", async () => { await expect(deleteEventAction(eventId)).rejects.toThrow( - `NEXT_REDIRECT:/host?deleted=${encodeURIComponent("Cookout")}`, + `NEXT_REDIRECT:/host?deleted=${encodeURIComponent(eventId)}`, ); await expect(getHostEvent(db, "host-1", eventId)).rejects.toThrow(); + expect(await findDeletedHostEvent(db, "host-1", eventId)).toMatchObject({ title: "Cookout" }); }); it("a different signed-in host gets a 404 and the event stays", async () => { @@ -90,3 +103,40 @@ describe("deleteEventAction", () => { expect((await getHostEvent(db, "host-1", eventId)).title).toBe("Cookout"); }); }); + +describe("cancelEventAction", () => { + it("cancels the event and returns to the edit page", async () => { + await expect(cancelEventAction(eventId)).rejects.toThrow( + `NEXT_REDIRECT:/host/events/${eventId}/edit`, + ); + expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeInstanceOf(Date); + }); + + it("a different signed-in host gets a 404 and nothing changes", async () => { + session.hostId = "host-2"; + await expect(cancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeNull(); + }); +}); + +describe("uncancelEventAction", () => { + it("reinstates a cancelled event", async () => { + await cancelEvent(db, "host-1", eventId); + await expect(uncancelEventAction(eventId)).rejects.toThrow( + `NEXT_REDIRECT:/host/events/${eventId}/edit`, + ); + expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeNull(); + }); + + it("a different signed-in host gets a 404", async () => { + await cancelEvent(db, "host-1", eventId); + session.hostId = "host-2"; + await expect(uncancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + expect((await getHostEvent(db, "host-1", eventId)).cancelledAt).toBeInstanceOf(Date); + }); + + it("cannot touch a deleted event", async () => { + await db.update(events).set({ deletedAt: new Date() }).where(eq(events.id, eventId)); + await expect(uncancelEventAction(eventId)).rejects.toThrow("NEXT_NOT_FOUND"); + }); +}); diff --git a/src/app/host/events/[id]/edit/actions.ts b/src/app/host/events/[id]/edit/actions.ts index 55a4c30..2f07d15 100644 --- a/src/app/host/events/[id]/edit/actions.ts +++ b/src/app/host/events/[id]/edit/actions.ts @@ -6,7 +6,7 @@ import type { EditEventState } from "@/components/EditEventForm"; import { getDb } from "@/db/client"; import { requireHost } from "@/server/auth"; import { NotFoundError, ValidationError } from "@/services/errors"; -import { deleteEvent, updateEvent } from "@/services/events"; +import { cancelEvent, deleteEvent, uncancelEvent, updateEvent } from "@/services/events"; // The host comes from the session. The event id arrives through `bind`, but in Next 16 // bound arguments are sent to the browser and can be altered, so they are NOT protected: @@ -17,6 +17,10 @@ function text(formData: FormData, key: string): string { return typeof v === "string" ? v : ""; } +function editPath(eventId: string): string { + return `/host/events/${encodeURIComponent(eventId)}/edit`; +} + /** Saves the edited details, then returns to the Guests tab. */ export async function updateEventAction( eventId: string, @@ -42,16 +46,47 @@ export async function updateEventAction( redirect(`/host/events/${encodeURIComponent(eventId)}`); } -/** Deletes the event (and everything on it), then returns to the dashboard to confirm. */ +/** + * Moves the event to the bin (a soft delete) and returns to the dashboard, which + * offers Undo for 30 days. Nothing is destroyed yet: purgeDeletedEvents cleans up. + */ export async function deleteEventAction(eventId: string): Promise { const host = await requireHost(); - let title: string; try { - ({ title } = await deleteEvent(await getDb(), host.id, eventId)); + await deleteEvent(await getDb(), host.id, eventId); + } catch (error) { + if (error instanceof NotFoundError) notFound(); + throw error; + } + revalidatePath("/host"); + revalidatePath(`/host/events/${encodeURIComponent(eventId)}`, "layout"); + redirect(`/host?deleted=${encodeURIComponent(eventId)}`); +} + +/** Calls the event off (guests keep the link, RSVPs close) and returns to the edit page. */ +export async function cancelEventAction(eventId: string): Promise { + const host = await requireHost(); + try { + await cancelEvent(await getDb(), host.id, eventId); } catch (error) { if (error instanceof NotFoundError) notFound(); throw error; } + revalidatePath(`/host/events/${encodeURIComponent(eventId)}`, "layout"); + revalidatePath("/host"); + redirect(editPath(eventId)); +} + +/** Reopens RSVPs on a cancelled event and returns to the edit page. */ +export async function uncancelEventAction(eventId: string): Promise { + const host = await requireHost(); + try { + await uncancelEvent(await getDb(), host.id, eventId); + } catch (error) { + if (error instanceof NotFoundError) notFound(); + throw error; + } + revalidatePath(`/host/events/${encodeURIComponent(eventId)}`, "layout"); revalidatePath("/host"); - redirect(`/host?deleted=${encodeURIComponent(title)}`); + redirect(editPath(eventId)); } diff --git a/src/app/host/events/[id]/edit/page.tsx b/src/app/host/events/[id]/edit/page.tsx index a606c63..bda3010 100644 --- a/src/app/host/events/[id]/edit/page.tsx +++ b/src/app/host/events/[id]/edit/page.tsx @@ -1,8 +1,14 @@ +import { CancelEventSection } from "@/components/CancelEventSection"; import { DeleteEventSection } from "@/components/DeleteEventSection"; import { EditEventForm } from "@/components/EditEventForm"; import { dateToZonedLocal } from "@/lib/zoned-time"; import { loadHostEvent } from "@/server/host-event"; -import { deleteEventAction, updateEventAction } from "./actions"; +import { + cancelEventAction, + deleteEventAction, + uncancelEventAction, + updateEventAction, +} from "./actions"; // Reads the session and the database, so it must never be prerendered. export const dynamic = "force-dynamic"; @@ -10,6 +16,7 @@ export const dynamic = "force-dynamic"; export default async function EditEventPage({ params }: PageProps<"/host/events/[id]/edit">) { // Layouts and pages are authorised independently: check ownership here too. const { event } = await loadHostEvent((await params).id); + const cancelled = event.cancelledAt !== null; return (
@@ -24,6 +31,14 @@ export default async function EditEventPage({ params }: PageProps<"/host/events/ action={updateEventAction.bind(null, event.id)} backHref={`/host/events/${encodeURIComponent(event.id)}`} /> +
+ {event.cancelledAt ? ( +
+ +

+ Guests see this event as cancelled and can't RSVP. Everything is still here, + so you can reinstate it from Edit details. +

+
+ +
+
+
+ ) : null} +
@@ -66,13 +85,16 @@ export default async function EventLayout({
-
-
-

Invite your guests

-

Guests RSVP from this link. No account needed.

-
- -
+ {/* A cancelled event takes no RSVPs, so don't invite anyone to it. */} + {event.cancelledAt ? null : ( +
+
+

Invite your guests

+

Guests RSVP from this link. No account needed.

+
+ +
+ )}
diff --git a/src/app/host/page.tsx b/src/app/host/page.tsx index 2ada45b..09c2d24 100644 --- a/src/app/host/page.tsx +++ b/src/app/host/page.tsx @@ -2,25 +2,66 @@ import Link from "next/link"; import { CopyLinkButton } from "@/components/CopyLinkButton"; import { SignOutButton } from "@/components/SignOutButton"; import { Card } from "@/components/ui/Card"; +import { Pill } from "@/components/ui/Pill"; +import { SubmitButton } from "@/components/ui/SubmitButton"; import { Wordmark } from "@/components/ui/Wordmark"; import { getDb } from "@/db/client"; +import { splitEventsByTime } from "@/lib/event-state"; import { formatWhen } from "@/lib/format"; import { requireHost } from "@/server/auth"; import { getBaseUrl, shareUrl } from "@/server/base-url"; -import { listHostEvents } from "@/services/events"; +import { + findDeletedHostEvent, + listHostEvents, + purgeDeletedEvents, + type EventSummary, +} from "@/services/events"; +import { restoreEventAction } from "./actions"; // Reads the session and the database, so it must never be prerendered. export const dynamic = "force-dynamic"; +function EventTicket({ event, baseUrl }: { event: EventSummary; baseUrl: string }) { + return ( +
  • +
    + + {event.title} + {event.cancelledAt ? ( + + Cancelled + + ) : null} + + {formatWhen(event.startsAt, event.location, event.timezone)} + + +

    + Plan for + ~{event.planFor} +

    +
    +
    + +
    +
  • + ); +} + export default async function HostDashboardPage({ searchParams }: PageProps<"/host">) { const host = await requireHost(); - const [events, baseUrl] = await Promise.all([ - getDb().then((db) => listHostEvents(db, host.id)), - getBaseUrl(), - ]); - // Set by deleteEventAction; the notice stays until the host follows the Dismiss link. + const db = await getDb(); + // Lazy, host-scoped purge: anything deleted more than 30 days ago goes for good. + await purgeDeletedEvents(db, host.id); + const [events, baseUrl] = await Promise.all([listHostEvents(db, host.id), getBaseUrl()]); + + // Set by deleteEventAction (the id, never the title): the notice stays until the + // host follows the Dismiss link or Undo restores the event. const deleted = (await searchParams).deleted; - const deletedTitle = typeof deleted === "string" && deleted.trim() ? deleted : null; + const deletedId = typeof deleted === "string" && deleted.trim() ? deleted : null; + const deletedEvent = deletedId ? await findDeletedHostEvent(db, host.id, deletedId) : null; + + const { upcoming, past } = splitEventsByTime(events); return (
    @@ -41,17 +82,28 @@ export default async function HostDashboardPage({ searchParams }: PageProps<"/ho - {deletedTitle ? ( + {deletedEvent ? (

    - Deleted “{deletedTitle}”. + Deleted “{deletedEvent.title}”.

    - - Dismiss - +
    +
    + + Undo + +
    + + Dismiss + +
    ) : null} @@ -66,30 +118,33 @@ export default async function HostDashboardPage({ searchParams }: PageProps<"/ho ) : ( -
      - {events.map((event) => ( -
    • -
      - - {event.title} - - {formatWhen(event.startsAt, event.location, event.timezone)} - - -

      - Plan for - ~{event.planFor} -

      -
      -
      - -
      -
    • - ))} -
    + <> + {upcoming.length > 0 ? ( +
    +

    + Upcoming +

    +
      + {upcoming.map((event) => ( + + ))} +
    +
    + ) : null} + + {past.length > 0 ? ( +
    + + Past events ({past.length}) + +
      + {past.map((event) => ( + + ))} +
    +
    + ) : null} + )}
    ); diff --git a/src/components/CancelEventSection.test.tsx b/src/components/CancelEventSection.test.tsx new file mode 100644 index 0000000..8b8c0a5 --- /dev/null +++ b/src/components/CancelEventSection.test.tsx @@ -0,0 +1,30 @@ +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { CancelEventSection } from "./CancelEventSection"; + +describe("CancelEventSection", () => { + it("offers Cancel event for a live event and posts the bound action", async () => { + const action = vi.fn(); + render(); + + expect(screen.getByRole("heading", { name: "Cancel event" })).toBeInTheDocument(); + expect(screen.getByText(/Guests keep the link/)).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Reinstate event" })).not.toBeInTheDocument(); + + fireEvent.click(screen.getByRole("button", { name: "Cancel event" })); + await waitFor(() => expect(action).toHaveBeenCalledTimes(1)); + expect(action.mock.calls[0][0]).toBeInstanceOf(FormData); + }); + + it("offers Reinstate event once cancelled", async () => { + const action = vi.fn(); + render(); + + expect(screen.getByRole("heading", { name: "Event cancelled" })).toBeInTheDocument(); + expect(screen.getByText(/can't RSVP/)).toBeInTheDocument(); + expect(screen.queryByRole("button", { name: "Cancel event" })).not.toBeInTheDocument(); + + fireEvent.click(screen.getByRole("button", { name: "Reinstate event" })); + await waitFor(() => expect(action).toHaveBeenCalledTimes(1)); + }); +}); diff --git a/src/components/CancelEventSection.tsx b/src/components/CancelEventSection.tsx new file mode 100644 index 0000000..7652b15 --- /dev/null +++ b/src/components/CancelEventSection.tsx @@ -0,0 +1,41 @@ +"use client"; + +import { Card } from "./ui/Card"; +import { SubmitButton } from "./ui/SubmitButton"; + +/** A server action already bound to the event id (see cancelEventAction). */ +export type CancelEventAction = (formData: FormData) => void | Promise; + +/** + * Cancel or reinstate an event without deleting it. Cancelling keeps the guest + * link working (with RSVPs closed) and everything on the event in place. + */ +export function CancelEventSection({ + cancelled, + action, +}: { + cancelled: boolean; + action: CancelEventAction; +}) { + return ( + +

    {cancelled ? "Event cancelled" : "Cancel event"}

    +

    + {cancelled + ? "Guests see the cancellation notice and can't RSVP. Everything is still here, so you can reinstate it if plans change." + : "Call it off without deleting anything. Guests keep the link, see that it's cancelled, and can't RSVP."} +

    +
    + {cancelled ? ( + + Reinstate event + + ) : ( + + Cancel event + + )} +
    +
    + ); +} diff --git a/src/components/CancelledEventNotice.tsx b/src/components/CancelledEventNotice.tsx new file mode 100644 index 0000000..0f2cf48 --- /dev/null +++ b/src/components/CancelledEventNotice.tsx @@ -0,0 +1,30 @@ +import type { ReactNode } from "react"; + +/** + * The cancellation notice shared by the guest slip and the host masthead. + * Ketchup marks it as the one thing to read first; nothing here takes actions. + */ +export function CancelledEventNotice({ + hostName, + children, +}: { + /** Guests see "Cancelled by "; the host's own screens leave it off. */ + hostName?: string; + children?: ReactNode; +}) { + return ( +
    +

    + {hostName ? <>Cancelled by {hostName} : "Cancelled"} +

    + {children ?? ( +

    + This event was called off, so RSVPs are closed. +

    + )} +
    + ); +} diff --git a/src/components/CopyCancellationButton.test.tsx b/src/components/CopyCancellationButton.test.tsx new file mode 100644 index 0000000..bd8727d --- /dev/null +++ b/src/components/CopyCancellationButton.test.tsx @@ -0,0 +1,34 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; +import { CopyCancellationButton } from "./CopyCancellationButton"; + +describe("CopyCancellationButton", () => { + it("copies a cancellation message with the event title and when", async () => { + const writeText = vi.fn().mockResolvedValue(undefined); + Object.defineProperty(navigator, "clipboard", { configurable: true, value: { writeText } }); + render(); + + fireEvent.click(screen.getByRole("button", { name: "Copy cancellation message" })); + + expect(await screen.findByRole("status")).toHaveTextContent("Cancellation copied"); + expect(writeText).toHaveBeenCalledWith( + "Sorry everyone, Burger Night on Sat, Nov 14 · 7pm is cancelled. Hope to see you at the next one.", + ); + }); + + it("keeps the complete message available when clipboard access fails", async () => { + Object.defineProperty(navigator, "clipboard", { + configurable: true, + value: { writeText: vi.fn().mockRejectedValue(new Error("denied")) }, + }); + render(); + + fireEvent.click(screen.getByRole("button", { name: "Copy cancellation message" })); + + const message = await screen.findByLabelText("Cancellation message"); + expect(message).toHaveValue( + "Sorry everyone, Sunday lunch on Sun, Nov 15 · noon is cancelled. Hope to see you at the next one.", + ); + expect(screen.getByRole("status")).toHaveTextContent("Select the message below"); + }); +}); diff --git a/src/components/CopyCancellationButton.tsx b/src/components/CopyCancellationButton.tsx new file mode 100644 index 0000000..aed100d --- /dev/null +++ b/src/components/CopyCancellationButton.tsx @@ -0,0 +1,54 @@ +"use client"; + +import { useState } from "react"; +import { Button } from "./ui/Button"; + +/** The host copies this into their group chat; Spread never sends it. */ +export function cancellationMessage(title: string, when: string): string { + return `Sorry everyone, ${title} on ${when} is cancelled. Hope to see you at the next one.`; +} + +/** Prepares a cancellation message for the host to send; never sends a message itself. */ +export function CopyCancellationButton({ title, when }: { title: string; when: string }) { + const [state, setState] = useState<"idle" | "copied" | "failed">("idle"); + const message = cancellationMessage(title, when); + + async function copy() { + try { + await navigator.clipboard.writeText(message); + setState("copied"); + } catch { + setState("failed"); + } + } + + return ( +
    + + {state !== "idle" ? ( +

    + {state === "copied" + ? "Cancellation copied. Paste it into your group chat." + : "Couldn't copy. Select the message below and copy it manually."} +

    + ) : null} + {state === "failed" ? ( +