Performance optimisation for large user sets
epic-admin-portal-user-management-task-012 — Profile and optimise the screen for organisations with hundreds to thousands of users. Implement list virtualisation, debounced filter application, query result caching with appropriate invalidation, and measure scroll performance. Ensure initial load and filter operations complete within acceptable time thresholds.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 6 - 158 tasks
Can start after Tier 5 completes
Implementation Notes
Implement cursor-based pagination (using the last fetched user's ID or offset) rather than page-number-based to avoid duplicate records when users are inserted between pages. Store paginated results in the BLoC state as a Map
Set ListView.builder's itemExtent to the known row height (e.g., 72dp) to enable O(1) scroll position calculations and avoid layout passes. For cache invalidation, emit a CacheInvalidationEvent from the BLoC whenever a mutation (toggle, role change, invite) completes successfully. Run Flutter DevTools' 'Rebuild Stats' overlay during development to identify any inadvertent full-list rebuilds triggered by parent state changes.
Testing Requirements
Performance tests: use Flutter's `WidgetTester.pump` benchmarks and integration_test package to measure frame rendering times with a list of 500+ mocked users. Profile with Flutter DevTools (CPU + widget rebuild timeline) and document the before/after frame rate. Unit tests: (1) debounce correctly delays emission by 400ms; (2) cache returns mocked data on second identical call without triggering a new Supabase fetch; (3) cache invalidation clears the affected query on role/status change. Widget tests: (1) ListView.builder is used (verify itemCount and itemBuilder are present); (2) pagination triggers next-page fetch when last visible item index reaches threshold.
Regression test: ensure filter + pagination combination returns correct results (not duplicates or missing items).
Displaying NHF users with membership in up to 5 local chapters in a flat list view without duplicating entries requires a non-trivial aggregation query. Incorrect query design could result in duplicated user rows or missing chapter affiliations, confusing admins and causing incorrect role assignments.
Mitigation & Contingency
Mitigation: Design the user list query to GROUP BY user_id and aggregate chapter affiliations as an array field. Use AdminRepository's typed models to surface this aggregated structure to the UI. Validate with a test dataset containing users in 5 chapters.
Contingency: If aggregation query complexity proves too high for real-time filtering, implement a separate multi-chapter affiliation fetch triggered only when a specific user row is expanded, reducing query complexity for the base list.
Composable multi-dimensional filters (role + chapter + status + certification state) applied server-side against an org with 2,000+ users may produce slow queries, particularly when filtering by certification state requires joining an additional table.
Mitigation & Contingency
Mitigation: Ensure the relevant filter columns (role, status, chapter_id, certification_expiry) are indexed in Supabase. Use cursor-based pagination rather than OFFSET to maintain consistent performance at high page numbers. Profile filter query combinations against a large dataset during development.
Contingency: If multi-filter performance degrades in production, introduce a denormalised search index table updated on user status changes, allowing the list query to filter from a single table.
Deactivating a user account that has ongoing activity assignments, open expense claims, or active chapter affiliations may leave orphaned records or break downstream workflows if the deactivation does not trigger correct cascade handling.
Mitigation & Contingency
Mitigation: Define and document the expected state of each dependent record type on user deactivation before implementing the toggle. Implement deactivation as a UserManagementService operation that checks for and warns about open dependencies before persisting. Write integration tests covering each dependency type.
Contingency: If orphaned record issues are discovered post-launch, provide an admin-accessible reconciliation view that surfaces users with inconsistent dependency states and allows manual resolution without requiring a code deploy.