15 KiB
1. ✅ Error Filtering Logic Inconsistency (RESOLVED)
Note: This issue has been resolved by implementing a type-based error system.
The filtering logic in ValidationCell.tsx previously relied on string matching, which was fragile:
// Old implementation (string-based matching)
const filteredErrors = React.useMemo(() => {
return !isEmpty(value)
? errors.filter(error => !error.message?.toLowerCase().includes('required'))
: errors;
}, [value, errors]);
// New implementation (type-based filtering)
const filteredErrors = React.useMemo(() => {
return !isEmpty(value)
? errors.filter(error => error.type !== ErrorType.Required)
: errors;
}, [value, errors]);
The solution implemented:
- Added an
ErrorTypeenum intypes.tsto standardize error categorization - Updated all error creation to include the appropriate error type
- Modified error filtering to use the type property instead of string matching
- Ensured consistent error handling across the application
Guidelines for future development:
- Always use the
ErrorTypeenum when creating errors - Never rely on string matching for error filtering
- Ensure all error objects include the
typeproperty - Use the appropriate error type for each validation rule:
ErrorType.Requiredfor required field validationsErrorType.Regexfor regex validationsErrorType.Uniquefor uniqueness validationsErrorType.Customfor custom validationsErrorType.Apifor API-based validations
2. ⚠️ Redundant Error Processing (PARTIALLY RESOLVED)
Note: This issue has been partially resolved by the re-rendering optimizations.
The system still processes errors in multiple places:
- In
ValidationCell.tsx, errors are filtered by the optimizedprocessErrorsfunction - In
useValidation.tsx, errors are generated at the field level - In
ValidationContainer.tsx, errors are manipulated at the container level
While the error processing has been optimized to be more efficient, there is still some redundancy in how errors are handled across components. However, the current implementation has mitigated the performance impact.
Improvements made:
- Created a central
processErrorsfunction in ValidationCell that efficiently handles error filtering - Implemented a batched update system to reduce redundant error processing
- Added better memoization to avoid reprocessing errors when not needed
Future improvement opportunities:
- Further consolidate error processing logic into a single location
- Create a dedicated error handling service or hook
- Implement a more declarative approach to error handling
3. Race Conditions in Async Validation
async validations could create race conditions:
- If a user types quickly, multiple validation requests might be in flight
- Later responses could overwrite more recent ones if they complete out of order
- The debouncing helps but doesn't fully solve this issue
4. Memory Leaks in Timeout Management
The validation timeouts are stored in refs:
const validationTimeoutsRef = useRef<Record<number, NodeJS.Timeout>>({});
While there is cleanup on unmount, if rows are added/removed dynamically, timeouts for deleted rows might not be properly cleared.
5. ✅ Inefficient Error Storage (RESOLVED)
Status: RESOLVED
Problem
Previously, validation errors were stored in multiple locations:
- In the
validationErrorsMap inuseValidationState - In the row data itself as
__errors
This redundancy caused several issues:
- Inconsistent error states between the two storage locations
- Increased memory usage by storing the same information twice
- Complex state management to keep both sources in sync
- Difficulty reasoning about where errors should be accessed from
Solution
We've implemented a unified error storage approach by:
- Making the
validationErrorsMap inuseValidationStatethe single source of truth for all validation errors - Removed the
__errorsproperty from row data - Updated all validation functions to interact with the central error store instead of modifying row data
- Modified UPC validation to use the central error store
- Updated all components to read errors from the
validationErrorsMap instead of row data
Key Changes
- Modified
dataMutations.tsto stop storing errors in row data - Updated the
Metatype to remove the__errorsproperty - Modified the
RowDatatype to remove the__errorsproperty - Updated the
useValidationhook to return errors separately from row data - Modified the
useAiValidationhook to work with the central error store - Updated the
useFiltershook to check for errors in thevalidationErrorsMap - Modified the
ValidationTableandValidationCellcomponents to read errors from thevalidationErrorsMap
Benefits
- Single Source of Truth: All validation errors are now stored in one place
- Reduced Memory Usage: No duplicate storage of error information
- Simplified State Management: Only one state to update when errors change
- Cleaner Data Structure: Row data no longer contains validation metadata
- More Maintainable Code: Clearer separation of concerns between data and validation
Future Improvements
While this refactoring addresses the core issue of inefficient error storage, there are still opportunities for further optimization:
- ✅ Redundant Error Processing:
The validation process still performs some redundant calculations that could be optimized.This has been largely addressed by the re-rendering optimizations. - Race Conditions: Async validation can lead to race conditions when multiple validations are triggered in quick succession.
- Memory Leaks: The timeout management for validation could be improved to prevent potential memory leaks.
- Tight Coupling: Components are still tightly coupled to the validation state structure.
- Error Prioritization: The system doesn't prioritize errors well, showing all errors at once rather than focusing on the most critical ones first.
Validation Flow
The validation process now works as follows:
-
Error Generation:
- Field-level validations generate errors based on validation rules
- Row-level hooks add custom validation errors
- Table-level validations (like uniqueness checks) add errors across rows
-
Error Storage:
- All errors are stored in the
validationErrorsMap inuseValidationState - The Map uses row indices as keys and objects of field errors as values
- All errors are stored in the
-
Error Display:
- The
ValidationTablecomponent checks thevalidationErrorsMap to highlight rows with errors - The
ValidationCellcomponent receives errors for specific fields from thevalidationErrorsMap - Errors are filtered in the UI to avoid showing "required" errors for fields with values
- The
This focused refactoring approach has successfully addressed a critical issue while keeping changes manageable and targeted.
6. ✅ Excessive Re-rendering (RESOLVED)
Status: RESOLVED
Problem
The validation system was suffering from excessive re-renders due to several key issues:
- Inefficient Error Filtering: The ValidationCell component was filtering errors on every render
- Redundant Error Processing: The same validation work was repeated in multiple components
- Poor Memoization: Components were inadequately memoized, causing unnecessary re-renders
- Inefficient Batch Updates: The state update system wasn't optimally batching changes
These issues led to performance problems, especially with large datasets, and affected the user experience.
Solution
We've implemented a comprehensive optimization approach:
- Optimized Error Processing: Created an efficient
processErrorsfunction in ValidationCell that calculates all derived state in one pass - Enhanced Memoization: Improved memo comparison functions to avoid unnecessary rerenders
- Improved Batch Updates: Redesigned the batching system to aggregate multiple changes before state updates
- Single Update Pattern: Implemented a queue-based update mechanism that applies multiple state changes at once
Key Changes
- Added a more efficient error processing function in ValidationCell
- Created an enhanced error comparison function to properly compare error arrays
- Improved the memo comparison function in ValidationCell
- Added a batch update system in useValidationState
- Implemented a queue-based update mechanism for row modifications
Benefits
- Improved Performance: Reduced render cycles = faster UI response
- Better User Experience: Less lag when editing large datasets
- Reduced Memory Usage: Fewer component instantiations and temporary objects
- Increased Scalability: The application can now handle larger datasets without slowdown
- Maintainable Code: More predictable update flow that's easier to debug and extend
Guidelines for future development
- Use the
processErrorsfunction for error filtering and processing - Ensure React.memo components have proper comparison functions
- Use the batched update system for state changes
- Maintain stable references to objects and functions
- Use appropriate React hooks (useMemo, useCallback) with correct dependencies
- Avoid unnecessary recreations of arrays, objects, and functions
7. Complex Error Merging Logic
When merging errors from different sources, the logic is complex and potentially error-prone:
// Merge field errors and row hook errors
const mergedErrors: Record<string, InfoWithSource> = {}
// Convert field errors to InfoWithSource
Object.entries(fieldErrors).forEach(([key, errors]) => {
if (errors.length > 0) {
mergedErrors[key] = {
message: errors[0].message,
level: errors[0].level,
source: ErrorSources.Row,
type: errors[0].type || ErrorType.Custom
}
}
})
This only takes the first error for each field, potentially hiding important validation issues.
8. ✅ Inconsistent Error Handling for Empty Values (PARTIALLY RESOLVED)
Note: This issue has been partially resolved by standardizing the isEmpty function and error type system.
The system previously had different approaches to handling empty values:
- Some validations skipped empty values unless they're required
- Others processed empty values differently
- The
isEmptyfunction was defined multiple times with slight variations
The solution implemented:
- Standardized the
isEmptyfunction implementation - Ensured consistent error type usage for required field validations
- Made error filtering consistent across the application
Guidelines for future development:
- Always use the shared
isEmptyfunction for checking empty values - Ensure consistent handling of empty values across all validation rules
- Use the
ErrorType.Requiredtype for all required field validations
9. Tight Coupling Between Components
The validation system is tightly coupled across components:
ValidationCellneeds to understand the structure of errorsValidationTableneeds to extract and pass the right errorsValidationContainerdirectly manipulates the error structure
This makes it harder to refactor or reuse components independently.
10. Limited Error Prioritization
There's no clear prioritization of errors:
- When multiple errors exist for a field, which one should be shown first?
- Are some errors more important than others?
- The current system mostly shows the first error it finds
A more robust approach would be to have a consistent error source identification system and a clear prioritization strategy for displaying errors.
Let me explain how these hooks fit together to create the validation errors that eventually get filtered in the ValidationCell component:
The Validation Flow
-
useValidationState Hook: This is the main state management hook used by the
ValidationContainercomponent. It:- Manages the core data state (
data) - Tracks validation errors in a Map (
validationErrors) - Provides functions to update and validate rows
- Manages the core data state (
-
useValidation Hook: This is a utility hook that provides the core validation logic:
validateField: Validates a single field against its validation rulesvalidateRow: Validates an entire row, field by fieldvalidateTable: Runs table-level validationsvalidateUnique: Checks for uniqueness constraintsvalidateData: Orchestrates the complete validation process
How Errors Are Generated
Validation errors come from multiple sources:
-
Field-Level Validations: In
useValidation.tsx, thevalidateFieldfunction checks individual fields against rules like:required: Field must have a valueregex: Value must match a patternmin/max: Numeric constraints
-
Row-Level Validations: The
validateRowfunction inuseValidation.tsxruns:- Field validations for each field in the row
- Special validations for required fields like supplier and company
- Custom row hooks provided by the application
-
Table-Level Validations:
validateUniquechecks for duplicate values in fields marked as uniquevalidateTableruns custom table hooks for cross-row validations
-
API-Based Validations: In
useValidationState.tsxandValidationContainer.tsx:- UPC validation via API calls
- Item number uniqueness checks
The Error Flow
- Errors are collected in the
validationErrorsMap inuseValidationState - This Map is passed to
ValidationTableas a prop ValidationTableextracts the relevant errors for each cell and passes them toValidationCell- In
ValidationCell, the errors are filtered based on whether the cell has a value:// Updated implementation using type-based filtering const filteredErrors = React.useMemo(() => { return !isEmpty(value) ? errors.filter(error => error.type !== ErrorType.Required) : errors; }, [value, errors]);
Key Insights
-
Error Structure: Errors now have a consistent structure with type information:
type ErrorObject = { message: string; level: string; // 'error', 'warning', etc. source?: ErrorSources; // Where the error came from type: ErrorType; // The type of error (Required, Regex, Unique, etc.) } -
Error Sources: Errors can come from:
- Field validations (required, regex, etc.)
- Row validations (custom business logic)
- Table validations (uniqueness checks)
- API validations (UPC checks)
-
Error Types: Errors are now categorized by type:
ErrorType.Required: Field is required but emptyErrorType.Regex: Value doesn't match the regex patternErrorType.Unique: Value must be unique across rowsErrorType.Custom: Custom validation errorsErrorType.Api: Errors from API calls
-
Error Filtering: The filtering in
ValidationCellis now more robust:- When a field has a value, errors of type
ErrorType.Requiredare filtered out - When a field is empty, all errors are shown
- When a field has a value, errors of type
-
Performance Optimizations:
- Batch processing of validations
- Debounced updates to avoid excessive re-renders
- Memoization of computed values