| 260 | } |
| 261 | |
| 262 | export function useSyncStatus() { |
| 263 | const currentSyncStatusJson = localStorage.getItem(`syncStatus`) |
| 264 | const currentSyncStatus: [SyncStatus, string] = currentSyncStatusJson |
| 265 | ? JSON.parse(currentSyncStatusJson) |
| 266 | : [`initial-sync`, `Starting sync...`] |
| 267 | const [syncStatus, setSyncStatus] = |
| 268 | useState<[SyncStatus, string]>(currentSyncStatus) |
| 269 | |
| 270 | useEffect(() => { |
| 271 | const handleStorageChange = (e: StorageEvent) => { |
| 272 | if (e.key === `syncStatus` && e.newValue) { |
| 273 | const [newStatus, message] = JSON.parse(e.newValue) |
| 274 | setSyncStatus([newStatus, message]) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | window.addEventListener(`storage`, handleStorageChange) |
| 279 | |
| 280 | return () => { |
| 281 | window.removeEventListener(`storage`, handleStorageChange) |
| 282 | } |
| 283 | }, []) |
| 284 | |
| 285 | return syncStatus |
| 286 | } |
| 287 | |
| 288 | let initialSyncDone = false |
| 289 | |