@actuate-media/cms-admin 0.17.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/__tests__/lib/page-editor-service-api.test.d.ts +2 -0
  2. package/dist/__tests__/lib/page-editor-service-api.test.d.ts.map +1 -0
  3. package/dist/__tests__/lib/page-editor-service-api.test.js +155 -0
  4. package/dist/__tests__/lib/page-editor-service-api.test.js.map +1 -0
  5. package/dist/__tests__/lib/pages-service-fetch.test.d.ts +2 -0
  6. package/dist/__tests__/lib/pages-service-fetch.test.d.ts.map +1 -0
  7. package/dist/__tests__/lib/pages-service-fetch.test.js +125 -0
  8. package/dist/__tests__/lib/pages-service-fetch.test.js.map +1 -0
  9. package/dist/__tests__/lib/post-editor-service.test.js +75 -23
  10. package/dist/__tests__/lib/post-editor-service.test.js.map +1 -1
  11. package/dist/__tests__/lib/posts-service.test.js +132 -142
  12. package/dist/__tests__/lib/posts-service.test.js.map +1 -1
  13. package/dist/__tests__/views/page-section-editor.test.d.ts +2 -0
  14. package/dist/__tests__/views/page-section-editor.test.d.ts.map +1 -0
  15. package/dist/__tests__/views/page-section-editor.test.js +56 -0
  16. package/dist/__tests__/views/page-section-editor.test.js.map +1 -0
  17. package/dist/__tests__/views/pages-list-view.test.d.ts +2 -0
  18. package/dist/__tests__/views/pages-list-view.test.d.ts.map +1 -0
  19. package/dist/__tests__/views/pages-list-view.test.js +102 -0
  20. package/dist/__tests__/views/pages-list-view.test.js.map +1 -0
  21. package/dist/__tests__/views/posts-list-view.render.test.d.ts +2 -0
  22. package/dist/__tests__/views/posts-list-view.render.test.d.ts.map +1 -0
  23. package/dist/__tests__/views/posts-list-view.render.test.js +73 -0
  24. package/dist/__tests__/views/posts-list-view.render.test.js.map +1 -0
  25. package/dist/actuate-admin.css +1 -1
  26. package/dist/index.d.ts +0 -2
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +0 -2
  29. package/dist/index.js.map +1 -1
  30. package/dist/lib/page-editor-service.d.ts +1 -0
  31. package/dist/lib/page-editor-service.d.ts.map +1 -1
  32. package/dist/lib/page-editor-service.js +55 -0
  33. package/dist/lib/page-editor-service.js.map +1 -1
  34. package/dist/lib/pages-service.d.ts +26 -0
  35. package/dist/lib/pages-service.d.ts.map +1 -1
  36. package/dist/lib/pages-service.js +65 -38
  37. package/dist/lib/pages-service.js.map +1 -1
  38. package/dist/lib/post-editor-service.d.ts +6 -4
  39. package/dist/lib/post-editor-service.d.ts.map +1 -1
  40. package/dist/lib/post-editor-service.js +32 -37
  41. package/dist/lib/post-editor-service.js.map +1 -1
  42. package/dist/lib/posts-service.d.ts.map +1 -1
  43. package/dist/lib/posts-service.js +91 -160
  44. package/dist/lib/posts-service.js.map +1 -1
  45. package/dist/views/Pages/PagesListView.d.ts.map +1 -1
  46. package/dist/views/Pages/PagesListView.js +71 -46
  47. package/dist/views/Pages/PagesListView.js.map +1 -1
  48. package/dist/views/Posts/NewPostPicker.d.ts +5 -4
  49. package/dist/views/Posts/NewPostPicker.d.ts.map +1 -1
  50. package/dist/views/Posts/NewPostPicker.js +5 -4
  51. package/dist/views/Posts/NewPostPicker.js.map +1 -1
  52. package/dist/views/Posts/PostTypesView.d.ts +5 -4
  53. package/dist/views/Posts/PostTypesView.d.ts.map +1 -1
  54. package/dist/views/Posts/PostTypesView.js.map +1 -1
  55. package/dist/views/Posts/PostsListView.d.ts.map +1 -1
  56. package/dist/views/Posts/PostsListView.js +8 -0
  57. package/dist/views/Posts/PostsListView.js.map +1 -1
  58. package/dist/views/page-editor/PagePreview.d.ts.map +1 -1
  59. package/dist/views/page-editor/PagePreview.js +10 -1
  60. package/dist/views/page-editor/PagePreview.js.map +1 -1
  61. package/dist/views/page-editor/PageSectionEditor.d.ts.map +1 -1
  62. package/dist/views/page-editor/PageSectionEditor.js +22 -2
  63. package/dist/views/page-editor/PageSectionEditor.js.map +1 -1
  64. package/dist/views/page-editor/sections/ArticleBodySection.d.ts +6 -2
  65. package/dist/views/page-editor/sections/ArticleBodySection.d.ts.map +1 -1
  66. package/dist/views/page-editor/sections/ArticleBodySection.js +8 -8
  67. package/dist/views/page-editor/sections/ArticleBodySection.js.map +1 -1
  68. package/dist/views/post-editor/PostEditorCanvas.js +7 -1
  69. package/dist/views/post-editor/PostEditorCanvas.js.map +1 -1
  70. package/dist/views/post-editor/PostFieldsModal.d.ts.map +1 -1
  71. package/dist/views/post-editor/PostFieldsModal.js +10 -4
  72. package/dist/views/post-editor/PostFieldsModal.js.map +1 -1
  73. package/dist/views/post-editor/PostHeader.d.ts.map +1 -1
  74. package/dist/views/post-editor/PostHeader.js +4 -3
  75. package/dist/views/post-editor/PostHeader.js.map +1 -1
  76. package/dist/views/post-editor/PostPreview.d.ts.map +1 -1
  77. package/dist/views/post-editor/PostPreview.js +11 -1
  78. package/dist/views/post-editor/PostPreview.js.map +1 -1
  79. package/dist/views/post-editor/PostSectionEditor.d.ts +22 -2
  80. package/dist/views/post-editor/PostSectionEditor.d.ts.map +1 -1
  81. package/dist/views/post-editor/PostSectionEditor.js +4 -1
  82. package/dist/views/post-editor/PostSectionEditor.js.map +1 -1
  83. package/package.json +2 -2
  84. package/src/__tests__/lib/page-editor-service-api.test.ts +180 -0
  85. package/src/__tests__/lib/pages-service-fetch.test.ts +160 -0
  86. package/src/__tests__/lib/post-editor-service.test.ts +78 -23
  87. package/src/__tests__/lib/posts-service.test.ts +134 -163
  88. package/src/__tests__/views/page-section-editor.test.tsx +65 -0
  89. package/src/__tests__/views/pages-list-view.test.tsx +119 -0
  90. package/src/__tests__/views/posts-list-view.render.test.tsx +84 -0
  91. package/src/index.ts +0 -2
  92. package/src/lib/page-editor-service.ts +61 -0
  93. package/src/lib/pages-service.ts +88 -35
  94. package/src/lib/post-editor-service.ts +41 -40
  95. package/src/lib/posts-service.ts +109 -180
  96. package/src/views/Pages/PagesListView.tsx +113 -82
  97. package/src/views/Posts/NewPostPicker.tsx +5 -4
  98. package/src/views/Posts/PostTypesView.tsx +5 -4
  99. package/src/views/Posts/PostsListView.tsx +9 -0
  100. package/src/views/page-editor/PagePreview.tsx +10 -1
  101. package/src/views/page-editor/PageSectionEditor.tsx +25 -1
  102. package/src/views/page-editor/sections/ArticleBodySection.tsx +12 -16
  103. package/src/views/post-editor/PostEditorCanvas.tsx +7 -1
  104. package/src/views/post-editor/PostFieldsModal.tsx +36 -21
  105. package/src/views/post-editor/PostHeader.tsx +4 -4
  106. package/src/views/post-editor/PostPreview.tsx +12 -1
  107. package/src/views/post-editor/PostSectionEditor.tsx +22 -5
  108. package/dist/views/PostEditor.d.ts +0 -6
  109. package/dist/views/PostEditor.d.ts.map +0 -1
  110. package/dist/views/PostEditor.js +0 -79
  111. package/dist/views/PostEditor.js.map +0 -1
  112. package/dist/views/Posts.d.ts +0 -5
  113. package/dist/views/Posts.d.ts.map +0 -1
  114. package/dist/views/Posts.js +0 -73
  115. package/dist/views/Posts.js.map +0 -1
  116. package/src/views/PostEditor.tsx +0 -257
  117. package/src/views/Posts.tsx +0 -375
