diff --git a/docs/buzz-help.md b/docs/buzz-help.md index 1696b2c33f..10caf67960 100644 --- a/docs/buzz-help.md +++ b/docs/buzz-help.md @@ -738,6 +738,11 @@ and are asked to sign in again in Buzz. is shortened with an ellipsis, hover over it or use the arrow keys to select it and see the full name in a tooltip. A chosen or picked emoji is followed by a space, unless one already follows, so you can keep typing. +- **Reminders**, in the desktop app when the plugin is on: choose **Remind me** + in a sent message's ⋯ menu. A confirmation names the due time, and the message + shows an alarm mark while the reminder is pending. In **Reminders**, a + reminder's due time snoozes it to a preset or **Custom…** date and time. A + reminder's note can't be edited once set. - **Reminder counts** show how many reminders are due in a small capsule on the app tile's corner. It shows `99+` above 99; its accessible label keeps the exact count. diff --git a/docs/plugin-architecture.md b/docs/plugin-architecture.md index bd529c3f90..26b00cac09 100644 --- a/docs/plugin-architecture.md +++ b/docs/plugin-architecture.md @@ -2748,8 +2748,10 @@ the entry appears; throwing matchers are skipped. `icon` renders beside the titl Choosing the entry mounts `component` with `{ message, session, close, canOpen, open }` inside the row, typically a dialog; it stays mounted until it calls `close()`. The optional `marker` receives `{ message, session }` and renders beside the timestamp, such as a -state mark. It renders only while `matches` passes, so hiding the menu entry also -hides its mark. The host owns the menu, focus restoration and row lifetime: a row +state mark. A grouped message shows no byline, so its marks get a caption line of +their own above the message, which takes no space while they render nothing; like +the hidden byline, copying the row leaves them out. It renders only while +`matches` passes, so hiding the menu entry also hides its mark. The host owns the menu, focus restoration and row lifetime: a row with an open action stays mounted while it scrolls out of view. Icon, component and marker failures each render nothing and leave the row and menu usable. The bundled Reminders plugin is the first consumer. diff --git a/src/bundled/reminders/RemindDialog.test.tsx b/src/bundled/reminders/RemindDialog.test.tsx index 285eca628d..0db4543f86 100644 --- a/src/bundled/reminders/RemindDialog.test.tsx +++ b/src/bundled/reminders/RemindDialog.test.tsx @@ -6,6 +6,7 @@ import { fireEvent, render, screen, + waitFor, } from "@testing-library/react"; import { afterEach, assert, expect, it, vi } from "vitest"; import type { ChannelMessage } from "../../features/relay/contracts"; @@ -14,7 +15,9 @@ import { foldMessages } from "../../features/relay/fold"; import type { RelaySession } from "../../features/relay/session"; import { keypair, signed } from "../../features/relay/testing"; import userEvent from "@testing-library/user-event"; -import { RemindDialog } from "./RemindDialog"; +import { ToastProvider } from "../../shared/design-system/ui/Toast"; +import { formatDue, nowSeconds } from "./model"; +import { RemindDialog, SnoozeDialog } from "./RemindDialog"; afterEach(() => { cleanup(); @@ -287,6 +290,209 @@ it("reveals labelled custom fields and saves a future time with the note", async ); }); +it("starts Custom date & time at the next 9am, so it never opens on a past time", async () => { + for (const [clock, date] of [ + ["2026-11-04T08:30", "2026-11-04"], + ["2026-11-04T09:00", "2026-11-05"], + ["2026-11-04T17:45", "2026-11-05"], + ] as const) { + vi.useFakeTimers({ toFake: ["Date"], now: new Date(clock) }); + try { + cleanup(); + const user = userEvent.setup(); + const create = vi.fn(async (..._args: unknown[]) => {}); + dialog(create); + await user.click(screen.getByRole("combobox", { name: "When" })); + await user.click( + await screen.findByRole("option", { name: "Custom date & time" }), + ); + expect(screen.getByLabelText("Reminder date")).toHaveValue(date); + expect(screen.getByLabelText("Reminder time")).toHaveValue("09:00"); + expect(screen.queryByRole("status")).toBeNull(); + const save = screen.getByRole("button", { name: "Set reminder" }); + expect(save).toBeEnabled(); + await user.click(save); + expect(create).toHaveBeenCalledExactlyOnceWith( + expect.objectContaining({ eventId: "m" }), + new Date(`${date}T09:00`).getTime() / 1000, + "", + ); + } finally { + vi.useRealTimers(); + } + } +}); + +it("confirms a saved reminder with its due time", async () => { + const create = vi.fn(async (..._args: unknown[]) => {}); + const close = vi.fn(); + render( + + + , + ); + await userEvent + .setup() + .click(screen.getByRole("button", { name: "Set reminder" })); + expect(close).toHaveBeenCalledOnce(); + const at = create.mock.calls[0]?.[1] as number; + // Sonner mounts a toast transparent, then fades it in. + await waitFor(() => + expect(screen.getByText(`Reminder set for ${formatDue(at)}`)).toBeVisible(), + ); +}); + +it("starts a custom snooze at the reminder's due time while it is ahead, else the next 9am", () => { + const snooze = (due: number | undefined) => + render( + null} + />, + ); + snooze(new Date("2099-06-20T16:15").getTime() / 1000); + expect(screen.getByLabelText("Reminder date")).toHaveValue("2099-06-20"); + expect(screen.getByLabelText("Reminder time")).toHaveValue("16:15"); + expect(screen.queryByRole("status")).toBeNull(); + expect(screen.getByRole("button", { name: "Save" })).toBeEnabled(); + // A past or undisplayable due time starts from the next 9am: today's until + // it passes, then tomorrow's, so Custom… never opens on a past time. + for (const [clock, date] of [ + ["2026-11-04T08:30", "2026-11-04"], + ["2026-11-04T09:00", "2026-11-05"], + ["2026-11-04T17:45", "2026-11-05"], + ] as const) { + vi.useFakeTimers({ toFake: ["Date"], now: new Date(clock) }); + try { + for (const due of [nowSeconds() - 60, 8_640_000_000_001, undefined]) { + cleanup(); + snooze(due); + expect(screen.getByLabelText("Reminder date")).toHaveValue(date); + expect(screen.getByLabelText("Reminder time")).toHaveValue("09:00"); + expect(screen.queryByRole("status")).toBeNull(); + expect(screen.getByRole("button", { name: "Save" })).toBeEnabled(); + } + } finally { + vi.useRealTimers(); + } + } +}); + +it("saves an untouched custom snooze at its due time, even in the hour daylight saving repeats", async () => { + // In Los Angeles 01:30 on 2027-11-07 comes round twice: PDT, then PST. + const first = 1_825_576_200; + const second = first + 3600; + const zone = process.env.TZ; + process.env.TZ = "America/Los_Angeles"; + vi.useFakeTimers({ toFake: ["Date"], now: (first - 3600) * 1000 }); + try { + const user = userEvent.setup(); + const open = (due: number) => { + cleanup(); + const save = vi.fn(async (_notBefore: number) => {}); + const props = { + title: "Snooze reminder", + cancelled: false, + save, + close: vi.fn(), + finalFocus: () => null, + }; + const view = render(); + return { + saved: async () => { + await user.click(screen.getByRole("button", { name: "Save" })); + await waitFor(() => expect(props.close).toHaveBeenCalledOnce()); + return save.mock.calls; + }, + move: (to: number) => + view.rerender(), + }; + }; + const date = () => screen.getByLabelText("Reminder date"); + const time = () => screen.getByLabelText("Reminder time"); + const enter = (value: string) => + fireEvent.change(time(), { target: { value } }); + + const later = open(second); + expect(date()).toHaveValue("2027-11-07"); + expect(time()).toHaveValue("01:30"); + expect(await later.saved()).toEqual([[second]]); + const earlier = open(first); + expect(time()).toHaveValue("01:30"); + expect(await earlier.saved()).toEqual([[first]]); + // An entered time is read as the hour's first pass, as anywhere else. + const edited = open(second); + enter("01:45"); + expect(await edited.saved()).toEqual([[first + 900]]); + const restored = open(second); + enter("01:45"); + enter("01:30"); + expect(await restored.saved()).toEqual([[second]]); + // Another device moving the reminder leaves the fields, and what they + // stand for, as they opened. + const moved = open(second); + moved.move(second + 86_400); + expect(time()).toHaveValue("01:30"); + expect(await moved.saved()).toEqual([[second]]); + // Between the two passes the fields would read as past, so the dialog + // starts from the next 9am instead. + vi.setSystemTime((first + 900) * 1000); + open(second); + expect(date()).toHaveValue("2027-11-07"); + expect(time()).toHaveValue("09:00"); + } finally { + vi.useRealTimers(); + if (zone === undefined) delete process.env.TZ; + else process.env.TZ = zone; + } +}); + +it("keeps a cancelled reminder's custom snooze open, with what was typed, but won't save it", async () => { + const save = vi.fn(() => Promise.reject(new Error("offline"))); + const props = { + title: "Snooze reminder", + due: new Date("2099-06-20T16:15").getTime() / 1000, + save, + close: vi.fn(), + finalFocus: () => null, + }; + const view = render(); + fireEvent.change(screen.getByLabelText("Reminder time"), { + target: { value: "17:30" }, + }); + await userEvent.setup().click(screen.getByRole("button", { name: "Save" })); + expect(await screen.findByRole("alert")).toHaveTextContent( + "The reminder could not be updated. Try again.", + ); + view.rerender(); + expect(screen.getByRole("status")).toHaveTextContent( + "This reminder was cancelled.", + ); + // There is nothing to try again, so the notice replaces the save error. + expect(screen.queryByRole("alert")).toBeNull(); + expect(screen.getByLabelText("Reminder time")).toHaveValue("17:30"); + const button = screen.getByRole("button", { name: "Save" }); + expect(button).toBeDisabled(); + fireEvent.click(button); + expect(save).toHaveBeenCalledOnce(); + expect(screen.getByRole("button", { name: "Cancel" })).toBeEnabled(); +}); + it("stays open after a stray outside click", async () => { const close = vi.fn(); render( diff --git a/src/bundled/reminders/RemindDialog.tsx b/src/bundled/reminders/RemindDialog.tsx index dafa70f407..4d9b7aac96 100644 --- a/src/bundled/reminders/RemindDialog.tsx +++ b/src/bundled/reminders/RemindDialog.tsx @@ -1,4 +1,4 @@ -import { useRef, useState } from "react"; +import { useRef, useState, type RefObject } from "react"; import type { ChannelMessage } from "../../features/relay/contracts"; import type { RelaySession } from "../../features/relay/session"; import { messagePreview } from "../../features/notifications/content"; @@ -8,10 +8,15 @@ import { Field } from "../../shared/design-system/ui/Field"; import { Select } from "../../shared/design-system/ui/Select"; import { Input } from "../../shared/design-system/ui/Input"; import { Textarea } from "../../shared/design-system/ui/Textarea"; +import { useToastNotification } from "../../shared/design-system/ui/Toast"; import { TIME_PRESETS, + customInputs, + dueInputs, formatDue, + nextDayAt9am, parseCustomDateTime, + parseCustomOrDue, shiftedFrom, todayDateString, } from "./model"; @@ -30,10 +35,12 @@ export function RemindDialog({ const fields = useRef(null); const [when, setWhen] = useState(TIME_PRESETS[0]?.label ?? "custom"); const [note, setNote] = useState(""); - const [date, setDate] = useState(todayDateString); - const [time, setTime] = useState("09:00"); + const [start] = useState(defaultCustomInputs); + const [date, setDate] = useState(start.date); + const [time, setTime] = useState(start.time); const [pending, setPending] = useState(false); const [error, setError] = useState(); + const notify = useToastNotification(); const custom = parseCustomDateTime(date, time); const preset = TIME_PRESETS.find((item) => item.label === when); const submit = async (notBefore: number) => { @@ -52,6 +59,7 @@ export function RemindDialog({ notBefore, note, ); + notify(`Reminder set for ${formatDue(notBefore)}`, "success"); close(); } catch { setError("The reminder could not be saved. Try again."); @@ -65,12 +73,7 @@ export function RemindDialog({ dismissOnOutsideClick={false} onOpenChange={(open) => !open && close()} preventClose={pending} - initialFocus={(type) => - type === "touch" || window.matchMedia?.("(pointer: coarse)").matches - ? (fields.current?.closest('[role="dialog"]') ?? null) - : (fields.current?.querySelector('[role="combobox"]') ?? - null) - } + initialFocus={fieldFocus(fields, '[role="combobox"]')} title="Remind me" description="Choose when to be reminded about this message." actions={ @@ -113,38 +116,13 @@ export function RemindDialog({ ]} /> {when === "custom" && ( - <> -
- - setDate(event.target.value)} - /> - - - setTime(event.target.value)} - /> - -
- {custom === null && ( -

- Choose a date and time in the future. -

- )} - {custom !== null && shiftedFrom(time, custom) && ( -

- That time is skipped by daylight saving. The reminder will be - set for {formatDue(custom)}. -

- )} - + )}