Skip to content

Commit

Permalink
Tag Tracking+: add error handling to refreshCount (AprilSylph#1280)
Browse files Browse the repository at this point in the history
Co-authored-by: April Sylph <[email protected]>
  • Loading branch information
marcustyphoon and AprilSylph authored Sep 17, 2023
1 parent c65b4da commit 22104c8
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/scripts/tag_tracking_plus.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,43 @@ let sidebarItem;
const refreshCount = async function (tag) {
if (!trackedTags.includes(tag)) return;

const savedTimestamp = timestamps[tag] ?? 0;
const {
response: {
timeline: {
elements = [],
links
let unreadCountString = '⚠️';

try {
const savedTimestamp = timestamps[tag] ?? 0;
const {
response: {
timeline: {
elements = [],
links
}
}
} = await apiFetch(
`/v2/hubs/${encodeURIComponent(tag)}/timeline`,
{ queryParams: { limit: 20, sort: 'recent' } }
);

const posts = elements.filter(({ objectType, displayType, recommendedSource }) =>
objectType === 'post' &&
displayType === undefined &&
recommendedSource === null
);

let unreadCount = 0;

for (const { timestamp } of posts) {
if (timestamp <= savedTimestamp) {
break;
} else {
unreadCount++;
}
}
} = await apiFetch(
`/v2/hubs/${encodeURIComponent(tag)}/timeline`,
{ queryParams: { limit: 20, sort: 'recent' } }
);

const posts = elements.filter(({ objectType, displayType, recommendedSource }) =>
objectType === 'post' &&
displayType === undefined &&
recommendedSource === null
);

let unreadCount = 0;

for (const { timestamp } of posts) {
if (timestamp <= savedTimestamp) {
break;
} else {
unreadCount++;
}
}

const showPlus = unreadCount === posts.length && links?.next;
const unreadCountString = `${unreadCount}${showPlus ? '+' : ''}`;
const showPlus = unreadCount === posts.length && links?.next;
unreadCountString = `${unreadCount}${showPlus ? '+' : ''}`;
} catch (exception) {
console.error(exception);
}

[document, ...(!sidebarItem || document.contains(sidebarItem) ? [] : [sidebarItem])]
.flatMap(node =>
Expand Down

0 comments on commit 22104c8

Please sign in to comment.