fix: update drafts badge when drafts change in another tab - #138
Merged
Conversation
Drafts are stored in localStorage and the sidebar badge was only refreshed from the composer's in-page save/remove events, so saving or deleting a draft in one tab left the count stale in every other open tab until a reload. Listen for the window storage event (fired in other tabs on localStorage writes) and refresh the badge when the drafts:available list changes or storage is cleared.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The drafts badge in the sidebar (and the bottom bar on mobile) goes stale across tabs. Save or delete a draft in one tab, and every other open tab keeps showing the old count until the page is reloaded.
Notifications don't have this problem because the server pushes `event:notifications.updateCount` over socket.io to every connected tab. Drafts, however, live entirely in `localStorage` on the client. `setupDrafts()` only refreshes the badge on page load and on the composer's `action:composer.drafts.save` / `action:composer.drafts.remove` jQuery events, which are per-tab.
Fix
Listen for the `storage` event on `window`. Browsers fire it in every other tab of the same origin whenever `localStorage` is written. The badge is refreshed when the `drafts:available` list (the key `drafts.getAvailableCount()` derives from) changes, or when storage is cleared (`key === null`).
Guests are unaffected: their drafts are in `sessionStorage`, which is per-tab and does not fire the event. The dropdown list itself needs no change since it is re-rendered on every `shown.bs.dropdown`.
Test