- announcements collection: info/success/warning/error variants, banner/modal/dialog/toast display types, up to 3 action buttons, dismissible flag, activeFrom/activeUntil windows, audience targeting (all/users/roles) and path include/exclude matching copied from the Shims pattern - pure client-safe evaluation lib (enabled, time window, audience via roleDoc IDs, path match) plus per-browser localStorage dismissals - AnnouncementHost mounted in the (frontend) layout: banners render inline, toasts via sonner custom markup, modals and confirmation dialogs queue one at a time - permissions announcements:create/read/update/delete + admin:announcements:manage - description is Lexical richText rendered via RichText/lexical-content on all surfaces; role targeting uses the dynamic Roles collection - hand-written migrations with snapshot sidecars (collection, roleDocs targeting, description richtext conversion) + integration tests (12 passing)
225 lines
6.3 KiB
TypeScript
225 lines
6.3 KiB
TypeScript
import { CollectionConfig } from "payload";
|
|
import { requirePermission } from "@/utils/access-control/hasPermission";
|
|
|
|
/**
|
|
* Announcements: admin-authored notifications rendered in the frontend app
|
|
* as banners, modals, confirmation dialogs, or toasts. Evaluation (time
|
|
* window, audience, path matching) happens client-side via the pure helpers
|
|
* in `src/lib/announcements/evaluate.ts`, mirroring the Shims pattern.
|
|
*/
|
|
export const Announcements: CollectionConfig = {
|
|
slug: "announcements",
|
|
labels: {
|
|
singular: "Announcement",
|
|
plural: "Announcements",
|
|
},
|
|
admin: {
|
|
group: "Announcements",
|
|
},
|
|
access: {
|
|
create: requirePermission("announcements:create"),
|
|
read: requirePermission("announcements:read"),
|
|
update: requirePermission("announcements:update"),
|
|
delete: requirePermission("announcements:delete"),
|
|
},
|
|
defaultSort: "-createdAt",
|
|
fields: [
|
|
{
|
|
name: "title",
|
|
type: "text",
|
|
required: true,
|
|
admin: {
|
|
description: "Headline shown in every display type.",
|
|
},
|
|
},
|
|
{
|
|
name: "subtitle",
|
|
type: "text",
|
|
admin: {
|
|
description: "Optional secondary line shown under the title.",
|
|
},
|
|
},
|
|
{
|
|
name: "description",
|
|
type: "richText",
|
|
admin: {
|
|
description: "Optional longer body text (rendered with formatting on the frontend).",
|
|
},
|
|
},
|
|
{
|
|
name: "variant",
|
|
type: "select",
|
|
required: true,
|
|
defaultValue: "info",
|
|
options: [
|
|
{ label: "Info", value: "info" },
|
|
{ label: "Success", value: "success" },
|
|
{ label: "Warning", value: "warning" },
|
|
{ label: "Error", value: "error" },
|
|
],
|
|
admin: {
|
|
description: "Colors the announcement (and the toast style).",
|
|
},
|
|
},
|
|
{
|
|
name: "displayType",
|
|
type: "select",
|
|
required: true,
|
|
defaultValue: "banner",
|
|
options: [
|
|
{ label: "Banner", value: "banner" },
|
|
{ label: "Modal", value: "modal" },
|
|
{ label: "Confirmation Dialog", value: "dialog" },
|
|
{ label: "Toast", value: "toast" },
|
|
],
|
|
admin: {
|
|
description:
|
|
"How the announcement is presented. Dialogs show their configured buttons; modals get a default OK button when none are set.",
|
|
},
|
|
},
|
|
{
|
|
name: "enabled",
|
|
type: "checkbox",
|
|
defaultValue: true,
|
|
admin: {
|
|
description: "Disabled announcements are never shown, regardless of targeting rules.",
|
|
},
|
|
},
|
|
{
|
|
name: "dismissible",
|
|
type: "checkbox",
|
|
defaultValue: true,
|
|
admin: {
|
|
description:
|
|
"Whether the user can dismiss it for this browser until its window ends. Non-dismissible announcements reappear on every page load while active.",
|
|
},
|
|
},
|
|
{
|
|
name: "activeFrom",
|
|
type: "date",
|
|
admin: {
|
|
date: { pickerAppearance: "dayAndTime" },
|
|
description: "Optional. The announcement is not shown before this moment.",
|
|
},
|
|
},
|
|
{
|
|
name: "activeUntil",
|
|
type: "date",
|
|
admin: {
|
|
date: { pickerAppearance: "dayAndTime" },
|
|
description: "Optional. The announcement stops being shown after this moment.",
|
|
},
|
|
},
|
|
{
|
|
name: "buttons",
|
|
type: "array",
|
|
label: "Buttons",
|
|
maxRows: 3,
|
|
admin: {
|
|
description:
|
|
"Optional action buttons (used by confirmation dialogs and modals; banners and toasts show them inline where supported).",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "label",
|
|
type: "text",
|
|
required: true,
|
|
},
|
|
{
|
|
name: "href",
|
|
type: "text",
|
|
admin: {
|
|
description:
|
|
"Optional internal path to navigate to when clicked (e.g. '/logistics/shipments'). Leave empty for a close-only button.",
|
|
},
|
|
},
|
|
{
|
|
name: "style",
|
|
type: "select",
|
|
defaultValue: "default",
|
|
options: [
|
|
{ label: "Default", value: "default" },
|
|
{ label: "Outline", value: "outline" },
|
|
{ label: "Destructive", value: "destructive" },
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "targeting",
|
|
type: "group",
|
|
label: "Targeting",
|
|
admin: {
|
|
description: "Who should this announcement be shown to?",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "audience",
|
|
type: "select",
|
|
required: true,
|
|
defaultValue: "all",
|
|
options: [
|
|
{ label: "All Users", value: "all" },
|
|
{ label: "Specific Users", value: "users" },
|
|
{ label: "By Role", value: "roles" },
|
|
],
|
|
},
|
|
{
|
|
name: "users",
|
|
type: "relationship",
|
|
relationTo: "users",
|
|
hasMany: true,
|
|
admin: {
|
|
condition: (data, siblingData) =>
|
|
(siblingData?.audience ?? data?.targeting?.audience) === "users",
|
|
description: "Only these specific users will see this announcement.",
|
|
},
|
|
},
|
|
{
|
|
name: "roles",
|
|
type: "relationship",
|
|
relationTo: "roles",
|
|
hasMany: true,
|
|
admin: {
|
|
condition: (data, siblingData) =>
|
|
(siblingData?.audience ?? data?.targeting?.audience) === "roles",
|
|
description:
|
|
"Users holding any of these roles (the dynamic Roles collection) will see this announcement.",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: "paths",
|
|
type: "group",
|
|
label: "Path Matching",
|
|
admin: {
|
|
description: "On which pages should this announcement be shown?",
|
|
},
|
|
fields: [
|
|
{
|
|
name: "matchType",
|
|
type: "select",
|
|
required: true,
|
|
defaultValue: "all",
|
|
options: [
|
|
{ label: "All Pages", value: "all" },
|
|
{ label: "Include Only", value: "include" },
|
|
{ label: "Exclude", value: "exclude" },
|
|
],
|
|
},
|
|
{
|
|
name: "paths",
|
|
type: "text",
|
|
hasMany: true,
|
|
admin: {
|
|
condition: (data, siblingData) =>
|
|
(siblingData?.matchType ?? data?.paths?.matchType) !== "all",
|
|
description:
|
|
"URL path prefixes to match. E.g. '/logistics' matches /logistics and /logistics/market. Leading slash required.",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|