@@ -1,25 +1,16 @@
1
1
  /**
2
- * Posts service — thin client over the existing REST endpoints,
3
- * shaped around the data the Posts admin area needs.
2
+ * Posts service — thin client over the CMS REST endpoints, shaped around the
3
+ * data the Posts admin area needs.
4
4
  *
5
- * V1 strategy: aggregate across post-typed collections client-side
6
- * (one `/collections/:slug` call per type) and join authors via
7
- * `/users`. Each view component pulls from `fetchPosts()` so when
8
- * a server-side `/api/cms/posts` endpoint lands later, only this
9
- * file changes callers consume the same `Post[]` / `PostType[]`
10
- * shapes.
5
+ * `fetchPosts` calls the server-side aggregate endpoint `GET /posts`, which
6
+ * does the cross-collection filter / sort / paginate at the DB level (one
7
+ * query + one count) and returns the post rows alongside dataset-wide type
8
+ * counts and the contributor list. This keeps the Posts list O(page) instead
9
+ * of pulling every document per type into the browser, and lets it scale past
10
+ * the old ~500-row-per-type client cap.
11
11
  *
12
- * Why not a single `/collections/all` call?
13
- * - Each collection has its own access-control check, so listing
14
- * across collections must respect per-collection `read` rules.
15
- * The existing endpoint enforces this per-call; mixing them
16
- * server-side would need a new auth boundary.
17
- * - V1 traffic is small (the seed is 15 rows); a few extra
18
- * parallel requests are well under the rate-limit budget.
19
- *
20
- * Promote to a single endpoint when:
21
- * - Any post-type collection grows past ~5k rows, OR
22
- * - Page-load latency exceeds 300ms in the admin metrics.
12
+ * Access control still lives server-side: the endpoint only returns rows from
13
+ * collections the caller's scope can read.
23
14
  */
