|
| 1 | +import type { Block, Field, FlattenedBlock } from '../fields/config/types.js' |
| 2 | +import type { SanitizedConfig } from '../index.js' |
| 3 | +import type { JsonObject } from '../types/index.js' |
| 4 | + |
| 5 | +import { fieldAffectsData, fieldShouldBeLocalized, tabHasName } from '../fields/config/types.js' |
| 6 | + |
| 7 | +type FilterDataToSelectedLocalesArgs = { |
| 8 | + configBlockReferences: SanitizedConfig['blocks'] |
| 9 | + docWithLocales: JsonObject |
| 10 | + fields: Field[] |
| 11 | + parentIsLocalized?: boolean |
| 12 | + selectedLocales: string[] |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Filters localized field data to only include specified locales. |
| 17 | + * For non-localized fields, returns all data as-is. |
| 18 | + * For localized fields, if selectedLocales is provided, returns only those locales. |
| 19 | + * If selectedLocales is not provided and field is localized, returns all locales. |
| 20 | + */ |
| 21 | +export function filterDataToSelectedLocales({ |
| 22 | + configBlockReferences, |
| 23 | + docWithLocales, |
| 24 | + fields, |
| 25 | + parentIsLocalized = false, |
| 26 | + selectedLocales, |
| 27 | +}: FilterDataToSelectedLocalesArgs): JsonObject { |
| 28 | + if (!docWithLocales || typeof docWithLocales !== 'object') { |
| 29 | + return docWithLocales |
| 30 | + } |
| 31 | + |
| 32 | + const result: JsonObject = {} |
| 33 | + |
| 34 | + for (const field of fields) { |
| 35 | + if (fieldAffectsData(field)) { |
| 36 | + const fieldIsLocalized = fieldShouldBeLocalized({ field, parentIsLocalized }) |
| 37 | + |
| 38 | + switch (field.type) { |
| 39 | + case 'array': { |
| 40 | + if (Array.isArray(docWithLocales[field.name])) { |
| 41 | + result[field.name] = docWithLocales[field.name].map((item: JsonObject) => |
| 42 | + filterDataToSelectedLocales({ |
| 43 | + configBlockReferences, |
| 44 | + docWithLocales: item, |
| 45 | + fields: field.fields, |
| 46 | + parentIsLocalized: fieldIsLocalized, |
| 47 | + selectedLocales, |
| 48 | + }), |
| 49 | + ) |
| 50 | + } |
| 51 | + break |
| 52 | + } |
| 53 | + |
| 54 | + case 'blocks': { |
| 55 | + if (field.name in docWithLocales && Array.isArray(docWithLocales[field.name])) { |
| 56 | + result[field.name] = docWithLocales[field.name].map((blockData: JsonObject) => { |
| 57 | + let block: Block | FlattenedBlock | undefined |
| 58 | + if (configBlockReferences && field.blockReferences) { |
| 59 | + for (const blockOrReference of field.blockReferences) { |
| 60 | + if (typeof blockOrReference === 'string') { |
| 61 | + block = configBlockReferences.find((b) => b.slug === blockData.blockType) |
| 62 | + } else { |
| 63 | + block = blockOrReference |
| 64 | + } |
| 65 | + } |
| 66 | + } else if (field.blocks) { |
| 67 | + block = field.blocks.find((b) => b.slug === blockData.blockType) |
| 68 | + } |
| 69 | + |
| 70 | + if (block) { |
| 71 | + return filterDataToSelectedLocales({ |
| 72 | + configBlockReferences, |
| 73 | + docWithLocales: blockData, |
| 74 | + fields: block?.fields || [], |
| 75 | + parentIsLocalized: fieldIsLocalized, |
| 76 | + selectedLocales, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return blockData |
| 81 | + }) |
| 82 | + } |
| 83 | + break |
| 84 | + } |
| 85 | + |
| 86 | + case 'group': { |
| 87 | + // Named groups create a nested data structure |
| 88 | + if ( |
| 89 | + fieldAffectsData(field) && |
| 90 | + field.name in docWithLocales && |
| 91 | + typeof docWithLocales[field.name] === 'object' |
| 92 | + ) { |
| 93 | + result[field.name] = filterDataToSelectedLocales({ |
| 94 | + configBlockReferences, |
| 95 | + docWithLocales: docWithLocales[field.name] as JsonObject, |
| 96 | + fields: field.fields, |
| 97 | + parentIsLocalized: fieldIsLocalized, |
| 98 | + selectedLocales, |
| 99 | + }) |
| 100 | + } else { |
| 101 | + // Unnamed groups pass through the same data level |
| 102 | + const nestedResult = filterDataToSelectedLocales({ |
| 103 | + configBlockReferences, |
| 104 | + docWithLocales, |
| 105 | + fields: field.fields, |
| 106 | + parentIsLocalized, |
| 107 | + selectedLocales, |
| 108 | + }) |
| 109 | + Object.assign(result, nestedResult) |
| 110 | + } |
| 111 | + break |
| 112 | + } |
| 113 | + |
| 114 | + default: { |
| 115 | + // For all other data-affecting fields (text, number, select, etc.) |
| 116 | + if (field.name in docWithLocales) { |
| 117 | + const value = docWithLocales[field.name] |
| 118 | + |
| 119 | + // If the field is localized and has locale data |
| 120 | + if (fieldIsLocalized && value && typeof value === 'object' && !Array.isArray(value)) { |
| 121 | + // If selectedLocales is provided, filter to only those locales |
| 122 | + if (selectedLocales && selectedLocales.length > 0) { |
| 123 | + const filtered: Record<string, unknown> = {} |
| 124 | + for (const locale of selectedLocales) { |
| 125 | + if (locale in value) { |
| 126 | + filtered[locale] = value[locale] |
| 127 | + } |
| 128 | + } |
| 129 | + if (Object.keys(filtered).length > 0) { |
| 130 | + result[field.name] = filtered |
| 131 | + } |
| 132 | + } else { |
| 133 | + // If no selectedLocales, include all locales |
| 134 | + result[field.name] = value |
| 135 | + } |
| 136 | + } else { |
| 137 | + // Non-localized field or non-object value |
| 138 | + result[field.name] = value |
| 139 | + } |
| 140 | + } |
| 141 | + break |
| 142 | + } |
| 143 | + } |
| 144 | + } else { |
| 145 | + // Layout-only fields that don't affect data structure |
| 146 | + switch (field.type) { |
| 147 | + case 'collapsible': |
| 148 | + case 'row': { |
| 149 | + // These pass through the same data level |
| 150 | + const nestedResult = filterDataToSelectedLocales({ |
| 151 | + configBlockReferences, |
| 152 | + docWithLocales, |
| 153 | + fields: field.fields, |
| 154 | + parentIsLocalized, |
| 155 | + selectedLocales, |
| 156 | + }) |
| 157 | + Object.assign(result, nestedResult) |
| 158 | + break |
| 159 | + } |
| 160 | + |
| 161 | + case 'tabs': { |
| 162 | + for (const tab of field.tabs) { |
| 163 | + if (tabHasName(tab)) { |
| 164 | + // Named tabs create a nested data structure |
| 165 | + if (tab.name in docWithLocales && typeof docWithLocales[tab.name] === 'object') { |
| 166 | + result[tab.name] = filterDataToSelectedLocales({ |
| 167 | + configBlockReferences, |
| 168 | + docWithLocales: docWithLocales[tab.name], |
| 169 | + fields: tab.fields, |
| 170 | + parentIsLocalized, |
| 171 | + selectedLocales, |
| 172 | + }) |
| 173 | + } |
| 174 | + } else { |
| 175 | + // Unnamed tabs pass through the same data level |
| 176 | + const nestedResult = filterDataToSelectedLocales({ |
| 177 | + configBlockReferences, |
| 178 | + docWithLocales, |
| 179 | + fields: tab.fields, |
| 180 | + parentIsLocalized, |
| 181 | + selectedLocales, |
| 182 | + }) |
| 183 | + Object.assign(result, nestedResult) |
| 184 | + } |
| 185 | + } |
| 186 | + break |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return result |
| 193 | +} |
0 commit comments