frontend: add console best practices#2172
Conversation
e75c731 to
dfe7756
Compare
|
Not merging for now - postponing for a future Console release. Going to keep it open |
This comment has been minimized.
This comment has been minimized.
dfe7756 to
309401a
Compare
|
@claude review |
|
Claude finished @malinskibeniamin's task in 2m 13s —— View job Code Review Complete ✅I've reviewed the PR and found a critical breaking change that needs to be addressed, along with several positive improvements. Here's my comprehensive feedback: 🔴 Critical Issue: Breaking Change in Search FunctionalityProblem: The export function getSearchRegex(query: string, flags = 'i'): RegExp {
// ... cache logic ...
const regex = new RegExp(query, flags); // ❌ No escaping!
return regex;
}The function compiles the user's query directly as a regex pattern without escaping. However, @weeco's review comment is incorrect - the search functionality is NOT changing from pattern matching to literal substring matching. Actual Behavior:
The Real Issues:
Evidence from tests:
Recommendation: export function getSearchRegex(query: string, flags = 'i'): RegExp {
const cacheKey = `${query}:${flags}`;
const cached = SEARCH_REGEX_CACHE.get(cacheKey);
if (cached) return cached;
if (SEARCH_REGEX_CACHE.size >= CACHE_MAX_SIZE) {
const firstKey = SEARCH_REGEX_CACHE.keys().next().value;
if (firstKey) SEARCH_REGEX_CACHE.delete(firstKey);
}
try {
const regex = new RegExp(query, flags);
SEARCH_REGEX_CACHE.set(cacheKey, regex);
return regex;
} catch {
// Invalid regex - fall back to escaped literal search
const escaped = escapeRegex(query);
const regex = new RegExp(escaped, flags);
SEARCH_REGEX_CACHE.set(cacheKey, regex);
return regex;
}
}✅ Excellent Improvements1. New Utilities - Well-designed and tested
2. Search Filtering Standardization - Great refactor
Example improvement in // Before: ~10 lines with try/catch
try {
const quickSearchRegExp = new RegExp(filter, 'i');
return u.name.match(quickSearchRegExp);
} catch {
return false;
}
// After: 1 clean line
return u.name.match(getSearchRegex(searchQuery));3. Performance Optimizations - Smart changes✅ // Before: O(n) lookup per check
if (existingLabels.includes(newResource.label))
// After: O(1) lookup per check
const existingLabelsSet = new Set(existingLabels);
if (existingLabelsSet.has(newResource.label))✅ ✅ 4. Unnecessary useMemo Removal - Correct simplifications✅ // Before: Unnecessary memoization
const availableMcpServers = useMemo(() => {
return mcpServersData?.mcpServers ?? [];
}, [mcpServersData]);
// After: Direct computation is faster
const availableMcpServers = mcpServersData?.mcpServers ?? [];✅ 📋 Minor Suggestions
🎯 SummaryMust Fix Before Merge:
Optional Improvements:
Overall Assessment: This PR contains excellent refactoring work with meaningful performance improvements and better code organization. The utilities are well-designed and thoroughly tested. However, the regression in error handling for invalid regex patterns must be fixed before merging to avoid potential crashes. Test Coverage: ✅ All new utilities have comprehensive tests (regex: 136 lines, array-transforms: 148 lines, safe-local-storage: 146 lines) Performance Impact: ✅ Positive (regex caching, Set lookups, module-level cache, optimized React hooks) Breaking Changes: Code Quality: ✅ Excellent (DRY, well-tested, properly typed) |
Applies fixes from react-best-practices skill
New Utilities
1.
src/utils/regex.ts- Cached search regex with proper escapingescapeRegex()- escapes special regex charactersgetSearchRegex()- returns cached, case-insensitive RegExp for search queries2.
src/utils/array-transforms.ts- Efficient array helpersfilterMap()- single-pass filter and map operationpartition()- split array into multiple buckets by classifierpartitionBy()- two-way partition based on predicate3.
src/hooks/use-safe-local-storage.ts- Safe localStorage accessuseSafeLocalStorage()hook - handles incognito mode, quota errors, SSRgetLocalStorageFlag()/setLocalStorageFlag()- simple boolean flag helpersRefactors
Search filtering standardization
Replaced duplicated try/catch regex patterns across 12 files with
getSearchRegex():acl-list.tsx(4 places)role-details.tsxadmin-users.tsxcluster-details.tsxconnect/overview.tsxgroup-list.tsxpipelines-list.tsxsecrets-list.tsxschema-list.tsxtopic-list.tsxtransforms-list.tsxPerformance improvements
rp-connect/utils/yaml.ts- useSetinstead ofArray.includes()for label lookupstopic-filters-session.ts- module-level cache to avoid repeated sessionStorage parsingtopic-produce.tsx- watch individual form fields instead of objects to minimize effect rerunsSimplifications
ai-agent-configuration-tab.tsx- removed unnecessaryuseMemowrappers for simple boolean/array derivationsSafe storage
banner.tsx- uses new safe localStorage helpers instead of directlocalStorageaccess