24
15
  import { cmsApi } from './api.js'
25
16
 
@@ -133,204 +124,142 @@ function collectionToPostType(c: CollectionApiRow, count = 0): PostType {
133
124
  }
134
125
  }
135
126
 
136
- // ─── Users → Authors ─────────────────────────────────────────────────
127
+ // ─── Authors ─────────────────────────────────────────────────────────
137
128
 
138
- interface UserApiRow {
139
- id: string
140
- name?: string | null
141
- email: string
129
+ function initialsFromName(name: string | null | undefined): string {
130
+ const trimmed = name?.trim()
131
+ if (!trimmed) return '?'
132
+ const parts = trimmed.split(/\s+/)
133
+ const first = parts[0]?.[0] ?? ''
134
+ const last = parts.length > 1 ? (parts[parts.length - 1]?.[0] ?? '') : ''
135
+ return (first + last).toUpperCase() || '?'
142
136
  }
143
137
 
144
- function initialsFromName(name: string | null | undefined, email: string): string {
145
- if (name && name.trim().length > 0) {
146
- const parts = name.trim().split(/\s+/)
147
- const first = parts[0]?.[0] ?? ''
148
- const last = parts.length > 1 ? (parts[parts.length - 1]?.[0] ?? '') : ''
149
- return (first + last).toUpperCase() || (email[0] ?? '?').toUpperCase()
150
- }
151
- return (email[0] ?? '?').toUpperCase()
152
- }
138
+ // ─── Posts (server-side aggregate) ───────────────────────────────────
153
139
 
154
- async function fetchAuthorsMap(): Promise<Map<string, PostAuthor>> {
155
- const res = await cmsApi<{ users: UserApiRow[] } | UserApiRow[]>('/users?pageSize=200')
156
- const list: UserApiRow[] = Array.isArray(res.data)
157
- ? res.data
158
- : ((res.data as { users?: UserApiRow[] } | null)?.users ?? [])
159
- const map = new Map<string, PostAuthor>()
160
- for (const u of list) {
161
- map.set(u.id, {
162
- id: u.id,
163
- name: u.name?.trim() || u.email,
164
- initials: initialsFromName(u.name, u.email),
165
- })
166
- }
167
- return map
168
- }
169
-
170
- // ─── Posts ───────────────────────────────────────────────────────────
171
-
172
- interface DocumentApiRow {
140
+ interface PostsApiPost {
173
141
  id: string
174
142
  collection: string
175
143
  title: string | null
176
144
  slug: string | null
177
145
  status: PostStatus
178
- createdById: string | null
179
- updatedById: string | null
180
146
  publishedAt: string | null
181
147
  scheduledAt: string | null
182
148
  createdAt: string
183
149
  updatedAt: string
184
- data: Record<string, unknown> | null
150
+ seoScore: number | null
151
+ author: { id: string; name: string } | null
185
152
  }
186
153
 
187
- function extractSeoScore(data: Record<string, unknown> | null): number | null {
188
- if (!data) return null
189
- const candidates = ['seoScore', 'seo_score', 'metaScore']
190
- for (const k of candidates) {
191
- const v = data[k]
192
- if (typeof v === 'number' && Number.isFinite(v)) return v
193
- }
194
- return null
154
+ interface PostsApiType {
155
+ slug: string
156
+ singular: string
157
+ plural: string
158
+ description: string | null
159
+ urlPrefix: string
160
+ color: string | null
161
+ icon: string | null
162
+ fieldCount: number
163
+ count: number
164
+ }
165
+
166
+ interface PostsApiResponse {
167
+ posts: PostsApiPost[]
168
+ total: number
169
+ page: number
170
+ pageSize: number
171
+ types: PostsApiType[]
172
+ authors: Array<{ id: string; name: string }>
195
173
  }
196
174
 
197
- function pickDisplayDate(d: DocumentApiRow): string {
175
+ /** Display date matches the old client behaviour: scheduled/published date when set, else updated. */
176
+ function pickDisplayDate(d: PostsApiPost): string {
198
177
  if (d.status === 'SCHEDULED' && d.scheduledAt) return d.scheduledAt
199
178
  if (d.status === 'PUBLISHED' && d.publishedAt) return d.publishedAt
200
179
  return d.updatedAt
201
180
  }
202
181
 
203
- interface ListEnvelope {
204
- docs: DocumentApiRow[]
205
- total: number
182
+ function buildPostsQueryString(query: PostsQuery): string {
183
+ const params = new URLSearchParams()
184
+ if (query.search?.trim()) params.set('search', query.search.trim())
185
+ if (query.postType && query.postType !== 'all') params.set('postType', query.postType)
186
+ if (query.status && query.status !== 'all') params.set('status', query.status)
187
+ if (query.author && query.author !== 'all') params.set('author', query.author)
188
+ if (query.sortBy) params.set('sortBy', query.sortBy)
189
+ if (query.sortDirection) params.set('sortDirection', query.sortDirection)
190
+ params.set('page', String(Math.max(1, query.page ?? 1)))
191
+ params.set('pageSize', String(Math.max(1, query.pageSize ?? 10)))
192
+ const qs = params.toString()
193
+ return qs ? `?${qs}` : ''
206
194
  }
207
195
 
208
196
  export async function fetchPosts(query: PostsQuery = {}): Promise<PostsResult> {
209
- const cols = await fetchPostCollections()
210
- const authorsMap = await fetchAuthorsMap()
211
-
212
- // Pull every doc for each post type (cap at 500 to avoid runaway
213
- // requests; pagination + filtering still happens client-side over
214
- // this set). When a type grows past 500 docs we'll need the
215
- // server-side aggregate endpoint described in this file's header.
216
- const perTypeRaw = await Promise.all(
217
- cols.map(async (c) => {
218
- const res = await cmsApi<ListEnvelope>(
219
- `/collections/${encodeURIComponent(c.slug)}?pageSize=500&sort=updatedAt&order=desc`,
220
- )
221
- const docs = (res.data?.docs ?? []) as DocumentApiRow[]
222
- return { col: c, docs }
223
- }),
224
- )
225
-
226
- // Map docs → Post objects.
227
- const allPosts: Post[] = []
228
- for (const { col, docs } of perTypeRaw) {
229
- for (const d of docs) {
230
- const author = (d.createdById && authorsMap.get(d.createdById)) ||
231
- (d.updatedById && authorsMap.get(d.updatedById)) || {
232
- id: '',
233
- name: 'Unknown',
234
- initials: '??',
235
- }
236
- allPosts.push({
237
- id: d.id,
238
- title: d.title ?? (d.data?.title as string) ?? '(untitled)',
239
- slug: d.slug ?? (d.data?.slug as string) ?? '',
240
- postTypeId: col.slug,
241
- postTypeName: col.labels.singular,
242
- postTypeAccentColor: col.color ?? null,
243
- authorId: author.id,
244
- authorName: author.name,
245
- authorInitials: author.initials,
246
- status: d.status,
247
- publishDate: d.publishedAt ?? d.scheduledAt ?? null,
248
- updatedDate: pickDisplayDate(d),
249
- seoScore: extractSeoScore(d.data),
250
- createdAt: d.createdAt,
251
- updatedAt: d.updatedAt,
252
- })
253
- }
254
- }
197
+ const page = Math.max(1, query.page ?? 1)
198
+ const pageSize = Math.max(1, query.pageSize ?? 10)
255
199
 
256
- // Compute per-type counts BEFORE filtering — the tabs at the top
257
- // of the page reflect the dataset, not the active query.
258
- const countsBySlug = new Map<string, number>()
259
- for (const p of allPosts) {
260
- countsBySlug.set(p.postTypeId, (countsBySlug.get(p.postTypeId) ?? 0) + 1)
200
+ const res = await cmsApi<PostsApiResponse>(`/posts${buildPostsQueryString(query)}`)
201
+ if (res.error || !res.data) {
202
+ throw new Error(res.error ?? 'Failed to load posts')
261
203
  }
262
- const types: PostType[] = cols.map((c) => collectionToPostType(c, countsBySlug.get(c.slug) ?? 0))
263
-
264
- // Apply filters / search.
265
- const search = (query.search ?? '').trim().toLowerCase()
266
- const filtered = allPosts.filter((p) => {
267
- if (query.postType && query.postType !== 'all' && p.postTypeId !== query.postType) return false
268
- if (query.status && query.status !== 'all' && p.status !== query.status) return false
269
- if (query.author && query.author !== 'all' && p.authorId !== query.author) return false
270
- if (search) {
271
- const haystack = `${p.title} ${p.slug} ${p.authorName} ${p.postTypeName}`.toLowerCase()
272
- if (!haystack.includes(search)) return false
204
+ const r = res.data
205
+
206
+ const typeBySlug = new Map<string, PostsApiType>()
207
+ for (const t of r.types) typeBySlug.set(t.slug, t)
208
+
209
+ const types: PostType[] = r.types.map((t) => ({
210
+ id: t.slug,
211
+ slug: t.slug,
212
+ name: t.plural,
213
+ singularName: t.singular,
214
+ description: t.description ?? null,
215
+ urlBase: `/${t.urlPrefix || t.slug}`,
216
+ accentColor: t.color ?? null,
217
+ icon: t.icon ?? null,
218
+ postCount: t.count,
219
+ fieldCount: t.fieldCount,
220
+ active: true,
221
+ }))
222
+
223
+ const posts: Post[] = r.posts.map((d) => {
224
+ const type = typeBySlug.get(d.collection)
225
+ const authorName = d.author?.name?.trim() || 'Unknown'
226
+ return {
227
+ id: d.id,
228
+ title: d.title ?? '(untitled)',
229
+ slug: d.slug ?? '',
230
+ postTypeId: d.collection,
231
+ postTypeName: type?.singular ?? d.collection,
232
+ postTypeAccentColor: type?.color ?? null,
233
+ authorId: d.author?.id ?? '',
234
+ authorName,
235
+ authorInitials: d.author ? initialsFromName(d.author.name) : '??',
236
+ status: d.status,
237
+ publishDate: d.publishedAt ?? d.scheduledAt ?? null,
238
+ updatedDate: pickDisplayDate(d),
239
+ seoScore: d.seoScore,
240
+ createdAt: d.createdAt,
241
+ updatedAt: d.updatedAt,
273
242
  }
274
- return true
275
243
  })
276
244
 
277
- // Sort.
278
- const sortBy = query.sortBy ?? 'date'
279
- const dir = query.sortDirection ?? 'desc'
280
- filtered.sort((a, b) => {
281
- const cmp = comparePostsFor(sortBy, a, b)
282
- return dir === 'asc' ? cmp : -cmp
283
- })
284
-
285
- // Paginate.
286
- const page = Math.max(1, query.page ?? 1)
287
- const pageSize = Math.max(1, query.pageSize ?? 10)
288
- const start = (page - 1) * pageSize
289
- const paged = filtered.slice(start, start + pageSize)
290
-
291
- // Unique authors from the full set (so the dropdown lists every
292
- // contributor, not just the currently visible page).
293
- const authorIds = new Set<string>()
294
- const authors: PostAuthor[] = []
295
- for (const p of allPosts) {
296
- if (p.authorId && !authorIds.has(p.authorId)) {
297
- authorIds.add(p.authorId)
298
- authors.push({ id: p.authorId, name: p.authorName, initials: p.authorInitials })
299
- }
300
- }
301
- authors.sort((a, b) => a.name.localeCompare(b.name))
245
+ const authors: PostAuthor[] = r.authors
246
+ .map((a) => ({
247
+ id: a.id,
248
+ name: a.name?.trim() || 'Unknown',
249
+ initials: initialsFromName(a.name),
250
+ }))
251
+ .sort((a, b) => a.name.localeCompare(b.name))
302
252
 
303
253
  return {
304
- posts: paged,
305
- total: filtered.length,
306
- page,
307
- pageSize,
254
+ posts,
255
+ total: r.total,
256
+ page: r.page ?? page,
257
+ pageSize: r.pageSize ?? pageSize,
308
258
  types,
309
259
  authors,
310
260
  }
311
261
  }
312
262
 
313
- function comparePostsFor(by: NonNullable<PostsQuery['sortBy']>, a: Post, b: Post): number {
314
- switch (by) {
315
- case 'title':
316
- return a.title.localeCompare(b.title)
317
- case 'type':
318
- return a.postTypeName.localeCompare(b.postTypeName)
319
- case 'author':
320
- return a.authorName.localeCompare(b.authorName)
321
- case 'status':
322
- return a.status.localeCompare(b.status)
323
- case 'seo': {
324
- const av = a.seoScore ?? -1
325
- const bv = b.seoScore ?? -1
326
- return av - bv
327
- }
328
- case 'date':
329
- default:
330
- return new Date(a.updatedDate).getTime() - new Date(b.updatedDate).getTime()
331
- }
332
- }
333
-
334
263
  // ─── Bulk mutations ──────────────────────────────────────────────────
335
264
 
336
265
  export async function bulkUpdatePosts(