import React from 'react' import classNames from 'classnames' import { ReportError } from './ReportError' import { IReportTask, IReportError } from '../report' export interface IReportTaskProps { task: IReportTask taskNumber: number tasksCount: number } export function ReportTask(props: IReportTaskProps) { const { task, taskNumber, tasksCount } = props const taskFile = removeBaseUrl(task.resource.path || '') const splitTableFile = splitFilePath(taskFile) const reportErrors = getReportErrors(task) return (
{/* Heading */}

{task.resource.path ? ( {splitTableFile.base} {splitTableFile.sep} {splitTableFile.name} ) : ( {task.resource.name} )} {!task.valid && ( {task.stats.errors} )} Task {taskNumber} of {tasksCount}

{/* Error groups */} {Object.values(reportErrors).map((reportError) => ( ))}
) } // Helpers export function removeBaseUrl(text: string) { return text.replace(/https:\/\/raw\.githubusercontent\.com\/\S*?\/\S*?\/\S*?\//g, '') } export function splitFilePath(path: string) { const parts = path.split('/') return { name: parts.pop(), base: parts.join('/'), sep: parts.length ? '/' : '', } } export function getReportErrors(task: IReportTask) { const reportErrors: { [code: string]: IReportError } = {} for (const error of task.errors) { if (!task.resource.schema) continue const header = task.resource.schema.fields.map((field) => field.name) // Prepare reportError let reportError = reportErrors[error.code] if (!reportError) { reportError = { count: 0, code: error.code, name: error.name, tags: error.tags, description: error.description, header, messages: [], data: {}, } } // Prepare cells let data = reportError.data[error.rowPosition || 0] if (!data) { const values = error.cells || error.labels || [] data = { values, errors: new Set() } } // Ensure blank row if (error.code === 'blank-row') { data.values = header.map(() => '') } // Ensure missing cell if (error.code === 'missing-cell') { // TODO: use type system instead of "!" data.values[error.fieldPosition! - 1] = '' } // Add row errors if (error.fieldPosition) { data.errors.add(error.fieldPosition) } else if (data.values) { data.errors = new Set(data.values.map((_, index) => index + 1)) } // Save reportError reportError.count += 1 reportError.messages.push(error.message) reportError.data[error.rowPosition || 0] = data reportErrors[error.code] = reportError } return reportErrors }