( data: SheetState, allData: SheetState[], viewMode: SheetViewMode, sheetValidationErrors: ImporterValidationError[], errorColumnFilter: string | null, sheetDefinition: SheetDefinition, searchPhrase: string, enumLabelDict: EnumLabelDict )
| 38 | } |
| 39 | |
| 40 | export function useFilteredRowData( |
| 41 | data: SheetState, |
| 42 | allData: SheetState[], |
| 43 | viewMode: SheetViewMode, |
| 44 | sheetValidationErrors: ImporterValidationError[], |
| 45 | errorColumnFilter: string | null, |
| 46 | sheetDefinition: SheetDefinition, |
| 47 | searchPhrase: string, |
| 48 | enumLabelDict: EnumLabelDict |
| 49 | ) { |
| 50 | const rowData = useMemo(() => { |
| 51 | let rows = data.rows; |
| 52 | switch (viewMode) { |
| 53 | case 'errors': |
| 54 | rows = data.rows.filter((_, index) => |
| 55 | sheetValidationErrors.some((error) => error.rowIndex === index) |
| 56 | ); |
| 57 | break; |
| 58 | case 'valid': |
| 59 | rows = data.rows.filter( |
| 60 | (_, index) => |
| 61 | !sheetValidationErrors.some((error) => error.rowIndex === index) |
| 62 | ); |
| 63 | break; |
| 64 | case 'all': |
| 65 | default: |
| 66 | rows = data.rows; |
| 67 | } |
| 68 | |
| 69 | if (errorColumnFilter != null) { |
| 70 | rows = rows.filter((row) => { |
| 71 | const rowIndex = findRowIndex(allData, sheetDefinition.id, row); |
| 72 | const error = sheetValidationErrors.find( |
| 73 | (error) => |
| 74 | error.rowIndex === rowIndex && error.columnId === errorColumnFilter |
| 75 | ); |
| 76 | return error != null; |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | if (searchPhrase.trim() !== '') { |
| 81 | const normalizedSearch = normalizeValue(searchPhrase)!; |
| 82 | rows = rows.filter((row) => |
| 83 | sheetDefinition.columns.some((column) => { |
| 84 | const cellValue = row[column.id]; |
| 85 | |
| 86 | const { displayValue } = getCellDisplayValue( |
| 87 | sheetDefinition, |
| 88 | column, |
| 89 | cellValue, |
| 90 | enumLabelDict |
| 91 | ); |
| 92 | return normalizeValue(displayValue)?.includes(normalizedSearch); |
| 93 | }) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | return rows; |
no test coverage detected