Skip to content

Commit 4e07c33

Browse files
committed
fix(filter): update filtered badge on mark-as-read actions
The "· N filtered" badge was not updating after marking notifications as read, because storage.onChanged is debounced during animations. - Add onMarkAsReadSuccess callback to notification-renderer so popup refreshes the filtered count after a single mark-as-read - Reset filtered badge to 0 after mark-all-as-read clears storage - Extract refreshFilteredBadge to avoid unnecessary DOM rendering into the hidden filtered list container - Normalize remote filter rules (trim + drop empty strings) to prevent keywords: [""] from hiding all notifications - Validate rules array shape in getFilterGist before returning
1 parent 014c813 commit 4e07c33

4 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎src/background/service-worker.js‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,15 +1055,20 @@ async function handleMessage(message) {
10551055
}
10561056

10571057
function validateFilterRules(rules) {
1058-
// Drop malformed rules from untrusted remote data; valid rules pass through unchanged
1059-
return rules.filter(
1060-
(r) =>
1061-
Array.isArray(r?.repos) &&
1062-
Array.isArray(r?.keywords) &&
1063-
r.keywords.length > 0 &&
1064-
r.repos.every((x) => typeof x === "string") &&
1065-
r.keywords.every((x) => typeof x === "string"),
1066-
);
1058+
// Normalize and drop malformed rules from untrusted remote data
1059+
return rules
1060+
.filter(
1061+
(r) =>
1062+
Array.isArray(r?.repos) &&
1063+
Array.isArray(r?.keywords) &&
1064+
r.repos.every((x) => typeof x === "string") &&
1065+
r.keywords.every((x) => typeof x === "string"),
1066+
)
1067+
.map((r) => ({
1068+
repos: r.repos.map((x) => x.trim()).filter(Boolean),
1069+
keywords: r.keywords.map((x) => x.trim()).filter(Boolean),
1070+
}))
1071+
.filter((r) => r.keywords.length > 0);
10671072
}
10681073

10691074
async function acceptRemoteFilter(valid, updatedAt) {
@@ -1087,9 +1092,6 @@ async function applyRemoteRules(gistId) {
10871092
return { success: false, error: "gist_not_found" };
10881093
}
10891094
const { rules, updatedAt: remoteUpdatedAt } = result;
1090-
if (!Array.isArray(rules)) {
1091-
return { success: false, error: "invalid_data" };
1092-
}
10931095
const valid = validateFilterRules(rules);
10941096
const local = await storage.getNotificationFilter();
10951097
if (JSON.stringify(local) === JSON.stringify(valid)) {

‎src/lib/github-api.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,9 @@ class GitHubAPI {
10541054
if (!file) return null;
10551055
try {
10561056
const parsed = JSON.parse(file.content);
1057-
return { rules: parsed.rules || [], updatedAt: parsed.updatedAt || null };
1057+
if (!Array.isArray(parsed.rules)) return null;
1058+
return { rules: parsed.rules, updatedAt: parsed.updatedAt || null };
10581059
} catch {
1059-
// Corrupted content → treat as empty so the next push overwrites with valid local data
10601060
return null;
10611061
}
10621062
}

‎src/popup/notification-renderer.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let config = {
6565
sendMessage: async () => {},
6666
onUserAction: () => {},
6767
onMarkRepoAsRead: () => {},
68+
onMarkAsReadSuccess: () => {},
6869
};
6970

7071
/**
@@ -135,7 +136,14 @@ export function formatTimeAgo(dateString) {
135136
* @returns {HTMLElement} Notification list item element
136137
*/
137138
function createNotificationItem(notif, repoHeader, repoFullName) {
138-
const { notificationsList, emptyState, markAllBtn, sendMessage, onUserAction } = config;
139+
const {
140+
notificationsList,
141+
emptyState,
142+
markAllBtn,
143+
sendMessage,
144+
onUserAction,
145+
onMarkAsReadSuccess,
146+
} = config;
139147

140148
const li = document.createElement("li");
141149
li.className = "notification-item";
@@ -385,6 +393,7 @@ function createNotificationItem(notif, repoHeader, repoFullName) {
385393
}
386394

387395
nextFocusTarget?.focus({ preventScroll: true });
396+
onMarkAsReadSuccess();
388397
} else {
389398
// Failure: restore item
390399
restoreItem();

‎src/popup/popup.js‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@ async function markAllAsRead() {
746746
emptyState.hidden = false;
747747
markAllBtn.disabled = true;
748748
markAllBtn.replaceChildren(...originalNodes.map((n) => n.cloneNode(true)));
749+
updateFilteredBadge(0);
749750
} else {
750751
rollback();
751752
console.error("Failed to mark all as read:", result.error);
@@ -1720,12 +1721,17 @@ async function showFilter() {
17201721
hideCreator();
17211722
}
17221723

1723-
async function renderFilteredInFilterView() {
1724-
if (!filteredNotificationsList) return;
1724+
async function refreshFilteredBadge() {
17251725
const notifications = await storage.getNotifications();
17261726
const filtered = notifications.filter((n) => n.matchedRules?.length);
1727-
renderNotificationsInto(filteredNotificationsList, groupByRepo(filtered));
17281727
updateFilteredBadge(filtered.length);
1728+
return filtered;
1729+
}
1730+
1731+
async function renderFilteredInFilterView() {
1732+
if (!filteredNotificationsList) return;
1733+
const filtered = await refreshFilteredBadge();
1734+
renderNotificationsInto(filteredNotificationsList, groupByRepo(filtered));
17291735
}
17301736

17311737
async function toggleFilteredInFilterView() {
@@ -1936,6 +1942,7 @@ async function init() {
19361942
lastUserActionTime = Date.now();
19371943
},
19381944
onMarkRepoAsRead: handleMarkRepoAsRead,
1945+
onMarkAsReadSuccess: refreshFilteredBadge,
19391946
});
19401947

19411948
// Listen for system theme changes

0 commit comments

Comments
 (0)