1- import type { EntryToCreate , MappingReviewSuspendPayload , PreviewPayload } from '@types' ;
1+ import type { EntryToCreate , PreviewPayload } from '@types' ;
22import { collectReferencedTempIdsFromEntry } from '../services/referenceResolution' ;
33import { type ContentTypeDisplayInfo } from '../services/contentTypeService' ;
44import { orderEntriesByCreationOrder } from './previewPayload' ;
55import { getEntryDisplayTitle } from './getEntryDisplayTitle' ;
6- import { isPreviewPayload } from './utils' ;
76
87export interface CheckboxEntryListRow {
98 id : string ;
@@ -15,12 +14,6 @@ export interface CheckboxEntryListRow {
1514
1615export type ContentTypeDisplayInfoMap = ReadonlyMap < string , ContentTypeDisplayInfo > ;
1716
18- interface OverviewSourceEntry {
19- tempId : string ;
20- contentTypeName : string ;
21- entryTitle ?: string ;
22- }
23-
2417interface OrderedEntriesContext {
2518 entries : EntryToCreate [ ] ;
2619 entriesByTempId : Map < string , EntryToCreate > ;
@@ -34,17 +27,6 @@ interface TreeBuildContext {
3427 defaultLocale ?: string ;
3528}
3629
37- interface OverviewSourceTreeBuildContext {
38- orderedEntriesContext : OrderedOverviewSourceEntriesContext ;
39- childTempIdsByParentTempId : Map < string , string [ ] > ;
40- }
41-
42- interface OrderedOverviewSourceEntriesContext {
43- entries : OverviewSourceEntry [ ] ;
44- entriesByTempId : Map < string , OverviewSourceEntry > ;
45- orderedIndexByTempId : Map < string , number > ;
46- }
47-
4830function resolveContentTypeLabel (
4931 contentTypeId : string ,
5032 contentTypeDisplayInfoMap ?: ContentTypeDisplayInfoMap
@@ -119,24 +101,6 @@ function buildOrderedEntriesContext(orderedEntries: EntryToCreate[]): OrderedEnt
119101 } ;
120102}
121103
122- function buildOrderedOverviewSourceEntriesContext (
123- orderedEntries : OverviewSourceEntry [ ]
124- ) : OrderedOverviewSourceEntriesContext {
125- const entriesByTempId = new Map < string , OverviewSourceEntry > ( ) ;
126- const orderedIndexByTempId = new Map < string , number > ( ) ;
127-
128- orderedEntries . forEach ( ( entry , index ) => {
129- entriesByTempId . set ( entry . tempId , entry ) ;
130- orderedIndexByTempId . set ( entry . tempId , index ) ;
131- } ) ;
132-
133- return {
134- entries : orderedEntries ,
135- entriesByTempId,
136- orderedIndexByTempId,
137- } ;
138- }
139-
140104function collectChildTempIds ( childTempIdsByParentTempId : Map < string , string [ ] > ) : Set < string > {
141105 return new Set ( [ ...childTempIdsByParentTempId . values ( ) ] . flat ( ) ) ;
142106}
@@ -163,35 +127,6 @@ function buildRowTreeForTempId(
163127 ) ;
164128}
165129
166- function buildOverviewRow (
167- entry : OverviewSourceEntry ,
168- entryIndex : number ,
169- children : CheckboxEntryListRow [ ]
170- ) {
171- return {
172- id : entry . tempId ,
173- entryIndex,
174- contentTypeName : entry . contentTypeName ,
175- entryTitle : entry . entryTitle ,
176- children,
177- } ;
178- }
179-
180- function buildOverviewRowTreeForTempId (
181- tempId : string ,
182- context : OverviewSourceTreeBuildContext
183- ) : CheckboxEntryListRow | undefined {
184- const entry = context . orderedEntriesContext . entriesByTempId . get ( tempId ) ;
185- if ( ! entry ) return undefined ;
186-
187- const entryIndex = context . orderedEntriesContext . orderedIndexByTempId . get ( tempId ) ?? - 1 ;
188- const childRows = ( context . childTempIdsByParentTempId . get ( tempId ) ?? [ ] )
189- . map ( ( childTempId ) => buildOverviewRowTreeForTempId ( childTempId , context ) )
190- . filter ( ( row ) : row is CheckboxEntryListRow => row !== undefined ) ;
191-
192- return buildOverviewRow ( entry , entryIndex , childRows ) ;
193- }
194-
195130function buildTreeRootRows (
196131 context : TreeBuildContext ,
197132 childTempIds : Set < string >
@@ -209,22 +144,6 @@ function buildTreeRootRows(
209144 return roots ;
210145}
211146
212- function buildOverviewTreeRootRows (
213- context : OverviewSourceTreeBuildContext ,
214- childTempIds : Set < string >
215- ) : CheckboxEntryListRow [ ] {
216- const roots : CheckboxEntryListRow [ ] = [ ] ;
217-
218- for ( const entry of context . orderedEntriesContext . entries ) {
219- if ( childTempIds . has ( entry . tempId ) ) continue ;
220-
221- const row = buildOverviewRowTreeForTempId ( entry . tempId , context ) ;
222- if ( row ) roots . push ( row ) ;
223- }
224-
225- return roots ;
226- }
227-
228147function buildFlatRows (
229148 orderedEntries : EntryToCreate [ ] ,
230149 contentTypeDisplayInfoMap ?: ContentTypeDisplayInfoMap ,
@@ -238,95 +157,12 @@ function buildFlatRows(
238157 return rows ;
239158}
240159
241- function buildFlatOverviewRows ( orderedEntries : OverviewSourceEntry [ ] ) : CheckboxEntryListRow [ ] {
242- return orderedEntries . map ( ( entry , index ) => buildOverviewRow ( entry , index , [ ] ) ) ;
243- }
244-
245- function orderMappingReviewEntriesByCreationOrder (
246- entries : MappingReviewSuspendPayload [ 'entryBlockGraph' ] [ 'entries' ] ,
247- creationOrder : string [ ] | undefined
248- ) : MappingReviewSuspendPayload [ 'entryBlockGraph' ] [ 'entries' ] {
249- if ( ! creationOrder || creationOrder . length === 0 ) {
250- return entries ;
251- }
252-
253- const byTempId = new Map <
254- string ,
255- MappingReviewSuspendPayload [ 'entryBlockGraph' ] [ 'entries' ] [ number ]
256- > ( ) ;
257- for ( const entry of entries ) {
258- if ( entry . tempId ) {
259- byTempId . set ( entry . tempId , entry ) ;
260- }
261- }
262-
263- const ordered : MappingReviewSuspendPayload [ 'entryBlockGraph' ] [ 'entries' ] = [ ] ;
264- const placed = new Set < string > ( ) ;
265-
266- for ( const tempId of creationOrder ) {
267- if ( placed . has ( tempId ) ) continue ;
268- const entry = byTempId . get ( tempId ) ;
269- if ( entry ) {
270- ordered . push ( entry ) ;
271- placed . add ( tempId ) ;
272- }
273- }
274-
275- for ( const entry of entries ) {
276- if ( ! entry . tempId || ! placed . has ( entry . tempId ) ) {
277- ordered . push ( entry ) ;
278- if ( entry . tempId ) {
279- placed . add ( entry . tempId ) ;
280- }
281- }
282- }
283-
284- return ordered ;
285- }
286-
287- function buildChildTempIdsByParentTempIdFromReferenceGraph (
288- orderedEntriesContext : OrderedOverviewSourceEntriesContext ,
289- referenceGraph : MappingReviewSuspendPayload [ 'referenceGraph' ]
290- ) : Map < string , string [ ] > {
291- const childTempIdsByParentTempId = new Map < string , string [ ] > ( ) ;
292- const assignedChildIds = new Set < string > ( ) ;
293- const childTempIdsByParent = new Map < string , string [ ] > ( ) ;
294-
295- for ( const edge of referenceGraph . edges ?? [ ] ) {
296- if ( typeof edge . from !== 'string' || typeof edge . to !== 'string' ) {
297- continue ;
298- }
299-
300- const existing = childTempIdsByParent . get ( edge . from ) ?? [ ] ;
301- existing . push ( edge . to ) ;
302- childTempIdsByParent . set ( edge . from , existing ) ;
303- }
304-
305- for ( const entry of orderedEntriesContext . entries ) {
306- const referencedTempIds = childTempIdsByParent . get ( entry . tempId ) ?? [ ] ;
307-
308- for ( const referencedTempId of referencedTempIds ) {
309- if ( referencedTempId === entry . tempId ) continue ;
310- if ( ! orderedEntriesContext . entriesByTempId . has ( referencedTempId ) ) continue ;
311- if ( assignedChildIds . has ( referencedTempId ) ) continue ;
312-
313- assignedChildIds . add ( referencedTempId ) ;
314- const childTempIds = childTempIdsByParentTempId . get ( entry . tempId ) ?? [ ] ;
315- childTempIds . push ( referencedTempId ) ;
316- childTempIdsByParentTempId . set ( entry . tempId , childTempIds ) ;
317- }
318- }
319-
320- return childTempIdsByParentTempId ;
321- }
322-
323160export function buildCheckboxEntryList (
324161 payload : PreviewPayload ,
325162 contentTypeDisplayInfoMap ?: ContentTypeDisplayInfoMap ,
326163 defaultLocale ?: string
327164) : CheckboxEntryListRow [ ] {
328165 const { entries, referenceGraph } = payload ;
329-
330166 if ( entries . length === 0 ) {
331167 return [ ] ;
332168 }
@@ -352,57 +188,6 @@ export function buildCheckboxEntryList(
352188 return roots ;
353189}
354190
355- export function buildCheckboxEntryListFromMappingReviewPayload (
356- payload : MappingReviewSuspendPayload
357- ) : CheckboxEntryListRow [ ] {
358- const orderedGraphEntries = orderMappingReviewEntriesByCreationOrder (
359- payload . entryBlockGraph . entries ,
360- payload . referenceGraph . creationOrder
361- ) ;
362-
363- const orderedOverviewEntries : OverviewSourceEntry [ ] = orderedGraphEntries
364- . map ( ( entry ) => {
365- const tempId = entry . tempId ?. trim ( ) ;
366- if ( ! tempId ) {
367- return null ;
368- }
369-
370- const matchingContentType = payload . contentTypes . find (
371- ( contentType ) => contentType . sys . id === entry . contentTypeId
372- ) ;
373-
374- return {
375- tempId,
376- contentTypeName : matchingContentType ?. name ?. trim ( ) || entry . contentTypeId ,
377- entryTitle : tempId ,
378- } satisfies OverviewSourceEntry ;
379- } )
380- . filter ( ( entry ) : entry is OverviewSourceEntry => entry !== null ) ;
381-
382- if ( orderedOverviewEntries . length === 0 ) {
383- return [ ] ;
384- }
385-
386- const orderedEntriesContext = buildOrderedOverviewSourceEntriesContext ( orderedOverviewEntries ) ;
387- const childTempIdsByParentTempId = buildChildTempIdsByParentTempIdFromReferenceGraph (
388- orderedEntriesContext ,
389- payload . referenceGraph
390- ) ;
391- const roots = buildOverviewTreeRootRows (
392- {
393- orderedEntriesContext,
394- childTempIdsByParentTempId,
395- } ,
396- collectChildTempIds ( childTempIdsByParentTempId )
397- ) ;
398-
399- if ( roots . length === 0 ) {
400- return buildFlatOverviewRows ( orderedOverviewEntries ) ;
401- }
402-
403- return roots ;
404- }
405-
406191export function collectCheckboxEntryListRowIds ( rows : CheckboxEntryListRow [ ] ) : string [ ] {
407192 return rows . flatMap ( ( row ) => [ row . id , ...collectCheckboxEntryListRowIds ( row . children ) ] ) ;
408193}
0 commit comments