|
| 1 | +# Notification State Scoping |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Notification read state is now scoped per wallet address and network to ensure accurate unread counts when users switch between wallets or networks. |
| 6 | + |
| 7 | +## Storage Key Format |
| 8 | + |
| 9 | +``` |
| 10 | +tipstream_last_seen_{network}_{address} |
| 11 | +``` |
| 12 | + |
| 13 | +Examples: |
| 14 | +- `tipstream_last_seen_mainnet_SP31PKQVQZVZCK3FM3NH67CGD6G1FMR17VQVS2W5T` |
| 15 | +- `tipstream_last_seen_testnet_ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM` |
| 16 | + |
| 17 | +## Behavior |
| 18 | + |
| 19 | +### Address Switching |
| 20 | +When a user switches wallets: |
| 21 | +- Previous wallet's notification state is preserved |
| 22 | +- New wallet loads its own notification state |
| 23 | +- Unread counts are isolated per wallet |
| 24 | + |
| 25 | +### Network Switching |
| 26 | +When network changes: |
| 27 | +- Notification state is scoped to the new network |
| 28 | +- Each network maintains separate read state |
| 29 | +- Example: mainnet tips remain unread when viewing testnet |
| 30 | + |
| 31 | +### Session Management |
| 32 | +- **Logout**: State resets to 0 when address becomes null |
| 33 | +- **Reconnect**: Loads saved state for reconnected address |
| 34 | +- **Fresh wallet**: Starts with 0 last-seen timestamp |
| 35 | + |
| 36 | +## Migration |
| 37 | + |
| 38 | +### Legacy State |
| 39 | +Old installations used a single key: |
| 40 | +``` |
| 41 | +tipstream_last_seen_tip_ts |
| 42 | +``` |
| 43 | + |
| 44 | +### Migration Process |
| 45 | +1. On first load with an address, check for legacy key |
| 46 | +2. If legacy value exists and no scoped value, copy to scoped key |
| 47 | +3. Legacy key remains for backward compatibility |
| 48 | +4. Migration runs once per address-network pair |
| 49 | + |
| 50 | +### Example |
| 51 | +```javascript |
| 52 | +// Legacy (before) |
| 53 | +localStorage.getItem('tipstream_last_seen_tip_ts'); // 1234567890 |
| 54 | + |
| 55 | +// After migration for address SP123... on mainnet |
| 56 | +localStorage.getItem('tipstream_last_seen_mainnet_SP123...'); // 1234567890 |
| 57 | + |
| 58 | +// Original key unchanged |
| 59 | +localStorage.getItem('tipstream_last_seen_tip_ts'); // 1234567890 |
| 60 | +``` |
| 61 | + |
| 62 | +## API |
| 63 | + |
| 64 | +### Storage Functions |
| 65 | + |
| 66 | +```javascript |
| 67 | +import { |
| 68 | + getNotificationStorageKey, |
| 69 | + getLastSeenTimestamp, |
| 70 | + setLastSeenTimestamp, |
| 71 | + migrateLegacyNotificationState |
| 72 | +} from './lib/notificationStorage'; |
| 73 | + |
| 74 | +// Generate storage key |
| 75 | +const key = getNotificationStorageKey(address, network); |
| 76 | + |
| 77 | +// Get last seen timestamp |
| 78 | +const timestamp = getLastSeenTimestamp(address, network); |
| 79 | + |
| 80 | +// Save last seen timestamp |
| 81 | +setLastSeenTimestamp(address, network, timestamp); |
| 82 | + |
| 83 | +// Migrate legacy state (automatic, called by hook) |
| 84 | +const migrated = migrateLegacyNotificationState(address, network); |
| 85 | +``` |
| 86 | + |
| 87 | +### Hook Usage |
| 88 | + |
| 89 | +```javascript |
| 90 | +import { useNotifications } from './hooks/useNotifications'; |
| 91 | + |
| 92 | +function MyComponent() { |
| 93 | + const { notifications, unreadCount, markAllRead } = useNotifications(userAddress); |
| 94 | + |
| 95 | + // unreadCount is scoped to current address and network |
| 96 | + // markAllRead saves to scoped storage |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Testing |
| 101 | + |
| 102 | +### Multi-Account Tests |
| 103 | +Tests verify isolation between different wallets: |
| 104 | +```javascript |
| 105 | +// Different addresses have independent state |
| 106 | +setLastSeenTimestamp('SP111...', 'mainnet', 1000); |
| 107 | +setLastSeenTimestamp('SP222...', 'mainnet', 2000); |
| 108 | + |
| 109 | +getLastSeenTimestamp('SP111...', 'mainnet'); // 1000 |
| 110 | +getLastSeenTimestamp('SP222...', 'mainnet'); // 2000 |
| 111 | +``` |
| 112 | + |
| 113 | +### Network Isolation Tests |
| 114 | +Tests verify network-specific state: |
| 115 | +```javascript |
| 116 | +// Same address on different networks |
| 117 | +setLastSeenTimestamp('SP123...', 'mainnet', 1000); |
| 118 | +setLastSeenTimestamp('SP123...', 'testnet', 2000); |
| 119 | + |
| 120 | +getLastSeenTimestamp('SP123...', 'mainnet'); // 1000 |
| 121 | +getLastSeenTimestamp('SP123...', 'testnet'); // 2000 |
| 122 | +``` |
| 123 | + |
| 124 | +## Backward Compatibility |
| 125 | + |
| 126 | +- Existing users with legacy key continue to work |
| 127 | +- Legacy state migrates automatically on first load |
| 128 | +- No data loss during migration |
| 129 | +- Legacy key not deleted for safety |
| 130 | + |
| 131 | +## Edge Cases |
| 132 | + |
| 133 | +### Null Address |
| 134 | +```javascript |
| 135 | +getLastSeenTimestamp(null, 'mainnet'); // Returns 0 |
| 136 | +setLastSeenTimestamp(null, 'mainnet', 1000); // No-op |
| 137 | +``` |
| 138 | + |
| 139 | +### Null Network |
| 140 | +```javascript |
| 141 | +getLastSeenTimestamp('SP123...', null); // Returns 0 |
| 142 | +setLastSeenTimestamp('SP123...', null, 1000); // No-op |
| 143 | +``` |
| 144 | + |
| 145 | +### Invalid Data |
| 146 | +```javascript |
| 147 | +// Non-numeric value in storage |
| 148 | +localStorage.setItem(key, 'invalid'); |
| 149 | +getLastSeenTimestamp(address, network); // Returns 0 |
| 150 | +``` |
| 151 | + |
| 152 | +## Troubleshooting |
| 153 | + |
| 154 | +### Unread count incorrect after wallet switch |
| 155 | +- Check that userAddress prop changed |
| 156 | +- Verify hook re-rendered with new address |
| 157 | +- Inspect localStorage for scoped keys |
| 158 | + |
| 159 | +### Migration not working |
| 160 | +- Ensure legacy key exists |
| 161 | +- Check console for errors |
| 162 | +- Verify address and network are valid |
| 163 | + |
| 164 | +### State not persisting |
| 165 | +- Check localStorage is enabled |
| 166 | +- Verify no quota errors in console |
| 167 | +- Confirm markAllRead was called |
0 commit comments