File size: 1,086 Bytes
372531f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export const consolidateSourceAndImageBlocks = (groupedData: any[]) => {
  // Consolidate sourceBlocks
  const consolidatedSourceBlock = {
    type: 'sourceBlock',
    items: groupedData
      .filter(item => item.type === 'sourceBlock')
      .flatMap(block => block.items || [])
      .filter((item, index, self) => 
        index === self.findIndex(t => t.url === item.url)
      )
  };

  // Consolidate imageBlocks
  const consolidatedImageBlock = {
    type: 'imagesBlock',
    metadata: groupedData
      .filter(item => item.type === 'imagesBlock')
      .flatMap(block => block.metadata || [])
  };

  // Remove all existing sourceBlocks and imageBlocks
  groupedData = groupedData.filter(item => 
    item.type !== 'sourceBlock' && item.type !== 'imagesBlock'
  );

  // Add consolidated blocks if they have items
  if (consolidatedSourceBlock.items.length > 0) {
    groupedData.push(consolidatedSourceBlock);
  }
  if (consolidatedImageBlock.metadata.length > 0) {
    groupedData.push(consolidatedImageBlock);
  }

  return groupedData;
};