Repository: payloadcms/payload
File: packages/ui/src/forms/fieldSchemasToFormState/addFieldStatePromise.ts (TypeScript)

Code before:
            fieldState.disableFormData = true
          }
        }

        // Add field to state
        if (!omitParents && (!filter || filter(args))) {
          state[path] = fieldState
        }

        break
      }

      case 'blocks': {
        const blocksValue = Array.isArray(data[field.name]) ? data[field.name] : []

        const { promises, rowMetadata } = blocksValue.reduce(
          (acc, row, i: number) => {
            const blockTypeToMatch: string = row.blockType
            const block =
              req.payload.blocks[blockTypeToMatch] ??
              ((field.blockReferences ?? field.blocks).find(
                (blockType) => typeof blockType !== 'string' && blockType.slug === blockTypeToMatch,
              ) as FlattenedBlock | undefined)

            if (!block) {
              throw new Error(
                `Block with type "${row.blockType}" was found in block data, but no block with that type is defined in the config for field with schema path ${schemaPath}.`,
              )
            }

            const parentPath = path + '.' + i

            if (block) {
              row.id = row?.id || new ObjectId().toHexString()

              if (!omitParents && (!filter || filter(args))) {
                // Handle block `id` field
                const idKey = parentPath + '.id'

                state[idKey] = {
                  initialValue: row.id,
                  value: row.id,
                }

                if (includeSchema) {
                  state[idKey].fieldSchema = includeSchema
                    ? block.fields.find((blockField) => fieldIsID(blockField))
                    : undefined
                }

                // Handle `blockType` field

Review comment (suggestion):
Instead of initializing an empty array here, we need to safely handle undefined rows on the client-side. This is to ensure that form state responses aren't bloated with empty arrays. Relevant PR here: #9388

Code after:
              newRow.lastRenderedPath = previousRow.lastRenderedPath
            }

            // add addedByServer flag
            if (!previousRow) {
              newRow.addedByServer = true
            }

            const isCollapsed = isRowCollapsed({
              collapsedPrefs: preferences?.fields?.[path]?.collapsed,
              field,
              previousRow,
              row,
            })

            if (isCollapsed) {
              newRow.collapsed = true
            }

            acc.rows.push(newRow)

            return acc
          },
          {
            promises: [],
            rows: [],
          },
        )

        // Wait for all promises and update fields with the results
        await Promise.all(promises)

        if (rows) {
          fieldState.rows = rows
        }

        // Add values to field state
        if (data[field.name] !== null) {
          fieldState.value = forceFullValue ? arrayValue : arrayValue.length
          fieldState.initialValue = forceFullValue ? arrayValue : arrayValue.length

          if (arrayValue.length > 0) {
            fieldState.disableFormData = true
          }
        }

        // Add field to state
        if (!omitParents && (!filter || filter(args))) {
          state[path] = fieldState
        }