import type { CollectionConfig } from "payload"; import { ptfLexicalEditor } from "@/lib/lexical/editor"; import hasRoles from "@/utils/access-control/hasRoles"; function isStoryAuthor(user: Parameters[1]): boolean { return hasRoles(["admin", "developer"], user); } export const StoryBeats: CollectionConfig = { slug: "story-beats", labels: { singular: "Chronicle Entry", plural: "Chronicle Entries", }, admin: { group: "Chronicle", useAsTitle: "title", defaultColumns: ["title", "status", "occurredAt", "order", "mission"], listSearchableFields: ["title", "body"], description: "Chronicle entries. Drafts are visible to staff only; published entries are readable by any logged-in user on /story.", }, access: { // Guests see nothing; admin/developer see everything; any other logged-in // user sees only published beats. read: ({ req }) => { if (!req.user) return false; if (isStoryAuthor(req.user)) return true; return { status: { equals: "published" } }; }, create: ({ req }) => isStoryAuthor(req.user), update: ({ req }) => isStoryAuthor(req.user), delete: ({ req }) => isStoryAuthor(req.user), }, fields: [ { name: "title", type: "text", required: true, }, { name: "occurredAt", type: "date", admin: { description: "In-story timestamp shown with the chapter on /story. Does not affect chapter order.", }, }, { name: "order", type: "number", admin: { description: "Manual chapter sequence: if any entry has an order, /story displays entries in that exact order (ascending, first at top). With no order values, the newest entry shows first and chapter numbers follow creation date.", }, }, { name: "status", type: "select", required: true, defaultValue: "draft", index: true, options: [ { label: "Draft", value: "draft" }, { label: "Published", value: "published" }, ], }, { name: "body", type: "richText", required: true, editor: ptfLexicalEditor(), admin: { description: "Rich text body of the entry, rendered on /story with the Lexical renderer.", }, }, { name: "mission", type: "relationship", relationTo: "missions", admin: { description: "Optional mission this entry is tied to. Entries may stand alone.", }, }, { name: "voiceover", type: "relationship", relationTo: "story-voiceovers", admin: { description: "Optional audio narration for this entry. Rendered as a player on /story.", }, }, ], };