| 27 | * @returns The updated state |
| 28 | */ |
| 29 | export function walkAppState<UserData extends Data = Data>( |
| 30 | state: PrivateAppState<UserData>, |
| 31 | config: Config, |
| 32 | mapContent: ( |
| 33 | content: Content, |
| 34 | zoneCompound: string, |
| 35 | zoneType: ZoneType |
| 36 | ) => Content | void = (content) => content, |
| 37 | mapNodeOrSkip: ( |
| 38 | item: ComponentData, |
| 39 | path: string[], |
| 40 | index: number |
| 41 | ) => ComponentData | null = (item) => item |
| 42 | ): PrivateAppState<UserData> { |
| 43 | let newZones: Record<string, Content> = {}; |
| 44 | const newZoneIndex: ZoneIndex = {}; |
| 45 | const newNodeIndex: NodeIndex = {}; |
| 46 | |
| 47 | const processContent = ( |
| 48 | path: string[], |
| 49 | zoneCompound: string, |
| 50 | content: Content, |
| 51 | zoneType: ZoneType, |
| 52 | newId?: string |
| 53 | ): [string, Content] => { |
| 54 | const [parentId] = zoneCompound.split(":"); |
| 55 | const mappedContent = |
| 56 | (mapContent(content, zoneCompound, zoneType) ?? content) || []; |
| 57 | |
| 58 | const [_, zone] = zoneCompound.split(":"); |
| 59 | const newZoneCompound = `${newId || parentId}:${zone}`; |
| 60 | |
| 61 | const newContent = mappedContent.map((zoneChild, index) => |
| 62 | processItem(zoneChild, [...path, newZoneCompound], index) |
| 63 | ); |
| 64 | |
| 65 | newZoneIndex[newZoneCompound] = { |
| 66 | contentIds: newContent.map((item) => item.props.id), |
| 67 | type: zoneType, |
| 68 | }; |
| 69 | |
| 70 | return [newZoneCompound, newContent]; |
| 71 | }; |
| 72 | |
| 73 | const processRelatedZones = ( |
| 74 | item: ComponentData, |
| 75 | newId: string, |
| 76 | initialPath: string[] |
| 77 | ) => { |
| 78 | forRelatedZones( |
| 79 | item, |
| 80 | state.data, |
| 81 | (relatedPath, relatedZoneCompound, relatedContent) => { |
| 82 | const [zoneCompound, newContent] = processContent( |
| 83 | relatedPath, |
| 84 | relatedZoneCompound, |
| 85 | relatedContent, |
| 86 | "dropzone", |