6.2 KiB
Live Data Refresh Implementation
Overview
Implemented automatic live data refresh for the dashboard using IndexedDB polling with change detection. The dashboard now automatically updates when data changes in IndexedDB, providing real-time visibility into operational metrics.
Components Added
1. use-indexed-db-live.ts Hook
A new custom hook that provides live refresh functionality for IndexedDB data.
Features:
- Automatic change detection using checksums
- Configurable polling intervals (default: 2 seconds)
- Per-store subscription management
- Memory-efficient with automatic cleanup
- Manual refresh capability
- Optional enable/disable control
Usage:
const [data, setData, deleteData, refresh] = useIndexedDBLive<MyType[]>(
STORES.MY_STORE,
[],
{
enabled: true,
pollingInterval: 2000
}
)
2. LiveRefreshIndicator.tsx Component
A visual indicator that shows live refresh status on the dashboard.
Features:
- "Live" badge with animated refresh icon
- Last updated timestamp with relative time formatting
- Visual progress bar showing time until next refresh
- Smooth animations and transitions
- Automatic countdown display
Display Elements:
- Badge showing "Live" status
- Spinning icon during refresh
- Relative time ("just now", "2s ago", "5m ago")
- Progress bar counting down to next refresh
Updates to Existing Files
1. use-app-data.ts
Updated to use useIndexedDBLive instead of useIndexedDBState for all entity stores.
Changes:
- Accepts options for live refresh configuration
- Default polling interval: 2 seconds
- Can be enabled/disabled via options
- Applies to all entity stores (timesheets, invoices, payroll, workers, compliance, expenses, rate cards)
2. App.tsx
Updated to enable live refresh for the entire application.
const { ... } = useAppData({
liveRefresh: true,
pollingInterval: 2000
})
3. dashboard-view.tsx
Enhanced with live refresh indicator and automatic update tracking.
Changes:
- Added
LiveRefreshIndicatorcomponent to header - Tracks last update time using
useEffectmonitoring metrics changes - Indicator shows live status and countdown
4. hooks/index.ts
Added exports for new live refresh hooks.
How It Works
Change Detection
- Checksum Generation: For each store, generates a checksum based on item IDs, statuses, and update timestamps
- Polling: Every 2 seconds (configurable), checks all subscribed stores
- Comparison: Compares current checksum with last known checksum
- Notification: If changed, notifies all listeners for that store
- Refresh: Listeners reload data from IndexedDB
Subscription Management
- Stores only polled when there are active subscriptions
- Automatic cleanup when no listeners remain
- Efficient memory usage with WeakMap-style management
- Single polling loop shared across all subscribers
Performance Optimizations
- Only generates checksums for subscribed stores
- Batches all store checks in single interval
- Uses lightweight checksum algorithm
- Stops polling when no active subscriptions
- Component-level control over refresh frequency
Configuration Options
Global Polling Interval
Set the polling interval for all live refresh hooks:
useIndexedDBLivePolling(1000) // Check every 1 second
Per-Hook Configuration
Configure polling for individual hooks:
const [data] = useIndexedDBLive(
STORES.TIMESHEETS,
[],
{
enabled: true, // Enable/disable live refresh
pollingInterval: 3000 // Check every 3 seconds
}
)
Manual Refresh
Trigger manual data refresh when needed:
const [data, setData, deleteData, refresh] = useIndexedDBLive(...)
// Manually refresh data
await refresh()
Benefits
Real-Time Updates
- Dashboard metrics update automatically when data changes
- No manual refresh required
- Immediate visibility into data changes
- Reduced stale data concerns
User Experience
- Live indicator provides visual feedback
- Users know when data was last updated
- Progress bar shows time until next refresh
- Smooth, non-disruptive updates
Developer Experience
- Drop-in replacement for
useIndexedDBState - Same API with additional options
- Easy to enable/disable per component
- Configurable polling intervals
Performance
- Efficient change detection
- Minimal re-renders (only on actual changes)
- Automatic cleanup prevents memory leaks
- Shared polling reduces overhead
Future Enhancements
Potential Improvements
- WebSocket Integration: Replace polling with WebSocket push notifications
- Smart Polling: Adjust polling frequency based on user activity
- Selective Store Refresh: Only refresh stores visible in current view
- Batch Updates: Debounce rapid changes to reduce re-renders
- Offline Detection: Pause polling when offline
- Error Recovery: Automatic retry on failed refreshes
Advanced Features
- Conflict Resolution: Handle concurrent edits from multiple tabs
- Optimistic Updates: Show changes immediately before confirmation
- Change Notifications: Toast messages for important updates
- Differential Updates: Only update changed records, not entire store
- Priority Queues: High-priority stores refresh more frequently
Testing Recommendations
Manual Testing
- Open dashboard and observe live indicator
- Open another tab and modify data in IndexedDB
- Verify dashboard updates within 2 seconds
- Check progress bar animation smoothness
- Verify relative timestamps update correctly
Automated Testing
- Mock IndexedDB changes and verify listeners called
- Test subscription cleanup on unmount
- Verify polling stops when no subscriptions
- Test manual refresh functionality
- Validate checksum generation accuracy
Documentation
Updated Files
src/hooks/NEW_HOOKS_LATEST.md- Added live refresh hook documentationsrc/hooks/README.md- Added to IndexedDB persistence sectionPRD.md- Updated dashboard feature with live refresh detailsLIVE_DATA_REFRESH.md- This comprehensive implementation guide
Hook Documentation
Complete API documentation available in:
- Type definitions in
use-indexed-db-live.ts - Usage examples in
NEW_HOOKS_LATEST.md - Integration patterns in
use-app-data.ts