Blocks / Notification preferences

Notification preferences

Events by channels, a switch per group, a digest, quiet hours and a way back to the defaults.

import { NotificationPreferences, inQuietHours, setChannel, setChannelForEvents, channelCoverage, samePrefs } from "@forgeron/ui";
/01 → Default
EventEmailPushChatSMS not set up
Activity
Someone mentions youIn a comment or a message.
Something is assigned to you
A due date is close
Billing
An invoice is paid
An invoice is lateOnce a day at most.
Security
A new device signs in
DigestOne message that gathers what is not urgent.
Quiet hoursNothing is held back.
2200
0700
import {
  NotificationPreferences,
  type NotificationChannel,
  type NotificationEvent,
  type NotificationPrefs,
} from "@forgeron/ui";
import { useState } from "react";

const events: NotificationEvent[] = [
  {
    id: "mention",
    group: "Activity",
    label: "Someone mentions you",
    description: "In a comment or a message.",
  },
  { id: "assign", group: "Activity", label: "Something is assigned to you" },
  { id: "due", group: "Activity", label: "A due date is close" },
  { id: "invoice-paid", group: "Billing", label: "An invoice is paid" },
  {
    id: "invoice-late",
    group: "Billing",
    label: "An invoice is late",
    description: "Once a day at most.",
  },
  { id: "sign-in", group: "Security", label: "A new device signs in" },
];

const channels: NotificationChannel[] = [
  { id: "email", label: "Email" },
  { id: "push", label: "Push" },
  { id: "chat", label: "Chat" },
  { id: "sms", label: "SMS", unavailable: true },
];

const defaults: NotificationPrefs = {
  channels: {
    mention: ["email", "push"],
    assign: ["email"],
    "invoice-paid": ["email"],
    "sign-in": ["email", "push"],
  },
  digest: "daily",
  quiet: { enabled: false, from: "22:00", to: "07:00" },
};

export default function Default() {
  const [prefs, setPrefs] = useState<NotificationPrefs>(defaults);
  return (
    <div style={{ width: "100%", maxWidth: 760 }}>
      <NotificationPreferences
        label="Notification preferences"
        events={events}
        channels={channels}
        value={prefs}
        onChange={setPrefs}
        defaults={defaults}
      />
    </div>
  );
}
API → Props
PropTypeDefaultDescription
labelstring—Names the matrix.
events{ id, label, description?, group? }[]—The rows; a group gets a header row with a switch per channel.
channels{ id, label, unavailable? }[]—The columns; unavailable ones are dimmed and cannot be turned on.
value / defaultValue / onChange{ channels: Record<eventId, channelId[]>, digest, quiet: { enabled, from, to } }—What was chosen. digest: off, hourly, daily, weekly.
defaultsNotificationPrefs—What Reset to default goes back to; without it, no button.
digest / quietHoursbooleantrueShow or hide each block.
disabledboolean—Locks everything.
classNamestring—Extra classes.